Code template generation method and system based on annotation markup language

Through the code template generation method based on annotation marking language, problems such as strong code intrusion and poor readability in the existing technology are solved, and the customization and unification of code templates are realized, development efficiency and quality are improved, and it is suitable for a variety of computer languages.

CN120045187AActive Publication Date: 2025-05-27CHINACCS INFORMATION IND
View PDF 10 Cites 0 Cited by

Patent Information

Application Number
CN202510088050.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-01-21
Publication Date
2025-05-27
Estimated Expiration
2045-01-21

AI Technical Summary

Technical Problem

The existing code template generation methods have problems such as strong code intrusion, poor readability, templates that cannot be compiled, run and cannot be tested, and the application of technologies such as OGNL is small.

Method used

Using a code template generation method based on the annotation markup language, code that meets R&D standards is generated by creating project framework templates, meta-object construction, adding code generation rules, parsing and executing annotation markup syntax trees.

Benefits of technology

It improves the efficiency and quality of code development, realizes the customization and unity of code templates, supports multiple computer languages, has a wide range of applications, and reduces R&D costs and complexity.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120045187A_ABST
    Figure CN120045187A_ABST
Patent Text Reader

Abstract

The invention provides a code templated generation method and system based on an annotation markup language, and relates to the technical field of code development, the generation method comprises the following steps: creating a project framework template; constructing a meta object; reading a global template file; analyzing annotation marks in the global template file to form a global template syntax tree; executing the global template syntax tree, and traversing the code template file; reading a code template file; analyzing annotation marks in the code template file to form a code template syntax tree; executing the code template syntax tree to generate a code; creating a code file and writing codes; the generation system comprises a meta-object, an annotation markup language, a frame template specification, a meta-object controller, a generator, a dialect controller, a file reader, an annotation markup language parser and an annotation markup language actuator. The project framework template has the advantages that the project framework template is provided, research and development standards are unified, code writing efficiency and quality are improved, and the project framework template is suitable for various computer languages.
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 development, and in particular to a code templatized generation system and method based on an annotation markup language. Background Art

[0002] At present, information technologies at home and abroad are developing rapidly, with various technologies emerging in an endless stream. While giving developers more choices, it also brings troubles to software companies. Because in a software company, there are often multiple R & D teams. Even if the company formulates R & D standards, due to the difficulty of supervision, each R & D team may vary in the implementation process, and it is difficult to unify the R & D standards. This easily leads to problems such as diverse technology selections, sensitivity to personnel flow, difficult implementation of R & D standards, poor component reusability, difficult adaptation of internal systems, high R & D costs, etc. Therefore, a code templatized generation method has emerged.

[0003] Mainstream code templatized generation methods usually use technologies such as OGNL (Object Graph Navigation Language) expressions and EL (Expression Language) expressions. These technologies can dynamically inject control flows such as branches and loops, and values of variable attributes into templates, thereby achieving template-based generation. However, when using technologies such as OGNL and EL for template development, there are problems such as strong code intrusion, poor readability, non-compilability, non-runability, and non-testability of the templates themselves. Only the generated code can be compiled, run, and tested, which is very inconvenient. In addition, technologies such as OGNL can usually only be used in specific languages, with a narrow application range.

[0004] How to solve the above technical problems is the subject faced by the present invention. Summary of the Invention

[0005] In order to solve the deficiencies of the prior art, on the one hand, the present invention provides a code templatized generation method that improves code development efficiency and quality; on the other hand, the present invention provides a code templatized generation system with good openness, strong accuracy, and applicable to multiple computer languages.

[0006] The technical solution adopted by the present invention to solve its technical problems is:

[0007] On the one hand, the present invention discloses a code templatized generation method based on an annotation markup language, including

[0008] S1: Create a project framework template: Write an initial set of project framework templates, define their directory structures, and according to the standard development process, write code template files for various entity models. Use global template files to define the code template files to be generated, and conduct a certain degree of unit testing to verify the accuracy of the templates;

[0009] S2: Meta-object construction: Design a data model according to the actual project requirements, define each parameter information, and use a meta-object controller to construct meta-objects such as template objects, project objects, entity objects, attribute objects, and foreign key objects from large to small;

