CN112817598A - Code injection method in compile time - Google Patents

Code injection method in compile time Download PDF

Info

Publication number
CN112817598A
CN112817598A CN202110051610.4A CN202110051610A CN112817598A CN 112817598 A CN112817598 A CN 112817598A CN 202110051610 A CN202110051610 A CN 202110051610A CN 112817598 A CN112817598 A CN 112817598A
Authority
CN
China
Prior art keywords
code
maven
class
replacement
file
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.)
Pending
Application number
CN202110051610.4A
Other languages
Chinese (zh)
Inventor
于亚丰
覃兆俊
陈立
何国平
宋修虎
陈怀狮
王永平
徐志安
严伟
赵丽丽
周强
钱华
陈依婷
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Zhejiang Huayun Information Technology Co Ltd
Original Assignee
Zhejiang Huayun Information Technology Co Ltd
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by Zhejiang Huayun Information Technology Co Ltd filed Critical Zhejiang Huayun Information Technology Co Ltd
Priority to CN202110051610.4A priority Critical patent/CN112817598A/en
Publication of CN112817598A publication Critical patent/CN112817598A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/43Checking; Contextual analysis
    • G06F8/433Dependency analysis; Data or control flow analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/70Software maintenance or management
    • G06F8/72Code refactoring

Abstract

The embodiment of the application provides a code injection method in a compiling period, which comprises the steps of determining the dependency relationship of a package where a replacing code is located in a target project, compiling the replacing code based on the dependency relationship, and obtaining a class packing file; adding a relocated-proj plug-in into a Maven management tool, analyzing the class packed file in the Maven management tool to obtain a replacement code, traversing the dependency library, and judging whether a replaceable code set exists or not; if so, writing the replacement code into the preset path of the target item. As only one Maven/Gradle plug is needed to be added, the service code of the original project is not modified, the runtime parameter is not modified, and additional runtime dependence is not introduced.

Description

