A code templating generation method and system based on annotated markup language
By using a code template generation method based on annotation markup language, the problem of difficulty in unifying R&D standards in software development is solved, achieving efficient cross-language code generation and template generation, and improving R&D efficiency and code quality.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CHINACCS INFORMATION IND
- Filing Date
- 2025-01-21
- Publication Date
- 2026-05-15
AI Technical Summary
Existing technologies in software development suffer from problems such as difficulty in unifying R&D standards, strong code intrusion, poor readability, and limited applicability, resulting in low efficiency of code template generation and difficulty in adapting to diverse needs.
A code template generation method based on annotation markup language is adopted. This method generates code that conforms to R&D standards by creating a project framework template, constructing meta-objects, adding code generation rules, and parsing and executing the annotation markup syntax tree.
It improves code writing efficiency and quality, enables cross-language code templates, supports diverse needs, ensures that code templates are compileable, runnable, and testable, and enhances R&D efficiency and standardization.
Smart Images

Figure CN120045187B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of code development technology, and in particular to a code template generation system and method based on annotation markup language. Background Technology
[0002] Currently, information technology is developing rapidly both domestically and internationally, with various technologies emerging and changing daily. While this provides developers with more choices, it also brings challenges to software companies. Because a software company often has multiple R&D teams, even if the company establishes R&D standards, the implementation by different teams may vary due to difficulties in supervision, making it difficult to unify standards. This can easily lead to problems such as diverse technology selection, sensitivity to personnel turnover, difficulty in implementing R&D standards, poor component reusability, difficulties in internal system adaptation, and high R&D costs. Therefore, code template generation methods have emerged.
[0003] Mainstream code template generation methods typically employ techniques such as OGNL (Object Graph Navigation Language) expressions and EL (Expression Language) expressions. These techniques can dynamically inject control flow elements like branches and loops, as well as variable and property values, into the template, thus achieving template-based generation. However, using OGNL and EL for template development presents several drawbacks, including strong code intrusion, poor readability, and the template itself being uncompileable, unrunnable, and untestable. Only the generated code can be compiled, run, and tested, which is highly inconvenient. Furthermore, OGNL and similar techniques are usually limited to specific languages, restricting their applicability.
[0004] How to solve the above-mentioned technical problems is the challenge facing this invention. Summary of the Invention
[0005] To address the shortcomings of existing technologies, this invention provides, on the one hand, a code template generation method to improve code development efficiency and quality; on the other hand, it provides a code template generation system that is open, accurate, and applicable to multiple computer languages.
[0006] The technical solution adopted by this invention to solve its technical problem is:
[0007] On one hand, this invention discloses a code template generation method based on annotation markup language, including...
[0008] 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, define the code template files to be generated using the global template file, and perform a certain degree of unit testing to verify the accuracy of the template.
[0009] S2: Meta-object construction: Design the data model and define the parameter information according to the actual needs of the project, and use the meta-object controller to build 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: Based on the parameter information defined in the meta-object, use comment markup language to add code generation rules to the global template file and code template file in the project framework template;
[0011] S4: Read the global template file: According to the framework template specification, locate the global template file in the framework template and read the file using a file reader;
[0012] S5: Parse the comment tags in the global template file to form a global template syntax tree: Use the comment tag parser to parse the comment tags 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 file: Use comments to mark the executor, 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 subsequent steps S7 to S10.
[0014] S7: Read code template files: Use a file reader to read each code template file to be generated;
[0015] S8: Parse the comment markers in the code template file to form a code template syntax tree: Use the comment marker 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 and generate code: Use comment markers to traverse and execute the code template syntax tree. The actual effect is to generate code according to the rules described by the comment markers.
[0017] S10: Create code file and write code: Calculate the actual path and filename of the code file according to the rules described by 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 processing the next code template file until the global template syntax tree traversal is complete.
[0018] Preferably, the meta-object in step S2 includes:
[0019] Template objects are used to describe the template information used when the project framework is generated;
[0020] Project objects are used to describe framework-level configuration parameters, such as path, version number, name, and scope of dependent components; entity objects are used to describe entity-level configuration parameters, such as class name, table name, and entity type.
[0021] Attribute objects are used to describe attribute-level configuration parameters, such as attribute name, field name, field format, value range, query method, etc.
[0022] Foreign key objects are used to describe the foreign key relationships of entity keys.
[0023] Preferably, the annotation markup language in step S1 is used to describe the specific processing logic to be performed on the code in the global template file and the code template file when generating code, specifically including the following annotation marks:
[0024] M1: inject, injection marker;
[0025] M2: block, code block marker;
[0026] M3: include, endinclude, dependency include tags;
[0027] M4: loop, endloop, loop marker;
[0028] M5: drop, discard the marker;
[0029] M6: generator, generator tag;
[0030] M7: flag, endflag, matching flags.
[0031] Preferably, the injected marker M1 has two forms:
[0032] One method is static injection, with the parsing expression inject('template_string','new_string'), which replaces the 'template_string' string in the marked code with the 'new_string' string.
[0033] One method is dynamic injection, with the parsing expression inject('template_string', object.attribute). Its function is to replace the string 'template_string' in the marked code with the attribute value 'attribute' of a meta-object 'object'.
[0034] The parsing expression of the M2 code block marker is block(object.codeBlock), which is used to inject the codeBlock value of a meta-object as the entire code block before the next line of code, in order to add some specific functional code;
[0035] The M3 dependency includes tags, with include and endinclude appearing in pairs. The parsing expressions are include('A,B,C,...') and endinclude(), respectively. Their function is that if the dependent components of the project framework to be generated contain one of A, B, C, ..., the code between include and endinclude will be generated; otherwise, it will be ignored.
[0036] The M4 loop marker, loop and endloop, appear in pairs, with the parsing expressions loop(object) and endloop() respectively. Its function is to traverse a certain meta-object object. During the traversal, code between loop and endloop is generated. The meta-objects that can be traversed include entity objects, attribute objects, and foreign key objects.
[0037] The M5 drop flag, parsed as drop(), is used to discard the next line of code. It describes auxiliary code snippets added to the template to make the template's logic complete, and does not participate in the final code generation.
[0038] The M6 generator tag, parsed as generator('G'), is used to generate code using a specific generator G, for generating non-text format files or generating certain special codes;
[0039] The M7 matching flags, flag and endflag, appear in pairs and are parsed as flag('X,Y,Z,...',object.attribute) and endflag(), respectively. Their function is to generate the code between flag and endflag when a certain attribute value of a meta-object object is among X, Y, Z, ..., otherwise ignore it.
[0040] Preferably, the comment markers are written in the comments preceding the code they describe, with each comment marker occupying one line and the comment markers being nested arbitrarily.
[0041] Preferably, the comment marker uses " / / " as the comment symbol in the global template file, while 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 filename of each code template file in the project framework template, and each line of code describes one code template file. The code template files to be generated are defined in the global template file as needed. Only the code template files listed in the global template file will be generated, improving the openness of the basic framework template and enabling template customization.
[0043] Preferably, the global template file has the ".template" extension. Using the ".template" extension distinguishes the global template file from other files.
[0044] On the other hand, the present invention discloses a code template generation system based on annotation markup language, including a W1 meta-object controller for controlling the construction of meta-objects and the reading of attributes of each meta-object;
[0045] W2 Generator is used to generate files in various formats such as code, configuration, images, and audio based on different template file types and comment tags.
[0046] The W3 dialect controller is used to provide dialect-based control over the syntactic differences of various databases that may be used.
[0047] W4 file reader, used to read template file content into the cache;
[0048] The W5 comment tag parser is used to parse comment tags line by line from the cache to form an executable syntax tree;
[0049] The W6 comment markup executor is used to traverse the syntax tree and perform processing operations corresponding to comment marks, including character injection, code block injection, dependency inclusion, looping, discarding, matching, calling a generator to generate code, and outputting files or code.
[0050] The beneficial effects of this invention are as follows: When a software company customizes a project framework template based on R&D standards, the generated code inherently conforms to these standards, technically supporting their implementation and significantly improving coding efficiency. Simultaneously, the code templates provided by this invention can be written in any high-level computer language, offering broad applicability. Multiple templates can be customized for different development languages and technology stacks to support diverse project needs. Only templates listed in the global template file are generated. Templates can be flexibly customized according to actual needs, defining a unified code structure and format, achieving unified data security and system security standards, developing default CRUD (Create, Read, Update, Query) interfaces, defining a unified interface return format, and providing reusable components and tools. This allows for rapid project configuration, avoids repetitive coding, improves R&D efficiency, and standardizes data model design. Comment markup languages are used in code comments to describe control flow elements such as branches and loops, as well as the generation of variable attribute injection tags. Through a file reader, comment markup parser, and comment markup executor, the reading and execution of comment marks are achieved, intruding on comments rather than code. The introduction of comment markup languages does not affect the logic of the code template itself, allowing the code template 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 is more efficient. Comment marks can be nested with each other, improving the readability of the overall logic. Attached Figure Description
[0051] Figure 1 The diagram shows examples of annotation markers for this invention in different computer languages.
[0052] Figure 2 This is a flowchart illustrating the code generation process of the present invention.
[0053] Figure 3 This is a structural diagram of the code template generation system of the present invention.
[0054] Figure 4 This is a schematic diagram of a code template for an embodiment of the present invention.
[0055] Figure 5 This is a schematic diagram of the final project framework generated in an embodiment of the present invention. Detailed Implementation
[0056] To clearly illustrate the technical features of this solution, the following detailed implementation method will be used to explain the solution.
[0057] See Figures 1-5As shown, this embodiment is a code template generation method based on annotation markup language, including the following steps: 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, define the code template files to be generated using the global template file, and perform a certain degree of unit testing to verify the accuracy of the template;
[0058] S2: Meta-object construction: Design the data model and define the parameter information according to the actual needs of the project, and use the meta-object controller to build 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: Based on the parameter information defined in the meta-object, use comment markup language to add code generation rules to the global template file and code template file in the project framework template;
[0060] S4: Read the global template file: According to the framework template specification, locate the global template file in the framework template and read the file using a file reader;
[0061] S5: Parse the comment tags in the global template file to form a global template syntax tree: Use the comment tag parser to parse the comment tags 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 file: Use comments to mark the executor, 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 subsequent steps S7 to S10.
[0063] S7: Read code template files: Use a file reader to read each code template file to be generated;
[0064] S8: Parse the comment markers in the code template file to form a code template syntax tree: Use the comment marker parser to parse the code template file line by line to form an executable code template syntax tree;
[0065] S9: Execute the code template syntax tree and generate code: Use comment markers to traverse and execute the code template syntax tree. The actual effect is to generate code according to the rules described by the comment markers.
[0066] S10: Create code file and write code: Calculate the actual path and filename of the code file according to the rules described by 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 processing the next code template file until the global template syntax tree traversal is complete.
[0067] Meta objects include: template objects, which describe the template information used when generating the project framework; project objects, which describe framework-level configuration parameters, such as path, version number, name, and scope of dependent components; entity objects, which describe entity-level configuration parameters, such as class name, table name, and entity type; attribute objects, which describe attribute-level configuration parameters, such as attribute name, field name, field format, value range, and query method; and foreign key objects, which describe 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 in the global template file and the code template file when generating code. Specifically, it includes the following annotation marks: M1: inject, injection markup; M2: block, code block markup; M3: include, endinclude, dependency inclusion markup; M4: loop, endloop, loop markup; M5: drop, drop markup; M6: generator, generator markup; M7: flag, endflag, matching markup.
[0069] Among them, the M1 injection marker has two forms: one is static injection, which parses inject('template_string','new_string'), and its function is to replace the template_string string in the marked code with the new_string string;
[0070] One method is dynamic injection, with the parsing expression inject('template_string', object.attribute). Its function is to replace the string 'template_string' in the marked code with the attribute value 'attr_ibute' of a meta-object object.
[0071] The parsing expression for the M2 code block marker is block(object.codeBlock), which injects the codeBlock value of a meta-object as the entire code block before the next line of code to add code with specific functions.
[0072] M3 dependency inclusion markers, with include and endinclude appearing in pairs, and their parsing expressions are include('A,B,C,...') and endinclude(), respectively. Their function is that if the dependent components of the project framework to be generated contain one of A, B, C, ..., the code between include and endinclude will be generated; otherwise, it will be ignored.
[0073] The M4 loop marker, loop and endloop, appear in pairs. The parsing expressions are loop(object) and endloop(), respectively. Its function is to traverse a meta-object object. During the traversal, code between loop and endloop is generated. The meta-objects that can be traversed are entity objects, attribute objects, and foreign key objects.
[0074] The M5 drop flag, parsed as drop(), is used to discard the next line of code. It describes auxiliary code snippets added to the template to make the template logic complete, and does not participate in the final code generation.
[0075] The M6 generator tag, parsed as generator('G'), is used to generate code using a specific generator G. It is used for generating non-text format files or generating certain special codes.
[0076] M7 matches the flag, flag and endflag appear in pairs, and their parsing expressions are flag('X,Y,Z,...',object.attribute) and endflag(), respectively. Their function is to generate the code between flag and endflag when a certain attribute value of a meta-object object is among X, Y, Z, ..., otherwise ignore it.
[0077] Comment tags are written within comments preceding the code they describe. Each comment tag occupies one line, and comment tags can be nested arbitrarily.
[0078] In the global template file, " / / " is used 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 that participate in code generation. Its format is as follows: the code content consists of the path and filename of each code template file in the project framework template, with each line of code describing one code template file. The code template files to be generated are defined in the global template file as needed. Only code template files listed in the global template file will be generated, improving the openness of the basic framework template and enabling template customization.
[0080] Global template files have the ".template" extension. Using the ".template" extension distinguishes global template files from other files.
[0081] In practical use, this invention provides a more detailed explanation of the code template generation method based on annotation markup language through the Java code template generation process.
[0082] First, develop a project framework template, such as Figure 4 As shown, the directory structure includes entities, void entities, mappers, services, controllers, etc. Following standard development procedures, code template files are written for various entity models (such as CRUD types, character types, tree structures, tree dictionaries, etc.), and the global_all.template global template file is used to define which code template files will be generated. At this point, the project framework template should be runnable, and a certain level of unit testing should be performed to verify the accuracy of the template.
[0083] Secondly, configure the template object in the meta object, selecting available templates and versions; configure the project object, defining various parameters such as the top-level package name (e.g., com.getall.jsdz.demo), project root path, version number, and dependent components; next, configure the entity object, defining the data model (e.g., User entity, Account entity, SysDept entity); then, for each entity, configure its property object, defining entity properties (e.g., userName, etc.); finally, configure the foreign key object, defining foreign key relationships between entities (e.g., the User entity should have a foreign key reference to the SysDept entity). Based on the parameter information configured in the meta object, use comment markup language to add code generation rules to the global template file and code template file in the project framework template.
[0084] Finally, a file reader reads the global template file and the code template file respectively; a comment marker parser parses the comment markers in the global template file and the code template file to form the corresponding syntax tree; then, a comment marker executor executes the syntax tree to finally generate the code. Figure 5 The image shown is a schematic diagram of the final generated project framework.
[0085] The technical features of this invention not described can be implemented by or using existing technology, and will not be repeated here. Of course, the above description is not a limitation of this invention, and this invention is not limited to the examples above. Any changes, modifications, additions or substitutions made by those skilled in the art within the scope of this invention should also be within the protection scope of this invention.
Claims
1. A code template generation method based on annotation markup language, characterized in that, Includes the following steps: 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 a global template file to define the code template files to be generated. S2: Meta-object construction: Design the data model and define the parameter information according to the actual needs of the project, and use the meta-object controller to construct the meta-object; S3: Add code generation rules: Based on the parameter information defined in the meta-object, use comment markup language to add code generation rules to the global template file and code template file in the project framework template; S4: Read the global template file: According to the framework template specification, locate the global template file in the project framework template and use a file reader to read the global template file; S5: Parse the comment tags in the global template file to form a global template syntax tree: Use the comment tag parser to parse 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 comments to mark the executor, 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 subsequent steps S7~S10. S7: Read code template files: For each code template file to be generated, use a file reader to read the code template file; S8: Parse the comment markers in the code template file to form a template syntax tree: Use the comment marker parser to parse 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 comment markers to traverse and execute the code template syntax tree. The actual effect is to generate code according to the rules described by the comment markers. S10: Create code file and write code: Calculate the actual path and filename of the code file according to the rules described by 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 processing 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, characterized in that, The meta-object in step S2 is used to describe code generation requirements and generation rules, specifically including: Template objects are used to describe the template information used when the project framework is generated; Project objects are used to describe framework-level configuration parameters; Entity objects are used to describe entity-level configuration parameters; Attribute objects are used to describe attribute-level configuration parameters; Foreign key objects are used to describe the foreign key relationships of entity keys.
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 code in the global template file and the code template file when generating code, specifically including the following annotation marks: M1: inject, injection marker; M2: block, code block marker; M3: include, endinclude, dependency include tags; M4: loop, endloop, loop marker; M5: drop, discard the marker; M6: generator, generator tag; M7: flag, endflag, matching flags.
4. The code template generation method based on annotation markup language according to claim 3, characterized in that, The M1 injection marker has two forms: One method is static injection, with the parsing expression inject('template_string', 'new_string'), which replaces the 'template_string' string in the marked code with the 'new_string' string. One method is dynamic injection, with the parsing expression inject('template_string', object.attribute). Its function is to replace the string 'template_string' in the marked code with the attribute value 'attribute' of a meta-object 'object'. The parsing expression of the M2 code block marker is block(object.codeBlock), which is used to inject the codeBlock value of a meta-object as the entire code block before the next line of code, in order to add some specific functional code; The M3 dependency includes tags, with include and endinclude appearing in pairs. The parsing expressions are include('A,B,C,...') and endinclude(), respectively. Their function is that if the dependent components of the project framework to be generated contain one of A, B, C, ..., the code between include and endinclude will be generated; otherwise, it will be ignored. The M4 loop marker, loop and endloop, appear in pairs, with the parsing expressions loop(object) and endloop() respectively. Its function is to traverse a certain meta-object object. During the traversal, code between loop and endloop is generated. The meta-objects that can be traversed include entity objects, attribute objects, and foreign key objects. The M5 drop flag, parsed as drop(), is used to discard the next line of code. It describes auxiliary code snippets added to the template to make the template's logic complete, and does not participate in the final code generation. The M6 generator tag, parsed as generator('G'), is used to generate code using a specific generator G, for generating non-text format files or generating certain special codes; The M7 matching flags, flag and endflag, appear in pairs and are parsed as flag('X,Y,Z,...',object.attribute) and endflag(), respectively. Their function is to generate the code between flag and endflag when a certain attribute value of a meta-object object is among X, Y, Z, ..., otherwise ignore it.
5. The code template generation method based on annotation markup language according to claim 3, characterized in that, The comment markers are written in the comments before the code they describe, with each comment marker occupying one line, and comment markers can be nested arbitrarily.
6. The code template generation method based on annotation markup language according to claim 5, characterized in that, The comment marker uses " / / " as the comment symbol in the global template file, while 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 that participate in code generation. Its format is as follows: the code content is the path and filename 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, characterized in that, The global template file has the ".template" extension.
9. A code template generation system based on annotation markup language, characterized in that, The steps for performing the code template generation method based on annotation markup language according to any one of claims 1-8 include: The W1 metaobject controller is used to control the construction of metaobjects and the reading of the properties of each metaobject. The W2 generator is used to generate files in various formats based on different template file types and comment tag descriptions. The W3 dialect controller is used to provide dialect-based control over the syntactic differences of various databases that may be used. W4 file reader, used to read template file content into the cache; The W5 comment tag parser is used to parse comment tags line by line from the cache to form an executable syntax tree; The W6 comment marker executor is used to traverse the syntax tree and perform the processing operations corresponding to the comment markers.