[0010] S3: Add code generation rules: According to the parameter information defined by the meta-objects, use annotation markup language to add code generation rules to the global template files and code template files in the project framework template;

[0011] S4: Read the global template file: According to the framework template specifications, find the global template file in the framework template and use a file reader to read the file;

[0012] S5: Parse the annotation marks in the global template file to form a global template syntax tree: Use an annotation mark parser to parse the annotation marks in the global template file line by line to form an executable global template syntax tree;

[0013] S6: Execute the global template syntax tree and traverse the code template files: Use an annotation mark executor to traverse and execute the global template syntax tree. The actual effect is to traverse the code template files to be generated, and at the same time call the specified generator to generate code in the subsequent steps S7~S10;

[0014] S7: Read the code template file: For each code template file to be generated, use a file reader to read it;

[0015] S8: Parse the annotation marks in the code template file to form a code template syntax tree: Use an annotation mark parser to parse the code template file line by line to form an executable code template syntax tree;

[0016] S9: Execute the code template syntax tree to generate code: Use an annotation mark executor to traverse and execute the code template syntax tree. The actual effect is to generate code according to the rules described by the annotation marks;

[0017] S10: Create a code file and write the code: Calculate the actual path and file name of the code file according to the rules described by the global template syntax tree, create the code file, and at the same time, write the code generated in step S9 into the code file. Finally, return to step S7 to continue processing the next code template file until the global template syntax tree traversal is completed.

[0018] Preferably, the meta-objects in the step S2 include:

[0019] A template object, which is used to describe the template information used when generating a project framework;

[0020] A project object, which is used to describe the configuration parameters at the framework level, such as paths, version numbers, names, the scope of dependent components, etc.; An entity object, which is used to describe the configuration parameters at the entity level, such as class names, table names, entity types, etc.;

[0021] An attribute object, which is used to describe the configuration parameters at the attribute level, such as attribute names, field names, field formats, value ranges, query methods, etc.;

[0022] A foreign key object, which is used to describe the foreign key relationship of an entity key.

[0023] Preferably, the annotation markup language in the step S1 is used to describe the specific processing logic to be performed on the code of the global template file and the code template file when generating code, and specifically includes the following annotation marks:

[0024] M1: inject, an injection mark;

[0025] M2: block, a code block mark;

[0026] M3: include, endinclude, a dependency inclusion mark;

[0027] M4: loop, endloop, a loop mark;

[0028] M5: drop, a discard mark;

[0029] M6: generator, a generator mark;

[0030] M7: flag, endflag, a matching mark.

[0031] Preferably, the injection mark M1 has two forms:

[0032] One is static injection, and the analytical formula is inject('template_string', 'new_string'), and its function is to replace the template_string string in the marked code with the new_string string;

[0033] One is dynamic injection, and the analytical formula is inject('template_string', object.attribute), and its function is to replace the template_string string in the marked code with a certain attribute value attribute of a certain meta-object object;

[0034] The analytical formula of the M2 code block marker is block(object.codeBlock), and its function is to inject the codeBlock value of a certain meta-object as the entire code block before the next line of code, for adding code with specific functions;

[0035] The M3 dependency inclusion markers, include and endinclude, appear in pairs, and their analytical formulas are include('A,B,C,...') and endinclude() respectively. The function is that when one of A, B, C... is included in the dependent components of the project framework to be generated, the code between include and endinclude will be generated, otherwise it will be ignored;

[0036] The M4 loop markers, loop and endloop, appear in pairs, and their analytical formulas are loop(object) and endloop() respectively. The function is to traverse a certain meta-object object, and generate the code between loop and endloop during traversal. The meta-objects that can be traversed include entity objects, attribute objects, and foreign key objects;

[0037] The analytical formula of the M5 discard marker is drop(), and its function is to discard the next line of code, which is used to describe the auxiliary code segments added to make the logic of the template complete in the template and does not participate in the final code generation;

[0038] The analytical formula of the M6 generator marker is generator('G'), and its function is to use a specific generator G to generate code, which is used for the generation of non-text format files or the generation of certain special codes;

[0039] The M7 matching markers, flag and endflag, appear in pairs, and their analytical formulas are flag('X,Y,Z,...',object.attribute) and endflag() respectively. The function is that when a certain attribute value attribute of a certain meta-object object is among X, Y, Z..., the code between flag and endflag will be generated, otherwise it will be ignored.

