A code generation method, apparatus, electronic device, and storage medium
By monitoring the trigger events generated by code and using the automatic generator object to generate corresponding object code, the problem of inefficient traditional manual writing of similar codes is solved, and the efficiency of automatic code generation and development is improved.
Patent Information
- Application Number
- CN202011127652.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-10-20
- Publication Date
- 2025-04-01
- Estimated Expiration
- 2040-10-20
AI Technical Summary
In traditional solutions, many similar codes are manually written and generated by developers, resulting in large R&D work hours and inefficient code development.
Provides a code generation method, which monitors the trigger event generated by the code, obtains the built automatic generator object, and calls its code generation function to generate the target code corresponding to the trigger event. The automatic generator object is constructed through configuration information, data connection information, package name information and policy information.
It realizes automatic code generation, reduces the development time of redundant code and improves code development efficiency.
Smart Images

Figure CN114385175B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to the field of computer applications, and in particular to a code generation method, device, electronic device and storage medium. Background Art
[0002] In traditional solutions, many similar codes are manually written and generated by developers one by one. Obviously, this code generation solution will take up a lot of R&D man-hours and the code development efficiency is low. Summary of the invention
[0003] The embodiments of the present invention provide a code generation method, device, electronic device and storage medium to achieve the effect of automatic code generation.
[0004] In a first aspect, an embodiment of the present invention provides a code generation method, which may include:
[0005] When a trigger event of code generation is detected, the constructed automatic generator object corresponding to the trigger event is obtained, and the code generation function in the automatic generator object is called;
[0006] According to the output result of the code generation function, the target code corresponding to the triggering event is obtained;
[0007] The automatic generator object is pre-built through the following steps:
[0008] Obtaining the initial generator object, information configuration object, database configuration object, package configuration object, strategy object, and template information of the existing code corresponding to the triggering event obtained by the initial construction;
[0009] Configure the configuration information in the information configuration object, the data connection information in the database configuration object, the package name information in the package configuration object, and the policy information in the policy object respectively;
[0010] Assign each configuration result and template information to the initial generator object to obtain an automatic generator object, wherein the code generation function implements the code generation function according to each assignment result in the automatic generator object.
[0011] Optionally, the configuration information in the information configuration object may include:
[0012] configuring the code output path information, the code file creator information and / or the server naming information into the corresponding configuration information variables in the information configuration object respectively; and / or,
[0013] Configure the class setting information, language setting information, language annotation setting information and / or structure setting information to the corresponding configuration information variables in the information configuration object respectively, wherein the class setting information includes the class whose code file is open, the language setting information includes supporting the generation of kotlin language, the language annotation setting information includes supporting Swagger2 language annotation, and / or the structure setting information includes generating BaseResulMap structure and BaseColumnList structure;
[0014] Correspondingly, assigning each configuration result to the initial generator object may include: assigning the configured information configuration object to the globalConfig variable in the initial generator object.
[0015] Optionally, configure the data connection information in the database configuration object, which may include:
[0016] The data path information, database user name and / or database password are respectively configured into corresponding data connection information variables in the database configuration object, wherein the data path information includes database domain name, database name and / or database port;
[0017] Correspondingly, each configuration result is assigned to the initial generator object, including: assigning the configured database configuration object to the dataSource variable in the initial generator object.
[0018] Optionally, configure the package name information in the package configuration object, which may include:
[0019] Configure the package name information to the corresponding package name information variable in the package configuration object, where the package name information is the path information of the code file where the target code is located;
[0020] Correspondingly, each configuration result is assigned to the initial generator object, including: assigning the configured package configuration object to the PackageInfo variable in the initial generator object.
[0021] Optionally, configure the policy information in the policy object, which may include:
[0022] Convert the naming conventions of code files and database lists from underscores to camel case, and configure the conversion results to the corresponding configuration information variables in the strategy object; and / or,
[0023] Configuring class support information into corresponding configuration information variables in the strategy object, wherein the class support information includes that the entity class does not support the Lombok format; and / or,
[0024] Configure the effective list information to the corresponding configuration information variable in the policy object;
[0025] Correspondingly, each configuration result is assigned to the initial generator object, including: assigning the configured strategy object to the strategy variable in the initial generator object.
[0026] Optionally, get the constructed auto-generator object corresponding to the triggering event, which may include:
[0027] The existing code corresponding to the triggering event is obtained, the template information associated with the existing code is filtered out from each candidate information, and the automatic generator object is filtered out from each candidate generator object that has been constructed according to the template information.
[0028] Optionally, the code generation function can implement the code generation function through the following steps:
[0029] Connect to the database to be accessed according to the dataSource variable in the automatic generator object;
[0030] According to each assignment result in the automatic generator object, the field information obtained from the to-be-accessed list of the to-be-accessed database is used to generate the entity class of the to-be-accessed database;
[0031] Generate target code according to entity class and template information, and output the target code according to the PackageInfo variable in the automatic generator object.
[0032] In a second aspect, an embodiment of the present invention further provides a code generation device, which may include:
[0033] The function calling module is used to obtain the constructed automatic generator object corresponding to the triggering event and call the code generation function in the automatic generator object when a triggering event of code generation is detected;
[0034] The code generation module is used to obtain the target code corresponding to the triggering event according to the output result of the code generation function; wherein the automatic generator object is pre-built through the following modules:
[0035] An information acquisition module is used to obtain the initial generator object, information configuration object, database configuration object, package configuration object, policy object, and template information of the existing code corresponding to the trigger event obtained by the initial construction; an information configuration module is used to configure the configuration information in the information configuration object, the data connection information in the database configuration object, the package name information in the package configuration object, and the policy information in the policy object respectively; an object construction module is used to assign each configuration result and template information to the initial generator object to obtain an automatic generator object, wherein the code generation function implements the code generation function according to each assignment result in the automatic generator object.
[0036] In a third aspect, an embodiment of the present invention further provides an electronic device, which may include:
[0037] one or more processors;
[0038] A memory for storing one or more programs;
[0039] When one or more programs are executed by one or more processors, the one or more processors implement the code generation method provided by any embodiment of the present invention.
[0040] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the code generation method provided by any embodiment of the present invention.
[0041] The technical solution of the embodiment of the present invention configures the configuration information in the acquired information configuration object, the data connection information in the database configuration object, the package name information in the package configuration object, and the policy information in the policy object respectively, and assigns the configuration result and the acquired template information to the initial generator object, thereby obtaining a constructed automatic generator object, which can correspond to the existing code already developed by at least one developer; on this basis, when a trigger event for code generation is detected, the automatic generator object corresponding to the trigger event can be obtained, and the target code corresponding to the trigger event can be generated by calling the code generation function in the automatic generator object. The above technical solution, based on the automatic generator object corresponding to the target code to be generated, can automatically generate a target code that is relatively similar to the existing code already developed by the developer, thereby achieving the effect of improving the code development efficiency by reducing the development time of redundant code. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] Figure 1 is a flow chart of a code generation method in Embodiment 1 of the present invention;
[0043] Figure 2 is a flow chart of a code generation method in Embodiment 2 of the present invention;
[0044] Figure 3 is a flow chart of a code generation method in Embodiment 3 of the present invention;
[0045] Figure 4 is a structural block diagram of a code generating device in Embodiment 4 of the present invention;
[0046] Figure 5 It is a structural schematic diagram of an electronic device in Embodiment 5 of the present invention. DETAILED DESCRIPTION
[0047] The present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It is to be understood that the specific embodiments described herein are only used to explain the present invention, rather than to limit the present invention. It is also necessary to explain that, for ease of description, only parts related to the present invention, rather than all structures, are shown in the accompanying drawings.
[0048] Before introducing the embodiment of the present invention, the application scenario of the embodiment of the present invention is first exemplified: in practical applications, optionally, a class can be defined to handle the code generation function, and the specific definition of the class can be as follows: Class myCodeGen{}, and then some constant information that may be involved in the subsequent code generation process can be defined in the class, and the definition and meaning of the constant information can be as follows:
[0049]
[0050]
[0051] Next, you can define a main function to handle the code generation task. In this main function, you can execute the construction process of the automatic generator object and the code generation process. The definition of the main function is as follows:
[0052] @JvmStatic
[0053] fun main(args:Array <string>){}
[0054] Embodiment 1
[0055] Figure 1 It is a flowchart of a code generation method provided in the first embodiment of the present invention. This embodiment is applicable to the case where a target code similar to an existing code is automatically generated based on a constructed automatic generator object. The method can be executed by a code generation device provided in an embodiment of the present invention, which can be implemented by software and / or hardware, and the device can be integrated in an electronic device, which can be various user terminals or servers.
[0056] See also Figure 1 The method of the embodiment of the present invention specifically includes the following steps:
[0057] S110. Obtain the initial generator object, information configuration object, database configuration object, package configuration object, policy object, and template information of the existing code corresponding to the trigger event obtained by the initial construction, wherein the trigger event is a monitored event for implementing code generation.
[0058] Among them, the object obtained by the initial construction can be an object that has completed the initial construction and is waiting for information configuration. For example, in the above main function, a global initial generator object that is waiting for information configuration can be automatically constructed through the val autoGenerator=AutoGenerator() function; a global information configuration object gc is constructed by calling the val gc=GlobalConfig() function; after completing the above global configuration, a database configuration object dsc is constructed by calling the val dsc=DataSourceConfig() function; a package configuration object pc is constructed by the val pc=PackageConfig() function; on this basis, optionally, some custom configuration objects can also be constructed, such as constructing an injectionConfig object by calling the val injectionConfig=object:InjectionConfig(){override fun initMap(){}} function, and another example is by calling val focList=mutableListOf <fileoutconfig>() function to build a focList object. These two objects can store some descriptive information, such as the number of spaces when indenting, common basic rules, etc.; generate a strategy object strategy by calling val strategy = StrategyConfig() function; and so on.
[0059] For multiple existing codes developed by developers, each existing code can correspond to different template information, which can store general rules corresponding to the existing code, such as whether a certain function exists in the Mapper data. Of course, if two or more existing codes are similar, they can correspond to the same template information. Therefore, when a trigger event for code generation is detected, it can be determined based on the trigger event which existing code the target code to be generated is based on, and then the template information associated with the existing code can be obtained.
[0060] S120: respectively configure the configuration information in the information configuration object, the data connection information in the database configuration object, the package name information in the package configuration object, and the policy information in the policy object.
[0061] Among them, there are multiple ways to implement information configuration for each object. Exemplarily, the implementation method of configuring the configuration information in the information configuration object may include: configuring the code output path information, the code file creator information and / or the server naming information into the corresponding configuration information variables in the information configuration object respectively; and / or, configuring the class setting information, the language setting information, the language annotation setting information and / or the structure setting information into the corresponding configuration information variables in the information configuration object respectively, wherein the class setting information includes that the code file is an open class, the language setting information includes support for generating kotlin language, the language annotation setting information includes support for Swagger2 language annotations, and / or the structure setting information includes generating BaseResulMap structure and BaseColumnList structure.
[0062] Exemplarily, an implementation method for configuring the data connection information in a database configuration object may include: respectively configuring the data path information, database user name and / or database password into corresponding data connection information variables in the database configuration object, wherein the data path information includes a database domain name, a database name and / or a database port, and the database domain name, database name, database port, database user name and database password may all be constant information defined above.
[0063] Exemplarily, the implementation of configuring the package name information in the package configuration object may include: configuring the package name information into the corresponding package name information variable in the package configuration object. It should be noted that since the automatically generated target code can be stored in a code file, the path information of the code file can be represented by the package name information in the package configuration object, which means that if you want to find a code file, you need to first know which package the code file is in.
[0064] Exemplarily, configuring the focList object may generate path information of Mapper data, which may be an intermediate file for accessing the database to be accessed. The intermediate file may be found according to the path information of the intermediate file so as to be connected to the database to be accessed according to the intermediate file.
[0065] As another example, the implementation method for configuring the policy information in the policy object may include: converting the naming rules of the code files and database lists from underscores to camel case, and configuring the conversion results into the corresponding configuration information variables in the policy object; and / or, configuring the class support information into the corresponding configuration information variables in the policy object, wherein the class support information includes that the entity class does not support the Lombok format; and / or, configuring the effective list information into the corresponding configuration information variables in the policy object.
[0066] S130. Assign each configuration result and template information to the initial generator object to obtain a constructed automatic generator object.
[0067] Among them, the implementation method of assigning each configuration result to the initial generator object may include assigning the configured information configuration object to the globalConfig variable in the initial generator object; assigning the configured database configuration object to the dataSource variable in the initial generator object; assigning the configured package configuration object to the PackageInfo variable in the initial generator object; assigning the configured focList object to the injectionConfig object, and then assigning the assigned injectionConfig object to the cfg variable in the initial generator object; assigning the configured strategy object to the strategy variable in the initial generator object; assigning the template information to the templateEngine variable in the initial generator object by calling the autoGenerator.templateEngine=FreemarkerTemplateEngine() function; etc., which are not specifically limited here. Based on this, the assigned initial generator object can be used as a constructed automatic generator object.
[0068] It should be noted that the number of the automatic generator objects can be one, two or more, and each automatic generated object can correspond to at least one existing code. For example, if the difference between the existing code A and the existing code B is small, the two can correspond to the same automatic generator object; otherwise, the two can correspond to different automatic generator objects.
[0069] S140. When a triggering event for code generation is detected, the automatic generator object corresponding to the triggering event is obtained, and the code generation function in the automatic generator object is called so that the code generation function implements the code generation function according to each assignment result in the automatic generator object.
[0070] Among them, the trigger event can be an event for automatically generating a target code that is relatively similar to an existing code. Since the automatic generator objects corresponding to different existing codes may not be the same automatic generator objects, when the trigger event is detected, the automatic generator object corresponding to the trigger event is first obtained. Further, the code generation function in the automatic generator object can be called so that the code generation function implements the code generation function according to the various assignment results in the automatic generator object. Exemplarily, the code generation function can be executed by calling the autoGenerator.execute() function, and finally the target code corresponding to the above-mentioned various configurations can be generated. Such a target code can be called a template code.
[0071] S150. Obtain target code corresponding to the triggering event according to the output result of the code generation function.
[0072] The technical solution of the embodiment of the present invention configures the configuration information in the acquired information configuration object, the data connection information in the database configuration object, the package name information in the package configuration object, and the policy information in the policy object respectively, and assigns the configuration result and the acquired template information to the initial generator object, thereby obtaining a constructed automatic generator object, which can correspond to the existing code already developed by at least one developer; on this basis, when a trigger event for code generation is detected, the automatic generator object corresponding to the trigger event can be obtained, and the target code corresponding to the trigger event can be generated by calling the code generation function in the automatic generator object. The above technical solution, based on the automatic generator object corresponding to the target code to be generated, can automatically generate a target code that is relatively similar to the existing code already developed by the developer, thereby achieving the effect of improving the code development efficiency by reducing the development time of redundant code.
[0073] Embodiment 2
[0074] Figure 2 It is a flow chart of a code generation method provided in the second embodiment of the present invention. This embodiment is optimized based on the above-mentioned technical solutions. In this embodiment, optionally, obtaining a constructed automatic generator object corresponding to the triggering event may specifically include: obtaining the existing code corresponding to the triggering event, filtering out the template information associated with the existing code from each candidate information, and filtering out the automatic generator object from each constructed candidate generator object according to the template information. Among them, the explanations of the terms that are the same or corresponding to the above-mentioned embodiments are not repeated here.
[0075] See also Figure 2 The method of this embodiment may specifically include the following steps:
[0076] S210. Obtain the initial generator object, information configuration object, database configuration object, package configuration object, policy object, and template information of the existing code corresponding to the trigger event obtained by the initial construction, wherein the trigger event is a monitored event for implementing code generation.
[0077] S220: respectively configure the configuration information in the information configuration object, the data connection information in the database configuration object, the package name information in the package configuration object, and the policy information in the policy object.
[0078] S230. Assign each configuration result and template information to the initial generator object to obtain a constructed automatic generator object.
[0079] S240. When a trigger event for code generation is detected, the existing code corresponding to the trigger event is obtained, template information associated with the existing code is filtered out from each candidate information, and an automatic generator object is filtered out from each candidate generator object that has been constructed according to the template information.
[0080] Among them, for multiple existing codes developed by developers, each existing code can correspond to different template information respectively, and the template information can store general rules corresponding to the existing code, such as whether there is a certain function in the Mapper data. Of course, if two or more existing codes are similar, they can correspond to the same template information. Therefore, when a trigger event for code generation is monitored, it can be determined based on the trigger event which existing code is used as the benchmark for the target code to be generated, and then the template information associated with the existing code can be screened out from each candidate information, and each candidate information is a template information associated with different existing codes. Furthermore, since each candidate generator object is an automatic generator object associated with different template information, this is because a certain template information is involved in the construction process of each automatic generator object, so the automatic generator object can be screened out from each candidate generator object that has been constructed according to the template information.
[0081] S250, calling the code generation function in the automatic generator object so that the code generation function implements the code generation function according to each assignment result in the automatic generator object, and obtaining the target code corresponding to the triggering event according to the output result of the code generation function.
[0082] The technical solution of the embodiment of the present invention can obtain the existing code corresponding to the trigger event when a trigger event of code generation is monitored, so as to filter out template information from various candidate information according to the existing code, and subsequently accurately filter out the automatic generator object corresponding to the trigger event from various candidate generator objects that have been constructed according to the template information.
[0083] Embodiment 3
[0084] Figure 3 It is a flow chart of a code generation method provided in the third embodiment of the present invention. This embodiment is optimized based on the above-mentioned technical solutions. In this embodiment, optionally, the code generation function implements the code generation function through the following steps: connecting with the database to be accessed according to the dataSource variable in the automatic generator object; generating the entity class of the database to be accessed according to the field information obtained from the to-be-accessed list of the database to be accessed according to the assignment results in the automatic generator object; generating the target code according to the entity class and template information, and outputting the target code according to the PackageInfo variable in the automatic generator object. Among them, the explanations of the terms that are the same or corresponding to the above-mentioned embodiments are not repeated here.
[0085] See also Figure 3 The method of this embodiment may specifically include the following steps:
[0086] S310. Obtain the initial generator object, information configuration object, database configuration object, package configuration object, policy object, and template information of the existing code corresponding to the trigger event obtained by the initial construction, wherein the trigger event is a monitored event for implementing code generation.
[0087] S320: respectively configure the configuration information in the information configuration object, the data connection information in the database configuration object, the package name information in the package configuration object, and the policy information in the policy object.
[0088] S330. Assign each configuration result and template information to the initial generator object to obtain a constructed automatic generator object.
[0089] S340. When a triggering event for code generation is monitored, the automatic generator object corresponding to the triggering event is obtained, and the code generation function in the automatic generator object is called so that the code generation function is connected to the database to be accessed according to the dataSource variable in the automatic generator object; according to each assignment result in the automatic generator object, the field information obtained from the to-be-accessed list of the database to be accessed is used to generate the entity class of the database to be accessed; the target code is generated according to the entity class and template information, and the target code is output according to the PackageInfo variable in the automatic generator object.
[0090] Among them, if you want to generate the target code for accessing the database to be accessed, you need to first generate the entity class of the database to be accessed. Therefore, considering that the dataSource variable in the automatic generator object includes various data connection information related to the database to be accessed, such as database domain name, database name, database port, database user name, database password, etc., the database to be accessed can be connected according to the dataSource variable. Furthermore, since each assignment result in the automatic generator object has defined the relevant content of the code when accessing the database to be accessed, and the database to be accessed includes multiple lists to be accessed, the field information can be obtained from the list to be accessed of the database to be accessed according to each assignment result, and the entity class related to the entity class of the database to be accessed can be generated according to the field information, thereby generating the target code according to the entity class and template information. Furthermore, since the PackageInfo variable in the automatic generator object defines the path information of the code file where the target code is located, the target code can be output according to the PackageInfo variable.
[0091] S350: Obtain target code corresponding to the triggering event according to the output result of the code generation function.
[0092] According to the technical solution of the embodiment of the present invention, the code generation function is connected to the database to be accessed according to the dataSource variable in the automatic generator object, and the domain name, name, port, user name and / or password of the database to be accessed are stored in the dataSource variable; further, since each assignment result in the automatic generator object involves a class, language, structure, package, code file and / or database list, the field information obtained from the to-be-accessed list of the database to be accessed can be used to generate an entity class of the database to be accessed according to each assignment result, and the entity class is one of the necessary means to generate a code for accessing the database to be accessed; further, since the template information stores general rules for associating existing codes that are relatively similar to the target code to be generated, a target code that is relatively similar to the existing code can be generated according to the entity class and template information, and the target code is output according to the package name information in the PackageInfo variable in the automatic generator object, thereby achieving the effect of automatically generating a target code that is relatively similar to the existing code.
[0093] In order to better understand the specific implementation process of the above steps, the code generation method of this embodiment is exemplarily described below using the exemplary description described above as an example. For example, the specific content in the main function is as follows:
[0094] First, a global autogenerator object is constructed by calling the val autoGenerator = AutoGenerator() function. Then, a global information configuration object gc is constructed by calling the val gc = GlobalConfig() function. Next, the configuration information of the gc object is configured. The configuration information is as follows:
[0095] / / Configure code output path information
[0096] gc.outputDir = OUT_PUT_DIR
[0097] / / Configure the code creator information
[0098] gc.author="microzhang"
[0099] / / The configuration code file is an open class
[0100] gc.isOpen=true
[0101] / / Configure support for generating kotlin language
[0102] gc.isKotlin = true
[0103] / / Configure support for Swagger2 language annotations
[0104] gc.isSwagger2 = true
[0105] / / Configure the generated BaseResulMap structure
[0106] gc.isBaseResultMap = true
[0107] / / Configure the generated BaseColumnList structure
[0108] gc.isBaseColumnList = true
[0109] / / Configure Service naming information
[0110] gc.serviceName="%sService"
[0111] Next, by calling the autoGenerator.globalConfig=gc function, the gc object is assigned to the globalConfig variable in the autoGenerator object, so that the autoGenerator has the global configuration information.
[0112] On this basis, the corresponding data connection information is then configured. The configuration process of the data connection information can be: first, a data source configuration object dsc is constructed by calling the val dsc=DataSourceConfig() function, and then dsc.url=
[0113] The "jdbc:mysql: / / ${DB_HOST}:${DB_PORT} / ${DB_NAME}?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull" function configures the data path information url in the dsc object, where DB_HOST, DB_PORT, and DB_NAME are all constant information defined in the above exemplary description. These constant information are encoded in Unicode and the character encoding is UTF-8. The rewriteBatchedStatements parameter configuration and autoReconnect parameter configuration in jdbc are turned on. failOverReadOnly means automatic reconnection after a database exception, and it is not set to read-only after successful automatic reconnection. Add zeroDateTimeBehavior=convertToNull to the orm layer configuration so that no changes are required at the code level. Next, call dsc.username = DB_USER to configure the database user name (i.e., the user name of the data connection), and call dsc.password = DB_PASSWORD to configure the database password (i.e., the password of the data connection). Finally, call the autoGenerator.dataSource = dsc function to assign the dsc object to the dataSource field in the autoGenerator object, thus assigning the complete data connection configuration to the autoGenerator object.
[0114] Next, the package name information of the target code is configured. The specific configuration process is as follows: a package configuration object pc is generated through the val pc=PackageConfig() function, and then the package name information of the pc object is configured by calling the pc.parent=PACKAGE_NAME function, and then the pc object is assigned to the packageInfo field in the autoGenerator object by calling the autoGenerator.packageInfo=pc function.
[0115] Next, configure the relevant information of the custom configuration object. The specific configuration logic is as follows:
[0116]
[0117]
[0118] Function is used to configure the path information of the generated Mapper data. The Mapper data can be an intermediate file for accessing the database to be accessed. The intermediate file can be found according to the path information of the intermediate file, so as to connect to the database to be accessed according to the intermediate file and access the database to be accessed. Specifically, the path information is configured through the add function in the focList object. The add function implements the custom input of the path information by overriding fun outputFile. In this embodiment, the path information is gc.outputDir} / mapper / ${tableInfo? .entityName}Mapper${StringPool.DOT_XML. Then the focList object is assigned to injectionConfig.fileOutConfigList by calling the injectionConfig.fileOutConfigList=focList function, and finally the injectionConfig object is assigned to the cfg variable of autoGenerator by calling the autoGenerator.cfg=injectionConfig function.
[0119] Finally, you need to configure the generation strategy. The specific strategy configuration logic is as follows: Build a strategy object strategy by calling the valstrategy = StrategyConfig() function, and then call strategy.naming = NamingStrategy.underline_to_camel to configure the naming rules of the code file to convert underscores into camel case naming rules. Similarly, configure strategy.columnNaming = NamingStrategy.underline_to_camel to configure the naming rules of the database list to this rule. Then call strategy.isEntityLombokModel = false to indicate that the entity class does not support the Lombok format, and then call strategy.setInclude(*GEN_TABLE_LIST.toTypedArray()) to configure the effective list information into the object strategy, and finally assign the strategy object to the strategy field of the autoGenerator object by using the autoGenerator.strategy = strategy function.
[0120] The generated template information is configured by calling the autoGenerator.templateEngine = FreemarkerTemplateEngine() function, where FreemarkerTemplateEngine() is a template engine, and the template information generated by the template engine function can be assigned to the templateEngine variable in the autoGenerator object. Finally, the code generation function is executed by calling the autoGenerator.execute() function, where execute is the code generation function in the autoGenerator object, and finally execute can generate the final required template code according to the above configuration rules, thereby improving the code development efficiency.
[0121] Embodiment 4
[0122] Figure 4 This is a structural block diagram of a code generation device provided in Embodiment 4 of the present invention, and the device is used to execute the code generation method provided in any of the above embodiments. The device and the code generation method of the above embodiments belong to the same inventive concept, and the details not described in detail in the embodiment of the code generation device can refer to the embodiment of the above code generation method. Figure 4 The device may specifically include: an information acquisition module 410, an information configuration module 420, an object construction module 430, a function calling module 440 and a code generation module 450.
[0123] Among them, the information acquisition module 410 is used to obtain the initial generator object, information configuration object, database configuration object, package configuration object, policy object, and template information of the existing code corresponding to the trigger event obtained by the initial construction, wherein the trigger event is a monitored event for realizing code generation; the information configuration module 420 is used to respectively configure the configuration information in the information configuration object, the data connection information in the database configuration object, the package name information in the package configuration object, and the policy information in the policy object; the object construction module 430 is used to assign each configuration result and template information to the initial generator object to obtain the constructed automatic generator object; the function calling module 440 is used to obtain the automatic generator object corresponding to the trigger event when the code generation trigger event is monitored, and call the code generation function in the automatic generator object, so that the code generation function realizes the code generation function according to the assignment results in the automatic generator object; the code generation module module 450 is used to obtain the target code corresponding to the trigger event according to the output result of the code generation function.
[0124] Optionally, the information configuration module 420 may specifically include:
[0125] A first information configuration unit, wherein the first information configuration unit can be specifically used to:
[0126] The code output path information, the code file creator information and / or the server naming information are respectively configured into the corresponding configuration information variables in the information configuration object; and / or, the class setting information, the language setting information, the language annotation setting information and / or the structure setting information are respectively configured into the corresponding configuration information variables in the information configuration object, wherein the class setting information includes that the code file is an open class, the language setting information includes support for generating kotlin language, the language annotation setting information includes support for Swagger2 language annotation, and / or the structure setting information includes generating BaseResulMap structure and BaseColumnList structure;
[0127] Correspondingly, the object construction module 430 may specifically include: a first object construction unit, configured to assign the configured information configuration object to the globalConfig variable in the initial generator object.
[0128] Optionally, the information configuration module 420 may specifically include:
[0129] The second information configuration unit, wherein the second information configuration unit can be specifically used for:
[0130] The data path information, database user name and / or database password are respectively configured into corresponding data connection information variables in the database configuration object, wherein the data path information includes database domain name, database name and / or database port;
[0131] Correspondingly, the object construction module 430 may specifically include: a second object construction unit, configured to assign the configured database configuration object to the dataSource variable in the initial generator object.
[0132] Optionally, the information configuration module 420 may specifically include:
[0133] The third information configuration unit, wherein the third information configuration unit can be specifically used for:
[0134] Configure the package name information to the corresponding package name information variable in the package configuration object, where the package name information is the path information of the code file where the target code is located;
[0135] Correspondingly, the object construction module 430 may specifically include: a third object construction unit, configured to assign the configured package configuration object to the PackageInfo variable in the initial generator object.
[0136] Optionally, the information configuration module 420 may specifically include:
[0137] A fourth information configuration unit, wherein the fourth information configuration unit can be specifically used for:
[0138] Convert the naming rules of code files and database lists from underscores to camel case, and configure the conversion results to the corresponding configuration information variables in the policy object; and / or, configure the class support information to the corresponding configuration information variables in the policy object, where the class support information includes that the entity class does not support the Lombok format; and / or, configure the effective list information to the corresponding configuration information variables in the policy object;
[0139] Correspondingly, the object construction module 430 may specifically include: a fourth object construction unit, configured to assign the configured strategy object to the strategy variable in the initial generator object.
[0140] Optionally, the function calling module 440 may include:
[0141] The object acquisition unit is used to acquire the existing code corresponding to the trigger event, filter out the template information associated with the existing code from each candidate information, and filter out the automatic generator object from each candidate generator object that has been constructed according to the template information.
[0142] Optionally, the code generation function implements the code generation function through a code generation submodule, wherein the code generation submodule can be specifically used for:
[0143] Connect to the database to be accessed according to the dataSource variable in the automatic generator object;
[0144] According to each assignment result in the automatic generator object, the field information obtained from the to-be-accessed list of the to-be-accessed database is used to generate the entity class of the to-be-accessed database;
[0145] Generate target code according to entity class and template information, and output the target code according to the PackageInfo variable in the automatic generator object.
[0146] The code generation device provided by the fourth embodiment of the present invention configures the configuration information in the acquired information configuration object, the data connection information in the database configuration object, the package name information in the package configuration object, and the policy information in the policy object respectively through the cooperation of the information acquisition module, the information configuration module, and the object construction module, and assigns the configuration result and the acquired template information to the initial generator object, thereby obtaining a constructed automatic generator object, which can correspond to the existing code developed by at least one developer; the function call module and the code generation module cooperate with each other, and when a trigger event of code generation is detected, the automatic generator object corresponding to the trigger event can be obtained, and the target code corresponding to the trigger event can be generated by calling the code generation function in the automatic generator object. The above device, based on the automatic generator object corresponding to the target code to be generated, can automatically generate a target code that is relatively similar to the existing code already developed by the developer, thereby achieving the effect of improving the code development efficiency by reducing the development time of redundant code.
[0147] The code generation device provided in the embodiment of the present invention can execute the code generation method provided in any embodiment of the present invention, and has the functional modules and beneficial effects corresponding to the execution method.
[0148] It is worth noting that in the embodiment of the above-mentioned code generation device, the various units and modules included are only divided according to functional logic, but are not limited to the above-mentioned division, as long as the corresponding functions can be realized; in addition, the specific names of the functional units are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of the present invention.
[0149] Embodiment 5
[0150] Figure 5 A schematic diagram of the structure of an electronic device provided in Embodiment 5 of the present invention is shown in FIG. Figure 5 As shown, the electronic device includes a memory 510, a processor 520, an input device 530, and an output device 540. The number of processors 520 in the electronic device can be one or more. Figure 5 A processor 520 is taken as an example; the memory 510, the processor 520, the input device 530 and the output device 540 in the electronic device can be connected via a bus or other means. Figure 5 The connection via bus 550 is taken as an example.
[0151] The memory 510, as a computer-readable storage medium, can be used to store software programs, computer executable programs and modules, such as program instructions / modules corresponding to the code generation method in the embodiment of the present invention (e.g., the information acquisition module 410, the information configuration module 420, the object construction module 430, the function call module 440 and the code generation module 450 in the code generation device). The processor 520 executes various functional applications and data processing of the electronic device by running the software programs, instructions and modules stored in the memory 510, that is, implements the above-mentioned code generation method.
[0152] The memory 510 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created according to the use of the electronic device, etc. In addition, the memory 510 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some instances, the memory 510 may further include a memory remotely arranged relative to the processor 520, and these remote memories may be connected to the device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0153] The input device 530 may be used to receive input digital or character information and generate key signal input related to user settings and function control of the device. The output device 540 may include a display device such as a display screen.
[0154] Embodiment 6
[0155] Embodiment 6 of the present invention provides a storage medium including computer executable instructions, wherein the computer executable instructions are used to execute a code generation method when executed by a computer processor, the method comprising:
[0156] When a trigger event of code generation is detected, the constructed automatic generator object corresponding to the trigger event is obtained, and the code generation function in the automatic generator object is called;
[0157] According to the output result of the code generation function, the target code corresponding to the triggering event is obtained;
[0158] The automatic generator object is pre-built through the following steps:
[0159] Obtaining the initial generator object, information configuration object, database configuration object, package configuration object, strategy object, and template information of the existing code corresponding to the triggering event obtained by the initial construction;
[0160] Configure the configuration information in the information configuration object, the data connection information in the database configuration object, the package name information in the package configuration object, and the policy information in the policy object respectively;
[0161] Assign each configuration result and template information to the initial generator object to obtain an automatic generator object, wherein the code generation function implements the code generation function according to each assignment result in the automatic generator object.
[0162] Of course, the computer executable instructions of a storage medium including computer executable instructions provided by an embodiment of the present invention are not limited to the operations of the method described above, and can also execute related operations in the code generation method provided by any embodiment of the present invention.
[0163] Through the above description of the implementation methods, the technicians in the relevant field can clearly understand that the present invention can be implemented by means of software and necessary general hardware, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as a computer floppy disk, read-only memory (ROM), random access memory (RAM), flash memory (FLASH), hard disk or optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, server, or network device, etc.) to execute the methods described in each embodiment of the present invention.
[0164] Note that the above are only preferred embodiments of the present invention and the technical principles used. Those skilled in the art will understand that the present invention is not limited to the specific embodiments described herein, and that various obvious changes, readjustments and substitutions can be made by those skilled in the art without departing from the scope of protection of the present invention. Therefore, although the present invention has been described in more detail through the above embodiments, the present invention is not limited to the above embodiments, and may include more other equivalent embodiments without departing from the concept of the present invention, and the scope of the present invention is determined by the scope of the appended claims.< / fileoutconfig> < / string>
Claims
1. A code generation method, characterized in that: include: When a trigger event for code generation is detected, an automatically generated object corresponding to the trigger event is obtained, and a code generation function in the automatically generated object is called; wherein the trigger event is an event for automatically generating a target code similar to an existing code; Obtaining a target code corresponding to the trigger event according to an output result of the code generation function; The automatic generator object is pre-built through the following steps: Acquire the initial generator object, information configuration object, database configuration object, package configuration object, strategy object, and template information of the existing code corresponding to the trigger event obtained by the initial construction; respectively configuring the configuration information in the information configuration object, the data connection information in the database configuration object, the package name information in the package configuration object, and the policy information in the policy object; Assigning each configuration result and the template information to the initial generator object to obtain the automatic generator object, wherein the code generation function implements the code generation function according to each assignment result in the automatic generator object; The assigning of each configuration result to the initial generator object comprises: assigning the configured database configuration object to the dataSource variable in the initial generator object; The assigning of each configuration result to the initial generator object comprises: assigning the configured package configuration object to a PackageInfo variable in the initial generator object; The step of obtaining the constructed automatic generator object corresponding to the triggering event includes: Acquire the existing code corresponding to the trigger event, filter out the template information associated with the existing code from each candidate information, and filter out the automatic generator object from each candidate generator object that has been constructed according to the template information; The code generation function implements the code generation function through the following steps: Connecting to the database to be accessed according to the dataSource variable in the automatic generator object; Generate an entity class of the database to be accessed from the field information obtained from the to-be-accessed list of the database to be accessed according to each of the assignment results in the automatic generator object; The target code is generated according to the entity class and the template information, and the target code is output according to the PackageInfo variable in the automatic generator object.
2. The method according to claim 1, characterized in that The configuring the configuration information in the information configuration object includes: configuring the code output path information, the code file creator information and / or the server naming information into the corresponding configuration information variables in the information configuration object respectively; and / or, The class setting information, the language setting information, the language annotation setting information and / or the structure setting information are respectively configured into the corresponding configuration information variables in the information configuration object, wherein the class setting information includes that the code file is an open class, the language setting information includes supporting the generation of the kotlin language, the language annotation setting information includes supporting the Swagger2 language annotation, and / or the structure setting information includes generating a BaseResulMap structure and a BaseColumnList structure; Correspondingly, assigning each configuration result to the initial generator object includes: assigning the configured information configuration object to the globalConfig variable in the initial generator object.
3. The method according to claim 1, characterized in that The configuring the data connection information in the database configuration object includes: The data path information, database user name and / or database password are respectively configured into the corresponding data connection information variables in the database configuration object, wherein the data path information includes the database domain name, database name and / or database port.
4. The method according to claim 1, characterized in that: The configuring the package name information in the package configuration object includes: The package name information is configured into a corresponding package name information variable in the package configuration object, wherein the package name information is the path information of the code file where the target code is located.
5. The method according to claim 1, characterized in that The configuring the policy information in the policy object includes: Convert the naming rules of code files and database lists from underscores to camel case, and configure the conversion results to the corresponding configuration information variables in the strategy object; and / or, Configuring class support information into corresponding configuration information variables in the strategy object, wherein the class support information includes that the entity class does not support the Lombok format; and / or, Configuring the effective list information into the corresponding configuration information variable in the policy object; Correspondingly, assigning each configuration result to the initial generator object includes: assigning the configured strategy object to the strategy variable in the initial generator object.
6. A code generating device, characterized in that: include: A function calling module is used to obtain the constructed automatic generator object corresponding to the triggering event and call the code generation function in the automatic generator object when a triggering event of code generation is detected; wherein the triggering event is an event for automatically generating a target code similar to an existing code; A code generation module, used for obtaining a target code corresponding to the trigger event according to an output result of the code generation function; The automatic generator object is pre-built through the following modules: An information acquisition module, used to acquire the initial generator object, information configuration object, database configuration object, package configuration object, strategy object, and template information of the existing code corresponding to the trigger event obtained by the initial construction; An information configuration module, used to configure the configuration information in the information configuration object, the data connection information in the database configuration object, the package name information in the package configuration object, and the policy information in the policy object respectively; An object construction module, used for assigning each configuration result and the template information to the initial generator object to obtain the automatic generator object, wherein the code generation function implements the code generation function according to each assignment result in the automatic generator object; The object construction module specifically includes: a second object construction unit, which is used to assign the configured database configuration object to the dataSource variable in the initial generator object; The object construction module specifically includes: a third object construction unit, which is used to assign the configured package configuration object to the PackageInfo variable in the initial generator object; Function call module, including: An object acquisition unit is used to acquire the existing code corresponding to the trigger event, filter out the template information associated with the existing code from each candidate information, and filter out the automatic generator object from each candidate generator object that has been constructed according to the template information; The code generation function implements the code generation function through a code generation submodule, wherein the code generation submodule is specifically used for: Connect to the database to be accessed according to the dataSource variable in the automatic generator object; According to each assignment result in the automatic generator object, the field information obtained from the to-be-accessed list of the to-be-accessed database is used to generate the entity class of the to-be-accessed database; Generate target code according to entity class and template information, and output the target code according to the PackageInfo variable in the automatic generator object.
7. An electronic device, characterized in that: include: one or more processors; A memory for storing one or more programs; When the one or more programs are executed by the one or more processors, the one or more processors implement the code generation method according to any one of claims 1 to 5.
8. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the code generation method according to any one of claims 1 to 5 is implemented.