Method and device for automatically generating Bean copy code

The BeanUtils class interface code is generated through the Mapstruct mapper framework, and the bean copy is realized using native getters and setter methods, which solves the problems of low bean copy performance and cumbersome code in the existing technology, and improves the efficiency of Java software programming.

CN113535142BActive Publication Date: 2025-01-14ZHEJIANG DASOUCHE SOFTWARE TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202010300606.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-04-16
Publication Date
2025-01-14
Estimated Expiration
2040-04-16

AI Technical Summary

Technical Problem

In Java software programming, existing Bean copy tools achieve low copy performance through Java reflection or CGLIB dynamic proxy, and the user-defined mapper interface needs to be written for each class, resulting in cumbersome code and large workload, which reduces the Project development efficiency.

Method used

Using the Mapstruct mapper framework, the BeanUtils class interface code is generated by parsing the source object and the destination object's class source file, and the Bean copy code is realized by using native getter and setter methods to reduce the amount of code written by users and improve performance.

Benefits of technology

Improves the performance of bean copies, simplifies the code generation process, reduces the workload of developers, and improves project development efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113535142B_ABST
    Figure CN113535142B_ABST
Patent Text Reader

Abstract

The present application provides a method and device for automatically generating Bean copy code, which is applied to a development device using a Mapstruct mapper framework, and may include: selecting a source object and a destination object, the Bean copy code is used to copy the data to be copied from the source object to the destination object; respectively parsing the class source files of the source object and the destination object to obtain class information; injecting the class information into a template file to generate a BeanUtils class interface code corresponding to the source object and the destination object, wherein the BeanUtils class interface code is used to compile and generate the Bean copy code implemented using native getter and setter methods. Through the technical solution of the present application, a relatively concise Bean copy code with higher copy performance implemented using native getter and setter methods can be automatically generated, which relatively improves the project development efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of Java software programming, and in particular to a method and device for automatically generating Bean copy code. Background Art

[0002] In Java software programming, it is often necessary to copy data between different projects or between different layers of the same project. These data are usually located in objects (Beans), so the above copying process is also called Bean copying.

[0003] In the related art, one method is to use Bean copying tools such as Spring's BeanUtils and Apache's BeanUtils, but such Bean copying tools usually set the source object attributes to the target object through Java reflection or CGLIB dynamic proxy to achieve copying, so the copying performance is lower than that of native getter and setter methods; another method is for the user to define a mapper interface and declare the methods to be mapped, so as to automatically generate an interface implementation during the compilation phase, but this method requires writing an interface for each class to be copied and adding methods to be mapped, so when the number of classes to be copied is large, the code is cumbersome and the workload is large, thereby reducing the project development efficiency. Summary of the invention

[0004] In view of this, the present application provides a method and device for automatically generating Bean copy code to solve the problems existing in the related art.

[0005] To achieve the above objectives, this application provides the following technical solutions:

[0006] According to a first aspect of the present application, a method for automatically generating Bean copy code is proposed, which is applied to a development device using a Mapstruct mapper framework, and the method comprises:

[0007] A source object and a destination object are selected, and the Bean copy code is used to copy the data to be copied from the source object to the destination object;

[0008] Parsing the class source files of the source object and the target object respectively to obtain class information;

[0009] The class information is injected into the template file to generate BeanUtils class interface code corresponding to the source object and the destination object, wherein the BeanUtils class interface code is used to compile and generate the Bean copy code implemented using native getter and setter methods.

[0010] According to a second aspect of the present application, a device for automatically generating Bean copy code is proposed, which is applied to a development device using a Mapstruct mapper framework, and the device comprises:

[0011] An object acquisition unit, used for selecting a source object and a destination object, wherein the Bean copy code is used for copying the data to be copied from the source object to the destination object;

[0012] A file parsing unit, used for parsing the class source files of the source object and the target object to obtain class information;

[0013] A code generation unit is used to inject the class information into a template file to generate a BeanUtils class interface code corresponding to the source object and the destination object, wherein the BeanUtils class interface code is used to compile and generate the Bean copy code implemented using native getter and setter methods.

[0014] According to a third aspect of the present application, an electronic device is provided, comprising:

[0015] processor;

[0016] a memory for storing processor-executable instructions;

[0017] Wherein, the processor is configured to implement the steps of the method as described in any one of the above-mentioned first aspect solutions.

[0018] According to a fourth aspect of the present application, a computer-readable storage medium is provided, on which computer instructions are stored, and when the instructions are executed by a processor, the steps of the method described in any one of the above-mentioned first aspect solutions are implemented.