[0040] Preferably, the comment marker is written in the comment before the code it describes, and each comment marker occupies one line, and the comment markers can be nested arbitrarily.

[0041] Preferably, the comment marker uses " / / " as the comment symbol in the global template file, and in the code template file, the corresponding comment symbol is selected according to the specific computer language used.

[0042] Preferably, the global template file in step S1 specifies the code template files participating in code generation, and its format is as follows: The code content is the path and file name of each code template file in the project framework template, and each line of code describes a code template file. Define the code template files to be generated in the global template file as needed. Only the code template files listed in the global template file will be generated, which improves the openness of the basic framework template and realizes the customization of templates.

[0043] Preferably, the global template file has the suffix of ".template". By using the suffix of ".template", the global template file is distinguished from other files.

[0044] On the other hand, the present invention discloses a code templating generation system based on an annotation markup language, including a W1 meta-object controller for controlling the construction of meta-objects and reading the attributes of each meta-object;

[0045] A W2 generator for generating files in various formats such as code, configuration, pictures, and audio according to different template file types and the descriptions of annotation marks;

[0046] A W3 dialect controller for performing dialect-style control over the syntax differences of various databases that may be used;

[0047] A W4 file reader for reading the content of the template file into the cache;

[0048] A W5 annotation mark parser for parsing the annotation marks line by line from the cache to form an executable syntax tree;

[0049] A W6 annotation mark executor for traversing the syntax tree and performing the processing operations corresponding to the annotation marks, including character injection, code block injection, dependency inclusion, loop, discard, match, calling a certain generator to generate code, outputting files or code, etc.

[0050] The beneficial effects of the present invention are as follows: When a software company customizes a project framework template based on R & D standards, the generated code inherently conforms to the R & D standards, which not only supports the implementation of R & D standards from a technical level but also greatly improves the efficiency of code writing. At the same time, the code templates provided by the present invention can be written in any high-level computer language, with a wide range of applications. Multiple templates in different development languages and different technology stacks can be customized to support diverse requirements in projects. Only the template files listed in the global template file will be generated. According to actual needs, the templates can be flexibly customized to define a unified code structure and format, implement unified data security and system security standards, compile default add, delete, modify, and query interfaces, define a unified interface return format, provide reusable components and tools, quickly implement project configuration, avoid duplicate coding, improve R & D efficiency, and standardize data model design. In the code comments, a comment markup language is used to describe control flows such as branches and loops and the generation marks for variable attribute injection. Through a file reader, a comment markup parser, and a comment markup executor, the reading and execution of comment marks are realized, invading the comments rather than the code. The introduction of the comment markup language does not affect the inherent logic of the code template at all, enabling the code template itself to be written, compiled, run, and even tested according to normal syntax. Compared with technologies such as OGNL and EL, the development of code templates will be more efficient. The comment marks can be nested with each other, improving the readability of the overall logic. BRIEF DESCRIPTION OF THE DRAWINGS

[0051] Figure 1 FIG. is an example diagram of the comment marks of the present invention in different computer languages.

[0052] Figure 2 FIG. is a flowchart of code generation of the present invention.

[0053] Figure 3 FIG. is a system structure diagram of code template generation of the present invention.

[0054] Figure 4 FIG. is a schematic diagram of a code template of an embodiment of the present invention.

[0055] Figure 5 FIG. is a schematic diagram of the final generated project framework of an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0056] In order to clearly illustrate the technical features of this solution, the following will elaborate on this solution through specific embodiments.

[0057] Refer to Figures 1-5As shown in the figure, this embodiment is a method for generating code templates based on an annotation markup language, including the following steps: S1: Create a project framework template: Write an initial set of project framework templates, define their directory structures, and write code template files for various entity models according to the standard development process. Use a global template file to define the code template files to be generated, and conduct a certain degree of unit testing to verify the accuracy of the templates;

[0058] S2: Meta-object construction: Design a data model according to the actual requirements of the project, define each parameter information, and use a meta-object controller to construct meta-objects such as template objects, project objects, entity objects, attribute objects, and foreign key objects from large to small;

