Object copying method, apparatus, medium, product, and system
By using pre-defined annotations and annotation processors to generate static inner classes for object copying in Java development, the performance bottleneck caused by traditional reflective copying methods is solved, achieving efficient object copying without warm-up, and improving system startup speed and performance.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- 中国邮政储蓄银行股份有限公司
- Filing Date
- 2026-05-15
- Publication Date
- 2026-07-10
AI Technical Summary
Existing technologies cause performance bottlenecks in object copying operations in Java development, especially in microservice architectures. Traditional reflective copying methods require warm-up and dynamic bytecode generation, making it difficult to achieve ready-to-use functionality.
Preset annotations are used to mark attribute exclusion lists in the class definitions of the source and target objects. A Java annotation processor generates static inner classes and attribute copying code during the compilation phase. EnhancedBeanUtils is used for efficient copying, avoiding dynamic bytecode generation.
It achieves high performance and is ready to use at startup for object copying, reduces initialization overhead, and improves system startup speed and stability.
Smart Images

Figure CN122363958A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of banking system technology, and more specifically, to an object copying method, an object copying apparatus, a computer-readable storage medium, a computer program product, and an object copying system. Background Technology
[0002] In Java development, object copying (Bean Copy) is an extremely common but performance-prone operation. It's used for conversions between DTOs (Data Transfer Objects) and domain models, data transfer between different layers, object serialization / deserialization between microservices, and conversions between cached objects and database entities. With the increasing prevalence of microservice architectures and the rise in system complexity, object copying operations are playing an increasingly important role in systems, and traditional reflective copying methods are gradually becoming one of the system performance bottlenecks.
[0003] Existing solutions require determining whether to generate mapping classes at startup, and even preloading and caching bytecode. Although attempts are made to bypass the warm-up period, they still rely on runtime initialization. The complex runtime logic causes initialization overhead, resulting in longer system startup time. Summary of the Invention
[0004] The main objective of this application is to provide an object copying method, object copying apparatus, computer-readable storage medium, computer program product, and object copying system, so as to at least solve the problems that existing solutions require preheating and dynamic bytecode generation, making it difficult to achieve the effect of being ready to use immediately upon startup.
[0005] To achieve the above objectives, according to one aspect of this application, an object copying method is provided, comprising: annotating the class definitions of the source object and the target object with preset annotations, the preset annotations including an exclusion list of attributes; during the compilation phase, analyzing the abstract syntax trees of the source object and the target object through a Java annotation processor, generating static inner classes, and statically generating attribute copying related code in the static inner classes; statically generating an array enhancedFields based on the code; and statically generating copyFromSource and copyToTarget based on the code, wherein the array enhancedFields is used to store all non-excluded attribute names, arranged in order. The property names are listed, and `copyFromSource` is used to copy the property value from the source object to the current object. `copyToTarget` is used to copy the property value from the current object to the target object. The `copy` method in the copy utility class `EnhancedBeanUtils` is called, passing in the source object and the target object, to determine whether both the source object and the target object have been enhanced by the preset annotation. If it is determined that both the source object and the target object have been enhanced by the preset annotation, then `enhancedFields`, `copyFromSource`, and `copyToTarget` are called to perform the property copy.
[0006] Optionally, in the process of analyzing the abstract syntax tree of the source object and the target object through the Java annotation processor to generate a static inner class, the method further includes: pre-setting a target service in the copy utility class EnhancedBeanUtils, the target service being used to track and count the number of copy operations and performance metrics, and being generated during the compilation phase to record information about the copy operations in the form of static variables and methods.
[0007] Optionally, before performing attribute copying, the method further includes: if it is determined that the data types of the source object and the target object are different, determining a preset copy function corresponding to a set of multiple data types as the target copy function, wherein the set of multiple data types includes the data types of the source object and the target object; and calling the target copy function to convert the data types of the source object and the target object.
[0008] Optionally, during the attribute copying process, the method further includes: recording in the form of logs the types of the source object and the target object, the list of copied attributes, and the performance metrics of the copy operation, including time consumption and resource consumption.
[0009] Optionally, the method further includes: using an annotation processor to parse the copy strategy represented by the preset annotation and generate corresponding execution logic, wherein the copy strategy is a preset copy depth.
[0010] Optionally, after determining whether both the source object and the target object have been enhanced by the preset annotation using the copy method, the method further includes:
[0011] If the source object is determined, and / or the target object is not enhanced by the preset annotation, a preset copy mechanism is used to copy the attributes. The preset copy mechanism is BeanUtils or MapStruct.
[0012] According to another aspect of this application, an object copying apparatus is provided, comprising: a first processing unit, configured to annotate the class definitions of a source object and a target object using preset annotations, the preset annotations including an attribute exclusion list (excludes); a second processing unit, configured to analyze the abstract syntax trees of the source object and the target object during the compilation phase using a Java annotation processor, generate static inner classes, and statically generate attribute copying related code in the static inner classes; and a third processing unit, configured to statically generate an array `enhancedFields` based on the code, and statically generate `copyFromSource` and `copyToTarget` based on the code, wherein the array `enhancedFields` is used to store all non-excluded attribute names, arranged in order. The system lists the attribute names, and the `copyFromSource` method is used to copy the attribute value from the source object to the current object. The `copyToTarget` method is used to copy the attribute value from the current object to the target object. The fourth processing unit calls the `copy` method in the `EnhancedBeanUtils` utility class, passing in the source object and the target object, to determine whether both the source object and the target object have been enhanced by the preset annotation. The fifth processing unit, if it is determined that both the source object and the target object have been enhanced by the preset annotation, calls `enhancedFields`, `copyFromSource`, and `copyToTarget` to perform attribute copying.
[0013] According to another aspect of this application, a computer-readable storage medium is provided, the computer-readable storage medium including a stored program, wherein, when the program is executed, it controls the device on which the computer-readable storage medium is located to perform any of the methods described.
[0014] According to another aspect of this application, a computer program product is provided, comprising a computer program that, when executed by a processor, implements the steps of any of the methods described.
[0015] According to another aspect of this application, an object copying system is provided, comprising: one or more processors, a memory, and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, the one or more programs including methods for performing any one of the methods described.
[0016] Applying the technical solution of this application, the pre-defined `@Copier` annotation is applied to the class definitions of both the source and target objects. This annotation carries an exclusion list of attributes. This step allows developers to explicitly identify which attributes do not need to be considered during object copying, thereby optimizing the copying process and reducing unnecessary computational overhead. During the compilation phase, the Java annotation processor intervenes and analyzes the abstract syntax tree (AST) of the source and target objects. This processor not only identifies the class structure and attributes but also generates static inner classes and code related to attribute copying based on the exclusion list defined in the `@Copier` annotation. Since this code is statically generated at compile time, it does not consume runtime resources, avoiding the overhead of dynamic bytecode generation. Based on the analyzed and generated code, a static array `enhancedFields` is created, which stores all non-excluded attribute names in order. Simultaneously, two static methods, `copyFromSource` and `copyToTarget`, are generated. The `enhancedFields` array ensures the order and consistency of attributes during copying, while the `copyFromSource` and `copyToTarget` methods handle the actual data copying work, avoiding the delays of dynamic parsing and bytecode generation. At runtime, the `copy` method in the `EnhancedBeanUtils` utility class is called, passing in the source and target objects. This method first checks whether both objects are annotated with `@Copier`, indicating whether they have undergone static enhancement. This check ensures the system can intelligently identify which objects are ready for efficient copying and which need to fall back to the default processing. Once it's confirmed that both the source and target objects have been enhanced with the `@Copier` annotation, the `copy` method in `EnhancedBeanUtils` directly calls the `enhancedFields` array and the `copyFromSource` and `copyToTarget` methods to perform property copying. Because all necessary code is generated and embedded in the `.class` file during the compilation phase, the copying process requires no additional initialization or warm-up, achieving "ready to use" and high-performance copying. This solves the problem of existing solutions requiring warm-up and dynamic bytecode generation, making it difficult to achieve ready-to-use functionality. Attached Figure Description
[0017] The accompanying drawings, which form part of this application, are used to provide a further understanding of this application. The illustrative embodiments and descriptions of this application are used to explain this application and do not constitute an undue limitation of this application. In the drawings:
[0018] Figure 1A flowchart illustrating an object copying method according to an embodiment of this application is shown;
[0019] Figure 2 A structural block diagram of an object copying apparatus provided according to an embodiment of this application is shown. Detailed Implementation
[0020] It should be noted that, unless otherwise specified, the embodiments and features described in this application can be combined with each other. This application will now be described in detail with reference to the accompanying drawings and embodiments.
[0021] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present application, and not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of the present application.
[0022] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate for the embodiments of this application described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0023] As introduced in the background section, object copying (Bean Copy) is an extremely common but performance-prone operation in Java development. Conversions between DTOs (Data Transfer Objects) and domain models, data transfer between different layers, object serialization / deserialization between microservices, and conversions between cached objects and database entities are all part of this process. With the increasing prevalence of microservice architectures and the rise in system complexity, object copying operations are playing an increasingly important role, and traditional reflective copying methods are gradually becoming one of the system performance bottlenecks. To address the problem that existing solutions require preheating and dynamic bytecode generation, making it difficult to achieve a ready-to-use effect, embodiments of this application provide an object copying method, an object copying apparatus, a computer-readable storage medium, a computer program product, and an object copying system.
[0024] The technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention.
[0025] This embodiment provides an object copying method. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Also, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0026] Figure 1 This is a flowchart of an object copying method according to an embodiment of this application. For example... Figure 1 As shown, the method includes the following steps:
[0027] Step S101: In the class definitions of the source object and the target object, a preset annotation is used for annotation. The preset annotation includes an attribute exclusion list (excludes).
[0028] Step S102: During the compilation phase, the abstract syntax tree of the source object and the target object is analyzed by the Java annotation processor to generate a static inner class, and the code related to attribute copying is statically generated in the static inner class.
[0029] Step S103: Based on the above code, statically generate an array enhancedFields; and based on the above code, statically generate copyFromSource and copyToTarget. The array enhancedFields is used to store all non-excluded attribute names and arranges the attribute names in order. The copyFromSource is used to copy attribute values from the source object to the current object. The copyToTarget is used to copy attribute values from the current object to the target object.
[0030] Step S104: Call the copy method in the copy utility class EnhancedBeanUtils, passing in the source object and the target object, and use the copy method to determine whether the source object and the target object have been enhanced by the preset annotation.
[0031] Step S105: If it is determined that both the source object and the target object have been enhanced by the preset annotations, then the enhancedFields, copyFromSource, and copyToTarget are invoked to perform attribute copying.
[0032] copyFromSource: Reads property values from the source object (or "source object property extraction").
[0033] copyToTarget: Writes the attribute value to the target object (or "target object attribute population").
[0034] Specific implementation process:
[0035] Determining if the source and target objects have been enhanced: When calling the `EnhancedBeanUtils.copy` method, the first step is to check if the passed-in source and target objects have been enhanced by the `@Copier` annotation. This step is accomplished by checking if the classes contain static inner classes and methods generated during the compilation phase. If both objects have been enhanced, meaning they both have the `enhancedFields` array and the `copyFromSource` and `copyToTarget` methods, proceed to the next step; otherwise, jump to the process of using the default copy method (such as `BeanUtils`).
[0036] Performing property copying: Once it's confirmed that both objects have been enhanced, the `EnhancedBeanUtils.copy` method will enter the core logic of performing the copy. The specific process of the core logic is as follows:
[0037] Initialize the enhancedFields arrays of the source and target objects: Before performing the copy, the enhancedFields arrays are obtained from the source and target objects respectively. These arrays are statically generated at compile time, containing the names of all non-excluded attributes and sorted according to the order of the attributes in the class.
[0038] Property value copying: Property values are copied by calling the `copyFromSource` and `copyToTarget` methods. These methods are generated at compile time, ensuring runtime efficiency.
[0039] `copyFromSource`: This method retrieves a property value from the source object and copies it to the current object (i.e., the current state of the target object). For each property, it first uses the `enhancedFields` array and a binary search tool to determine if a property with the same name exists in both the source and target objects. If it does, it calls the corresponding index in `enhancedFields` to retrieve the property value from the source object and then uses the `copyToTarget` method to copy that value to the property in the target object.
[0040] `copyToTarget`: In contrast to `copyFromSource`, this method copies the property values of the current object (i.e., the source object) to the target object. Similarly, it iterates through the `enhancedFields` array, uses a binary search to ensure correct property matching, and then calls the index corresponding to the `enhancedFields` array to copy the property value.
[0041] Handling nested objects: If an object contains nested objects, `copyFromSource` and `copyToTarget` will recursively call themselves, copying the properties of the nested objects. This ensures deep copying, allowing copy operations to be completed successfully even with complex object structures.
[0042] End of copy: The copying process ends when all property values have been copied from the source object to the target object. At this point, the target object should contain copies of all non-excluded properties of the source object.
[0043] @Copier is used as part of the source and target object class definitions to instruct the annotation processor to perform special processing on the class during the compilation phase, thereby generating efficient copy logic.
[0044] EnhancedBeanUtils is primarily responsible for invoking and executing static inner classes and methods generated by the annotation processor during the compile phase at runtime. These methods include copyFromSource and copyToTarget, which are used to retrieve property values from the source object and copy those values to the target object. Furthermore, EnhancedBeanUtils is also responsible for determining whether the passed-in object has been enhanced with the @Copier annotation and, if necessary, falling back to the default copy strategy.
[0045] The `excludes` property allows developers to exclude properties that don't need to be copied during object copying. This is very useful for optimizing copy performance, especially when dealing with complex or large objects. For example, when copying a database entity with many fields to a simple Data Transfer Object (DTO), it may not be necessary to copy all properties. In this case, `excludes` can be used to avoid unnecessary data copying.
[0046] In the steps described above, the predefined `@Copier` annotation is applied to the class definitions of both the source and target objects. This annotation carries an exclusion list of attributes. This step allows developers to explicitly identify which attributes do not need to be considered during object copying, thereby optimizing the copying process and reducing unnecessary computational overhead. During the compilation phase, the Java annotation processor intervenes and analyzes the abstract syntax tree (AST) of the source and target objects. This processor not only identifies the class structure and attributes but also generates static inner classes and code related to attribute copying based on the exclusion list defined in the `@Copier` annotation. Since this code is statically generated at compile time, it does not consume runtime resources, avoiding the overhead of dynamic bytecode generation. Based on the analyzed and generated code, a static array `enhancedFields` is created, which stores all non-excluded attribute names in order. Simultaneously, two static methods, `copyFromSource` and `copyToTarget`, are generated. The `enhancedFields` array ensures the order and consistency of attributes during copying, while the `copyFromSource` and `copyToTarget` methods handle the actual data copying work, avoiding the delays of dynamic parsing and bytecode generation. At runtime, the `copy` method in the `EnhancedBeanUtils` utility class is called, passing in the source and target objects. This method first checks whether both objects are annotated with `@Copier`, indicating whether they have undergone static enhancement. This check ensures the system can intelligently identify which objects are ready for efficient copying and which need to fall back to the default processing. Once it's confirmed that both the source and target objects have been enhanced with the `@Copier` annotation, the `copy` method in `EnhancedBeanUtils` directly calls the `enhancedFields` array and the `copyFromSource` and `copyToTarget` methods to perform property copying. Because all necessary code is generated and embedded in the `.class` file during the compilation phase, the copying process requires no additional initialization or warm-up, achieving "ready to use" and high-performance copying. This solves the problem of existing solutions requiring warm-up and dynamic bytecode generation, making it difficult to achieve ready-to-use functionality.
[0047] In one embodiment of this application, during the process of analyzing the abstract syntax tree of the source object and the target object through a Java annotation processor to generate a static inner class, the method further includes: pre-setting a target service in the copy utility class EnhancedBeanUtils. The target service is used to track and count the number of copy operations and performance indicators, and is generated during the compilation phase to record information about the copy operations in the form of static variables and methods.
[0048] Define a performance monitoring annotation: First, define an annotation for performance monitoring, such as @PerformanceMonitor. This annotation can be used to mark the Bean copy operations that need to be monitored.
[0049] Extended Annotation Processor: Next, extend the existing annotation processor so that when the @PerformanceMonitor annotation is detected, in addition to generating the static inner class required for copying, it can also generate static variables and methods related to performance monitoring.
[0050] Static variables: Create one or more static variables to store the number of copy operations and performance metrics. For example, you could have a counter `static int copyCount = 0;` and a variable to accumulate time `static long totalCopyTimeNanos = 0;`.
[0051] Generate methods for starting and ending copy operations, such as startCopyMonitoring() and endCopyMonitoring(). These methods will record the timestamp of the start of the copy and calculate the time taken after the copy is completed, updating the total time taken and the number of copies.
[0052] Implement monitoring functionality for the copy utility class: In the EnhancedBeanUtils copy utility class, implement the functionality of calling monitoring methods before and after the copy method. When copying begins, startCopyMonitoring() is called; after copying is complete, endCopyMonitoring() is called, thereby automatically collecting performance metrics.
[0053] Performance data reporting and presentation: To make performance data meaningful, a way needs to be provided to report and present this data. This can be achieved through an external management interface or API, allowing developers to query statistics on copy operations, such as average copy time, maximum copy time, minimum copy time, and total number of copies.
[0054] By tracking copy operations in real time, developers can obtain detailed information about the frequency and execution time of copy operations, which is crucial for identifying and optimizing performance bottlenecks. Statically generated performance monitoring code eliminates the need for dynamically generated code during runtime, making the copy process more type-safe and easier to debug, especially in microservice and high-concurrency environments. The performance monitoring code is generated during the compilation phase, without affecting the clarity and readability of the source code. This means developers can focus on business logic without writing complex monitoring code. Based on the collected copy performance data, system administrators and developers can optimize resource allocation, such as adjusting thread pool size, optimizing data structures, or changing copy strategies, to improve the overall system's responsiveness and throughput. Over time, the system may undergo multiple refactorings and upgrades; automatically generated performance monitoring logic helps maintain consistency, reduces monitoring failures caused by human factors, and ensures the continuity and accuracy of monitoring data.
[0055] In one embodiment of this application, before performing attribute copying, the method further includes: if it is determined that the data types of the source object and the target object are different, determining a preset copy function corresponding to a set of multiple data types as the target copy function, wherein the set of multiple data types includes the data types of the source object and the target object; and calling the target copy function to convert the data types of the source object and the target object.
[0056] Type checking and mapping function selection: Before performing attribute copying, the data types of the source and target objects are checked. If different data types are found, the program searches a predefined set of multiple data types, which contains all possible data types and their conversion rules. The system then selects an appropriate preset copy function based on the specific data types of the source and target objects. This function enables the conversion from one data type to another.
[0057] Type conversion is performed by calling a pre-defined copy function: After the target copy function is determined, the program calls that function to convert the attribute values of the source object, ensuring that the converted values can be safely assigned to the corresponding attributes of the target object. This conversion may include conversions between basic data types (such as int to long, String to Date, etc.), or it may involve conversions of complex objects (such as conversions from a custom type A to type B).
[0058] Property copying: After type conversion, the system will begin copying the properties. At this point, the copy operation will be based on the converted type, ensuring that property values are correctly passed from the source object to the target object.
[0059] Using predefined copy functions for type conversion ensures that attribute values will not cause runtime errors due to type mismatches during the copying process, improving code robustness and reliability. Predefined copy functions are determined at compile time, avoiding the overhead of runtime dynamic type checking and conversion, thus improving the execution efficiency of attribute copying. Using predefined copy functions for type conversion allows complex conversion logic to be encapsulated within functions, making the copy code clearer and more concise, and reducing maintenance difficulty. By maintaining multiple data type collections and predefined copy functions, copying needs between objects of different types can be easily handled without writing dedicated copy logic for each pair of type objects, greatly enhancing the flexibility and extensibility of the copying mechanism.
[0060] In one embodiment of this application, during the execution of attribute copying, the method further includes: recording in the form of logs the types of the source object and the target object, the list of copied attributes, and the performance indicators of the copying operation, including time consumption and resource consumption.
[0061] Recording performance metrics such as time consumption and resource usage helps developers monitor the efficiency of copy operations, identify performance bottlenecks promptly, and perform necessary optimizations. For example, if copying certain attributes takes a long time, the copy logic for those attributes can be analyzed to see if further optimization is possible, or whether these attributes should be excluded from annotations. Recording the types of the source and target objects, as well as the list of copied attributes, helps in tracing and debugging errors during the copy process. For example, if a type mismatch exception occurs during copying, the logs can quickly pinpoint the problematic attribute, allowing for rapid code fixes. The logged list of copied attributes and object types can serve as supplementary documentation for code maintenance, helping maintainers quickly understand the context and purpose of copy operations. Furthermore, when team members or new developers need to understand and modify copy logic, these log records provide detailed information, reducing communication costs and comprehension difficulties.
[0062] In one embodiment of this application, the method further includes: using an annotation processor to parse the copy strategy represented by the preset annotation and generating corresponding execution logic, wherein the copy strategy is a preset copy depth.
[0063] In the context of object copying, copy depth refers to the level at which the copy operation penetrates the object's structure. Simply put, copy depth determines whether and how the copy operation handles nested objects or collections within an object. In a shallow copy, the copy depth is typically 1, meaning the copy operation only processes the properties of the top-level object and doesn't delve deeper to copy other objects or collections within the object. In a deep copy, the copy depth can be much greater; it recursively copies all nested objects and collections within the object until it reaches the lowest-level non-composite type.
[0064] By using annotation processors to parse copy strategies during the compile phase, the copy depth can be flexibly adjusted according to different needs. This makes copy operations more dynamic and customized, allowing developers to choose between deep or shallow copies without needing complex logic at runtime. Determining the copy depth and generating corresponding execution logic at compile time avoids dynamic parsing and decision-making at runtime, reducing unnecessary runtime overhead. Especially when the copy depth is fixed, annotation processors can generate highly targeted code, which is generally more efficient than generic runtime processing logic. Handling copy logic at compile time helps ensure type safety, as the compiler can check for potential type mismatches during the copy process. Furthermore, if the copy strategy is incorrect, such as a specified depth that does not match the object structure, these problems can be detected at compile time, rather than encountering exceptions at runtime, greatly enhancing the robustness and maintainability of the program.
[0065] In one embodiment of this application, after determining whether the source object and the target object have both been enhanced by the preset annotation using the above copy method, the method further includes: if it is determined that the source object and / or the target object have not been enhanced by the preset annotation, performing attribute copying using a preset copy mechanism, wherein the preset copy mechanism is BeanUtils or MapStruct.
[0066] Specifically, it ensures seamless property copying even between objects that don't use specific enhancement annotations. This allows the system to handle a wide variety of objects, not just those already enhanced with `@Copier`. When dealing with legacy code or objects from third-party libraries, this hybrid approach avoids unnecessary modifications to these external components, maintaining system compatibility and stability. BeanUtils and MapStruct are proven technologies widely present in the Java ecosystem. Falling back to these mature tools when objects aren't enhanced with `@Copier` reduces the risk of introducing new code and makes this invention easier to integrate with other systems or modules.
[0067] It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions, and although a logical order is shown in the flowchart, in some cases the steps shown or described may be executed in a different order than that shown here.
[0068] This application also provides an object copying apparatus. It should be noted that the object copying apparatus of this application can be used to execute the object copying method provided in this application. This apparatus is used to implement the above embodiments and preferred embodiments; details already described will not be repeated. As used below, the term "module" can refer to a combination of software and / or hardware that implements a predetermined function. Although the apparatus described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.
[0069] The object copying apparatus provided in the embodiments of this application will be described below.
[0070] Figure 2 This is a schematic diagram of an object copying apparatus according to an embodiment of this application. Figure 2 As shown, the device includes: a first processing unit 21, used to annotate the class definitions of the source object and the target object using preset annotations, wherein the preset annotations include an attribute exclusion list (excludes); a second processing unit 22, used during the compilation phase to analyze the abstract syntax tree of the source object and the target object using a Java annotation processor, generate static inner classes, and statically generate attribute copy-related code in the static inner classes; and a third processing unit 23, used to statically generate an array `enhancedFields` based on the above code, and statically generate `copyFromSource` and `copyToTarget` based on the above code, wherein the array `enhancedFields` is used to store all non-excluded attribute names, and the attribute names are arranged in order. The above-mentioned `copyFromSource` is used to copy the attribute value from the source object to the current object, and the above-mentioned `copyToTarget` is used to copy the attribute value from the current object to the target object. The fourth processing unit 24 is used to call the `copy` method in the copy utility class `EnhancedBeanUtils`, passing in the source object and the target object, so as to use the above-mentioned `copy` method to determine whether the source object and the target object have been enhanced by the above-mentioned preset annotation. The fifth processing unit 25 is used to call the above-mentioned `enhancedFields`, `copyFromSource`, and `copyToTarget` to perform attribute copying if it is determined that the source object and the target object have been enhanced by the above-mentioned preset annotation.
[0071] In one embodiment of this application, the second processing unit includes: a first processing module configured to, during the process of analyzing the abstract syntax tree of the source object and the target object through a Java annotation processor and generating a static inner class, pre-configure a target service in the copy utility class EnhancedBeanUtils. The target service is used to track and count the number of copy operations and performance metrics, and is generated during the compilation phase to record information about the copy operations in the form of static variables and methods.
[0072] In one embodiment of this application, the object copying device includes: a sixth processing unit configured to, before performing attribute copying, determine a preset copy function corresponding to a set of multiple data types as a target copy function if it is determined that the data types of the source object and the target object are different, wherein the set of multiple data types includes the data types of the source object and the target object; and a seventh processing unit configured to call the target copy function to convert the data types of the source object and the target object.
[0073] In one embodiment of this application, the fifth processing unit includes: a second processing module configured to record, in the form of a log, the types of the source object and the target object, the list of attributes to be copied, and the performance metrics of the copy operation during the execution of attribute copying, the performance metrics including time consumption and resource consumption.
[0074] In one embodiment of this application, the object copying device includes: an eighth processing unit configured to parse the copying strategy represented by the preset annotation using an annotation processor, and generate corresponding execution logic, wherein the copying strategy is a preset copying depth.
[0075] In one embodiment of this application, the object copying device includes: a ninth processing unit configured to, after determining whether the source object and the target object have both been enhanced by the preset annotation using the above copy method, perform attribute copying using a preset copying mechanism if it is determined that the source object and / or the target object have not been enhanced by the preset annotation, wherein the preset copying mechanism is BeanUtils or MapStruct.
[0076] The aforementioned object copying device includes a processor and a memory. The first processing unit, second processing unit, third processing unit, fourth processing unit, and fifth processing unit are all stored as program units in the memory. The processor executes the program units stored in the memory to achieve the corresponding functions. All of the above modules are located in the same processor; alternatively, the modules may be located in different processors in any combination.
[0077] The processor contains a kernel, which retrieves the corresponding program units from memory. One or more kernels can be configured. By adjusting kernel parameters, the problems of existing solutions requiring warm-up and dynamic bytecode generation, which make it difficult to achieve a ready-to-use effect upon startup, can be addressed.
[0078] The memory may include non-permanent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.
[0079] This invention provides a computer-readable storage medium including a stored program, wherein, when the program is executed, it controls the device where the computer-readable storage medium is located to execute the object copying method.
[0080] This invention provides a processor for running a program, wherein the program executes the object copying method during runtime.
[0081] This invention provides a device including a processor, a memory, and a program stored in the memory and executable on the processor. When the processor executes the program, it performs at least the following steps: annotating the class definitions of the source and target objects with preset annotations, the preset annotations including an attribute exclusion list (excludes); during the compilation phase, analyzing the abstract syntax trees of the source and target objects using a Java annotation processor, generating static inner classes, and statically generating attribute copying related code within the static inner classes; statically generating an array `enhancedFields` based on the above code; and statically generating `copyFromSource` and `copyToTarget` based on the above code, wherein the `enhancedFields` array is used to store the attribute copying information. There are non-excluded attribute names, arranged in order. `copyFromSource` copies attribute values from the source object to the current object, and `copyToTarget` copies attribute values from the current object to the target object. The `copy` method in the `EnhancedBeanUtils` utility class is called, passing in the source and target objects. The `copy` method determines whether both the source and target objects have been enhanced by the preset annotations. If both are confirmed to be enhanced, `enhancedFields`, `copyFromSource`, and `copyToTarget` are called to perform the attribute copy. The devices mentioned in this article can be servers, PCs, tablets, mobile phones, etc.
[0082] This application also provides a computer program product, which, when executed on a data processing device, is suitable for executing an initialization program having at least the following method steps: annotating the class definitions of the source object and the target object with preset annotations, the preset annotations including an attribute exclusion list (excludes); during the compilation phase, analyzing the abstract syntax tree of the source object and the target object using a Java annotation processor, generating static inner classes, and statically generating attribute copy-related code within the static inner classes; statically generating an array `enhancedFields` based on the above code; and statically generating `copyFromSource` and `copyToTarget` based on the above code, wherein the array `enhancedFields` is used to store all non-excluded attributes. The property names are listed in order. The `copyFromSource` method copies the property values from the source object to the current object, and the `copyToTarget` method copies the property values from the current object to the target object. The `copy` method in the `EnhancedBeanUtils` utility class is called, passing in the source object and the target object, to determine whether both the source and target objects have been enhanced by the preset annotations. If both the source and target objects have been enhanced by the preset annotations, then `enhancedFields`, `copyFromSource`, and `copyToTarget` are called to perform the property copy.
[0083] This application also provides an object copying system, including: one or more processors, a memory, and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs include methods for performing any of the above-described methods.
[0084] It is obvious to those skilled in the art that the modules or steps of the present invention described above can be implemented using general-purpose computing devices. They can be centralized on a single computing device or distributed across a network of multiple computing devices. They can be implemented using computer-executable program code, and thus can be stored in a storage device for execution by a computing device. In some cases, the steps shown or described can be performed in a different order than those described herein, or they can be fabricated as separate integrated circuit modules, or multiple modules or steps can be fabricated as a single integrated circuit module. Thus, the present invention is not limited to any particular combination of hardware and software.
[0085] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0086] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0087] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0088] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0089] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0090] Memory may include non-persistent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, like read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.
[0091] Computer-readable media include both permanent and non-permanent, removable and non-removable media that can store information by any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.
[0092] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0093] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0094] The above description is merely a preferred embodiment of this application and is not intended to limit this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the protection scope of this application.
Claims
1. A method for copying an object, characterized in that, include: In the class definitions of the source and target objects, preset annotations are used for marking, and the preset annotations include an attribute exclusion list (excludes). During the compilation phase, the Java annotation processor analyzes the abstract syntax tree of the source object and the target object, generates a static inner class, and statically generates code related to attribute copying in the static inner class; The code statically generates an array `enhancedFields`; and statically generates `copyFromSource` and `copyToTarget`, wherein the array `enhancedFields` is used to store all non-excluded attribute names and arranges the attribute names in order, the array `copyFromSource` is used to copy attribute values from the source object to the current object, and the array `copyToTarget` is used to copy attribute values from the current object to the target object; The copy method in the copy utility class EnhancedBeanUtils is called, passing in the source object and the target object, to determine whether the source object and the target object have both been enhanced by the preset annotation; If it is determined that both the source object and the target object have been enhanced by the preset annotation, then the enhancedFields, copyFromSource, and copyToTarget are invoked to perform attribute copying.
2. The method according to claim 1, characterized in that, In the process of generating static inner classes by analyzing the abstract syntax trees of the source object and the target object through a Java annotation processor, the method further includes: The EnhancedBeanUtils class, a copy utility class, pre-defines a target service. This target service is used to track and count the number of copy operations and performance metrics. It is generated during the compilation phase and records the information of the copy operations in the form of static variables and methods.
3. The method according to claim 1, characterized in that, Before performing the property copy, the method further includes: If it is determined that the data types of the source object and the target object are different, a preset copy function corresponding to the set of multiple data types is determined as the target copy function, wherein the set of multiple data types includes the data types of the source object and the target object; The target copy function is invoked to convert the data types of the source object and the target object.
4. The method according to claim 1, characterized in that, During the property copying process, the method further includes: The source and target objects are recorded in the form of logs, along with the list of copied attributes and the performance metrics of the copy operation, including time consumption and resource consumption.
5. The method according to claim 1, characterized in that, The method further includes: An annotation processor is used to parse the copy strategy represented by the preset annotation and generate the corresponding execution logic. The copy strategy is a preset copy depth.
6. The method according to claim 1, characterized in that, After determining whether both the source object and the target object have been enhanced by the preset annotation using the copy method, the method further includes: If the source object is determined, and / or the target object is not enhanced by the preset annotation, a preset copy mechanism is used to copy the attributes. The preset copy mechanism is BeanUtils or MapStruct.
7. An object copying device, characterized in that, include: The first processing unit is used to annotate the class definitions of the source object and the target object with preset annotations, wherein the preset annotations include an attribute exclusion list (excludes). The second processing unit is used to analyze the abstract syntax tree of the source object and the target object through the Java annotation processor during the compilation phase, generate a static inner class, and statically generate attribute copying related code in the static inner class; The third processing unit is used to statically generate an array `enhancedFields` based on the code; and to statically generate `copyFromSource` and `copyToTarget` based on the code, wherein the array `enhancedFields` is used to store all non-excluded attribute names and arrange the attribute names in order, the array `copyFromSource` is used to copy attribute values from the source object to the current object, and the array `copyToTarget` is used to copy attribute values from the current object to the target object; The fourth processing unit is used to call the copy method in the copy utility class EnhancedBeanUtils, passing in the source object and the target object, so as to use the copy method to determine whether the source object and the target object have been enhanced by the preset annotation; The fifth processing unit is used to call the enhancedFields, copyFromSource, and copyToTarget to perform attribute copying when it is determined that both the source object and the target object have been enhanced by the preset annotation.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored program, wherein, when the program is executed, it controls the device on which the computer-readable storage medium is located to perform the method according to any one of claims 1 to 6.
9. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method described in any one of claims 1 to 6.
10. An object copying system, characterized in that, include: One or more processors, a memory, and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, the one or more programs comprising methods for performing any one of claims 1 to 6.