Object conversion method, system and medium based on runtime bytecode editing

Through runtime bytecode editing technology, the object converter is dynamically generated, combined with object cache, and the problem of poor object conversion performance and code intrusion in the existing technology is solved, high-performance object conversion is realized, and reflection mechanism and compile-time plug-ins are avoided. rely.

CN116719531BActive Publication Date: 2024-11-26欧冶工业品股份有限公司
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202310709097.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-06-14
Publication Date
2024-11-26
Estimated Expiration
2043-06-14

AI Technical Summary

Technical Problem

Existing object conversion schemes have poor performance in high-frequency scenarios, and the reflection mechanism leads to performance losses. The solution based on compile-time code generation requires additional code definition and compilation plug-in configuration, which cannot solve the problems of code intrusion and compilation dependencies.

Method used

Runtime bytecode editing technology is used to dynamically generate object converters, and combined with object caching technology, binary string content is generated according to JVM specifications, realizing high-performance object conversion, avoiding performance losses of reflection mechanisms and code intrusion.

Benefits of technology

It solves the performance problem when object conversion is not determined in type, reduces the performance loss of bytecode generation and dynamic class loading, avoids code intrusion and plug-in dependence during compilation, and realizes high-performance object conversion.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116719531B_ABST
    Figure CN116719531B_ABST
Patent Text Reader

Abstract

The present invention provides an object conversion method, system, medium and device based on runtime bytecode editing, including: when an object converter for converting from a source object to a target object already exists in the cache, the object converter is directly called to complete the conversion task; otherwise, an object converter is dynamically generated during program execution according to the bytecode specification of the JVM, and high-performance object conversion is achieved in combination with object cache technology; the bytecode of the JVM is a binary string stored in the memory, the bytecode specification of the JVM specifies the format of the binary string, and the object converter is a binary string content generated according to the bytecode specification of the JVM. The present invention solves the problems of code intrusion and additional compile-time plug-in dependency by adopting runtime bytecode generation technology.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to an object conversion method, system and medium based on runtime bytecode editing. Background Art

[0002] There are currently two mainstream object conversion solutions: one is based on the reflection mechanism (typical tools are: BeanUtils tool in Spring framework and BeanUtils tool in Apache Common framework), which obtains the attribute information of the object through runtime reflection and completes the copy of the attribute value. The disadvantage of this solution is that each conversion operation requires runtime reflection, which will cause a large performance loss for scenarios with high conversion frequency (such as application servers with large concurrency) (in simulated performance tests, the operation time is about ten times different), and in extreme cases may affect the normal processing of the service; the other is based on the technical implementation of compile-time code generation (typical tools are: MapStruct), which solves the performance problem of the reflection solution, but usually requires inserting additional code definitions and annotations in the code, and requires executing additional annotation processors at compile time, which usually means that additional compilation plug-ins are needed and additional project configuration is required. When the program is preheated and running stably, its theoretical performance is almost the same as that of the present invention.

[0003] Patent document CN106933554A (application number: 201511023820.3) discloses a class conversion method and device. The method includes: obtaining a source class object and a target class object, wherein the source class object is an object of the source class and the target class object is an object of the target class; obtaining a mapping relationship between the source class object and the target class object; and performing conversion between the source class object and the target class object according to the mapping relationship and the mapper through a class converter, wherein the class converter pre-encapsulates a class conversion method, the mapper pre-encapsulates a class conversion rule, and the class conversion method is used to perform conversion between classes according to the class conversion rule. However, the patent does not use runtime bytecode generation technology, and cannot solve the problems of code intrusion and additional compile-time plug-in dependency. Summary of the invention

[0004] In view of the defects in the prior art, the purpose of the present invention is to provide an object conversion method, system and medium based on runtime bytecode editing.

[0005] The object conversion method based on runtime bytecode editing provided by the present invention includes: when an object converter for converting a source object to a target object already exists in the cache, the object converter is directly called to complete the conversion task; otherwise, the object converter is dynamically generated according to the bytecode specification of the JVM when the program is running, and high-performance object conversion is realized in combination with the object cache technology;