[0059] S3: Add code generation rules: According to the parameter information defined by the meta-objects, use the annotation markup language to add code generation rules to the global template file and code template files in the project framework template;

[0060] S4: Read the global template file: According to the framework template specification, find the global template file in the framework template, and use a file reader to read the file;

[0061] S5: Parse the annotation marks in the global template file to form a global template syntax tree: Use an annotation mark parser to parse the annotation marks in the global template file line by line to form an executable global template syntax tree;

[0062] S6: Execute the global template syntax tree and traverse the code template files: Use an annotation mark executor to traverse and execute the global template syntax tree. The actual effect is to traverse the code template files to be generated, and at the same time call the specified generator to generate code in the subsequent steps S7 to S10;

[0063] S7: Read the code template files: For each code template file to be generated, use a file reader to read;

[0064] S8: Parse the annotation marks in the code template files to form a code template syntax tree: Use an annotation mark parser to parse the code template files line by line to form an executable code template syntax tree;

[0065] S9: Execute the code template syntax tree to generate code: Use an annotation mark executor to traverse and execute the code template syntax tree. The actual effect is to generate code according to the rules described by the annotation marks;

[0066] S10: Create a code file and write code: Calculate the actual path and file name of the code file according to the rules described by the global template syntax tree, create the code file, and at the same time, write the code generated in step S9 into the code file; finally, return to step S7 to continue processing the next code template file until the global template syntax tree traversal is completed.

[0067] Meta-objects include: template objects, which are used to describe the template information used when generating the project framework; project objects, which are used to describe framework-level configuration parameters, such as paths, version numbers, names, scopes of dependent components, etc.; entity objects, which are used to describe entity-level configuration parameters, such as class names, table names, entity types, etc.; attribute objects, which are used to describe attribute-level configuration parameters, such as attribute names, field names, field formats, value ranges, query methods, etc.; foreign key objects, which are used to describe the foreign key relationships of entity keys.

[0068] The annotation markup language in step S1 is used to describe the specific processing logic to be performed on the code of the global template file and the code template file when generating code, and specifically includes the following annotation marks: M1: inject, injection mark; M2: block, code block mark; M3: include, endinclude, dependency inclusion mark; M4: loop, endloop, loop mark; M5: drop, discard mark; M6: generator, generator mark; M7: flag, endflag, matching mark.

[0069] Among them, the M1 injection mark has two forms: one is static injection, parsing inject('template_string', 'new_string'), the function of which is to replace the template_string string in the marked code with the new_string string;

[0070] One is dynamic injection, and the parsing formula is inject('template_string', object.attribute), the function of which is to replace the template_string string in the marked code with a certain attribute value attribute of a certain meta-object object;

[0071] The parsing formula of the M2 code block mark is block(object.codeBlock), and its function is to inject the codeBlock value of a certain meta-object as the entire code block before the next line of code, for adding code with specific functions;

[0072] M3 depends on tags. The tags "include" and "endinclude" appear in pairs. Their parsing formulas are include('A,B,C,...') and endinclude() respectively. The function is that when one of A, B, C... is included in the dependent components of the project framework to be generated, the code between "include" and "endinclude" will be generated; otherwise, it will be ignored.

[0073] M4 loop tags. The tags "loop" and "endloop" appear in pairs. Their parsing formulas are loop(object) and endloop() respectively. The function is to traverse a certain meta-object object, and generate the code between "loop" and "endloop" during traversal. The traversable meta-objects include entity objects, attribute objects, and foreign key objects.

[0074] M5 discard tag. The parsing formula is drop(). The function is to discard the next line of code, which is used to describe the auxiliary code segments added to make the logic of the template complete in the template and does not participate in the final code generation.

[0075] M6 generator tag. The parsing formula is generator('G'). The function is to use a specific generator G to generate code, which is used for the generation of non-text format files or the generation of certain special codes.

[0076] M7 matching tags. The tags "flag" and "endflag" appear in pairs. Their parsing formulas are flag('X,Y,Z,...',object.attribute) and endflag() respectively. The function is that when a certain attribute value attribute of a certain meta-object object is among X, Y, Z..., the code between "flag" and "endflag" will be generated; otherwise, it will be ignored.