Code injection method in compile time
Technical Field
The application belongs to the field of code development, and particularly relates to a code injection method in a compiling period.
Background
In software development, in some scenarios, codes in item dependent packages need to be replaced to implement special behaviors, for example, a certain section of codes under a certain dependent package has Bug (Bug) and needs to be corrected, and for example, it is desirable to uniformly modify request information (such as adding a request header, modifying HOST and the like) before sending an HTTP request, but a corresponding HTTP processing dependent package does not provide a corresponding modification API interface.
The current common implementation schemes are as follows:
1. and modifying the dependent package code, repackaging a new branch, and changing the item dependence into the branch.
2. And copying the dependent packages and the codes of the corresponding grandparent level dependencies into the item, deleting the corresponding dependent packages, and directly modifying in the item.
However, the inventor finds that a new dependency branch needs to be created in the scheme 1, which results in the need of maintaining the backbone version and the branch version synchronously, and the cost of the scheme is too high when only the dependency is simply modified (for example, the dependency package has 1000 files, and only 1 file needs to be modified in terms of requirement); the scheme 2 needs to analyze the dependency relationship and copy all the dependent codes to the project, which is extremely unfavorable for the maintenance of the project; scheme 3 is only effective when the item is an executable program, when the item is a class library and can be depended on by other items (for example, the dependency to be modified is dep _ x.jar, the current item is lab _ x.jar and is depended on by another item serv _ x.jar, and the formed serv _ x.jar depends on lab _ x.jar which depends on dep _ x.jar, and the code of dep _ x.jar is rewritten by lab _ x.jar), because of the mechanism of JVM loading, the uncertainty of the package-dependent sequence can be caused by the mechanism of JVM loading, and when the original dependent package (dep _ x.jar) is loaded before the class library item (lab _ x.jar), the code modified by the latter can be invalid.
Disclosure of Invention
The embodiment of the application provides a code injection method in a compiling period, and only one Maven/Gradle plug is required to be added, so that the service code of the original project is not modified, the runtime parameter is not required to be modified, and extra runtime dependence is not introduced.
Specifically, the code injection method in the compile time provided by the present application includes:
determining the dependency relationship of a package where the replacement code is located in the target project, and compiling the replacement code based on the dependency relationship to obtain a class package file;
adding a relocated-proj plug-in into a Maven management tool, analyzing the class packed file in the Maven management tool to obtain a replacement code, traversing the dependency library, and judging whether a replaceable code set exists or not;
if so, writing the replacement code into the preset path of the target item.
Optionally, the determining a dependency relationship of a package in which the replacement code is located in the target project, and compiling the replacement code based on the dependency relationship to obtain a class package file includes:
determining and adding the dependency relationship of the package where the replacement code is in the target project;
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 packaged file.
Optionally, the determining and adding the dependency relationship of the package in which the replacement code is located in the target item includes:
uniformly modifying a host file of the HTTP request, and adding the dependency relationship of the package where the replacement code is located in a profile field in the host file.
Optionally, the compiling in the Maven management tool based on the obtained replacement code logic to generate a class file includes:
copying the written code to src/test/java and src/test/resources;
executing the complie of the Maven management tool to complete compiling;
and copying the compiled class and the resource file to src/main/resources/< groupId > #ofthe replacement class library < artifact > #ofthe replacement class library < version > of the replacement class library.
Optionally, the adding the relocated-proj plug-in to the Maven management tool, analyzing 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, where the method includes:
adding a relocated-proj plug-in: adding the relocated-proj as a standard Maven plug-in to the Maven Build of the target-proj;
executing the mail command of Maven, and triggering the injection operation in the inject-plugin;
and packaging and issuing, executing the packaging and issuing command of Maven, and finishing the processing of target-proj.
Optionally, the executing the mail command of Maven triggers an injection operation in the inject-plugin, including:
decompressing replayed-proj gets alternative code: acquiring src/main/resources/compiled alternative code set in relocated-proj, and recording the alternative code set as c 1;
analyzing the dependency class library of the target item: acquiring all the dependency class libraries of the current project, and recording the library as c 2;
and taking intersection of the dependency class library and the replaceable code, and determining the code to be replaced: the dependent class library set c2 is traversed to see if a comparison exists in the result set c 1.
Optionally, if the replacement code exists, writing the replacement code into the preset path of the target item includes:
if so, indicating the code currently to be replaced;
the code determined to be replaced is written in target/classes.
Has the advantages that:
1. the method has low invasiveness, only needs to add one Maven/Gradle plug, does not modify the service code of the original project, does not modify the runtime parameters, and does not introduce additional runtime dependence.
2. The universality is strong, and all the JVM projects based on the Maven/Gradle are applicable.
3. The development is simple, and only the code needing to be modified needs to be concerned on the basis of an automatic flow.
Drawings
In order to more clearly illustrate the technical solutions of the present application, the drawings needed to be used in the description of the embodiments are briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present application, and it is obvious for those skilled in the art to obtain other drawings without creative efforts.
FIG. 1 is a flow chart illustrating a method for code injection during compile time as proposed herein;
fig. 2 is a block architecture diagram of an application of the code injection method in compile time proposed in the present application.
Detailed Description
To make the structure and advantages of the present application clearer, 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 code injection in a compile-time proposed by the present application includes:
11. determining the dependency relationship of a package where the replacement code is located in the target project, and compiling the replacement code based on the dependency relationship to obtain a class package file;
12. adding a relocated-proj plug-in into a Maven management tool, analyzing the class packed file in the Maven management tool to obtain a replacement code, traversing the dependency library, and judging whether a replaceable code set exists or not;
13. if so, writing the replacement code into the preset path of the target item.
In implementation, the step of obtaining the class package file corresponding to step 11 belongs to a code replacement compiling behavior, and the steps 12 and 13 correspond to determining whether an alternative code set belongs to a code injection behavior. The development and operation links of the project under the overview JVM comprise: development, compilation, packaging, deployment, execution, achieving code replacement must only operate in one or more of the above-described links.
The method supports multi-version automatic selection of the same library, for example, different versions of an http client can be simultaneously injected into a module to be replaced, and the implementation principle of automatically selecting a version Maven to be replaced according to the version of the http client depended on a target item is similar to that of gray, which is further explained below by taking Maven as an example, and fig. 2 is a module architecture diagram of the scheme.
Wherein, target item (target-proj): without limiting the Maven type, replacement code will eventually be automatically injected at the time of compilation of this project.
Code fragment entry to replace (relocated-proj): maven's plug type item, whose parent POM is inj-parent and whose dependency (dependency) is in inj-plug, contains all the code fragments to be replaced.
New dependency package after replacement (new-dep): without limiting the Maven type, the code logic to be replaced in the relocated-proj may only be used if additional dependencies are required (e.g., replacing an HTTP request plus a uniform ali cloud authentication process may require an SDK dependency referencing the ali cloud).
Code injection parent POM (inject-parent): the mem type item of Maven is a common parent of code injection plug-in (inject-plugin) and code fragment item to be replaced (relocated-proj), and this module contains the basic dependency package and core [ code replacement compilation behavior ] of Maven plug-in (described later).
Code injection plug-in (inject-plugin): the core of the method is that a Jar type project of Maven, the parent POM of the module is inject-parent, and the code injection logic is called up in the compilation (compile) stage through a Maven plug-in mechanism, so that the [ code injection behavior ] is realized.
Optionally, the determining a dependency relationship of a package in which the replacement code is located in the target item, and compiling the replacement code based on the dependency relationship to obtain a class package file, that is, step 11 includes:
111. determining and adding the dependency relationship of the package where the replacement code is in the target project;
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 packaged file.
In implementation, the code replacement compilation behavior is used for compiling the written code into a class file and packaging and issuing the class file, the main body of the operation is replayed-proj, and the core flow of the operation is as follows:
111. add dependencies to the package where the replacement code resides: because the scheme supports multiple versions of the same library, the dependence needs to be distinguished, a Maven Profile mode is recommended to be used, for example, a host of an HTTP request needs to be modified uniformly, and then profiles similar to the following can be added:
Figure BDA0002896514760000041
Figure BDA0002896514760000051
"replace. jar. x" in properties corresponds to the dependency to be added, and is added automatically by (in-file) in (in-parent) (description later)
112. Writing alternate code logic: the standard format is "< project root directory >/code/< groupId > #ofthe replacement class library < artifact > #ofthe replacement class library < version >/sources/resources >", the code is under the sources directory, the resource file is under the resources directory, and the code is organized using a standard packet structure. Such as by adding
code \ org. apache. httpcompontes # httpclient #4.5.3\ sources \ org \ apache \ http \ impl \ client \ closeable httpclient. java, modifying the code therein, rewriting to Host
113. Compiling the replacement code to generate a class file: executing the Maven command: mvn test-P inject-component, < corresponding profile >, such as mvn test-P inject-component, httpcomponents. httpparent.4.5.6, inject-component this profile is located in the inject-parent module, as exemplified below:
Figure BDA0002896514760000052
Figure BDA0002896514760000061
wherein, the specific content of step 113 is the key of the whole [ code replacement compilation behavior ], and is driven by the Maven plugin, and the process is as follows:
a) copying the written code to src/test/java and src/test/resources,
b) executing the complie of maven completes the compilation,
c) copying the compiled class and resource file to src/main/resources/< groupId > #ofthe replacement class library < artifact > #ofthe replacement class library < version > of the replacement class library
114. Packaging and releasing: and executing the packaging of the Maven and issuing a command to complete the processing of the relocated-proj.
Optionally, the relocated-proj add-in is added to the Maven management tool, the class package file is analyzed in the Maven management tool to obtain a replacement code, the dependency library is traversed, and whether a replaceable code set exists is determined, that is, step 12 includes:
121. adding a relocated-proj plug-in: adding the relocated-proj as a standard Maven plug-in to the Maven Build of the target-proj;
122. executing the mail command of Maven, and triggering the injection operation in the inject-plugin;
123. and packaging and issuing, executing the packaging and issuing command of Maven, and finishing the processing of target-proj.
In the implementation, the specific implementation of step 121 is to add relocated-proj as a standard Maven plug-in to the Maven Build of target-proj.
The specific implementation of step 122 is:
1221. decompressing replayed-proj gets alternative code: acquiring src/main/resources/compiled alternative code set in relocated-proj, and recording the alternative code set as c 1;
1222. analyzing the dependency class library of the target item: acquiring all the dependency class libraries of the current project, and recording the library as c 2;
1223. and taking intersection of the dependency class library and the replaceable code, and determining the code to be replaced: the dependent class library set c2 is traversed to see if a comparison exists in the result set c 1.
Optionally, if the replacement code exists, writing the replacement code into the preset path of the target item, that is, step 13 includes:
if so, indicating the code currently to be replaced;
the code determined to be replaced is written in target/classes.
In implementation, the scenario example complements: "
There are many items, have their own service addresses, need to do the unified management now, let these items visit a unified reverse proxy service, the latter realizes functions such as unified authority, current-limiting, etc., require each item to support this characteristic with the minimum transformation cost under the condition of not modifying the project code. "
The original projects may have more than 10, the HTTP requests of the projects have httpparent 3.x, 4.x and OKHttp, the projects directly request respective services to be changed into requests to a uniform service gateway, the requests are distributed to the services after being processed by authentication, current limiting, safety filtering, operation mark retaining and the like on the gateway, the scheme is very suitable for reducing the reconstruction cost of each project, replaced-proj can be written in the realization, the requests of various versions such as httpparent, OKHttp and the like are intercepted in the module and uniformly positioned to the gateway, and then the compiling dependency of replaced-proj is added in each project.
The above description is only exemplary of the present application and should not be taken as limiting the present application, as any modification, equivalent replacement, or improvement made within the spirit and principle of the present application should be included in the protection scope of the present application.