[0019] It can be seen from the above technical scheme that, on the one hand, after determining the source object and the destination object to be copied, the present application generates the BeanUtils class interface code based on the class information obtained by analyzing the class source files of the source object and the destination object, and the interface code will generate the Bean copy code implemented by Java native getter and setter methods during the compilation stage. Therefore, compared with the Bean copy tool that implements copying through Java reflection or CGLIB dynamic proxy, it has higher performance; on the other hand, the present application only needs to determine the source object and the destination object to be copied before generating the code, and does not need to operate each class to be copied in the source object and the destination object separately, which reduces the code that developers need to write and the generated code is relatively concise, with relatively higher project development efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0020] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the related technologies, the drawings required for use in the embodiments or the related technical descriptions are briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0021] Figure 1 It is a flow chart of a method for automatically generating Bean copy code shown in an exemplary embodiment of the present application;

[0022] Figure 2 It is a schematic diagram of a framework for automatically generating Bean copy code shown in an exemplary embodiment of the present application;

[0023] Figure 3 is a flow chart of another method for automatically generating Bean copy code according to an exemplary embodiment of the present application;

[0024] Figure 4 is a structural schematic diagram of an electronic device shown in an exemplary embodiment of the present application;

[0025] Figure 5 It is a block diagram of a device for automatically generating Bean copy code shown in an exemplary embodiment of the present application. DETAILED DESCRIPTION

[0026] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.

[0027] The terms used in this application are for the purpose of describing specific embodiments only and are not intended to limit this application. The singular forms of "a", "said", "above", and "the" used in this application and the appended claims are also intended to include plural forms unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used herein refers to and includes any or all possible combinations of one or more associated listed items.

[0028] In the process of developers (i.e., users of the present application) developing software projects using the Java programming language, Bean copying is often required: copying data between different objects, such as between different projects, between different layers of the same project (passing API layer parameters to the service layer, or passing Service layer parameters to the DAO layer), etc. Usually, the data types of the data to be copied in the source object and the destination object are different, but the attribute types and names are the same. There are usually two automatic Bean copying methods in the related art: one is to use Bean copying frameworks such as Spring and Apache to directly copy data, but this type of Bean copying framework essentially sets the source object attributes to the target object through Java reflection or CGLIB dynamic proxy to achieve copying, which is much lower than the copying performance of native getter and setter methods. The other is that the user defines the mapper interface and declares that mapping is required to automatically generate the interface implementation during the compilation phase, such as the MapStruct framework, but this method requires writing interfaces for each class to be copied and adding methods that need to be mapped. Therefore, when the number of classes to be copied is large, there are many repeated steps and a large workload, thereby reducing the overall copy efficiency.

[0029] Because the performance of Bean copying using Java native getter and setter methods is higher, in order to solve the above technical problems, improvements are made on the basis of the MapStruct framework, and a method for automatically generating Bean copy code is proposed. The BeanUtils class interface code generated by this method can generate Baen copy code based on Java native getter and setter methods during the compilation phase, thereby reducing the user's workload while ensuring the performance of Bean copying.

[0030] To further illustrate the present application, the following embodiments are provided. Obviously, the embodiments described are only part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field should fall within the scope of protection of the present application.

[0031] Figure 1 The following is a flow chart of a method for automatically generating Bean copy code according to an exemplary embodiment of the present application. The above method is applied to a development device using the Mapstruct mapper framework, such as Figure 1 As shown, the above method may include the following steps:

[0032] Step 102: determine a source object and a destination object, and the Bean copy code is used to copy the data to be copied from the source object to the destination object.

[0033] In one embodiment, the method for automatically generating Bean copy code can be implemented in the form of independent functional software, executable files, or functional plug-ins that can be called in project development software such as IDEA. Specifically, if it is independent functional software, the functional software needs to be pre-installed in a software and hardware environment with corresponding operating capabilities; if it is an executable file, the executable file needs to be pre-configured in a suitable path in a hardware environment with software operating capabilities; if it is a functional plug-in, the functional plug-in needs to be pre-connected with the project development software, so that the above functional plug-in can be called at a reasonable time and a reasonable location of the project when the project development software is used for project development to generate Bean copy code. In fact, the functional software, executable file or functional plug-in corresponding to the above method can be applied to a development device using the Mapstruct mapper framework. The specific operation mode of the above functional software, executable file or functional plug-in in the development device can refer to the content disclosed in the relevant technology, and this application does not limit this.

[0034] The Bean copy process is essentially the process of copying the data to be copied and its attributes in the source object to the destination object. For different source objects and destination objects, since the number of classes, class attributes and data to be copied in each class corresponding to each object are different, the Bean copy code used in the copy process is also different, making the Bean copy code targeted to the source object where the data to be copied is located and the destination object to be copied. Therefore, before generating the Bean copy code, it is necessary to first determine the source object and destination object corresponding to the data to be copied.