[0077] The comment tag is written in the comment before the code it describes. Each comment tag occupies one line, and the comment tags can be nested arbitrarily.

[0078] In the global template file, the comment tag uses " / / " as the comment symbol. In the code template file, the corresponding comment symbol is selected according to the specific computer language used.

[0079] The global template file in step S1 specifies the code template files participating in code generation. Its format is as follows: The code content is the path and file name of each code template file in the project framework template, and each line of code describes a code template file. Define the code template files to be generated in the global template file as needed. Only the code template files listed in the global template file will be generated, which improves the openness of the basic framework template and realizes the customization of the template.

[0080] The global template file has the suffix ".template". By using the suffix ".template", the global template file is distinguished from other files.

[0081] When the present invention is actually used: A method for generating code templates based on an annotation markup language provided by the present invention is further described in detail through the template generation process of Java code.

[0082] First, write a set of project framework templates. As Figure 4 shown, the directory structure includes entity, vo, mapper, service, controller, etc. According to the standard development process, code template files are written for various entity models (such as add / delete / update / query type, dictionary type, tree type, tree dictionary, etc.), and the global template file global_all.template is used to define which code template files will be generated. At this time, the project framework template should be runnable and unit tested to a certain extent to verify the accuracy of the template.

[0083] Secondly, configure the template object in the meta object, select available templates and versions; configure the project object, define various parameters such as the top-level package name (such as com.getall.jsdz.demo), the project root path, the version number, and the dependent components; configure the entity object again, define the data model (such as User user entity, Account bill entity, SysDept department entity, etc.); then for each entity, configure its property object to define entity properties (such as userName user name, etc.); finally, configure the foreign key object to define the foreign key relationship between entities (such as there should be a foreign key reference to the SysDept entity in the User entity). According to the parameter information configured in the meta object, code generation rules are added to the global template file and code template files in the project framework template using the annotation markup language.

[0084] Finally, read the global template file and code template files respectively through a file reader, use an annotation markup parser to parse the annotation marks in the global template file and code template files and form corresponding syntax trees, then execute the syntax trees through an annotation markup executor, and finally generate code. As Figure 5 shown, it is a schematic diagram of the finally generated project framework.

[0085] The technical features not described in the present invention can be realized by or adopted from the prior art and will not be elaborated here. Of course, the above description is not a limitation of the present invention, and the present invention is not limited to the above examples. Changes, modifications, additions, or substitutions made by those of ordinary skill in the art within the scope of the essence of the present invention should also fall within the protection scope of the present invention.

Claims

1. A code template generation method based on annotation markup language, characterized in that: The following steps are involved: S1: Create a project framework template: Write an initial project framework template, define its directory structure, write code template files for various entity models according to the standard development process, and use the global template file to define the code template files to be generated; S2: Meta-object construction: Design the data model according to the actual needs of the project, define the parameter information, and use the meta-object controller to build the meta-object; S3: Add code generation rules: According to the parameter information defined by the meta-object, use the annotation markup language to add code generation rules to the global template file and code template file in the project framework template; S4: Reading a global template file: according to the framework template specification, finding a global template file in the project framework template, and using a file reader to read the global template file; S5: parsing the comment tags in the global template file to form a global template syntax tree: using the comment tag parser, parsing the global template file line by line to form an executable global template syntax tree; S6: Execute the global template syntax tree and traverse the template file: Use the comment mark executor to traverse and execute the global template syntax tree. The actual effect is to traverse the code template file to be generated, and call the specified generator at the same time to generate code in the subsequent steps S7 to S10; S7: Reading the code template file: for each code template file to be generated, using a file reader to read the code template file; S8: parsing comment tags in the code template file to form a template syntax tree: using a comment tag parser, parsing the code template file line by line to form an executable code template syntax tree; S9: Execute the code template syntax tree and generate code: Use the comment mark executor to traverse and execute the code template syntax tree. The actual effect is to generate code according to the rules described by the comment mark; S10: Create a code file and write the code: calculate the actual path and file name of the code file according to the rules described in the global template syntax tree, and create the code file. At the same time, write the code generated in step S9 into the code file; finally, return to step S7 and continue to process the next code template file until the global template syntax tree traversal is completed.

