Code generation method supporting multiple generation

Through hierarchical isolation of code templates and the design of the generation layer and extension layer, the problem of frequent code generation and merging in software customization development is solved, development efficiency and quality are improved, and multiple code generation and merging are supported.

CN119311256AInactive Publication Date: 2025-01-14CHINACCS INFORMATION IND

Patent Information

Application Number
CN202411359391.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-27
Publication Date
2025-01-14
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

In software customization development, due to the changing customer needs and possible errors in data model design, the basic code needs to be frequently regenerated, and the generated code is mixed with the secondary development code, resulting in large workloads in code merging, which is prone to errors and omissions. , affects development efficiency and quality.

Method used

By layering and isolating the code template, it is divided into a generation layer and an extension layer. The generation layer contains generateable template classes, and the extension layer is used for secondary development to ensure that the generation layer code is stable and unmodifiable, thereby supporting multiple code generation and simplifying Code merging workload.

Benefits of technology

It greatly reduces the merging workload when code is repeatedly generated, reduces the error rate of merging work, improves development efficiency and quality, and supports repeated generation and merging of code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119311256A_ABST
    Figure CN119311256A_ABST
Patent Text Reader

Abstract

The invention discloses a code generation method supporting multiple generation, which comprises the following steps of: making a code template, and adding a control flow mark and a variable injection mark in a template file; a code file in the template is subjected to hierarchical isolation and divided into a generation layer and an extension layer, the generation layer comprises template classes capable of generating codes, and the extension layer comprises classes inheriting or combining the generation layer; defining required business logic and a data model; reading the template and the definition by using a code generator, and generating codes which comprise a generation layer code and an extension layer code; and setting the generation layer code file to be read-only. The method has the beneficial effects that through the hierarchical isolation design of the template, the generation layer containing the unified service logic generated by the template cannot be modified in the secondary development process, the code quantity of the expansion layer which can be modified in the template is extremely small, and only the inheriting and combining processes of generation layer classes are very simple.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code generation, and in particular to a method for supporting multiple code generation. Background Art

[0002] At present, when customizing and developing software, low-code tools, code generators and other technical means are usually used. By defining business logic and data models, basic codes can be directly generated, which can cover most of the business logic and greatly improve delivery efficiency. However, due to the personalization of customer customization needs, low-code tools or code generators often cannot fully meet their needs. At this time, programmers are still needed to carry out secondary development to implement the parts of the basic code that cannot fully meet the needs. The more complex the system business and the higher the degree of customization, the greater the proportion of secondary development code will be, and a problem will gradually emerge, seriously affecting the efficiency and quality of development. The problem is as follows: Due to the variability of customer needs, it is usually impossible to fully cope with future changes in demand through a one-time definition of business logic. In addition, due to the design capabilities of designers, errors or design changes in data models and structures are inevitable. Therefore, there is a situation where the basic code needs to be regenerated or even repeatedly. The code developed by programmers for secondary development is usually mixed with the generated basic code. The newly generated basic code cannot directly cover the original basic code, and programmers need to perform a lot of code comparison and merging work. In this link, errors and omissions are easy to occur, which affects development efficiency and quality. Summary of the invention

[0003] In order to reduce the merging workload when codes are repeatedly produced, the present invention provides a method that can support multiple code generation and designs a code template to cleverly separate the generated basic code from the secondary development code, thereby greatly reducing the merging workload when codes are repeatedly generated.

[0004] In order to achieve the above-mentioned invention purpose, the present invention provides a method that can support multiple code generation, the core idea of ​​which is: by layering and isolating the unified business logic generated by the template and the personalized logic extended by secondary development at the class level, the unified business logic generated by the template is ensured to be stable and unmodifiable, so that it can be generated repeatedly, greatly simplifying the workload of code merging and reducing the error rate of merging work. This method is suitable for code generation in object-oriented computer languages, and specifically includes the following steps: Step S1: Create a code template, add control flow markers and variable injection markers to the template file, so that code generation can be performed based on the template, business logic and data model; Step S2: The code files in the template are layered and isolated into a generation layer and an extension layer, wherein the generation layer contains template classes that can generate codes, and the extension layer contains classes that inherit or combine the generation layer to achieve secondary development; Step S3: define the required business logic and data model; Step S4: using a code generator to read the template and definition and generate code, including generation layer code and extension layer code; Step S5: Set the generated layer code file to read-only, prohibit the modification of the generated layer code during the secondary development process, and only allow code extension in the extension layer.

[0005] In the generation layer, the design pattern of the template method is used to reserve the uncertain business logic for the extension layer to implement through abstract methods.

[0006] The extension layer realizes the secondary development function by realizing the abstract method reserved in the generation layer, or by overloading, rewriting and extending the method in the generation layer.

[0007] When there are defects in the generated layer code, the generated layer code is regenerated by modifying the template.