[0006] The JVM bytecode is a binary string stored in the memory. The JVM bytecode specification specifies the format of the binary string. The object converter is a binary string content generated according to the JVM bytecode specification.

[0007] Preferably, the object conversion method includes:

[0008] Step S1: extracting attribute or method information according to the types of the source object and the target object to be converted;

[0009] Step S2: Generate the required object converter at runtime through bytecode editing technology according to the extracted attribute and method information, and put it into the cache after instantiation;

[0010] Step S3: Call the generated object converter to complete the conversion task from the source object to the target object.

[0011] Preferably, step S2 comprises:

[0012] Step S2.1: Analyze the properties and methods of the target object and filter out public properties and setter methods;

[0013] Step S2.2: Find the corresponding public attributes and getter methods in the attributes and methods of the source object according to the filtered public attributes and setter methods;

[0014] Step S2.3: Generate the bytecode of a new object converter based on the public properties and setter methods of the target object and the public properties and getter methods of the source object. The bytecode of the new object converter includes a method that receives the source object and the target object as parameters and returns the processed target object. The method logic calls the getter method of the source object to obtain the corresponding property value, and calls the setter method of the target object to assign the value.

[0015] Preferably, the step S2.3 comprises:

[0016] Through Java reflection technology, the class definitions of the target object and the source object are obtained, and the names and field types / parameter types of the corresponding properties and methods are found. The following processing is performed on each public property and setter method in the target object: Find out whether there are entries with the same name and type in the public properties and getter methods in the source object. If so, generate a method that assigns the source object field to the target field object. After searching for methods for all fields, combine the generated converter method bytecode into the bytecode of a converter class, and finally generate a complete converter bytecode.

[0017] The object conversion system based on runtime bytecode editing provided by the present invention includes: when an object converter for converting a source object to a target object already exists in the cache, the object converter is directly called to complete the conversion task; otherwise, the object converter is dynamically generated according to the bytecode specification of the JVM when the program is running, and high-performance object conversion is realized in combination with the object cache technology;

[0018] The JVM bytecode is a binary string stored in the memory. The JVM bytecode specification specifies the format of the binary string. The object converter is a binary string content generated according to the JVM bytecode specification.

[0019] Preferably, the object conversion system comprises:

[0020] Module M1: extracting attribute or method information according to the types of source and target objects to be converted;

[0021] Module M2: Based on the extracted attribute and method information, the required object converter is generated at runtime through bytecode editing technology, and is instantiated and placed in the cache;

[0022] Module M3: Call the generated object converter to complete the conversion task from source object to target object.

[0023] Preferably, the module M2 comprises:

[0024] Module M2.1: Analyze the properties and methods of the target object and filter out public properties and setter methods;

[0025] Module M2.2: Find the corresponding public properties and getter methods in the properties and methods of the source object according to the filtered public properties and setter methods;

[0026] Module M2.3: Generate the bytecode of a new object converter based on the public properties and setter methods of the target object and the public properties and getter methods of the source object. The bytecode of the new object converter contains a method that receives the source object and the target object as parameters and returns the processed target object. The method logic calls the getter method of the source object to obtain the corresponding property value, and calls the setter method of the target object to assign the value.

[0027] Preferably, the module M2.3 includes:

[0028] Through Java reflection technology, the class definitions of the target object and the source object are obtained, and the names and field types / parameter types of the corresponding properties and methods are found. The following processing is performed on each public property and setter method in the target object: Find out whether there are entries with the same name and type in the public properties and getter methods in the source object. If so, generate a method that assigns the source object field to the target field object. After searching for methods for all fields, combine the generated converter method bytecode into the bytecode of a converter class, and finally generate a complete converter bytecode.

[0029] According to the computer-readable storage medium storing a computer program provided by the present invention, when the computer program is executed by a processor, the steps of the object conversion method based on runtime bytecode editing are implemented.

[0030] The electronic device provided according to the present invention comprises a memory, a processor and a computer program stored in the memory and executable on the processor. When the computer program is executed by the processor, the steps of the object conversion method based on runtime bytecode editing are implemented.

[0031] Compared with the prior art, the present invention has the following beneficial effects:

[0032] (1) The present invention adopts runtime bytecode generation technology to dynamically generate an object converter, thereby solving the problem that when converting between objects of uncertain types, the conversion operation can only be performed through a reflection mechanism with poor performance;

[0033] (2) The present invention solves the performance loss problem caused by bytecode generation and dynamic class loading process by using local cache to store dynamically generated object converters.

[0034] (3) The present invention solves the problems of code intrusion and additional compile-time plug-in dependency by adopting runtime bytecode generation technology. BRIEF DESCRIPTION OF THE DRAWINGS

[0035] Other features, objects and advantages of the present invention will become more apparent from the detailed description of non-limiting embodiments made with reference to the following drawings:

[0036] Figure 1 The present invention is a flow chart of a high-performance object conversion method for a JVM platform based on runtime bytecode editing. DETAILED DESCRIPTION

[0037] The present invention is described in detail below in conjunction with specific embodiments. The following embodiments will help those skilled in the art to further understand the present invention, but are not intended to limit the present invention in any form. It should be noted that, for those of ordinary skill in the art, several changes and improvements can also be made without departing from the concept of the present invention. These all belong to the protection scope of the present invention.

[0038] Embodiment 1:

[0039] The present invention provides an object conversion method based on runtime bytecode editing. In program development on the JVM platform, it is often necessary to perform conversions between objects of different types at a high frequency (copying the same field data to each other). According to the JVM bytecode specification, a converter object is dynamically generated when the program is running and combined with object caching technology to achieve high-performance object conversion.

[0040] like Figure 1 , specifically including the following steps:

[0041] Step 1: Convert the source object to the target object. First, check whether there is a corresponding object converter in the cache. If there is, directly call the converter to complete the conversion task. If there is no corresponding object converter in the cache, continue with the subsequent steps.

[0042] Step 2: Extract attribute or method information according to the types of the source object and target object to be converted;

[0043] Step 3: Based on the extracted attribute and method information, the required object converter is generated at runtime through bytecode editing technology and is instantiated and placed in the cache;

[0044] Step 4: Call the generated object converter to complete the conversion task from the source object to the target object.

[0045] The step 3 comprises the following steps:

[0046] Step 3.1: Analyze the properties and methods of the target object and filter out public properties and setter methods;

[0047] Step 3.2: Find the corresponding public properties and getter methods in the properties and methods of the source object according to the public properties and setter methods filtered out in the previous step;

[0048] Step 3.3: According to the JVM specification, generate a new bytecode of the object converter. The object contains a method that receives the source object and the target object as parameters and returns the processed target object. The method logic calls the getter method of the source object to obtain the corresponding attribute value, and calls the setter method of the target object to assign the value.

[0049] The step 3.3 comprises:

[0050] Through Java reflection technology, the class definitions of the target object and the source object are obtained, and the names and field types / parameter types of the corresponding properties and methods are found. The following processing is performed on each public property and setter method in the target object: Find out whether there are entries with the same name and type in the public properties and getter methods in the source object. If so, generate a method that assigns the source object field to the target field object. After searching for methods for all fields, combine the generated converter method bytecode into the bytecode of a converter class, and finally generate a complete converter bytecode.

[0051] The present invention provides a computer-readable storage medium storing a computer program, wherein when the computer program is executed by a processor, the steps of the object conversion method based on runtime bytecode editing are implemented.

[0052] The present invention provides an electronic device, comprising a memory, a processor and a computer program stored in the memory and executable on the processor. When the computer program is executed by the processor, the steps of the object conversion method based on runtime bytecode editing are implemented.

[0053] Specific examples are:

[0054] Define two classes: SimpleTestBeanSrc as the source object type, SimpleTestBeanDist as the target object type. To make the logic clearer, the two classes are defined as the same structure: public classSimpleTestBeanDist {private String filedA;private int filedB;public StringgetFiledA() {return filedA;}public void setFiledA(String filedA){this.filedA = filedA;}public int getFiledB() {return filedB;}public voidsetFiledB(int filedB) {this.filedB = filedB;}}

[0055] Accordingly, through the above steps, the generated processor conversion method bytecode is translated into the following instruction sequence (including comments):

[0056] aload_2