[0035] In one embodiment, the source object and the destination object may be determined according to an object configuration instruction issued by a user; wherein, when the object configuration instruction is an empty instruction, the source object and the destination object are determined according to preset general configuration data.

[0036] In one embodiment, the object configuration instruction is also used to set the package name and / or class name suffix of the source object, and the package name and / or class name suffix of the destination object.

[0037] It is understandable that when it is necessary to accept object configuration instructions or other instructions issued by the user, the above-mentioned development device can display controls corresponding to different technology types in the interactive interface for the user to click and select, or it can display a technology type name input box in the interactive interface for the user to enter the technology type name or name keyword they want to select in the input box. Of course, other methods are also possible, and this application does not limit this.

[0038] In another embodiment, the source object and the destination object that need to be copied by the Bean can be determined based on the parameters of the project currently developed by the development device, the preset configuration, the objects involved in the current code and / or the business functions of the current code.

[0039] Step 104: parse the class source files of the source object and the target object respectively to obtain class information.

[0040] After determining the source object and the destination object, first obtain the class source files of the source object and the destination object, and then parse the class source files to obtain the corresponding class information. Specifically, the class source files of the source object and the destination object can be obtained separately, or the class source files of the source object and the destination object can be obtained in association. Among them, the class source files can be pre-generated and saved in the Java file corresponding to the object, or saved in association with the object in other storage paths; the class source files can also be temporarily generated when they need to be obtained.

[0041] Similar to the above-mentioned acquisition of class source files, the class source files of the source object and the class source files of the destination object can be parsed separately; or the class source files and the class source files of the destination object can be parsed in association, and this application does not limit this. Among them, the class information obtained by the above-mentioned parsing has a certain corresponding relationship with the source object or the destination object and its corresponding class.

[0042] In one embodiment, according to the technology selection instruction issued by the user, the selected technology type is determined; and a template file matching the selected technology type is selected to generate the BeanUtils class interface code. Taking the above-mentioned function plug-in corresponding to the method as an example, a variety of standard technology types can be pre-integrated in the function plug-in for users to choose. Among them, each technology type corresponds to a set of (one or more) template files, and the template files corresponding to any technology type should be different from the template files corresponding to other technology types. The template files corresponding to any technology type are used to generate the BeanUtils class interface code matching the technology type in a targeted manner. In fact, users can also select a custom technology type, and the custom template files corresponding to the custom technology type can be pre-configured by the user or configured through the above-mentioned technology selection instructions.

[0043] In one embodiment, the above selection of a template file matching the selected technology type includes: selecting a template file from a preset template repository, wherein the template repository includes at least one of the following: an open source template file library, a dedicated template file library, and a custom template file library. It can be understood that, corresponding to the above technology selection instruction, when the user selects a standard technology type provided by the function plug-in, the annotation template file corresponding to the selected standard technology type can be downloaded from the above open source template file library or the dedicated template file library, wherein the above download process can be automatically performed after the user issues the technology selection instruction, or can be manually downloaded by the user. When the user selects a custom technology type, the custom template file corresponding to the selected custom technology type can be downloaded from the above custom template file library, which is custom generated or configured by the user before the project development; the selected custom technology type can also be generated or configured by the user through the technology selection instruction or other related configuration instructions.

[0044] Step 106, injecting the class information into the template file to generate BeanUtils class interface code corresponding to the source object and the destination object, wherein the BeanUtils class interface code is used to compile and generate the Bean copy code implemented using native getter and setter methods.

[0045] After parsing and obtaining the class information corresponding to the above source object and the destination object, the above class information is injected into the template file to generate the BeanUtils class interface code corresponding to the above source object and the destination object. The BeanUtils class interface code provides the called interface for implementing bean copy for the project to be developed. The above solution runs under the Mapstruct mapper framework, and the BeanUtils class interface code finally generated contains annotation information about the implementation class and the Mapstruct mapper framework, and the above annotation information about the implementation class ensures that the BeanUtils class interface code generates the Bean copy code implemented by the Java native getter and setter methods during the compilation stage.

[0046] It can be seen that after determining the source object and destination object to be copied, the present application generates the BeanUtils class interface code based on the class information obtained by analyzing the class source files of the source object and the destination object, and the interface code will generate the Bean copy code implemented by Java native getter and setter methods during the compilation stage. Therefore, compared with the Bean copy tool that implements copying through Java reflection or CGLIB dynamic proxy, it has higher performance; moreover, the present application only needs to determine the source object and destination object to be copied before generating the code, and does not need to operate each class to be copied in the source object and the destination object separately, which reduces the amount of code that developers need to write manually and the generated code is relatively concise, which improves the project development efficiency to a certain extent.