[0008] When business requirements change or design changes occur, the method further includes the following steps: W1: Redefine business logic and data model based on existing requirements and / or design; W2: Use the code generator to regenerate the code including the generation layer and the extension layer; W3: Covers the generated layer code to ensure the latest generation layer code; W4: Merge the extension layer code based on the merge function of the version management tool.

[0009] When a template version is upgraded or a bug is fixed, the following steps are also included: V1: Upgrade the template and remake it; V2: Use the code generator to regenerate the code including the generation layer and the extension layer; V3: Overwrite the generated layer code to apply the new template modifications; V4: Merge the extension layer code based on the version management tool to ensure that the extension logic of secondary development is retained.

[0010] The code generation in step S4 is to select different generation modes based on specific requirements to achieve code generation; the generation modes include full generation mode, incremental generation mode, layer generation only mode and fast clearing mode.

[0011] The full generation mode is used for the initial generation of code or the full generation after the template is upgraded; The incremental generation mode is used to generate related classes separately when new additions or changes occur in the data model; The generation layer only mode is used to output the generation layer code separately when the business logic definition changes; The quick clearing mode is used to delete incorrectly generated code files in batches.

[0012] Among them, the generation layer and the extension layer are distinguished by specific package names or namespaces, the generation layer uses the "gen" package name or namespace, and the extension layer uses the "ext" package name or namespace; the code template can be configured through the user interface provided by the code generation tool, allowing the user to define or modify the generation mode as needed.

[0013] The beneficial effects of the present invention are as follows: the present invention uses a hierarchical isolation design of the template so that the generation layer containing the unified business logic generated by the template will not be modified during the secondary development process, but the extension layer that will be modified has very little code in the template, which is only the inheritance and combination of the generation layer class, and the merging process is very simple. Compared with the original method, it can greatly improve efficiency and quality, thereby better supporting repeated generation and merging of code; in addition, through the design of multiple generation modes such as full generation, incremental generation, generation layer only, and fast clearing, the flexibility of generated code and the efficiency of code merging are further improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0014] Figure 1 This is a flow chart of code generation in Examples 1 and 2 of the present invention.

[0015] Figure 2 This is a flowchart of the changes defined in Examples 1 and 2 of the present invention.

[0016] Figure 3 This is a flow chart of template change in Examples 1 and 2 of the present invention. DETAILED DESCRIPTION

[0017] In order to clearly illustrate the technical features of this solution, this solution is described below through a specific implementation method.

[0018] Example 1 See also Figure 1-3 , an embodiment of the present invention provides a method that can support multiple code generation, comprising the following steps: Step S1: Create a code template, add control flow markers such as branches and loops and variable injection markers in the class template file, so that code generation can be performed based on the template, business logic and data model; Step S2: The code files in the template are layered and isolated into a generation layer and an extension layer, wherein the generation layer contains template classes that can generate codes, and the extension layer contains classes that inherit or combine the generation layer to achieve secondary development; Step S3: define the required business logic and data model; Step S4: using a code generator to read the template and definition and generate code, including generation layer code and extension layer code; Step S5: Set the generated layer code file to read-only, prohibit modification of the generated layer code during secondary development, and only allow code extension in the extension layer. If there are defects in the generated layer code, the template should be modified and the layer should be regenerated.

[0019] The generation layer contains template classes that can define production codes based on business logic and data models. They are used to generate basic functional codes and use the design pattern of template methods in these classes. Business logic that cannot be determined in the production phase is reserved for subclass implementation through abstract methods. The generation layer code can be distinguished by specific inclusion or namespaces, such as "gen".

[0020] In the extension layer, based on the object-oriented computer language characteristics, the abstract methods reserved for the production layer classes are implemented; on the one hand, when the default function code of the production layer cannot meet the needs, the secondary development functions can be implemented in the extension layer through overloading methods, overriding methods or extending methods. The extension layer can be distinguished by specific includes or namespaces, such as "ext".

[0021] When there are defects in the generated layer code, the generated layer code is regenerated by modifying the template.

[0022] When business requirements change or design changes occur, the method also includes the following steps: W1: Redefine business logic and data model based on existing requirements and / or design; W2: Use the supporting low-code tool or code generator to read the module and definition, and regenerate the code including the generation layer and the extension layer; W3: Covers the generated layer code to ensure the latest generation layer code; W4: Merge the extension layer code based on the merge function of the version management tool. In the template, the extension layer code actually only contains the inheritance or combination of the generation layer class, the amount of code is very small, and the inheritance or combination relationship is unlikely to be changed during secondary development, so the merging process is easy to complete. When a template version is upgraded or a bug is fixed, the following steps are also included: V1: Upgrade functions, fix defects, and remake; V2: Use the supporting low-code tool or code generator to read the template and definition, and regenerate the code including the generation layer and the extension layer; V3: Override the generated layer code to directly apply the new template; V4: Merge the extension layer code based on the version management tool to ensure that the extension logic of secondary development is retained; because in the template, the extension layer code actually only contains the inheritance or combination of the generation layer class, the amount of code is very small, and during secondary development, the inheritance or combination relationship is unlikely to be changed, so the merging process is easy to complete.