Claims (7)

1. A method of code injection during compile time, the method comprising:
determining the dependency relationship of a package where the replacement code is located in the target project, and compiling the replacement code based on the dependency relationship to obtain a class package file;
adding a relocated-proj plug-in into a Maven management tool, analyzing the class packed file in the Maven management tool to obtain a replacement code, traversing the dependency library, and judging whether a replaceable code set exists or not;
if so, writing the replacement code into the preset path of the target item.
2. The method for code injection during compilation period of claim 1, wherein the determining the dependency relationship of the package where the replacement code is located in the target project, and compiling the replacement code based on the dependency relationship to obtain a class package file comprises:
determining and adding the dependency relationship of the package where the replacement code is in the target project;
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 packaged file.
3. The method of claim 2, wherein determining and adding a dependency relationship of a package in which a replacement code is located in a target project comprises:
uniformly modifying a host file of the HTTP request, and adding the dependency relationship of the package where the replacement code is located in a profile field in the host file.
4. The method of claim 2, wherein compiling in a Maven management tool based on the obtained replacement code logic to generate a class file comprises:
copying the written code to src/test/java and src/test/resources;
executing the complie of the Maven management tool to complete compiling;
and copying the compiled class and the resource file to src/main/resources/< groupId > #ofthe replacement class library < artifact > #ofthe replacement class library < version > of the replacement class library.
5. The method of claim 1, wherein the adding a relocated-proj plug-in to a Maven management tool, parsing a class package file in the Maven management tool to obtain a replacement code, traversing a dependency library, and determining whether a replaceable code set exists comprises:
adding a relocated-proj plug-in: adding the relocated-proj as a standard Maven plug-in to the Maven Build of the target-proj;
executing the mail command of Maven, and triggering the injection operation in the inject-plugin;
and packaging and issuing, executing the packaging and issuing command of Maven, and finishing the processing of target-proj.
6. The method for code injection during compile time according to claim 5, wherein the executing of the mail command of Maven triggers the injection operation in the inject-plugin, comprising:
decompressing replayed-proj gets alternative code: acquiring src/main/resources/compiled alternative code set in relocated-proj, and recording the alternative code set as c 1;
analyzing the dependency class library of the target item: acquiring all the dependency class libraries of the current project, and recording the library as c 2;
and taking intersection of the dependency class library and the replaceable code, and determining the code to be replaced: the dependent class library set c2 is traversed to see if a comparison exists in the result set c 1.
7. The method of claim 1, wherein writing replacement code to the predetermined path of the target item if any comprises:
if so, indicating the code currently to be replaced;
the code determined to be replaced is written in target/classes.
CN202110051610.4A 2021-01-13 2021-01-13 Code injection method in compile time Pending CN112817598A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110051610.4A CN112817598A (en) 2021-01-13 2021-01-13 Code injection method in compile time

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110051610.4A CN112817598A (en) 2021-01-13 2021-01-13 Code injection method in compile time