[0047] The following takes the framework for automatically generating Bean copy code corresponding to the method described in this application (hereinafter referred to as the code generation framework) as an example to further illustrate the above method. Figure 2 This is a schematic diagram of a framework for automatically generating Bean copy code shown in an exemplary embodiment of the present application. The framework can be built based on Mapstruct under the Maven framework, and the framework can mainly include: an entry layer 201, a core layer 202, a template layer 203, and a project dependency 204. Among them, the entry layer 201, the core layer 202, and the template layer 203 can respectively include multiple classes, and the project dependency 204 can include multiple dependencies. The specific functions of the above multiple classes and multiple dependencies in this framework are shown in Table 1 below.

[0048] Table 1

[0049]

[0050] When developing a software project that requires Bean copying, users can first build the overall framework of the project to be developed, and then use the Maven command to call the above code generation framework to generate the BeanUtils class interface code for the data to be copied in the source object, and use the interface code as part of the overall framework of the project to be developed. On the basis of this part of the code, other related codes are expanded to achieve the development of the business functions of the software project. During the compilation phase of the project, the called part of the interface code will generate the Bean copy code implemented by the native getter and setter methods, thereby achieving the corresponding Bean copy function of copying the data to be copied from the source object to the destination object.

[0051] Let's assume that the above framework is provided to users in the form of a functional plug-in in the project development software. At this time, the specific process of using the above code generation framework to generate the BeanUtils class interface code for the data to be copied in the source object is as follows:

[0052] 1. At a certain stage of the project development process, the user invokes the code generation framework 200 through the plug-in selection control or the preset plug-in call statement provided in the project development software, and enters the functional plug-in from the framework entrance 2011; 2. The user performs user configuration 2012 through the configuration control provided by the plug-in: an object configuration instruction can be issued to specify the source object and the destination object, and a technology selection instruction can also be issued to select the corresponding technology type; 3. The above code generation framework 200 obtains the class source file based on the source object and the destination object configured by the user, and loads the above class source file through the class source loading 2013; 4. The class source analysis 2023 is performed through QDox 2042 parses the above class source file to obtain the corresponding class information. At the same time, the template data construction 2024 constructs data based on the above class information and other necessary parameters based on the analysis results of the information and data interaction between each layer in the model analysis 2022; the tool class 2021 is required in the process of parsing the above class source file and constructing the template data; 5. The above code generation framework 200 determines the corresponding template file based on the selected technology type corresponding to the technology selection instruction issued by the above user. If the user does not select the technology, the default technology type is selected and the default configuration is performed through the general configuration file 2043. Then, the above class information and constructed data are injected into the template file under the FreeMarker 2043 framework to generate the BeanUtils class interface code. The BeanUtils class interface code finally generated by the above process contains relevant annotation information about the basic implementation, which ensures that the called BeanUtils class interface code will generate Bean copy code implemented using native getter and setter methods during the compilation stage.

[0053] Combine the following Figure 3 ,by Figure 2 Taking the code generation framework shown in the figure as an example, the method of automatically generating Bean copy code is further explained. Figure 3 This is another flow chart of a method for automatically generating Bean copy code according to an exemplary embodiment of the present application. First of all, it should be noted that: Figure 2 The code generation framework is built based on the Mapstruct integrated environment, and the output of the framework after running is the BeanUtils class interface code generated based on the Java language. The method may include the following steps:

[0054] Step 302: Receive a command from a user.

[0055] The user can configure the objects or related parameters involved by issuing instructions (corresponding to user configuration 2012). In one embodiment, the user can issue an object configuration instruction to specify the source object and the destination object; wherein, when the object configuration instruction is an empty instruction, the source object and the destination object are determined according to the preset general configuration data. The user can specify the object name of the source object and / or the destination object, the project package name of the project, the data type and attributes in the project, and other information through the above object configuration instructions. Compared with using all default objects, the Bean copy code generated by the user configuring the above source object and destination object is more targeted.

[0056] Specifically, the object configuration instruction issued by the user can be configured only for the source object, and the corresponding destination object is a preset default destination object; it can also be configured only for the destination object, and the corresponding source object is a preset default source object; it can also be configured for both the source object and the destination object. The above-mentioned object configuration instruction can also be an empty instruction. In the case of an empty instruction, the source object and the target object are determined according to the preset general configuration data (corresponding to the general configuration file 2032), wherein the default source object and the default destination object are preset in the general configuration data. The number of source objects and destination objects configured by the above-mentioned object configuration instruction can be one or more; and the above-mentioned default source object and default destination object can be all objects involved in the project currently developed by the development device or some of the objects that meet the preset conditions. The above-mentioned object configuration instruction issued by the user can be one instruction, or it can be composed of multiple sub-instructions, wherein the object configuration instruction or any sub-instruction can configure one or more parameters such as the object name, data type, object access path, etc. of the source object or the destination object.