[0023] The code generation in step S4 is to select different generation modes based on specific requirements to achieve code generation; the generation modes include full generation mode, incremental generation mode, layer generation only mode and fast clearing mode.

[0024] Full generation mode: All template files are defined in the meta template, which can generate the most complete code at one time, including the generation layer and the extension layer. It is used for the initial generation of code or the full generation after the template is upgraded. Incremental generation mode, in which template files related to the data model are defined in the meta-template, and are used to generate related classes separately when the data model is added or changed; Only the generated layer mode defines only the template file of the generated layer in the meta template. It is used to output the generated layer code separately when the business logic definition changes, so as to directly overwrite the original code. Quick clear mode, create bat and sh script templates for batch deletion of files, and define these deletion script templates in the meta template to batch delete the incorrectly generated code files.

[0025] Among them, the generation layer and the extension layer are distinguished by specific package names or namespaces, the generation layer uses the "gen" package name or namespace, and the extension layer uses the "ext" package name or namespace; the code template can be configured through the user interface provided by the code generation tool, allowing users to define or modify the generation mode as needed.

[0026] Example 2 In order to make the ideas, technical solutions and advantages of the present invention more clear, this embodiment takes the template making and generation process of Java code as an example, and refers to the attached Figure 1-3 , the present invention is further described in detail.

[0027] Suppose you need to create a set of MVC architecture templates based on Java. Through the definition of the data model, you can generate the commonly used add, delete, modify and query interfaces for a single table, and perform routine data security processing to achieve basic code generation for a simple system.

[0028] For this scenario, SpringBoot can be used as the basic framework, MybatisFlex as the ORM framework, and the MybatisFlex listener can be used to implement data security processing. The following is implemented through four stages: template creation and hierarchical isolation, data model definition, code generation, and secondary development: U1, template making and layered isolation: Under the MVC architecture, controllers are usually used for interface processing, services for business logic processing, mappers for database operations, entities are mapped one by one to database tables, QO objects are passed as query parameters, VO objects are passed as query return objects, and listeners are used to expand the database reading and writing functions of entities.

[0029] Therefore, in the generation layer, create a template class BaseTemplateController, inject the TemplateService class of the extension layer, provide default interface templates such as adding, batch adding, modifying, batch modifying, deleting, batch deleting, counting, querying, and paging querying, and reserve abstract methods for subclasses, including adding verification, modifying verification, deleting verification, etc.; create a template BaseTemplateService, inject the TemplateMapper class of the extension layer, provide default method templates such as adding, batch adding, modifying, batch modifying, deleting, batch deleting, counting, querying, and paging querying; create a template BaseTemplateMapper, inherit the BaseMapper of the MybatisFlex framework <templateentity>, with built-in default database operations such as add, delete, modify, and query; create a template BaseTemplateEntity, which can generate primary key attributes, foreign key attributes and relationships, common attributes, deletion marks, creation time, creator and other specific attributes according to the data model definition; create a template BaseTemplateListener, which provides default listening methods for entity object insertion, modification, deletion and entity attribute setting, and handles data security-related storage encryption, reading and decryption and other conventional logic here. As well as supporting exception classes, global exception handling classes, tool classes, constants, etc., which are not listed in detail here. At the same time, set the package name of the generation layer to gen for distinction.

[0030] In the extension layer, create a template class TemplateController, inherit the BaseTemplateController class, and provide a default implementation of the abstract method; create a template TemplateService, inherit the BaseTemplateService class; create a template TemplateMapper, inherit the BaseTemplateMapper; create a template TemplateListener, inherit the BaseTemplateListener; create a template TemplateEntity, inherit the BaseTemplateEntity, and set the listener to TemplateListener; create a template TemplateQO, inherit the TemplateEntity; create a template TemplateVO, inherit the TemplateEntity, and combine another entity class associated with the foreign key. At the same time, set the package name of the extension layer to ext for distinction.

[0031] U2, data model definition: design and define the data model, including entities and corresponding tables, attributes and corresponding fields, attribute data structure, primary key, foreign key, description, etc.

[0032] For example, you can define a user entity model User, corresponding to the database table name sys_user, database scheme name, including fields such as primary key userId, login name loginName, password password, nickname nickName, foreign key department deptId, status status, creator createBy, creation time createTime, etc.

[0033] And so on for other data models.

[0034] U3, generate code: develop a matching code generator, read and parse the U1 template, traverse the data model defined by U2, and generate the corresponding code.