2. The code template generation method based on annotation markup language according to claim 1 is characterized in that: The meta-object in step S2 is used to describe code generation requirements and generation rules, specifically including: Template object, used to describe the template information used when generating the project framework; Project object, used to describe framework-level configuration parameters; Entity object, used to describe entity-level configuration parameters; Property objects, used to describe property-level configuration parameters; Foreign key object, used to describe the foreign key relationship of entity key.

3. The code template generation method based on annotation markup language according to claim 1, characterized in that: The annotation markup language in step S1 is used to describe the specific processing logic to be performed on the codes of the global template file and the code template file when generating the code, and specifically includes the following annotation marks: M1: inject, injection mark; M2: block, code block mark; M3: include, endinclude, dependent include tags; M4: loop, endloop, loop mark; M5: drop, discard mark; M6: generator, generator tag; M7: flag, endflag, matching mark.

4. The code template generation method based on annotation markup language according to claim 3 is characterized in that: The M1 injection marker has two forms: One is static injection, the parsing expression is inject('template_string','new_string'), which replaces the template_string string in the marked code with the new_string string; One is dynamic injection, the parsing expression is inject('template_string',object.attribute), which replaces the template_string string in the marked code with a property value attribute of a meta-object object; The parsing expression of the M2 code block tag is block(object.codeBlock), which is used to inject the codeBlock value of a certain meta-object as the entire code block before the next line of code to add some specific function codes; The M3 dependency includes tags, include and endinclude appear in pairs, and the parsed expressions are include('A,B,C,...') and endinclude() respectively. The function is that when the dependency component of the project framework to be generated contains one of A, B, C..., the code between include and endinclude will be generated, otherwise it will be ignored; The M4 loop tag, loop, and endloop appear in pairs, and the analytical expressions are loop(object) and endloop(), respectively. The function is to traverse a certain meta-object object, and the code between loop and endloop is generated during the traversal. The meta-objects that can be traversed include entity objects, attribute objects, and foreign key objects. The M5 discard mark, whose parsing expression is drop(), is used to discard the next line of code. It is used to describe the auxiliary code fragment added to the template to make the logic of the template complete and does not participate in the final code generation; The M6 ​​generator tag, whose parsing expression is generator('G'), is used to generate code using a specific generator G, which is used for generating non-text format files or generating some special codes; The M7 matching tag, flag, and endflag appear in pairs, and the parsed expressions are flag('X,Y,Z,...',object.attribute) and endflag(), respectively. Its function is to generate the code between flag and endflag when an attribute value attribute of a meta-object object is between X, Y, Z..., otherwise it is ignored.

5. The code template generation method based on annotation markup language according to claim 3 is characterized in that: The comment mark is written in the comment before the code to be described, each comment mark occupies one line, and the comment marks can be nested arbitrarily.

6. The code template generation method based on annotation markup language according to claim 5 is characterized in that: The comment mark uses " / / " as the comment symbol in the global template file, and in the code template file, the corresponding comment symbol is selected according to the specific computer language used.

7. The code template generation method based on annotation markup language according to claim 1, characterized in that: The global template file in step S1 specifies the code template files involved in code generation, and its format is as follows: the code content is the path and file name of each code template file in the project framework template, and each line of code describes a code template file.

8. The code template generation method based on annotation markup language according to claim 7 is characterized in that: The global template file has a suffix of ".template".

9. A code template generation system based on annotation markup language, characterized in that: include: W1 meta-object controller, used to control the construction of meta-objects and the reading of properties of each meta-object; W2 generator, used to generate files of various formats according to different template file types and descriptions of comment tags; W3 dialect controller, used to control the dialect style of syntax differences of various databases that may be used; W4 file reader, used to read the template file content into the cache; W5 comment token parser, used to parse comment tokens line by line from the cache to form an executable syntax tree; W6 comment marker executor is used to traverse the syntax tree and perform processing operations corresponding to the comment markers.

Citation Information

Patent Citations

  • Transplantable unit code automatic generation method based on aviation embedded open system architecture

    CN111782194A

  • Code processing method and device and storage medium

    CN112130837A

  • Code generation method and device

    CN114879964A

  • Optimization method and device for auxiliary code automatic annotation, medium and terminal

    CN115454497A

  • Code generation method and device, electronic equipment and medium

    CN116578282A