[0057] In another embodiment, the object configuration instruction is also used to set the package name and / or class name suffix of the source object, and the package name and / or class name suffix of the destination object. The user sets the above information of the source object and the destination object by himself, avoiding the cumbersome code that may be caused by adopting the default of all objects, and helping to generate a more concise and targeted Bean copy code. Specifically, the object configuration instruction can only configure the package name of the source object and the package name of the destination object, or only configure the class name suffix of the destination object and the class name suffix of the destination object, or configure the package name and class name suffix of the source object and the destination object at the same time. Among them, the package name of the source object can include the project package name of the project where the source object is located; the class name suffix of the source object can be one or more, such as the class name suffix of all classes corresponding to the source object, the class name suffix of the public class corresponding to the source object, the class name suffix of some classes that meet preset conditions in all classes corresponding to the source object, or the class name suffix of the class corresponding to the data to be copied in the source object, etc. For example, when the class in the source object is UserDTO class and / or ActionDTO class, the corresponding class name suffix is ​​DTO; when the class in the source object is txt class, the corresponding class name suffix is ​​txt, and other class names are similar. For the destination object, its corresponding package name and / or class name suffix is ​​similar to the above source object, and will not be repeated here.

[0058] Of course, when the above object configuration instructions do not set the package name and / or class name suffix of the source object and the package name and / or class name suffix of the destination object, the default configuration data contained in the preset general configuration file can be used for configuration. For example, when the user does not configure the package name, all project packages in the source object or some project packages that meet the preset conditions are used by default.

[0059] In another embodiment, the user can also issue a technology selection instruction to select the standard technology type provided by the above code generation framework or the user-defined custom technology type as the technology type actually adopted. Among them, the above code generation framework can provide multiple standard technology types for users to choose from, such as the database connection technology of the DAO layer can include MyBatis framework, Hibernate framework, MyBatis-Plus framework, etc.; and the user-defined custom technology type and its corresponding custom template file can be generated and configured by the user in advance or temporarily. The user selects the technology type that is more suitable for the project to be developed or the one he is more familiar with, which can improve the applicability of the method; and allowing users to customize technology types meets the user's different programming requirements for various project functions, further expanding the applicable scenarios of this method.

[0060] Users can also issue a path configuration instruction containing project information such as the project package name and / or project name corresponding to the project to be developed, to specify the generation path of the BeanUtils class interface code that is ultimately generated by the code generation framework. Since users are allowed to customize the code generation path, it not only avoids the error problem that may be caused by directly using the default path during code generation, but also eliminates the tedious code path transfer operation that may be required after the default path is generated.

[0061] Of course, users can also issue other configuration instructions according to project functions and actual conditions. This application does not limit the number of instructions issued by users and their specific content. It is understandable that Beans with features can be developed according to preset rules, and the above object configuration instructions that need to be issued by users can be automatically mapped, such as automatically mapping classes under packages such as dto and entity, etc., so that users do not need to manually configure objects, but only need to simply run the plug-in, further improving project development efficiency.

[0062] Step 304: determine the source object and the destination object.

[0063] In one embodiment, when the user issues an object configuration instruction in the above step 302, the source object and the destination object can be determined based on the above object configuration instruction; wherein the object configuration instruction may be an empty instruction. In the case of an empty instruction, the source object and the destination object that need to be copied by the Bean are determined based on the preset general configuration data.

[0064] In another embodiment, the source object and the destination object that need to be copied by the Bean can be determined based on other parameters of the project currently being developed by the development device, other preset configurations, objects involved in the current code and / or functions of the current code.

[0065] Step 306, determine and obtain the template file.

[0066] Corresponding to the technology selection instruction issued by the user in step 302, if the user selects a standard technology type, the above template file (corresponding to template file 2031) can be obtained from the template file library pre-stored in the above function plug-in, or from a standard file library, an open source file library, a self-built file library or other paths where template files are stored. If the user selects a custom technology type, the corresponding custom template file can be pre-generated by the user and saved in a self-built file library or other storage space after custom configuration, so as to obtain the above custom template file from the self-built file library or other storage space in this step; the custom template can also be temporarily generated and configured by the technology selection instruction or other related instructions issued by the user in step 302, and this application does not limit this.

