Method and system for generating a converter
By dynamically generating an object converter at runtime and combining it with object caching technology, the problems of performance loss and code intrusion in the object conversion process in the existing technology are solved, and high-performance and low-intrusion object conversion effects are achieved.
Patent Information
- Application Number
- CN202411446433.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-14
- Publication Date
- 2025-01-21
AI Technical Summary
The prior art has problems of performance losses and code intrusion in the object conversion process, especially in high concurrency scenarios, the performance problems of the reflection mechanism and the dependency problems of the compile-time plug-in are difficult to solve.
Runtime bytecode editing technology is used to dynamically generate object converters, and combined with object caching technology, to achieve high-performance object conversion, avoiding the performance loss of the reflection mechanism and the dependence on compile-time plug-ins.
By dynamically generated an object converter, the performance problems of the type uncertain object conversion were solved, reducing the dependence of code intrusion and compilation period plug -in, and improving the performance and efficiency of the object conversion.
Smart Images

Figure CN119336338A_ABST
Abstract
Description
[0001] This application is a divisional application of the following original application:
[0002] Filing date of original application: June 14, 2023
[0003] Application number of the original application: 202310709097.2
[0004] The title of the invention of the original application: Object conversion method, system, medium and device based on runtime bytecode editing Technical Field
[0005] The present invention relates to the field of computer technology, and in particular to a method and system for generating a converter. Background Art
[0006] 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.
[0007] 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
[0008] 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.
[0009] 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;
[0010] 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.
[0011] Preferably, the object conversion method includes:
[0012] Step S1: extracting attribute or method information according to the types of the source object and the target object to be converted;
[0013] 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;
[0014] Step S3: Call the generated object converter to complete the conversion task from the source object to the target object.
[0015] Preferably, step S2 comprises:
[0016] Step S2.1: Analyze the properties and methods of the target object and filter out public properties and setter methods;
[0017] 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;
[0018] Step S2.3: Generate the bytecode of a new object converter based on the public attributes and setter methods of the target object and the public attributes 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 attribute value, and calls the setter method of the target object to assign the value.
[0019] Preferably, the step S2.3 comprises:
[0020] 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 to assign the source object field to the target field object. After searching for methods for all fields, combine the generated converter method bytecodes into the bytecode of a converter class, and finally generate a complete converter bytecode.
[0021] 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 when the program is running according to the bytecode specification of the JVM, and high-performance object conversion is realized in combination with the object cache technology;
[0022] 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.
[0023] Preferably, the object conversion system comprises:
[0024] Module M1: extracting attribute or method information according to the types of source and target objects to be converted;
[0025] 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;
[0026] Module M3: Call the generated object converter to complete the conversion task from source object to target object.
[0027] Preferably, the module M2 comprises:
[0028] Module M2.1: Analyze the properties and methods of the target object and filter out public properties and setter methods;
[0029] 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;
[0030] 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.
[0031] Preferably, the module M2.3 includes:
[0032] 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 to assign the source object field to the target field object. After searching for methods for all fields, combine the generated converter method bytecodes into the bytecode of a converter class, and finally generate a complete converter bytecode.
[0033] 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.
[0034] 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.
[0035] Compared with the prior art, the present invention has the following beneficial effects:
[0036] (1) The present invention dynamically generates an object converter by using runtime bytecode generation technology, 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;
[0037] (2) The present invention solves the performance loss problem caused by bytecode generation and dynamic class loading processes by using local cache to store dynamically generated object converters.
[0038] (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
[0039] 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:
[0040] 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
[0041] 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.
[0042] Embodiment 1:
[0043] The present invention provides an object conversion method based on runtime bytecode editing. In program development on a 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.
[0044] like Figure 1 , specifically including the following steps:
[0045] 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.
[0046] Step 2: Extract attribute or method information according to the types of the source object and target object to be converted;
[0047] 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;
[0048] Step 4: Call the generated object converter to complete the conversion task from the source object to the target object.
[0049] The step 3 comprises the following steps:
[0050] Step 3.1: Analyze the properties and methods of the target object and filter out public properties and setter methods;
[0051] 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;
[0052] 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.
[0053] The step 3.3 comprises:
[0054] 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.
[0055] 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.
[0056] 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.
[0057] Specific examples are:
[0058] Define two classes: SimpleTestBeanSrc as the source object type and SimpleTestBeanDist as the target object type. To make the logic clearer, the two classes are defined as the same structure:
[0059] public class SimpleTestBeanDist{
[0060] private String filedA;
[0061] private int filedB;
[0062]
[0063] Accordingly, through the processing of the above steps, the bytecode of the generated processor conversion method is translated into the following instruction sequence (including comments):
[0064] aload_2
[0065] checkcast#12 / / class io / vulcan / bean / beans / SimpleTestBeanDist
[0066] aload_1
[0067] checkcast#14 / / class io / vulcan / bean / beans / SimpleTestBeanSrc
[0068] invokevirtual#18 / / Method io / vulcan / bean / beans / SimpleTestBeanSrc.getFiledA:()Ljava / lang / String;
[0069] checkcast#20 / / class java / lang / String
[0070] invokevirtual#24 / / Method io / vulcan / bean / beans / SimpleTestBeanDist.setFiledA:(Ljava / lang / String;)V
[0071] aload_2
[0072] checkcast#12 / / class io / vulcan / bean / beans / SimpleTestBeanDist
[0073] aload_1
[0074] checkcast#14 / / class io / vulcan / bean / beans / SimpleTestBeanSrc
[0075] invokevirtual#28 / / Method io / vulcan / bean / beans / SimpleTestBeanSrc.getFiledB:()I
[0076] invokevirtual#32 / / Method io / vulcan / bean / beans / SimpleTestBeanDist.setFiledB:(I)V
[0077] aload_2
[0078] areturn
[0079] Embodiment 2:
[0080] 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.
[0081] 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.
[0082] 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.
[0083] 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.
[0084] 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 out 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, and 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.
[0085] 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.
[0086] 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. A method for generating a converter, characterized in that include: Step 1: Extract attribute or method information according to the types of source and target objects to be converted; Step 2: Based on the extracted attribute and method information, dynamically generate an object converter according to the JVM bytecode specification when the program is running; The object converter is used to convert between different types of objects in program development on the JVM platform; 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.
2. The method for generating a converter according to claim 1, characterized in that: During object conversion, data in the same fields are copied to each other; The object converter is instantiated and placed in the cache; If an object converter for converting from the source object to the target object already exists in the cache, the object converter is directly called to complete the conversion task.
3. The method for generating a converter according to claim 1, characterized in that: The step 2 comprises: Step 2.1: Analyze the properties and methods of the target object and filter out public properties and setter methods; Step 2.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; Step 2.3: Generate the bytecode of the 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.
4. The method for generating a converter according to claim 3, characterized in that: The step 2.3 includes: 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.
5. The method for generating a converter according to claim 4, characterized in that: 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.
6. A system for generating a converter, characterized in that 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, dynamically generate object converters according to the JVM bytecode specification when the program is running; The object converter is used to convert between different types of objects in program development on the JVM platform; 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.
7. The system for generating a converter according to claim 6, characterized in that: During object conversion, data in the same fields are copied to each other; The object converter is instantiated and placed in the cache; If an object converter for converting from the source object to the target object already exists in the cache, the object converter is directly called to complete the conversion task.
8. The system for generating a converter according to claim 6, characterized in that: 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 property value, and calls the setter method of the target object to assign the value.
9. The system for generating a converter according to claim 8, characterized in that: The module M2.3 includes: 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.
10. The system for generating a converter according to claim 9, characterized in that: 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.
Citation Information
Patent Citations
Class transformation method and device
CN106933554A