[0057] checkcast #12 / / class io / vulcan / bean / beans / SimpleTestBeanDist

[0058] aload_1

[0059] checkcast #14 / / class io / vulcan / bean / beans / SimpleTestBeanSrc

[0060] invokevirtual #18 / / Method io / vulcan / bean / beans / SimpleTestBeanSrc.getFiledA:()Ljava / lang / String;

[0061] checkcast #20 / / class java / lang / String

[0062] invokevirtual #24 / / Method io / vulcan / bean / beans / SimpleTestBeanDist.setFiledA:(Ljava / lang / String;)V

[0063] aload_2

[0064] checkcast #12 / / class io / vulcan / bean / beans / SimpleTestBeanDist

[0065] aload_1

[0066] checkcast #14 / / class io / vulcan / bean / beans / SimpleTestBeanSrc

[0067] invokevirtual #28 / / Method io / vulcan / bean / beans / SimpleTestBeanSrc.getFiledB:()I

[0068] invokevirtual #32 / / Method io / vulcan / bean / beans / SimpleTestBeanDist.setFiledB:(I)V

[0069] aload_2

[0070] areturn

[0071] Embodiment 2:

[0072] The present invention also provides an object conversion system based on runtime bytecode editing. The object conversion system based on runtime bytecode editing can be implemented by executing the process steps of the object conversion method based on runtime bytecode editing, that is, those skilled in the art can understand the object conversion method based on runtime bytecode editing as a preferred implementation of the object conversion system based on runtime bytecode editing.

[0073] The object conversion system based on runtime bytecode editing provided by the present invention includes: when an object converter for converting from a source object to a target object already exists in the cache, the object converter is directly called to complete the conversion task; otherwise, an object converter is dynamically generated during program execution according to the bytecode specification of the JVM, and high-performance object conversion is achieved in combination with object caching technology; the bytecode of the JVM is a binary string stored in the memory, the bytecode specification of the JVM stipulates the format of the binary string, and the object converter is a binary string content generated according to the bytecode specification of the JVM.

[0074] The object conversion system includes: module M1: extracting attribute or method information according to the types of source objects and target objects to be converted; module M2: generating the required object converter at runtime through bytecode editing technology based on the extracted attribute and method information, and placing it in the cache after instantiation; module M3: calling the generated object converter to complete the conversion task from source object to target object.

[0075] The module M2 includes: module M2.1: analyzing the properties and methods of the target object, filtering out public properties and setter methods; module M2.2: searching for corresponding public properties and getter methods in the properties and methods of the source object according to the filtered public properties and setter methods; module M2.3: generating the bytecode of a new object converter based on the public properties and setter methods of the target object and the public properties and getter methods of the source object. The bytecode of the new object converter includes a method that receives the source object and the target object as parameters and returns the processed target object. The method logic calls the getter method of the source object to obtain the corresponding property value, and calls the setter method of the target object to assign the value.

[0076] The module M2.3 includes: obtaining the class definitions of the target object and the source object through Java reflection technology, finding the names and field types / parameter types of the corresponding properties and methods, and performing the following processing on each public property and setter method in the target object: finding whether there are entries with the same name and type in the public properties and getter methods in the source object, and if so, generating a method to assign the source object field to the target field object, after searching for the methods of all fields, combining the generated converter method bytecode into the bytecode of a converter class, and finally generating a complete converter bytecode.

[0077] Those skilled in the art know that, in addition to implementing the system, device and its various modules provided by the present invention in a purely computer-readable program code, it is entirely possible to implement the same program in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers and embedded microcontrollers by logically programming the method steps. Therefore, the system, device and its various modules provided by the present invention can be considered as a hardware component, and the modules included therein for implementing various programs can also be considered as structures within the hardware component; the modules for implementing various functions can also be considered as both software programs for implementing the method and structures within the hardware component.

[0078] The above describes the specific embodiments of the present invention. It should be understood that the present invention is not limited to the above specific embodiments, and those skilled in the art can make various changes or modifications within the scope of the claims, which does not affect the essence of the present invention. In the absence of conflict, the embodiments of the present application and the features in the embodiments can be combined with each other arbitrarily.

Claims