[0067] In another embodiment, when the user does not issue a technology selection instruction, the technology type and its corresponding template file can be determined based on multiple factors according to the functional characteristics of the current software project to be developed, the data characteristics of the determined source object and target object, and / or the performance requirements of the project to be developed for Bean copy. Of course, the preset default technology type and its corresponding template file can also be directly used.

[0068] Among them, there can be one or more template files corresponding to any one technology type, and this application does not limit this, but the template file corresponding to any one technology type should be different from the template files corresponding to other technology types. The template file can store basic code information such as keywords, variables or classes, and the information format in the template file corresponding to any one technology type corresponds to the technology type, so the database operation code generated based on the template file also conforms to the above information format.

[0069] When the user selects any technology type, the above code generation framework will generate the final BeanUtils class interface code based on the template file corresponding to the selected technology type. For example, when the user selects the MyBatis framework as the database connection technology of the DAO layer, the MyBatis framework will be used to generate the BeanUtils class interface code of the DAO layer.

[0070] Step 308, obtaining the class source file.

[0071] After determining the source object and the destination object, first obtain the class source files of the source object and the destination object. Specifically, the class source files of the source object and the destination object can be obtained separately and independently, such as first obtaining the class source file of the source object, and then obtaining the class source file of the destination object; or, first obtaining the class source file of the destination object, and then obtaining the class source file of the source object. The class source files of the source object and the destination object can also be obtained in association, such as first obtaining the class source file of the source object, and then generating the class source file of the destination object according to the class correspondence between the source object and the destination object; or, first obtaining the class source file of the destination object, and then generating the class source file of the source object according to the class correspondence between the destination object and the source object. The present application does not limit the above-mentioned method or order of obtaining the class source files.

[0072] Taking the class source file of the source object as an example, the class source file can be pre-generated and saved in the Java file corresponding to the source object, or associated with the source object and saved in other storage paths; the class source file can also be temporarily generated when it is needed. Among them, in the case of pre-generating the above-mentioned class source file, the update cycle or aging time of the class source file can be preset according to the project operation rules: after reaching the preset time, a new class source file is generated according to the class corresponding to the current object to replace the class source file of the previous cycle; the class source file can also be updated in an event-triggered manner: when the class corresponding to the source object changes, the change event triggers the update of the current class source file. The corresponding class in the obtained class source file can be one or more of System, StringBuilder, String, Math, Date, File, FileInputStream, FileOutputStream, HashMap, etc., and this application does not limit this.

[0073] Of course, you can also obtain the basic information of the source object or the destination object, the association information between the source object and the destination object (corresponding model analysis 2022), the data exchange information between the DAO layer, Service layer and Controller layer in the project to be developed, and other dynamic information in the project to be developed. This application does not limit the type of information obtained and the specific method of obtaining it.

[0074] In one embodiment, when it is known from the class source file that the class names in the source object and the destination object are the same, the name of the source object or the destination object can be automatically modified to the combined package name according to a preset rule to avoid compilation errors in the final generated BeanUtils interface code. Of course, if the user knows that the class names in the source object and the destination object are the same when issuing an object configuration instruction, the user can also actively modify the name of the source object or the destination object to the combined package name when issuing the object configuration instruction.

[0075] Step 310: parse the class source file to obtain class information.

[0076] After obtaining the class source files of the above-mentioned source object and destination object, use QDox to analyze all Java classes corresponding to the source object and the destination object (corresponding class source analysis 2023) to determine whether the attribute types and name types corresponding to the Java classes corresponding to the source object and the Java classes corresponding to the destination object are consistent, because data can only be copied between the two if the above-mentioned information of the source object and the destination object is consistent.

[0077] When it is determined that the attribute types and name types corresponding to the Java class corresponding to the source object and the Java class corresponding to the destination object are consistent, that is, the source object and the destination object meet the Bean copy condition, first initialize the class resources corresponding to the source object and the destination object (corresponding class source loading 2013); then initialize the above configuration made by the user and make corresponding assignments, and use the preset default configuration for the parameters not configured by the user; finally, traverse and parse all class names and class attributes in the class source files corresponding to the source object and the destination object to obtain class information and necessary class data. Among them, the above class data can also be obtained through object query or association relationship calculation instead of parsing.

