CN112817598B - Code injection method for compiling period - Google Patents
Code injection method for compiling period Download PDFInfo
- Publication number
- CN112817598B CN112817598B CN202110051610.4A CN202110051610A CN112817598B CN 112817598 B CN112817598 B CN 112817598B CN 202110051610 A CN202110051610 A CN 202110051610A CN 112817598 B CN112817598 B CN 112817598B
- Authority
- CN
- China
- Prior art keywords
- code
- maven
- replacement
- class
- replaced
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
- 238000002347 injection Methods 0.000 title claims abstract description 22
- 239000007924 injection Substances 0.000 title claims abstract description 22
- 238000000034 method Methods 0.000 title claims abstract description 15
- 238000004806 packaging method and process Methods 0.000 claims abstract description 25
- 238000012360 testing method Methods 0.000 claims description 6
- 238000012545 processing Methods 0.000 claims description 5
- 230000006837 decompression Effects 0.000 claims description 3
- 230000008520 organization Effects 0.000 claims description 3
- 230000001419 dependent effect Effects 0.000 description 12
- 238000011161 development Methods 0.000 description 4
- 238000012986 modification Methods 0.000 description 3
- 230000004048 modification Effects 0.000 description 3
- 238000010586 diagram Methods 0.000 description 2
- 239000012634 fragment Substances 0.000 description 2
- 230000007246 mechanism Effects 0.000 description 2
- 239000008186 active pharmaceutical agent Substances 0.000 description 1
- 230000009286 beneficial effect Effects 0.000 description 1
- 238000001914 filtration Methods 0.000 description 1
- 238000012423 maintenance Methods 0.000 description 1
- 238000012856 packing Methods 0.000 description 1
- 230000008569 process Effects 0.000 description 1
- 239000000243 solution Substances 0.000 description 1
- 239000013589 supplement Substances 0.000 description 1
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/40—Transformation of program code
- G06F8/41—Compilation
- G06F8/43—Checking; Contextual analysis
- G06F8/433—Dependency analysis; Data or control flow analysis
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/70—Software maintenance or management
- G06F8/72—Code refactoring
Landscapes
- Engineering & Computer Science (AREA)
- General Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Software Systems (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Stored Programmes (AREA)
Abstract
The embodiment of the application provides a code injection method in compiling period, which comprises the steps of determining the dependency relationship of a package where a replacement code is located in a target item, compiling the replacement code based on the dependency relationship to obtain a class packaging file; adding replaced-proj plug-in to a Maven management tool, analyzing a class packaging file in the Maven management tool to obtain a replacement code, traversing a dependency library, and judging whether a replaceable code set exists or not; if so, the replacement code is written into the preset path of the target item. As only one Maven/Gradle plug is needed to be added, the service codes of the original project are not modified, the runtime parameters are not modified, and additional runtime dependence is not introduced.
Description
Technical Field
The application belongs to the field of code development, and particularly relates to a code injection method at a compiling period.
Background
In software development, in some scenarios, code in a project-dependent package needs to be replaced to implement special behavior, for example, a certain piece of code under a certain dependent package has Bug to be corrected, for example, request information (such as adding a request header, modifying HOST, etc.) needs to be uniformly modified before an HTTP request is sent, but the corresponding HTTP processing dependent package does not provide a corresponding modification API interface.
The current common implementation schemes are as follows:
1. The dependency package code is modified, the new branch is repackaged, and the item dependency is changed to this branch.
2. The dependent package and its corresponding grandparent dependent code are copied into the project and the corresponding dependent package is deleted and then modified directly in the project.
However, the inventor finds that the scheme 1 needs to make a new dependent branch, so that the backbone version and the branch version need to be maintained synchronously, and the scheme cost is too high for the case of simply modifying the dependence (for example, 1000 files are included in the dependence package, and only 1 file is required to be modified); scheme 2 requires analysis of dependencies, copying all the dependent codes to the project, which is extremely unfavorable for maintenance of the project; scheme 3 is only valid when an item is an executable program, when an item is a class library that would be relied upon by other items (e.g., the dependency to be modified is dep_x.jar, the current item is lab_x.jar, and is relied upon by another item serv_x.jar, resulting in serv_x.jar being dependent on lab_x.jar, lab_x.jar being dependent on dep_x.jar, and the code of dep_x.jar being overwritten by lab_x.jar) would result in uncertainty in the order of loading the dependent packets due to the mechanism of JVM loading, and when the original dependent packet (dep_x.jar) is loaded prior to the class library item (lab_x.jar), the code of the latter modification would fail.
Disclosure of Invention
The embodiment of the application provides a code injection method at the compiling period, which only needs to add one Maven/Gradle plug in, does not modify the service code of the original project, does not modify the runtime parameters, and does not introduce extra runtime dependence.
Specifically, the code injection method for the compiling period provided by the application comprises the following steps:
Determining the dependency relationship of the package where the replacement code is located in the target item, compiling the replacement code based on the dependency relationship, and obtaining a class packaging file;
Adding replaced-proj plug-in to a Maven management tool, analyzing a class packaging file in the Maven management tool to obtain a replacement code, traversing a dependency library, and judging whether a replaceable code set exists or not;
if so, the replacement code is written into the preset path of the target item.
Optionally, determining a dependency relationship of the package in which the replacement code is located in the target item, compiling the replacement code based on the dependency relationship to obtain a class packaging file, including:
Determining and adding a dependency relationship of a package where the replacement code is located in the target item;
Writing replacement code logic using a standard package structure organization;
Compiling in a Maven management tool based on the obtained replacement code logic to generate a class file;
And packaging the obtained class file in a Maven management tool to obtain a class packaging file.
Optionally, the determining and adding the dependency relationship of the package where the replacement code is located in the target item includes:
And uniformly modifying a host file of the HTTP request, and adding the dependency relationship of the package where the replacement code is in a profile field in the host file.
Optionally, compiling in the Maven management tool based on the obtained replacement code logic to generate a class file includes:
Copying the written codes to the src/test/java and under the src/test/resources;
performing compile of the Maven management tool to complete compiling;
Copying the compiled class and resource file to the src/main/resources/< groupId > # of the replacement class library < artifact > # of the replacement class library < version of the replacement class library >.
Optionally, the adding replaced-proj plug-in to the Maven management tool, parsing the class package file in the Maven management tool to obtain a replacement code, traversing the dependency library, and determining whether a replaceable code set exists includes:
adding replaced-proj plugins: adding replaced-proj as a standard Maven plug-in under the Maven Build of target-proj;
executing a command of Maven to trigger injection operation in the object-plug;
and packaging and issuing, and executing a Maven packaging and issuing command to complete the target-proj processing.
Optionally, the executing a command of Maven triggers an injection operation in the object-plug in, including:
Decompression replaced-proj obtains alternative code: acquiring a compiled replaceable code set of src/main/resources in replaced-proj, and recording as c1;
Analyzing a dependency class library of the target item: acquiring all dependency libraries of the current project, and recording as c2;
Relying on class libraries to intersect with alternative code to determine the code to be replaced: traversing the dependency class library set c2, comparing whether it exists in the result set c 1.
Optionally, if the replacing code exists, writing the replacing code into a preset path of the target item, including:
if so, representing the code currently to be replaced;
the code determined to be replaced is written in target/classes.
The beneficial effects are that:
1. The invasiveness is low, only one Maven/Gradle plug is needed to be added, the service codes of the original project are not modified, the runtime parameters are not modified, and extra runtime dependence is not introduced.
2. The universality is strong, and all the JVM items based on Maven/Gradle are applicable.
3. The development is simple, and only the code itself to be modified needs to be concerned based on an automatic flow.
Drawings
In order to more clearly illustrate the technical solutions of the present application, the drawings that are needed in the description of the embodiments will be briefly described below, it being obvious that the drawings in the following description are only some embodiments of the present application, and that other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
FIG. 1 is a flow chart of a code injection method at compile time according to the present application;
fig. 2 is a block diagram of the code injection method of the present application.
Detailed Description
In order to make the structure and advantages of the present application more apparent, the structure of the present application will be further described with reference to the accompanying drawings.
Specifically, as shown in fig. 1, the method for injecting code at compile time provided by the present application includes:
11. determining the dependency relationship of the package where the replacement code is located in the target item, compiling the replacement code based on the dependency relationship, and obtaining a class packaging file;
12. Adding replaced-proj plug-in to a Maven management tool, analyzing a class packaging file in the Maven management tool to obtain a replacement code, traversing a dependency library, and judging whether a replaceable code set exists or not;
13. if so, the replacement code is written into the preset path of the target item.
In implementation, the step of obtaining the class package file corresponding to the step 11 belongs to code replacement compiling behavior, and the step of judging whether the replaceable code set exists or not corresponding to the steps 12 and 13 belongs to code injection behavior. The development and operation links of the project under the longitudinal JVM comprise: development, compilation, packaging, deployment, operation must only be performed in one or more of the aforementioned links to accomplish code replacement.
Supporting multi-version automatic selection of the same type of library, for example, different versions of HTTP CLIENT can be simultaneously injected into a module to be replaced, and the implementation principle of automatic selection of versions Maven and Gralde to be replaced according to HTTP CLIENT version on which a target item depends is similar, and only Maven is taken as an example for further explanation, and fig. 2 is a module architecture diagram of the scheme.
Wherein, target-proj: the Maven type is not limited, and finally, replacement codes can be automatically injected during compiling of the project.
Code segment item to be replaced (replaced-proj): the plug in type item of Maven, the parent POM of this module is an object-parent, and depends on (dependency) on the object-plug in, including all code fragments to be replaced.
New dependency package after replacement (new-dep): without limitation to Maven types, code logic to be replaced in replaced-proj may only be used if additional dependencies are needed (e.g., replacing HTTP requests plus a unified alicloud authentication process may require SDK dependencies referencing alicloud).
Code injection parent POM (object-parent): the Maven's pon type item is a common parent of the code injection plug-in (object-plug) and the code fragment item to be replaced (replaced-proj), and this module contains the Maven plug-in's basic dependency package and core [ code replacement compilation behavior ] (described later).
Code injection plug-in (object-plug in): the Jar type item of Maven, the father POM of the module is an object-parent, and the core is that code injection logic is called up in the compiling (common) period through a Maven plug-in mechanism, so that code injection behavior is realized.
Optionally, the determining the dependency relationship of the package in which the replacement code is located in the target item, compiling the replacement code based on the dependency relationship to obtain a class package file, that is, step 11 includes:
111. Determining and adding a dependency relationship of a package where the replacement code is located in the target item;
112. writing replacement code logic using a standard package structure organization;
113. compiling in a Maven management tool based on the obtained replacement code logic to generate a class file;
114. and packaging the obtained class file in a Maven management tool to obtain a class packaging file.
In implementation, the code replacement compiling behavior is used for compiling the written code into class files and packaging and publishing, the main body of the operation is replaced-proj, and the core flow is as follows:
111. Adding dependencies of the package in which the replacement code resides: because the scheme supports multiple versions of the same class of libraries, dependency needs to be distinguished, a Maven Profile mode is recommended to be used, for example, host of HTTP requests needs to be uniformly modified, and then the following Profile can be added:
"replace. Jar. X" in properties corresponds to the dependency that needs to be added, automatically added by the [ object-common ] profile in object-parent (described later)
112. Writing replacement code logic: the standard format is "< project root directory >/code/< groupId > # of replacement class library < artifact > # of replacement class library < version >/< sources/sources >" of replacement class library, code is under sources directory, resource file is under sources directory, code is organized using standard package structure. Such as add
code\org.apache.httpcomponents#httpclient#4.5.3\sources\org\apache\http\impl\client\CloseableHttpClient.java Class, modify code therein, rewrite to Host
113. Compiling the replacement codes to generate class files: executing Maven command: mvn test-P object-command, < corresponding profile > such as mvn test-P object-command, httpcom, httpcom.4.5.6, the profile of object-command being located in the object-parent module, examples are as follows:
The specific content of step 113 is the key of the whole [ code replacement compiling behavior ], and is driven by a Maven plug-in, and the flow is as follows:
a) Copying the written code to the src/test/java and under the src/test/resources,
B) Compile executing maven completes compilation,
C) Copying the compiled class and resource file to the src/main/resources/< groupId > # of the replacement class library < artifact > # of the replacement class library < version of the replacement class library >
114. Packaging and publishing: and executing the packing of Maven and issuing a command to complete replaced-proj processing.
Optionally, the replaced-proj plug-in is added to a Maven management tool, the class package file is parsed in the Maven management tool to obtain a replacement code, the dependency library is traversed, and whether a replaceable code set exists is judged, namely step 12 includes:
121. adding replaced-proj plugins: adding replaced-proj as a standard Maven plug-in under the Maven Build of target-proj;
122. executing a command of Maven to trigger injection operation in the object-plug;
123. And packaging and issuing, and executing a Maven packaging and issuing command to complete the target-proj processing.
In implementation, the specific implementation of step 121 is to add replaced-proj as a standard Maven plug-in to Maven Build of target-proj.
The specific implementation content of step 122 is:
1221. decompression replaced-proj obtains alternative code: acquiring a compiled replaceable code set of src/main/resources in replaced-proj, and recording as c1;
1222. Analyzing a dependency class library of the target item: acquiring all dependency libraries of the current project, and recording as c2;
1223. Relying on class libraries to intersect with alternative code to determine the code to be replaced: traversing the dependency class library set c2, comparing whether it exists in the result set c 1.
Optionally, if the replacing code exists, the replacing code is written into the preset path of the target item, namely, step 13 includes:
if so, representing the code currently to be replaced;
the code determined to be replaced is written in target/classes.
In implementations, the scene example supplements: "
There are multiple items with their own service addresses that now need to be managed in a unified way to access a unified reverse proxy service that implements unified rights, restrictions, etc. that requires the items to support this feature at a minimum modification cost without modifying the item code. "
The original project can be more than 10, HTTP requests of the original project are httpclient. X and 4.X, and OKHttp are also used, the direct requests of each project are changed into requests to a unified service gateway, authentication, current limiting, safe filtering, operation mark remaining and the like are processed on the gateway and then distributed to each service, the scheme is very suitable for reducing the reconstruction cost of each project, replaced-proj can be compiled on the realization, the requests of each version such as HTTPCLIENT, OKHTTP are intercepted in the module and are uniformly positioned to the gateway, and then the compiling dependence of replaced-proj is added in each project.
The foregoing is illustrative of the present application and is not to be construed as limiting thereof, but rather, the present application is to be construed as limited to the appended claims.
Claims (4)
1. A method for code injection at compile time, the method comprising:
Determining the dependency relationship of the package where the replacement code is located in the target item, compiling the replacement code based on the dependency relationship, and obtaining a class packaging file;
Adding replaced-proj plug-in to a Maven management tool, analyzing a class packaging file in the Maven management tool to obtain a replacement code, traversing a dependency library, and judging whether a replaceable code set exists or not;
if so, writing the replacement code into a preset path of the target item;
determining the dependency relationship of the package where the replacement code is located in the target item, compiling the replacement code based on the dependency relationship to obtain a class packaging file, and comprising:
Determining and adding a dependency relationship of a package where the replacement code is located in the target item;
Writing replacement code logic using a standard package structure organization;
Compiling in a Maven management tool based on the obtained replacement code logic to generate a class file;
Packaging the obtained class files in a Maven management tool to obtain class packaging files;
The determining and adding the dependency relationship of the package of the replacement code in the target item comprises the following steps:
Uniformly modifying a host file of the HTTP request, and adding the dependency relationship of the package where the replacement code is in a profile field in the host file;
The compiling in the Maven management tool based on the obtained replacement code logic to generate a class file comprises the following steps:
Copying the written codes to the src/test/java and under the src/test/resources;
performing compile of the Maven management tool to complete compiling;
Copying the compiled class and resource file to the src/main/resources/< groupId > # of the replacement class library < artifact > # of the replacement class library < version of the replacement class library >.
2. The method for injecting code at compile time according to claim 1, wherein adding replaced-proj plug-ins to a Maven management tool, parsing class package files in the Maven management tool to obtain replacement codes, traversing a dependency library, and determining whether a set of replaceable codes exists comprises:
adding replaced-proj plugins: adding replaced-proj as a standard Maven plug-in under the Maven Build of target-proj;
executing a command of Maven to trigger injection operation in the object-plug;
and packaging and issuing, and executing a Maven packaging and issuing command to complete the target-proj processing.
3. The code injection method of claim 2, wherein executing a command of Maven triggers an injection operation in an object-plug, comprising:
Decompression replaced-proj obtains alternative code: acquiring a compiled replaceable code set of src/main/resources in replaced-proj, and recording as c1;
Analyzing a dependency class library of the target item: acquiring all dependency libraries of the current project, and recording as c2;
Relying on class libraries to intersect with alternative code to determine the code to be replaced: traversing the dependency class library set c2, comparing whether it exists in the result set c 1.
4. The code injection method of claim 1, wherein the writing the replacement code, if any, to the preset path of the target item comprises:
if so, representing the code currently to be replaced;
the code determined to be replaced is written in target/classes.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202110051610.4A CN112817598B (en) | 2021-01-13 | 2021-01-13 | Code injection method for compiling period |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202110051610.4A CN112817598B (en) | 2021-01-13 | 2021-01-13 | Code injection method for compiling period |
Publications (2)
Publication Number | Publication Date |
---|---|
CN112817598A CN112817598A (en) | 2021-05-18 |
CN112817598B true CN112817598B (en) | 2024-06-25 |
Family
ID=75870228
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202110051610.4A Active CN112817598B (en) | 2021-01-13 | 2021-01-13 | Code injection method for compiling period |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN112817598B (en) |
Families Citing this family (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113741935B (en) * | 2021-08-27 | 2024-03-26 | 西藏宁算科技集团有限公司 | Project and product synchronous iteration upgrading method |
Family Cites Families (19)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US7644402B1 (en) * | 2004-03-17 | 2010-01-05 | Sun Microsystems, Inc. | Method for sharing runtime representation of software components across component loaders |
US8504651B2 (en) * | 2006-12-11 | 2013-08-06 | Optima Computers Llc | Code injection system and method of operation |
US8856724B2 (en) * | 2011-06-20 | 2014-10-07 | Ebay Inc. | Systems and methods for incremental software development |
US20150082298A1 (en) * | 2013-09-19 | 2015-03-19 | Qiu Shi WANG | Packaging and deploying hybrid applications |
US20150100945A1 (en) * | 2013-10-08 | 2015-04-09 | International Business Machines Corporation | Resuming a software build process |
US20160117154A1 (en) * | 2014-10-23 | 2016-04-28 | Telefonaktiebolaget L M Ericsson (Publ) | Automated software include graph and build environment analysis and optimization in compiled language |
US9785420B2 (en) * | 2016-02-11 | 2017-10-10 | International Business Machines Corporation | Analysis of source code for deployment |
US10824442B2 (en) * | 2016-03-01 | 2020-11-03 | 650 Industries, Inc. | Method and apparatus for loading multiple differing versions of a native library into a native environment |
CN106250160A (en) * | 2016-08-03 | 2016-12-21 | 汉柏科技有限公司 | A kind of Rapid transplant compiles the method for program of increasing income |
CN106844310A (en) * | 2017-01-22 | 2017-06-13 | 南京南瑞继保电气有限公司 | A kind of structured text code process method and system |
CN106980504B (en) * | 2017-03-28 | 2022-07-01 | 腾讯科技(深圳)有限公司 | Application program development method and tool and equipment thereof |
CN107577469B (en) * | 2017-08-21 | 2019-12-17 | 厦门悦讯信息科技股份有限公司 | software packaging and publishing management method |
KR101987048B1 (en) * | 2017-09-15 | 2019-06-10 | 주식회사 티맥스 소프트 | Easy-to-use type of compile-time dependency injection method and device in the java platform |
CN108647037A (en) * | 2018-04-28 | 2018-10-12 | 新疆熙菱信息技术股份有限公司 | Automatically dispose method and system based on shell scripts |
US10860300B2 (en) * | 2018-08-28 | 2020-12-08 | Red Hat, Inc. | Direct function call substitution using preprocessor |
CN110413314A (en) * | 2019-07-24 | 2019-11-05 | 深圳市盟天科技有限公司 | A kind of file publishing method, device, equipment and storage medium |
CN111897570B (en) * | 2020-07-15 | 2024-04-12 | 杭州安恒信息技术股份有限公司 | Multi-dependency item file extraction method and device based on Maven plug-in |
CN112083947A (en) * | 2020-07-27 | 2020-12-15 | 上海云刹技术有限公司 | Software package issuing method for supply chain multi-language environment |
CN112114871B (en) * | 2020-09-28 | 2023-09-05 | 北京奇艺世纪科技有限公司 | Code sharing method, device, server, terminal and medium |
-
2021
- 2021-01-13 CN CN202110051610.4A patent/CN112817598B/en active Active
Non-Patent Citations (1)
Title |
---|
Maven在Java项目中的引入及应用;江日念等;电脑知识与技术;第9卷(第21期);第4842-4851页 * |
Also Published As
Publication number | Publication date |
---|---|
CN112817598A (en) | 2021-05-18 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US8869106B2 (en) | Language service provider management using application context | |
US8028268B2 (en) | System and method for building software package for embedded system | |
Altekar et al. | OPUS: Online Patches and Updates for Security. | |
US8813047B2 (en) | Yet another transformation language (YATL) | |
US9047407B2 (en) | State capture after execution in dependent sequences | |
US20150294114A1 (en) | Application randomization | |
CN108536451B (en) | Method and device for embedding embedded point of application program | |
EP2939111A1 (en) | Extending a development environment | |
US9032378B2 (en) | Available symbol set generation using untyped variable | |
Xu et al. | Freedom: Engineering a state-of-the-art dom fuzzer | |
CN108762825B (en) | Method and system for realizing heavy load of dynamic library | |
CN108614702B (en) | Byte code optimization method and device | |
US11314863B2 (en) | Behavioral threat detection definition and compilation | |
US20110067018A1 (en) | Compiler program, compilation method, and computer system | |
US11663326B2 (en) | Behavioral threat detection definition and compilation | |
US9047100B2 (en) | Abstract syntax tree transformation | |
CN112882718B (en) | Compiling processing method, device, equipment and storage medium | |
CN112817598B (en) | Code injection method for compiling period | |
CN115543294B (en) | Method for generating visual dependency tree of dynamic link library on Linux system | |
CN109857444B (en) | Application program updating method and device, electronic equipment and readable storage medium | |
EP1696316B1 (en) | Code morphing for testing | |
US20180129490A1 (en) | Object ordering preservation during lto link stage | |
US20180275976A1 (en) | Link time optimization in presence of a linker script using path based rules | |
Yu et al. | Reducing build time through precompilations for evolving large software | |
CN110058876B (en) | Method, device, server and storage medium for expanding compatibility of wireshark software |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant |