CN116243911A - Component replacement method and device, electronic equipment and storage medium - Google Patents
Component replacement method and device, electronic equipment and storage medium Download PDFInfo
- Publication number
- CN116243911A CN116243911A CN202310061304.8A CN202310061304A CN116243911A CN 116243911 A CN116243911 A CN 116243911A CN 202310061304 A CN202310061304 A CN 202310061304A CN 116243911 A CN116243911 A CN 116243911A
- Authority
- CN
- China
- Prior art keywords
- component
- class
- replaced
- calling
- target
- 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
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
- G06F8/36—Software reuse
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/44—Arrangements for executing specific programs
- G06F9/445—Program loading or initiating
- G06F9/44521—Dynamic linking or loading; Link editing at or after load time, e.g. Java class loading
- G06F9/44526—Plug-ins; Add-ons
-
- Y—GENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
- Y02—TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
- Y02D—CLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
- Y02D10/00—Energy efficient computing, e.g. low power processors, power management or thermal management
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- General Engineering & Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Stored Programmes (AREA)
Abstract
The embodiment of the application provides a component replacement method, a device, electronic equipment and a storage medium, wherein the method comprises the following steps: determining a target class in a byte code file of a target program; wherein the target class is a class in the target program for calling a component to be replaced; determining a method meeting a preset selection condition in the target class as a method to be processed; the predetermined selection condition is the same as the name of the first method and is matched with the parameter list corresponding to the first method; the first method is a method for calling the component to be replaced; replacing the byte codes used for representing the to-be-processed method with byte codes used for representing a second method in the byte code file; wherein the second method is a method for calling a replacement component corresponding to the component to be replaced. By means of the scheme, components in the program can be automatically replaced without invasiveness to source code.
Description
Technical Field
The present disclosure relates to the field of software development technologies, and in particular, to a component replacement method, a device, an electronic apparatus, and a storage medium.
Background
Some methods are used in JAVA programs to call components to achieve corresponding functions, and APIs (Application Programming Interface, application program interfaces) belong to one of the methods. For example, fastjson is a JSON parsing library of open source, which is widely used as a component in server code of Java application by a plurality of Java programs to realize serialization and deserialization between Java objects and JSON data, and can be called by using Fastjson API in the programs.
Some components sometimes have security holes, and therefore, it is necessary to replace the components having security holes with other more stable components, thereby improving the security of the program. For example, in recent years, fastjson is frequently popped out of security holes, which adversely affects the data security of users, so that the Fastjson is replaced by other JSON parsing libraries which are more mature and stable, and the aim of improving the security without affecting the program functions can be achieved.
However, the APIs provided by different JSON parsing libraries are different, so that the code characterizing the Fastjson API needs to be replaced with the code characterizing the APIs of other JSON libraries, thereby implementing component replacement. However, the source code of the application program is directly replaced, which may cause program operation errors, increase the subsequent maintenance difficulty of the program, and if a large number of program items to be replaced exist, the workload of replacing the source code is larger, the efficiency is lower, and the overall upgrade cost is larger.
Therefore, how to automatically replace components in a program without invasiveness to source code becomes a problem to be solved.
Disclosure of Invention
An object of an embodiment of the present application is to provide a component replacement method, apparatus, electronic device, and storage medium, so as to achieve the purpose of automatically replacing components without invasiveness to source code. The specific technical scheme is as follows:
in a first aspect of the application, there is provided a component replacement method, applied to a component replacement tool, the method including:
determining a target class in a byte code file of a target program; wherein the target class is a class in the target program for calling a component to be replaced;
determining a method meeting a preset selection condition in the target class as a method to be processed; the predetermined selection condition is the same as the name of the first method and is matched with the parameter list corresponding to the first method; the first method is a method for calling the component to be replaced;
replacing the byte codes used for representing the to-be-processed method with byte codes used for representing a second method in the byte code file; wherein the second method is a method for calling a replacement component corresponding to the component to be replaced.
Optionally, the step of determining the target class in the byte code file of the target program includes:
determining a target class with a specified name in a byte code file of a target program; wherein the specified name is a name of a class for calling the component to be replaced.
Optionally, the determining manner of the class of the component to be replaced in the target program includes:
and determining the class to which the method for calling the component to be replaced belongs from the source code of the target program, and obtaining the class for calling the component to be replaced in the target program.
Optionally, the component replacement tool is a tool built based on the Java Instrument API interface.
Optionally, the loading path of the component replacement tool is added in a JAVA virtual machine JVM start parameter of the target program;
the component replacement tool is started at the stage of class loading of the bytecode file of the target program in the JVM.
Optionally, after replacing, in the byte code file, the byte code used to characterize the method to be processed with the byte code used to characterize the second method, the method further includes:
outputting prompt information; wherein the hint information is used to characterize that the bytecode of the method to be processed has been replaced.
In a second aspect of the present application, there is also provided a component replacement apparatus for use with a component replacement tool, the apparatus comprising:
the first determining module is used for determining a target class in a byte code file of the target program; wherein the target class is a class in the target program for calling a component to be replaced;
the second determining module is used for determining a method meeting a preset selection condition in the target class as a method to be processed; the preset selection condition is the same as the name of a first method and is matched with a parameter list corresponding to the first method, and the first method is a method for calling the component to be replaced;
the replacing module is used for replacing the byte codes used for representing the to-be-processed method with byte codes used for representing a second method in the byte code file; wherein the second method is a method for calling a replacement component corresponding to the component to be replaced.
Optionally, the first determining module includes:
a determining unit configured to determine, in a bytecode file of a target program, a target class having a specified name; wherein the specified name is a name of a class for calling the component to be replaced.
In a third aspect of the present application, there is further provided an electronic device, including a processor, a communication interface, a memory, and a communication bus, where the processor, the communication interface, and the memory complete communication with each other through the communication bus;
a memory for storing a computer program;
and the processor is used for realizing any component replacement method when executing the program stored in the memory.
In a fourth aspect of the application, there is also provided a computer readable storage medium having a computer program stored therein, which when executed by a processor, implements any of the component replacement methods described above.
In a fifth aspect of the application, there is also provided a computer program product containing instructions which, when run on a computer, cause the computer to perform any of the component replacement methods described above.
According to the component replacement method provided by the embodiment of the application, the target class in the byte code file of the target program is determined, the method meeting the preset selection condition is determined in the target class, and the byte code used for representing the method to be processed is replaced by the byte code used for representing the second method in the byte code file.
In the above scheme, the byte code representing the method to be processed is replaced with: the byte code used for representing the second method is used for calling the corresponding replacement component of the component to be replaced, so that the effect that the component to be replaced is replaced by the corresponding replacement component when the target program runs can be achieved; and the modification is performed at the byte code level, and has no influence on the source code after component replacement is realized. Thus, by the present solution, an automated, non-invasive replacement of components to the source code can be achieved.
Drawings
In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below.
FIG. 1 is a flow chart of a component replacement method provided in an embodiment of the present application;
fig. 2 is a diagram showing a hint information display example provided in an embodiment of the present application;
FIG. 3 is a block diagram of a system provided in an embodiment of the present application;
FIG. 4 is a schematic diagram of a security processing apparatus according to an embodiment of the present disclosure;
fig. 5 is a schematic structural diagram of an electronic device according to an embodiment of the present application.
Detailed Description
The technical solutions in the embodiments of the present application will be described below with reference to the drawings in the embodiments of the present application.
In the following, the terms of art referred to in this application will be explained first:
byte code: java is so "compiled to run once" because JVM (Java Virtual Machine ) abstracts between Java source code and the operating system. The JVM executes a code called bytecode, which is compiled by a Java compiler into bytecodes, which the JVM converts into instructions on the corresponding operating system.
Java Instrument: is a set of API interfaces provided by Java, based on which a developer can implement an Agent program independent of the application program to modify and replace the implementation of certain classes.
The core of Java Instrument is to realize a ClassFileTransfomer interface and a transform method of the interface, and the transform method returns a byte array, namely the converted byte code.
Class loading: when creating an object, the JVM needs to load the byte code file of the class through the class loader, so as to obtain a class object, and then the creation and initialization of the class object are completed.
In order to achieve automatic and non-invasive component replacement of source code, the embodiment of the application provides a component replacement method, a device and electronic equipment.
A component replacement method provided in the embodiments of the present application will be first described below.
The component replacement method provided by the embodiment of the application can be applied to a component replacement tool. In a specific application, the component replacement tool may be a plug-in program or a separately running program. In practical applications, the component replacement tool may be applied to smart phones, tablet computers, desktop computers, and the like. And, the application is not limited to the operating system to which the component replacement tool is applied, for example: the component replacement tool may run on a Windows system (Microsoft operating system), an IOS system (a mobile operating system developed by apple Inc.) or an Android system, etc.
The component replacement tool may be built based on one or more of a Java Instrument API interface module, a bytecode operation module, and a Java Agent module.
The byte code operation module can use byte code operation tools. The bytecode manipulation tool may complete modification and generation of the bytecode, such as CGLIB (Code Generation Library ), javaist (javaist is a class library of open source analyzing, editing and creating Java bytecode), ASM (ASM is a Java bytecode manipulation framework), and the like. The present solution is not limited to the bytecode operation tool, and any bytecode operation tool can be applied to the present solution.
The core of the Java Instrument API interface is to implement a classfileTransfomer interface, which defines a class file converter, and the transform method of the interface is called when the class file is loaded, and in the transform method, the input byte code can be replaced by using a byte code operation tool, and a new byte code array is generated and returned, wherein the array is the converted byte code. In order to implement the transform method in the JVM, a Java Agent needs to be defined, and an Instrument is injected into the JVM by means of the capability of the Java Agent. The Java Instrument API interface module of the component replacement tool of the scheme comprises a Java Instrument API interface, and can implement the transform method of the ClassFileTransfomer interface in the Java Instrument API interface.
Java Agent is essentially understood to be a plug-in, which is a jar package. The Java Agent starting mode is different from the ordinary Jar package, and the ordinary Jar package is started through main functions of specified classes. However, java agents cannot be started alone and must be attached to a Java application to run. The Java Agent has two implementation modes, one is to add a Jar packet in the JVM starting parameter of an application program to realize a premain method, wherein the Jar packet is added in the JVM starting parameter of the application program to realize the loading of the Jar packet when the JVM of a target program is started, and the premain method can realize the loading of the Jar packet before the main function of the target program is operated, so that the method can realize component replacement when the byte code file of the target program is loaded in a class; another method is to implement the agentin method by calling an Attach API, where the agentin method can change and reload the bytecode of the class that has been loaded during program running, and the Attach API is used to provide the capability of JVM inter-process communication, and dynamically Attach the jar package to the Java process of the specified process through the Attach API. The Java Agent module may implement the premain method and the agentmain method, and specific implementation manners are described in detail in the embodiments described later.
It should be noted that, the component replacement tool may be constructed in different manners by using the above modules, so as to implement the function of replacing the component in different scenarios. For the sake of clarity of the solution and clarity of the layout, the specific construction method of the component replacement tool in different scenarios will be described later in connection with other embodiments.
The component replacement method provided by the embodiment of the application can include the following steps:
determining a target class in a byte code file of a target program; wherein the target class is a class in the target program for calling a component to be replaced;
determining a method meeting a preset selection condition in the target class as a method to be processed; the preset selection condition is the same as the name of a first method and is matched with a parameter list corresponding to the first method, and the first method is a method for calling the component to be replaced;
replacing the byte codes used for representing the to-be-processed method with byte codes used for representing a second method in the byte code file; wherein the second method is a method for calling a replacement component corresponding to the component to be replaced.
According to the component replacement method provided by the embodiment of the application, the target class in the byte code file of the target program is determined, the method meeting the preset selection condition is determined in the target class, and the byte code used for representing the method to be processed is replaced by the byte code used for representing the second method in the byte code file.
In the above scheme, the byte code representing the method to be processed is replaced with: the byte code used for representing the second method is used for calling the corresponding replacement component of the component to be replaced, so that the effect that the component to be replaced is replaced by the corresponding replacement component when the target program runs can be achieved; and the modification is performed at the byte code level, and has no influence on the source code after component replacement is realized. Thus, by the present solution, an automated, non-invasive replacement of components to the source code can be achieved.
A component replacement method provided in an embodiment of the present application is described below with reference to the accompanying drawings.
As shown in fig. 1, a component replacement method provided in an embodiment of the present application, applied to a component replacement tool, may include the following steps:
S101, determining a target class in a byte code file of a target program;
wherein the target class is a class in the target program for calling a component to be replaced;
the applicant found in the course of implementing the present application that: the source code of the target program is processed to realize component replacement, which may cause program operation errors and increase the subsequent maintenance difficulty of the program, while the byte code file of the target program is processed, so that the component replacement can be realized while the source code of the target program is not influenced, and the effect that the component is replaced when the program is operated is achieved; the present application thus enables component replacement by processing the bytecode file. The byte code file is compiled by a Java compiler through target program source codes.
The component to be replaced may be a component with security hole in the target program, or may be a component with update requirement in the target program, which is not limited in this application, and any component in the target program may be used as the component to be replaced. The component replacement is realized by processing the byte code file, the replacement of the byte code corresponding to the method for calling the component to be replaced in the byte code file is required to be completed, and the method corresponding to the byte code to be replaced and the class corresponding to the method are required to be known for realizing the byte code replacement. The method can determine the target class in the byte code file by identifying the name of the target class, the code of the target class, the identification corresponding to the target class and the like.
Optionally, in one implementation, the determining the target class in the bytecode file of the target program includes:
determining a target class with a specified name in a byte code file of a target program; wherein the specified name is a name of a class for calling the component to be replaced.
The method comprises the steps of replacing byte codes corresponding to a method for calling the component to be replaced in a byte code file to realize component replacement, so that a target class can be determined in the byte code file, and in specific implementation, the target class can be determined by using the class name, and the class with the same name as the class for calling the component to be replaced in the byte code file can be determined as the target class.
In practical application, when the component to be replaced has security holes or needs to be upgraded, the scheme can be implemented to replace the component to be replaced, so that the component to be replaced is determined, information of the component to be replaced can be known in advance, such as code, parameter list and name of the component to be replaced corresponding to a method for calling the component to be replaced. In some cases, the class that invokes the component to be replaced may be known in advance, so that the above steps may be performed directly with the class name that invokes the component to be replaced, and the target class with the specified name is determined in the bytecode file of the target program.
In a specific application, the class for calling the component to be replaced is not necessarily determined, and thus the class for calling the component to be replaced may be determined according to known information of the component to be replaced.
Optionally, in one implementation manner, the determining manner of the class used for calling the component to be replaced in the target program includes:
and determining the class to which the method for calling the component to be replaced belongs from the source code of the target program, and obtaining the class for calling the component to be replaced in the target program.
In a specific application, the class where the component to be replaced is located can be found in the source code by knowing the code corresponding to the method for calling the component to be replaced, but the class where the component to be replaced is located is manually found and recorded, which is complex and low in efficiency, so that the program source code can be detected, and the class where the code identical to the code corresponding to the method for calling the component to be replaced is detected, namely the class for calling the component to be replaced. At this time, the above steps may be performed based on the class name of the detected class, and the target class having the specified name may be determined in the byte code file of the target program.
By the above-described manner of determining the class to which the method for calling the component to be replaced belongs, since the class for calling the component to be replaced can be determined in the above-described manner for any target program, the efficiency of determining the class for calling the component to be replaced is improved.
S102, determining a method meeting a preset selection condition in the target class as a method to be processed;
the preset selection condition is the same as the name of a first method and is matched with a parameter list corresponding to the first method, and the first method is a method for calling the component to be replaced;
the names of the methods for calling the components to be replaced and the parameter list corresponding to the methods for calling the components to be replaced are known in advance, wherein the parameter list comprises the number of parameters and the parameter types. Specifically, the method with the same parameter type and/or parameter number as the first method may be determined as the method to be processed according to the parameter type and/or parameter number of the method with the same name as the first method determined in the target class. For example, if the first method has only 1 parameter, a method having only 1 parameter in the methods having the same name as the first method is determined as the method to be processed. In order to improve the accuracy of determining the method to be processed, the method to be processed can be determined from two dimensions by simultaneously utilizing the parameter types and the parameter numbers.
In one implementation, a method identical to a first method name in a target class in a byte code file of a target program may be acquired by a byte code operation module in a component replacement tool, and a parameter list of the method identical to the first method name is acquired. And determining whether the method with the same name as the first method is a method to be processed or not according to the number and/or the type of the parameters in the parameter list by utilizing the acquired parameter list. For example, the number of parameters in the parameter list corresponding to the first method is one, and the component replacement tool may find, as the method to be processed, a method with the number of parameters being 1 in the same method as the first method name by using the code of parametertypes.
S103, replacing the byte codes used for representing the to-be-processed method with byte codes used for representing a second method in the byte code file;
wherein the second method is a method for calling a replacement component corresponding to the component to be replaced.
In the method to be processed determined in the above step, the byte code used for representing the method to be processed is replaced by the byte code used for representing the second method, so that the component to be replaced can be replaced by the corresponding replacement component. In one implementation, the byte code representing the method to be processed may be deleted, and the byte code implementing the second method may be inserted at the same location, so as to implement component replacement.
In the scheme, the byte code can be operated by using a byte code operation module in the component replacement tool, byte code operation tools such as CGLIB, javassist, ASM and the like can be used in the byte code operation module, the byte code operation tools are not limited, and any byte code operation tool can be applied to the scheme.
Optionally, replacing the byte code used for representing the method to be processed with the byte code used for representing the second method in the byte code file further comprises:
outputting prompt information; wherein the hint information is used to characterize that the bytecode of the method to be processed has been replaced.
Illustratively, fig. 2 shows an exemplary hint information, where java.exe displayed in the first line in the hint information represents the name of the JVM virtual machine process, the second line display Fastjson Agent premain start … represents that the component replacement method starts to be implemented, and the third line display Fastjson Agent args: null, representing a parameter returned after the component replacement method is realized, intercepting a fourth line of display toJSON string method, representing that a component to be replaced has been replaced, enabling a fifth line of display ObjectMapper to be successfully created, and enabling a sixth line of display to output two attributes of a map object, wherein name is an, and age is 18; the toJSONSTING method is a method for calling the Fastjson, and the ObjectMapper is a class provided by Jackson and is used for converting java objects and json formats into each other. The two attributes of name and age of the map object are output, which indicates that the java object and json format have been converted, so that it can be verified that Fastjson has been replaced by Jackson.
The content represented by the prompt information can be various, such as component replacement start, method replacement, method interception or component replacement, etc., which can be preset; the prompt information can also be displayed in the form of popup windows and the like, the content of the prompt information and the display mode of the prompt information are not limited by the scheme, and the prompt information can be displayed through a display tool capable of communicating with the component replacement tool.
The scheme displays the prompt information to indicate that the byte codes for realizing the to-be-processed method are replaced, the program process can be visually displayed, and the use experience of a user is improved.
The component replacement method is applied to the component replacement tool, and the component replacement tool can be applied to different scenes by different construction modes of the component replacement tool, so that the component replacement method is realized, and the different construction modes of the component replacement tool and the application of the component replacement tool in different scenes are specifically introduced.
Optionally, in one implementation, the component replacement tool is a tool built based on the Java Instrument API interface.
The component replacement tool may be a program composed of a Java Instrument API interface module, a bytecode operation module, and a Java Agent module. The Java Instrument API interface module and the byte code operation module are used for realizing the component replacement method, and the Java Agent module can enable the component replacement tool to be attached to the target program for operation. The Java Agent has two implementation modes, one is to add a jar packet into JVM starting parameters of an application program to realize a premain method, and the other is to dynamically add the jar packet into a Java process of a designated process by calling an attach api to realize the Agent method.
One scenario is one in which component replacement occurs during the class loading phase. In the scenario, the loading path of the component replacement tool is added in the JAVA virtual machine JVM start parameter of the target program; the component replacement tool is started at the stage of class loading of the bytecode file of the target program in the JVM.
When creating an object, the JVM needs to load the byte code file of the target program through the class loader, so as to obtain a class object, and then the creation and initialization of the class object are completed. Therefore, the byte code is replaced when the class loading starts, and the class loading is continued after the replacement, so that the creation and the initialization of the replaced class object can be completed, and the component replacement is realized.
Realizing the component replacement method in the class loading stage requires realizing a premain method of Java Agent; the component replacement tool is equivalent to an Agent program, namely a Jar package; the premain method may implement loading component replacement tools before the main function runs. The Java Agent module in the component replacement tool can realize a premain method. Implementing the premain method requires adding a component replacement tool's boot path in the JVM boot parameters of the target program. After the starting path of the component replacement tool is added, when the JVM is started, the component replacement tool starts to work, and component replacement is achieved.
By taking a fastson as an example, a fastson class can be newly created to realize a premain method, the fastson class is the Java Agent module, a fastson transformer class is newly created to realize a transform method of a ClassFileTransformer interface, the fastson transformer class is the Java Instrument API interface module, the fastson transformer interface is called in the premain method, and the replacement of the byte code corresponding to the called fastson can be realized in the class loading stage, wherein the transform method uses a byte code operation tool to replace the byte code, and the byte code operation tool is the byte code operation module. And packing the FastjsonAgent, fastjsonTransformer file and the file corresponding to the byte code operation tool into an agent. By adding the agent. Jar package into the JVM starting parameter of the target program, the component replacement method can be completed in the loading stage of the target program class when the target program is started. Illustratively, -java agent: < path > \agent. Jar, path is an addition path, and is added according to the actual path in actual use.
Another scenario is a scenario in which component replacement is performed on a running object program. At this point the classes in the JVM have been loaded, component replacement may be performed by replacing and reloading the bytecode of the target program. Implementation of this method needs to be achieved through an agentin method, which can change and reload the bytecode of the class that has been loaded during program running. The component replacement tool corresponds to an Agent program, namely a Jar package. The Agent method is realized by a Java Agent module.
In the scheme, an Attach API is needed, the Attach API has the function of providing the communication capability between JVM processes, and the jar packet is dynamically added into the Java process of the appointed process through the Attach API, so that the Agentmain method can be realized, and the component replacement method is realized to replace the component when the program runs. After replacement, the class is reloaded, and the program runs later, with the component to be replaced having been replaced with the corresponding replacement component. The Attach API can be driven by the outside or can be called by a component replacement tool to realize the function of dynamically attaching the jar packet to the Java process of the appointed process.
By taking Fastjson as an example, a Fastjson class can be newly established to realize an Agentmain method, and a transform method of a Fastjson transform interface is added in the Agentmain method, so that the byte code corresponding to the called Fastjson can be replaced when the program runs. The above component replacement method can be implemented by the transform method of the fastjson transform interface, where the transform method of the fastjson transform interface is the same as in the above embodiment; fastjsonAgent, fastjsonTransformer and bytecode manipulation tools can be packaged as an agent. Jar, which is the component replacement tool described above. The agent. Jar package is dynamically added into the Java process of the appointed process through the Attach API, so that the replacement of the component during the running of the program can be realized.
Another scenario is one in which the target program has not yet been component replaced by a JVM loaded bytecode file. At this time, the component replacement tool may call only the byte code operation module, and implement the above component replacement method steps on the byte code file of the target program, thereby implementing component replacement. Specifically, the byte code operation tool in the byte code operation module may be CGLIB, javassist, ASM, etc., and the byte code operation tool is not limited in this scheme, and any byte code operation tool may be applied in this scheme.
The component replacement tool not only comprises a Java Instrument API interface module, a byte code operation module and a Java Agent module, but also can comprise other codes or modules required for realizing a component replacement method, such as a module for calling a main function of each module, a module for calling an Attach API, and the like.
Optionally, in one implementation, the component replacement tool is a tool built based on a bytecode operation tool interface.
In particular, the component replacement tool may be an application built based on a bytecode operation module. The component replacement tool may be applied to a scenario where a target program has not been component replaced by a bytecode file loaded by a JVM. The component replacement tool calls the byte code operation module to realize the component replacement method steps on the byte code file of the target program, thereby realizing component replacement. Specifically, the byte code operation tool in the byte code operation module may be CGLIB, javassist, ASM, etc., and the byte code operation tool is not limited in this scheme, and any byte code operation tool may be applied in this scheme. The component replacement tool in the scheme does not only comprise a byte code operation module, but also can comprise codes or modules required by other component replacement methods, such as a main function for calling the byte code operation module, and the like.
According to the component replacement method provided by the embodiment of the application, the target class in the byte code file of the target program is determined, the method meeting the preset selection condition is determined in the target class, and the byte code used for representing the method to be processed is replaced by the byte code used for representing the second method in the byte code file.
In the above scheme, the byte code representing the method to be processed is replaced with: the byte code used for representing the second method is used for calling the corresponding replacement component of the component to be replaced, so that the effect that the component to be replaced is replaced by the corresponding replacement component when the target program runs can be achieved; and the modification is performed at the byte code level, and has no influence on the source code after component replacement is realized. Thus, by the present solution, an automated, non-invasive replacement of components to the source code can be achieved.
For a better understanding of the present solution, a component replacement method will be described below by means of a specific embodiment.
In a specific embodiment, an Agent program may be developed using Java Instrument API to implement the transform method of the classfiletransform interface, which may implement the component replacement method described above using byte code replacement tools to replace fastjson components in the target program with jackson components. The ClassFileTransformer can redefine or convert each class, and particularly, the ClassFileTransformer can intercept a class file in operation, so that the replacement of byte codes corresponding to components to be replaced in the byte code file is achieved. In the transform method of the classfiletransform interface, byte codes may be replaced using a byte code manipulation tool. The Agent program is the component replacement tool. Specifically, in this embodiment, the Agent program is an Agent. Wherein FastjsonArgent is similar to the Java Agent module described above; the fastjsontransducer is similar to the Java Instrument API interface module, and can implement the transducer method of the classfiletransducer interface. The embodiment scheme can realize the premain method and realize component replacement in the loading stage of the byte code file class of the target program. The following describes the steps of replacing the Agent program implementation component in detail.
Step one, determining a class for calling the component to be replaced, and ignoring if the class is not the class for calling the component to be replaced.
This step is similar to the target class step in the bytecode file of the target program described above. Illustratively, com.alibaba.fastjson.json is a class for calling a component to be replaced, and given the class name, com.alibaba.fastjson.json can be determined in the bytecode file from the class name. There may be multiple classes in a target program, so that a class for calling the component to be replaced needs to be determined, so that a method for calling the component to be replaced in the class is found later.
Step two, obtaining class information of the class by using Java.
This step uses javaist to obtain the code corresponding to class information for the class. The javaist is a bytecode operation tool, and corresponds to the above-mentioned bytecode operation module, and can be used to obtain class information of a target class in a target program, such as information of a name, a method, an attribute, a parameter list, and the like of the target class. The javalist is only a specific example, and other bytecode operation tools can be applied to the scheme, such as CGLIB, ASM, etc., and the scheme does not limit the bytecode operation tools
And thirdly, acquiring a method of class declaration by using Java, traversing the method in sequence, and determining a method name for calling the component to be replaced.
The step is similar to the step of determining the method meeting the preset selection condition in the target class and is used as the step of the to-be-processed method, namely, the method meeting the parameter list corresponding to the method for calling the to-be-replaced component and having the same name as the method for calling the to-be-replaced component in the target class is found. The parameter list comprises the number of parameters and the type of parameters, and whether the method with the same name as the method for calling the component to be replaced is a method to be processed can be determined through the number of parameters and/or the type of parameters.
The toJSONString method is a method to be processed, and step two has already obtained the method name and the parameter list corresponding to the method in the target class, so the method to be processed can be determined according to the parameter list information of the toJSONString method known in advance. In one implementation, the toJSONString method is one of the API, toJSONString methods for calling Fastjson, and the condition that the number of parameters is 1 is used to find the corresponding toJSONString method.
And step four, modifying the realization of the method, and calling the API of the Jackson to realize the same function instead.
This step is similar to the above described step of replacing the bytecode used to characterize the method to be processed with the bytecode used to characterize the second method in the bytecode file, and the bytecode operation tool is used to replace the bytecode implementing the API calling Fastjson with the bytecode implementing the API calling Jackson.
And step five, returning the modified byte codes.
After the modified byte codes are returned, the JVM carries out the steps of subsequent class loading and the like on the modified byte code files, and the program is normally operated. As shown in FIG. 3, a system block diagram of the scheme is shown, in the figure, an Agent program is a component replacement tool, a FastjsonTransformer is a ClassFileTransformer interface in the Java Instrument API interface module, application is an Application, an internal implementation method of the target program is changed through the Agent program, a transform method of the ClassFileTransformer interface is implemented after the Agent program runs, a fastjson component is replaced by a Jackson component, but the name of a class is unchanged, and a class name com. When the program actually runs, the component has been replaced, for example, the original fastjson component is replaced with a Jackson component in the figure, but the source code is not changed. The fastjson component and Jackson component in the above-described scheme are merely examples, and other components may also be applied to the above-described scheme, which is not limited in this embodiment.
According to the scheme provided by the embodiment, the byte codes of the components to be replaced in the implementation call can be replaced by the byte codes of the corresponding implementation call replacement components, for example, the byte codes of the fastjson components are replaced by the byte codes of the Jackson components, and therefore the effect that the fastjson components are replaced by the Jackson components in the program running process can be achieved. And since the modification is performed at the byte code level, no effect is exerted on the source code after component replacement is implemented. Thus, an automated, non-intrusive replacement of components with respect to source code may be achieved.
The embodiment of the application also provides a component replacing device, as shown in fig. 4, which comprises:
a first determining module 401, configured to determine a target class in a bytecode file of a target program; wherein the target class is a class in the target program for calling a component to be replaced;
a second determining module 402, configured to determine, as a method to be processed, a method that meets a predetermined selection condition in the target class; the preset selection condition is the same as the name of a first method and is matched with a parameter list corresponding to the first method, and the first method is a method for calling the component to be replaced;
A replacing module 403, configured to replace, in the bytecode file, a bytecode used for characterizing the method to be processed with a bytecode used for characterizing a second method; wherein the second method is a method for calling a replacement component corresponding to the component to be replaced.
According to the code processing method, the target class in the byte code file of the target program is determined, the method meeting the preset selection condition is determined in the target class, and the byte code used for representing the method to be processed is replaced by the byte code used for representing the second method in the byte code file.
The byte code of the method to be processed is the byte code for realizing the replaced component, and the second method is a method for calling the replaced component corresponding to the replaced component, and the two methods are replaced, so that the replaced component called in the program can be replaced by the corresponding replaced component. And since the modification is performed at the byte code level, no effect is exerted on the source code after component replacement is implemented. Thus, an automated, non-intrusive replacement of components with respect to source code may be achieved.
Optionally, the first determining module includes:
A determining unit configured to determine, in a bytecode file of a target program, a target class having a specified name; wherein the specified name is a name of a class for calling the component to be replaced.
Optionally, the determining manner of the class of the component to be replaced in the target program includes:
and determining the class to which the method for calling the component to be replaced belongs from the source code of the target program, and obtaining the class for calling the component to be replaced in the target program.
Optionally, the component replacement tool is a tool built based on the Java Instrument API interface.
Optionally, the loading path of the component replacement tool is added in a JAVA virtual machine JVM start parameter of the target program;
the component replacement tool is started at the stage of class loading of the bytecode file of the target program in the JVM.
Optionally, the apparatus further includes:
the output module is used for outputting prompt information; wherein the hint information is used to characterize that the bytecode of the method to be processed has been replaced.
The embodiment of the application also provides an electronic device, as shown in fig. 5, including a processor 501, a communication interface 502, a memory 503, and a communication bus 504, where the processor 501, the communication interface 502, and the memory 503 complete communication with each other through the communication bus 504,
A memory 503 for storing a computer program;
the processor 501 is configured to implement the steps of any of the component replacement methods described above when executing the program stored in the memory 503.
The communication bus mentioned by the above terminal may be a peripheral component interconnect standard (Peripheral Component Interconnect, abbreviated as PCI) bus or an extended industry standard architecture (Extended Industry Standard Architecture, abbreviated as EISA) bus, etc. The communication bus may be classified as an address bus, a data bus, a control bus, or the like. For ease of illustration, the figures are shown with only one bold line, but not with only one bus or one type of bus.
The communication interface is used for communication between the terminal and other devices.
The memory may include random access memory (Random Access Memory, RAM) or non-volatile memory (non-volatile memory), such as at least one disk memory. Optionally, the memory may also be at least one memory device located remotely from the aforementioned processor.
The processor may be a general-purpose processor, including a central processing unit (Central Processing Unit, CPU for short), a network processor (Network Processor, NP for short), etc.; but also digital signal processors (Digital Signal Processor, DSP for short), application specific integrated circuits (Application Specific Integrated Circuit, ASIC for short), field-programmable gate arrays (Field-Programmable Gate Array, FPGA for short) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
In yet another embodiment provided herein, there is also provided a computer readable storage medium having a computer program stored therein, which when executed by a processor, implements the component replacement method of any of the above embodiments.
In yet another embodiment provided herein, there is also provided a computer program product containing instructions that, when run on a computer, cause the computer to perform the component replacement method of any of the above embodiments.
In the above embodiments, it may be implemented in whole or in part by software, hardware, firmware, or any combination thereof. When implemented in software, may be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When loaded and executed on a computer, produces a flow or function in accordance with embodiments of the present application, in whole or in part. The computer may be a general purpose computer, a special purpose computer, a computer network, or other programmable apparatus. The computer instructions may be stored in or transmitted from one computer-readable storage medium to another, for example, by wired (e.g., coaxial cable, optical fiber, digital Subscriber Line (DSL)), or wireless (e.g., infrared, wireless, microwave, etc.). The computer readable storage medium may be any available medium that can be accessed by a computer or a data storage device such as a server, data center, etc. that contains an integration of one or more available media. The usable medium may be a magnetic medium (e.g., floppy Disk, hard Disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid State Disk (SSD)), etc.
It is noted that relational terms such as first and second, and the like are used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Moreover, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
In this specification, each embodiment is described in a related manner, and identical and similar parts of each embodiment are all referred to each other, and each embodiment mainly describes differences from other embodiments. In particular, for the device embodiments, since they are substantially similar to the method embodiments, the description is relatively simple, and reference is made to the description of the method embodiments in part.
The foregoing description is only of the preferred embodiments of the present application and is not intended to limit the scope of the present application. Any modifications, equivalent substitutions, improvements, etc. that are within the spirit and principles of the present application are intended to be included within the scope of the present application.
Claims (10)
1. A component replacement method, applied to a component replacement tool, the method comprising:
determining a target class in a byte code file of a target program; wherein the target class is a class in the target program for calling a component to be replaced;
determining a method meeting a preset selection condition in the target class as a method to be processed; the predetermined selection condition is the same as the name of the first method and is matched with the parameter list corresponding to the first method; the first method is a method for calling the component to be replaced;
replacing the byte codes used for representing the to-be-processed method with byte codes used for representing a second method in the byte code file; wherein the second method is a method for calling a replacement component corresponding to the component to be replaced.
2. The component replacement method according to claim 1, wherein the determining the target class in the bytecode file of the target program includes:
Determining a target class with a specified name in a byte code file of a target program; wherein the specified name is a name of a class for calling the component to be replaced.
3. The component replacement method according to claim 1, wherein the determination means for calling the class of the component to be replaced in the object program includes:
and determining the class to which the method for calling the component to be replaced belongs from the source code of the target program, and obtaining the class for calling the component to be replaced in the target program.
4. The component replacement method according to claim 1 or 2, wherein,
the component replacement tool is a tool built based on the JavaInstrument API interface.
5. The component replacement method according to claim 1 or 2, wherein,
the loading path of the component replacement tool is added in the JVM starting parameter of the JAVA virtual machine of the target program;
the component replacement tool is started at the stage of class loading of the bytecode file of the target program in the JVM.
6. The component replacement method according to claim 1 or 2, wherein after replacing, in the bytecode file, the bytecode for characterizing the method to be processed with the bytecode for characterizing the second method, the method further comprises:
Outputting prompt information; wherein the hint information is used to characterize that the bytecode of the method to be processed has been replaced.
7. A component replacement device for use with a component replacement tool, the device comprising:
the first determining module is used for determining a target class in a byte code file of the target program; wherein the target class is a class in the target program for calling a component to be replaced;
the second determining module is used for determining a method meeting a preset selection condition in the target class as a method to be processed; the preset selection condition is the same as the name of a first method and is matched with a parameter list corresponding to the first method, and the first method is a method for calling the component to be replaced;
the replacing module is used for replacing the byte codes used for representing the to-be-processed method with byte codes used for representing a second method in the byte code file; wherein the second method is a method for calling a replacement component corresponding to the component to be replaced.
8. The component replacement device of claim 7, wherein the first determination module comprises:
A determining unit configured to determine, in a bytecode file of a target program, a target class having a specified name; wherein the specified name is a name of a class for calling the component to be replaced.
9. The electronic equipment is characterized by comprising a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory are communicated with each other through the communication bus;
a memory for storing a computer program;
a processor for implementing the method of any of claims 1-6 when executing a program stored on a memory.
10. A computer readable storage medium, characterized in that the computer readable storage medium has stored therein a computer program which, when executed by a processor, implements the method of any of claims 1-6.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202310061304.8A CN116243911A (en) | 2023-01-20 | 2023-01-20 | Component replacement method and device, electronic equipment and storage medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202310061304.8A CN116243911A (en) | 2023-01-20 | 2023-01-20 | Component replacement method and device, electronic equipment and storage medium |
Publications (1)
Publication Number | Publication Date |
---|---|
CN116243911A true CN116243911A (en) | 2023-06-09 |
Family
ID=86627121
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202310061304.8A Pending CN116243911A (en) | 2023-01-20 | 2023-01-20 | Component replacement method and device, electronic equipment and storage medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN116243911A (en) |
-
2023
- 2023-01-20 CN CN202310061304.8A patent/CN116243911A/en active Pending
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN110096338B (en) | Intelligent contract execution method, device, equipment and medium | |
US6571388B1 (en) | Building a custom software environment including pre-loaded classes | |
US8739190B2 (en) | Determining an extension to use to process an input object to a call in a program | |
US9274768B2 (en) | Runtime code hooking for print driver and functionality testing | |
US20160232017A1 (en) | System and Method for Reloading Constructors | |
US8438577B2 (en) | Method and system for extending scripting languages | |
CN108255545B (en) | Function calling method and device between components and modular architecture system | |
US10635504B2 (en) | API versioning independent of product releases | |
JP2005293578A (en) | Test case inheritance controlled via attribute | |
CN110113391B (en) | Client online method and device and client operation method and device | |
CN111290806A (en) | Calling method and device of application program interface, computer equipment and storage medium | |
CN111625225A (en) | Program specified data output method and device | |
CN111176722A (en) | Method and device for detecting file version of third-party library and storage medium | |
CN110609687A (en) | Compiling method, device, electronic equipment and storage medium | |
US20080141219A1 (en) | Multiple inheritance facility for java script language | |
CN114895935A (en) | Method and device for flashing vehicle ECU, electronic equipment and storage medium | |
CN111399842B (en) | Code compiling method and device | |
CN113032004B (en) | Method, apparatus and program product for managing development jobs in a development environment | |
CN111488286A (en) | Method and device for independently developing Android module | |
CN116243911A (en) | Component replacement method and device, electronic equipment and storage medium | |
CN110275710B (en) | Java local interface consistency checking method and system, storage medium and terminal | |
CN114356290A (en) | Data processing method and device and computer readable storage medium | |
EP1715413A1 (en) | Method and system for extending scripting languages | |
CN113760360A (en) | File generation method, device, equipment and storage medium | |
CN112052051A (en) | Plug-in processing method, device, equipment and storage medium |
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 |