[0078] Similar to the above-mentioned acquisition of class source files, in the process of parsing the class source files to obtain the corresponding class information, the class source files of the above-mentioned source objects and the class source files of the target objects can be parsed separately, such as parsing the class source files of the source files first, and then parsing the class source files of the target objects; or, parsing the class source files of the target objects first, and then parsing the class source files of the source objects. The class source files of the source object and the target object can also be associated and parsed, such as first parsing the class source files of the source files to obtain the class information of the source object, and then generating the class information of the target object according to the class correspondence between the source object and the target object; or first parsing the class source files of the target files to obtain the class information of the target object, and then generating the class information of the source object according to the class correspondence between the target object and the source object; at this time, only the class source files of the source object (or the target object) need to be parsed to obtain the class information of the two, which improves the speed of obtaining the class information. Tool class 2021 is required in the above-mentioned parsing and related data processing process.

[0079] Since the class source files corresponding to the source object and the target object may be of multiple types, there may be multiple types of class information obtained through parsing, such as instanceof, isInstance and / or RTTI (Run-Time Type Identification). You can only select the class information related to subsequent code generation for parsing and saving.

[0080] After the class information is parsed, the class information and the class data corresponding to it (corresponding to the template data structure 2024) are saved. Specifically, the class information corresponding to the source object and the class information corresponding to the destination object can be saved independently, or the class information corresponding to the source object and the destination object can be saved in association. Only the above-mentioned parsed class information can be saved, or the parsed class information and its corresponding class source file can be saved in association at the same time, so that the above-mentioned information can be used directly when it is used in the subsequent processing process to save acquisition time.

[0081] Regarding the acquisition of the template file in step 306 and the acquisition of the class source file and parsing to obtain the class information in steps 308 and 310, in this embodiment, as an exemplary embodiment, the template file can be acquired first and then the class source file can be acquired and parsed to obtain the class information, or the class source file can be acquired first and parsed to obtain the class information and then the template file can be acquired. In other words, there is no necessary order between "acquiring the template file" and "acquiring the class source file and parsing to obtain the class information", which can be adjusted according to the actual situation, and it is only necessary to ensure that both are completed before the class information is injected into the template file in step 312.

[0082] Step 312, injecting class information into the template file to generate BeanUtils class interface code.

[0083] The class information obtained by parsing the class source file is dynamically injected into the template file corresponding to the currently selected technology type to generate the BeanUtils class interface code. In fact, the class data corresponding to the class information and the dynamic data related to the project to be developed can also be injected into the template file under FreeMarker.

[0084] Because the above code generation framework is built in the Mapstruct integrated environment, the finally generated BeanUtils class interface code contains annotation information about the Mapstruct annotation processor. Similarly, it also contains the compilation information of the above interface code, so as to ensure that after the above interface is applied in the project to be developed, the Bean copy code implemented by the native getter and setter methods can be generated during the project compilation process according to the called interface code.

[0085] Step 314, compile the BeanUtils class interface code to generate the Bean copy code.

[0086] As mentioned above, the BeanUtils class interface code directly generated by the above code generation framework can be applied in the project to be developed. When the project is developed and run (of course, it can also be in the development and debugging stage), during the compilation stage of the project, the Bean copy code implemented by the native getter and setter methods will be generated according to the above BeanUtils class interface code. This part of the code is used to copy the data to be copied in the source object to the target object during the operation of the above project.

[0087] Figure 4 is a schematic diagram of the structure of an electronic device shown in an exemplary embodiment of the present application. Figure 4At the hardware level, the electronic device includes a processor 401, an internal bus 402, a network interface 403, a memory 404, and a non-volatile memory 405, and may also include hardware required for other services. The processor 401 reads the corresponding computer program from the non-volatile memory 405 into the memory 404 and runs it, forming a device for automatically generating Bean copy code at the logical level. Of course, in addition to the software implementation, this application does not exclude other implementation methods, such as logic devices or a combination of software and hardware, etc., that is to say, the execution subject of the following processing flow is not limited to each logic unit, but can also be hardware or logic devices.

[0088] Figure 5 A block diagram of an apparatus for automatically generating Bean copy code according to an exemplary embodiment of the present application is shown. Figure 5 In a software implementation, the device for automatically generating Bean copy code is applied to a development device using a Mapstruct mapper framework, and may include an object determination unit 501, a file parsing unit 502, and a code generation unit 503. Wherein:

[0089] The object determination unit 501 is used to determine a source object and a destination object, and the Bean copy code is used to copy the data to be copied from the source object to the destination object;

[0090] A file parsing unit 502, used to parse the class source files of the source object and the target object to obtain class information;

[0091] The code generation unit 503 is used to inject the class information into the template file to generate the BeanUtils class interface code corresponding to the source object and the destination object, wherein the BeanUtils class interface code is used to compile and generate the Bean copy code implemented using native getter and setter methods.

[0092] Optionally, the object acquisition unit is specifically used to:

[0093] Determine the source object and the destination object according to the object configuration instruction issued by the user;