Publications (1)

Publication Number Publication Date
CN112817598A true CN112817598A (en) 2021-05-18

Family

ID=75870228

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110051610.4A Pending CN112817598A (en) 2021-01-13 2021-01-13 Code injection method in compile time

Country Status (1)

Country Link
CN (1) CN112817598A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113741935A (en) * 2021-08-27 2021-12-03 的卢技术有限公司 Project and product synchronous iterative upgrading method

Citations (9)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20080140728A1 (en) * 2006-12-11 2008-06-12 Optima Computers Llc Code Injection System and Method of Operation
US20120324417A1 (en) * 2011-06-20 2012-12-20 Ebay Inc. Systems and methods for incremental software development
CN107577469A (en) * 2017-08-21 2018-01-12 厦门悦讯教育科技有限公司 A kind of Software package method for release management
CN108647037A (en) * 2018-04-28 2018-10-12 新疆熙菱信息技术股份有限公司 Automatically dispose method and system based on shell scripts
US20190095181A1 (en) * 2017-09-15 2019-03-28 Tmaxsoft Co., Ltd Easy-To-Use Type Of Compile-Time Dependency Injection Method And Device In The Java Platform
US20190310835A1 (en) * 2017-03-28 2019-10-10 Tencent Technology (Shenzhen) Company Limited Application development method, tool, and device, and storage medium
CN110413314A (en) * 2019-07-24 2019-11-05 深圳市盟天科技有限公司 A kind of file publishing method, device, equipment and storage medium
US20200073643A1 (en) * 2018-08-28 2020-03-05 Red Hat, Inc. Direct function call substitution using preprocessor
CN112114871A (en) * 2020-09-28 2020-12-22 北京奇艺世纪科技有限公司 Code sharing method, device, server, terminal and medium