1. An object conversion method based on runtime bytecode editing, characterized in that: include: If an object converter for converting source objects to target objects already exists in the cache, the object converter is directly called to complete the conversion task; otherwise, an object converter is dynamically generated during program execution according to the JVM bytecode specification, and high-performance object conversion is achieved in combination with object caching technology; The JVM bytecode is a binary string stored in memory. The JVM bytecode specification specifies the format of the binary string. The object converter is a binary string content generated according to the JVM bytecode specification. include: Step S1: extracting attribute or method information according to the types of the source object and the target object to be converted; Step S2: Generate the required object converter at runtime through bytecode editing technology according to the extracted attribute and method information, and put it into the cache after instantiation; Step S3: calling the generated object converter to complete the conversion task from the source object to the target object; The step S2 comprises: Step S2.1: Analyze the properties and methods of the target object and filter out public properties and setter methods; Step S2.2: Find the corresponding public attributes and getter methods in the attributes and methods of the source object according to the filtered public attributes and setter methods; Step S2.3: Generate the bytecode of a new object converter based on the public properties and setter methods of the target object and the public properties and getter methods of the source object. The bytecode of the new object converter includes a method that receives the source object and the target object as parameters and returns the processed target object. The method logic calls the getter method of the source object to obtain the corresponding property value and calls the setter method of the target object to assign the value. The step S2.3 comprises: Get the class definitions of the target object and the source object through Java reflection technology, find the names and field types / parameter types of the corresponding properties and methods, and perform the following processing on each public property and setter method in the target object: Find whether there are entries with the same name and type in the public properties and getter methods in the source object. If so, generate a method to assign the source object field to the target field object. After searching for methods of all fields, combine the generated converter method bytecodes into the bytecode of a converter class, and finally generate a complete converter bytecode; Define two classes: SimpleTestBeanSrc as the source object type and SimpleTestBeanDist as the target object type. The two classes are defined as the same structure. Translate the generated converter method bytecode into a sequence of instructions.

2. An object conversion system based on runtime bytecode editing, characterized in that: include: If an object converter for converting source objects to target objects already exists in the cache, the object converter is directly called to complete the conversion task; otherwise, an object converter is dynamically generated during program execution according to the JVM bytecode specification, and high-performance object conversion is achieved in combination with object caching technology; The JVM bytecode is a binary string stored in memory. The JVM bytecode specification specifies the format of the binary string. The object converter is a binary string content generated according to the JVM bytecode specification. include: Module M1: extracting attribute or method information according to the types of source and target objects to be converted; Module M2: Based on the extracted attribute and method information, the required object converter is generated at runtime through bytecode editing technology, and is instantiated and placed in the cache; Module M3: Call the generated object converter to complete the conversion task from source object to target object; The module M2 comprises: Module M2.1: Analyze the properties and methods of the target object and filter out public properties and setter methods; Module M2.2: Find the corresponding public properties and getter methods in the properties and methods of the source object according to the filtered public properties and setter methods; Module M2.3: Generate the bytecode of a new object converter based on the public properties and setter methods of the target object and the public properties and getter methods of the source object. The bytecode of the new object converter contains a method that receives the source object and the target object as parameters and returns the processed target object. The method logic calls the getter method of the source object to obtain the corresponding attribute value, and calls the setter method of the target object to assign the value. The module M2.3 includes: Get the class definitions of the target object and the source object through Java reflection technology, find the names and field types / parameter types of the corresponding properties and methods, and perform the following processing on each public property and setter method in the target object: Find whether there are entries with the same name and type in the public properties and getter methods in the source object. If so, generate a method to assign the source object field to the target field object. After searching for methods of all fields, combine the generated converter method bytecodes into the bytecode of a converter class, and finally generate a complete converter bytecode; Define two classes: SimpleTestBeanSrc as the source object type and SimpleTestBeanDist as the target object type. The two classes are defined as the same structure. Translate the generated converter method bytecode into a sequence of instructions.

3. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the object conversion method based on runtime bytecode editing according to claim 1 are implemented.

4. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the computer program is executed by a processor, the steps of the object conversion method based on runtime bytecode editing according to claim 1 are implemented.