[0094] Wherein, when the object configuration instruction is an empty instruction, the source object and the target object are determined according to preset general configuration data.

[0095] Optionally, the object configuration instruction is further used to set the package name and / or class name suffix of the source object, and the package name and / or class name suffix of the destination object.

[0096] Optionally, the device for automatically generating Bean copy code may further include:

[0097] A technology type determination unit 504 is used to determine the selected technology type according to the technology selection instruction issued by the user;

[0098] The template file selection unit 505 is used to select a template file matching the selected technology type to generate the BeanUtils class interface code.

[0099] Optionally, the template file selection unit is specifically used to:

[0100] The template file is selected from a preset template repository, wherein the template repository includes at least one of the following: an open source template file library, a dedicated template file library, and a custom template file library.

[0101] The implementation process of the functions and effects of each unit in the above-mentioned device is detailed in the implementation process of the corresponding steps in the above-mentioned method, and will not be repeated here.

[0102] In a typical configuration, an electronic device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0103] The memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.

[0104] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules 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 technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0105] For the above-mentioned device embodiments, since they basically correspond to the method embodiments, the relevant parts can refer to the partial description of the method embodiments. The device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the present application scheme. A person of ordinary skill in the art can understand and implement it without paying any creative work.

[0106] The above description is only a preferred embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application shall be included in the scope of protection of the present application.

Claims

1. A method for automatically generating Bean copy code, characterized in that: Applied to a development device using a Mapstruct mapper framework, the method comprises: Determine a source object and a destination object, the Bean copy code is used to copy the data to be copied from the source object to the destination object; Parsing the class source files of the source object and the target object respectively to obtain class information, including: traversing and parsing all class names and class attributes in the class source files of the source object and the target object to obtain class information; The class information is injected into the template file to generate BeanUtils class interface code corresponding to the source object and the destination object, wherein the BeanUtils class interface code is used to compile and generate the Bean copy code implemented using native getter and setter methods.

2. The method according to claim 1, characterized in that The determining of the source object and the destination object includes: Determine the source object and the destination object according to the object configuration instruction issued by the user; Wherein, when the object configuration instruction is an empty instruction, the source object and the destination object are determined according to preset general configuration data.

3. The method according to claim 2, characterized in that The object configuration instruction is also used to set the package name and / or class name suffix of the source object, and the package name and / or class name suffix of the destination object.

4. The method according to claim 1, characterized in that: Also includes: Determine the selected technology type according to the technology selection instruction issued by the user; A template file matching the selected technology type is selected to generate the BeanUtils class interface code.

5. The method according to claim 4, characterized in that The selecting of a template file matching the selected technology type includes: The template file is selected from a preset template repository, wherein the template repository includes at least one of the following: an open source template file library, a dedicated template file library, and a custom template file library.

6. A device for automatically generating Bean copy code, characterized in that: Applicable to a development device using a Mapstruct mapper framework, the device comprises: An object determination unit, used for determining a source object and a destination object, wherein the Bean copy code is used for copying the data to be copied from the source object to the destination object; The file parsing unit is used to parse the class source files of the source object and the target object to obtain class information, including: traversing and parsing all class names and class attributes in the class source files of the source object and the target object to obtain class information; A code generation unit is used to inject the class information into a template file to generate a BeanUtils class interface code corresponding to the source object and the destination object, wherein the BeanUtils class interface code is used to compile and generate the Bean copy code implemented using native getter and setter methods.

7. The device according to claim 6, characterized in that The object determination unit is specifically used for: Determine the source object and the destination object according to the object configuration instruction issued by the user; Wherein, when the object configuration instruction is an empty instruction, the source object and the destination object are determined according to preset general configuration data.

8. The device according to claim 7, characterized in that The object configuration instruction is also used to set the package name and / or class name suffix of the source object, and the package name and / or class name suffix of the destination object.

9. The device according to claim 6, characterized in that Also includes: A technology type determination unit, used to determine the selected technology type according to the technology selection instruction issued by the user; The template file selection unit is used to select a template file matching the selected technology type to generate the BeanUtils class interface code.

10. The device according to claim 9, characterized in that The template file selection unit is specifically used for: The template file is selected from a preset template repository, wherein the template repository includes at least one of the following: an open source template file library, a dedicated template file library, and a custom template file library.

11. An electronic device, characterized in that: include: processor; a memory for storing processor-executable instructions; The processor is configured to implement the method as claimed in any one of claims 1 to 5.

12. A computer-readable storage medium having computer instructions stored thereon, characterized in that: When the instruction is executed by a processor, the steps of the method according to any one of claims 1 to 5 are implemented.