Patent Citations (9)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20080140728A1 (en) * 2006-12-11 2008-06-12 Optima Computers Llc Code Injection System and Method of Operation
US20120324417A1 (en) * 2011-06-20 2012-12-20 Ebay Inc. Systems and methods for incremental software development
US20190310835A1 (en) * 2017-03-28 2019-10-10 Tencent Technology (Shenzhen) Company Limited Application development method, tool, and device, and storage medium
CN107577469A (en) * 2017-08-21 2018-01-12 厦门悦讯教育科技有限公司 A kind of Software package method for release management
US20190095181A1 (en) * 2017-09-15 2019-03-28 Tmaxsoft Co., Ltd 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
US20200073643A1 (en) * 2018-08-28 2020-03-05 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
CN112114871A (en) * 2020-09-28 2020-12-22 北京奇艺世纪科技有限公司 Code sharing method, device, server, terminal and medium

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
江日念等: "Maven在Java项目中的引入及应用", 电脑知识与技术, vol. 9, no. 21, pages 4842 - 4851 *

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113741935A (en) * 2021-08-27 2021-12-03 的卢技术有限公司 Project and product synchronous iterative upgrading method
CN113741935B (en) * 2021-08-27 2024-03-26 西藏宁算科技集团有限公司 Project and product synchronous iteration upgrading method

Similar Documents

Publication Publication Date Title
US9608893B2 (en) Methods and systems for parsing data objects
US8869106B2 (en) Language service provider management using application context
US8028268B2 (en) System and method for building software package for embedded system
CN107168749B (en) Compiling method, device, equipment and computer readable storage medium
US7614040B2 (en) System and method for efficiently analyzing and building interdependent resources in a software project
US7757212B2 (en) System and method for managing cross project dependencies at development time
US8984502B2 (en) Systems and methods for composing or decomposing a composite image for firmware update images
CN109614165B (en) Multi-version parallel operation method and device for COM (component object model) component
CN108536451B (en) Method and device for embedding embedded point of application program
EP2939111A1 (en) Extending a development environment
US9047407B2 (en) State capture after execution in dependent sequences
CN104714827A (en) Plug-in update method and device
US9298433B2 (en) Optimizing intermediate representation of script code for fast path execution
US20050262471A1 (en) System and method for managing cross project dependencies at development time
CN106775781B (en) Method and device for reducing application installation packages and electronic equipment
US9047100B2 (en) Abstract syntax tree transformation
US9032378B2 (en) Available symbol set generation using untyped variable
CN107508846B (en) Updating method and system of application client and terminal equipment
US11481245B1 (en) Program inference and execution for automated compilation, testing, and packaging of applications
CN112817598A (en) Code injection method in compile time
US20040172407A1 (en) Method and system of processing an encapsulated file at a management computer
KR20050079625A (en) Versioning support in object­oriented programming languages and tools
CN115098158A (en) SDK packaging method and device, computer equipment and storage medium
EP1136910A2 (en) A method of compiling code in an object oriented programming language
CN111610982B (en) Application program development method, device and related system

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