[0035] For example, for the above user entity model User, a series of related classes can be generated according to the template, including BaseUserController, BaseUserService, BaseUserMapper, BaseUserListener, BaseUserEntity of the generation layer and UserController, UserService, UserMapper, UserListener, UserEntity, UserQO, UserVO, etc. of the extension layer. And so on.

[0036] U4, secondary development: According to the agreement, secondary development is only carried out on the extension layer, and the generated layer code content is not changed.

[0037] For example, for the above user entity model User, there is a specific rule that user nicknames cannot be repeated, then you can overload the newly added and modified verification methods in UserController and add the corresponding judgment logic; for another example, if you need to add a new login interface, you can add the corresponding interface method in UserController; for another example, in terms of data security, there are specific requirements for the encryption method and algorithm of the password, and the general data security processing cannot meet them, then you can overload the entity object insertion, modification, deletion and entity attribute setting listening methods in UserListener. And so on.

[0038] To sum up, most of the business logic generated by the code generator will be concentrated in the generation layer codes such as BaseUserController, BaseUserService, BaseUserMapper, BaseUserListener, and BaseUserEntity; and all the logic of the programmer's secondary development is in the extension layer codes such as UserController, UserService, UserMapper, UserListener, UserEntity, UserQO, and UserVO. When there are changes in requirements, changes in data models, template upgrades, etc., you can safely repeat the code generation, overwrite and replace the files under the generation layer gen package, and merge the files under the extension layer ext package. The whole process is very efficient and is not prone to errors caused by merging.

[0039] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.< / templateentity>

Claims

1. A code generation method supporting multiple generation, suitable for code generation of object-oriented computer languages, characterized in that: The following steps are involved: Step S1: Create a code template and add control flow tags and variable injection tags to the template file; Step S2: Separate the code files in the template into layers, and divide them into a generation layer and an extension layer, wherein the generation layer contains template classes that can generate codes, and the extension layer contains classes that inherit or combine the generation layer; Step S3: define the required business logic and data model; Step S4: using a code generator to read the template and definition and generate code, including generation layer code and extension layer code; Step S5: Set the generated layer code file to read-only.

2. The code generation method according to claim 1, characterized in that: In the generation layer, the design pattern of the template method is used to reserve the uncertain business logic for the extension layer to implement through abstract methods.

3. The code generation method according to claim 2, characterized in that: The extension layer realizes the secondary development function by realizing the abstract method reserved in the generation layer, or by overloading, rewriting and extending the method in the generation layer.

4. The code generation method according to claim 1, characterized in that: When there are defects in the generated layer code, the generated layer code is regenerated by modifying the template.

5. The code generation method according to claim 1, when a business requirement change or a design change occurs, the method further comprises the following steps: W1: Redefine business logic and data model based on existing requirements and / or design; W2: Use the code generator to regenerate the code including the generation layer and the extension layer; W3: Covers the generated layer code to ensure the latest generation layer code; W4: Merge the extension layer code based on the merge function of the version management tool.

6. The code generation method according to claim 1, characterized in that: When a template version is upgraded or a bug is fixed, the following steps are also included: V1: Upgrade the template and remake it; V2: Use the code generator to regenerate the code including the generation layer and the extension layer; V3: Overwrite the generated layer code to apply the new template modifications; V4: Merge the extension layer code based on the version management tool to ensure that the extension logic of secondary development is retained.

7. The code generation method according to claim 1, characterized in that: The code generation in step S4 is to select different generation modes based on specific needs to achieve code generation; The generation modes include a full generation mode, an incremental generation mode, a layer generation only mode, and a fast clearing mode.

8. The code generation method according to claim 7, characterized in that: The full generation mode is used for the initial generation of code or the full generation after the template is upgraded; The incremental generation mode is used to generate related classes separately when new additions or changes occur in the data model; The generation layer only mode is used to output the generation layer code separately when the business logic definition changes; The quick clearing mode is used to delete incorrectly generated code files in batches.

9. The code generation method according to claim 1, characterized in that: The generation layer and the extension layer are distinguished by using specific package names or namespaces respectively.

10. The method according to claim 1, characterized in that The code template can be configured through a user interface provided by the code generation tool, allowing the user to define or modify the generation mode as needed.

Citation Information

Patent Citations

  • Webpage template generation method and device

    CN107247587A

  • Automatic code generation method based on java language

    CN110825369A

  • Restful service development method and device based on SSM framework

    CN112748908A

  • Web system back-end architecture design method and computer equipment

    CN114385145A

  • Java universal code generation optimization method and system

    CN116991390A

Cited By

  • Low-code management method, device and equipment supporting secondary development and repeated generation

    CN121879732A

  • A low-code management method, apparatus, and device that supports secondary development and iterative generation.

    CN121879732B