A code generation and quality optimization method based on static analysis and large language model

By combining static analysis and large language models, and utilizing UML model mapping and refined prompts to optimize the code generation process, the accuracy and quality issues of code generated by large language models are resolved, achieving efficient generation of high-quality class-level code.

CN120010858BActive Publication Date: 2025-10-28西交网络空间安全研究院 +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510150718.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-02-11
Publication Date
2025-10-28
Estimated Expiration
2045-02-11

AI Technical Summary

Technical Problem

In existing technologies, the accuracy and quality of code generated by large language models cannot be guaranteed. They lack the ability to understand and associate context when generating class-level code, and the syntactic correctness of the generated code cannot be guaranteed.

Method used

By combining static analysis and large language models, class skeleton code is generated through UML model mapping. The code is optimized using refined prompt word engineering and quality feedback. Multiple rounds of interaction and static analysis are conducted until erroneous calls are eliminated. Non-functional quality assessment is performed to generate high-quality code.

Benefits of technology

It improves the accuracy and quality of code generation, ensures the syntactic correctness and contextual understanding of the generated code, and enhances development efficiency and code maintainability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120010858B_ABST
    Figure CN120010858B_ABST
Patent Text Reader

Abstract

This invention discloses a code generation and quality optimization method based on static analysis and a large language model. The method includes: first, generating and verifying the initial code framework using a large language model based on requirements and functional descriptions; then, generating preliminary class-level code through interaction with the large language model via refined prompt word engineering; performing static analysis on the generated code to construct an abstract syntax tree and detect entities, dependencies, and potential erroneous calls; continuously optimizing the code through multiple iterative interactions based on the feedback results of the static analysis and the large language model; and finally, conducting a non-functional quality assessment of the optimized code to ensure high-quality class-level code generation. This invention not only automatically generates code frameworks based on requirements and functional descriptions but also improves code reliability and maintainability through static analysis and quality assessment feedback mechanisms, solving the problem of low-quality code generation in existing technologies.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to code generation technology in computer software engineering, and more particularly to a code generation and quality optimization method based on static analysis and large language models. Background Technology

[0002] Automatic code generation is an important research area in software engineering, widely applied in software development, database management, the Internet of Things (IoT), and finance. Among numerous methods, large language models (MLMs) have attracted widespread attention due to their powerful natural language understanding capabilities, enabling them to better comprehend natural language descriptions provided by developers or non-technical personnel, and requiring lower human and time costs compared to other methods. However, generating accurate code using large language models remains a challenging task, primarily because the accuracy and quality of the generated code cannot be guaranteed. To overcome this deficiency, existing automatic code generation methods based on large language models typically improve the code generation quality through prompt word engineering and dataset fine-tuning, but this also increases the difficulty of model construction.

[0003] Currently, for model-driven automatic code generation tasks, traditional UML-to-code mapping methods mainly generate class skeleton code, which is insufficient in terms of complexity and number of methods. Methods using large language models tend to generate method / function level code, but they haven't effectively addressed the challenge of contextual understanding when directly generating class-level code, and they cannot guarantee the syntactic correctness of the generated code, making them unreliable. Summary of the Invention

[0004] This invention aims to address the technical problems of existing automatic code generation technologies, such as inconsistent code quality, low development efficiency, difficulty in maintaining generated code, and low ability to understand and associate the context of generated class-level code. It proposes a code generation and quality optimization method based on static analysis and a large language model.

[0005] The technical solution adopted in this invention is:

[0006] A code generation and quality optimization method based on static analysis and large language models includes the following steps:

[0007] S1. UML Model Definition, Mapping, and Verification:

[0008] Based on the requirements and functional descriptions, the UML model is mapped to a preliminary code framework using forward engineering, and then validated. This process involves defining class structure, method responsibilities, and relationships between classes, mapping class definitions, package structure, attributes, interfaces, enumerations, and basic methods through class diagrams.

[0009] S2. Configure refined prompt word engineering:

[0010] A refined suggestion strategy based on a large language model is established. Through interaction between natural language and the model, preliminary class-level code completion is generated. The suggestion design covers input semantics, output customization, error recognition, suggestion improvement, and context control to optimize the accuracy, security, and generalization ability of the generated code.

[0011] S3. Static code analysis:

[0012] Static analysis is performed on the class-level code generated in step S2 to construct an abstract syntax tree (AST), analyze the entities and dependencies in the code, scan for erroneous calls in the code, and identify potential erroneous call problems by checking whether the called entity exists in its class.

[0013] S4. Code iteration and improvement based on quality feedback:

[0014] Based on the static analysis results, and using quality feedback, the code is continuously optimized and corrected through multiple rounds of interaction with the large language model via refined prompt words. Static analysis is repeated until erroneous calls and other issues in the code are completely eliminated.

[0015] S5. Non-functional quality assessment:

[0016] The code, after error checking and repair, undergoes a non-functional quality assessment, including maintainability, cyclomatic complexity, and performance metrics. If the assessment results do not meet predetermined standards, further optimization is performed through interaction with a large language model, ultimately generating high-quality code that meets the quality requirements.

[0017] Further, step S1 includes:

[0018] By using UML forward engineering (UML -> code), the UML model is mapped to the target implementation language, generating class skeleton code. This process defines the class structure, method responsibilities, and relationships between classes, mapping class definitions, package structure, attributes, interfaces, enumerations, and basic methods through class diagrams.

[0019] Further, step S2 includes:

[0020] Multiple prompt word patterns are employed, including input semantics, output customization, error recognition, prompt improvement, interaction optimization, and context control, to establish a refined prompt word strategy. Prompt words are optimized based on three dimensions: accuracy, security, and generalization ability of code generation, ensuring that the large language model provides initial refinement of the skeleton-like code generated in step S1.

[0021] Further, step S3 includes:

[0022] Static analysis is used to analyze the preliminary class-level code generated in step S2, construct its abstract syntax tree, parse the entities and dependencies in the code, and determine whether the called entity exists in its class, so as to identify and fix erroneous call problems.

[0023] Further, step S4 includes:

[0024] The error call results from step S3 are combined with the large language model, and the interaction quality with the large language model is improved through a refined prompt word method. The generated modified results are applied to the original code, followed by static analysis and iterative interaction until the number of error calls is reduced to zero.

[0025] Further, step S5 includes:

[0026] Perform non-functional quality assessments on the error-checked code, including evaluations of maintainability, cyclomatic complexity, etc. If the assessment score does not meet the standard, further optimization is performed through interaction with a large language model to improve code quality.

[0027] Compared with the prior art, the present invention has the following advantages and technical effects:

[0028] This invention combines a data model with a large language model, mapping UML diagrams to class skeleton code. The large language model then automatically processes the UML functional descriptions and class skeleton-level code, generating relatively complete class-level code. Furthermore, this invention incorporates advanced prompt word engineering and quality assessment feedback to ensure the correctness of the code's functionality, thus improving the quality of the generated code. Attached Figure Description

[0029] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following description is provided with accompanying drawings of the relevant technical solutions in the embodiments of the present invention or the prior art. It should be understood that the accompanying drawings described below are only for the convenience of clearly illustrating some embodiments of the technical solutions of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0030] Figure 1 This is a flowchart illustrating the steps of a code generation and quality optimization method based on static analysis and a large language model according to an embodiment of the present invention.

[0031] Figure 2 This is a flowchart illustrating the process of constructing a complete class skeleton code suggestion word through interaction with a large language model, according to an embodiment of the present invention.

[0032] Figure 3 This is a flowchart of an example of an error method call in the quality assessment feedback of this invention.

[0033] Figure 4 This is a flowchart illustrating the process of constructing and repairing error call prompts through interaction with a large language model, according to an embodiment of the present invention.

[0034] Figure 5 This is a graph showing the experimental measurement results of three code optimization levels during the code generation process of this invention embodiment;

[0035] Figure 6 This is a flowchart of a code generation and quality optimization method based on static analysis and a large language model, according to an embodiment of the present invention. Detailed Implementation

[0036] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0037] Preferred embodiments of the present invention will now be described with reference to the accompanying drawings. Those skilled in the art should understand that these embodiments are merely illustrative of the technical principles of the present invention and are not intended to limit the scope of protection of the present invention.

[0038] It should be noted that, unless there is any conflict, the embodiments and features in the embodiments of this application can be combined with each other.

[0039] To address the existing technical problems, this invention focuses on combining data models with large language models, mapping UML diagrams to class skeleton code, and then allowing the large language model to automatically process the UML functional descriptions and class skeleton-level code, thereby automatically generating relatively complete class-level code. Simultaneously, this invention incorporates advanced prompt word engineering and quality assessment feedback to improve the accuracy of code generation and quality optimization based on static analysis and large language models.

[0040] The method will be explained in detail below with reference to the accompanying drawings and specific embodiments.

[0041] like Figure 6 As shown, this embodiment is a code generation and quality optimization method based on static analysis and a large language model, including the following steps:

[0042] The first step is to construct class diagrams and generate code mappings based on the UML descriptions. Figure 1 S1 part:

[0043] S1. Define, map, and validate the UML model. Based on the requirements and functional descriptions, complete the initial code framework mapping and validation of the model, and generate the class skeleton code.

[0044] In this embodiment, the structure of classes, the responsibilities of methods, and the relationships between classes are defined. A UML class diagram is used to map the definitions, packages, attributes, interfaces, enumerations, and basic methods of classes to the corresponding code. Based on the requirements and functional descriptions of the task to be completed, and combined with the UML model definition, forward engineering (UML->CODE) is used to map the UML model to the language, transforming the model into class skeleton-level code.

[0045] The second step is to analyze the class diagram code and complete the initial code completion for the large language model. Figure 1 S2 part:

[0046] S2. Based on the skeleton code obtained in step S1, through setting up a refined prompt word project, interact with the large language model to complete the class-level code after initial completion.

[0047] like Figure 2 As shown, for the initial setup of the large language model, "role-playing" was first performed, assigning an identity to the large language model. Then, a specific interaction template was selected and applied, followed by interaction, where the customized template was selected to complete the interactive output. A templated language was used throughout the process, and the constructed prompts are universal and can handle various code generation scenarios. The preliminary code constructed in step S1 was iteratively modified through interaction with the large model at the class skeleton level, ultimately resulting in the preliminary class-level code with completed method bodies.

[0048] The third step is to perform static code analysis to check for erroneous calls and corresponding... Figure 1 S3 section:

[0049] S3. Perform static code analysis on the class-level code obtained in S2, complete the construction of the abstract syntax tree, entity and dependency analysis, and scan for erroneous call issues.

[0050] Specifically, it is necessary to verify the correctness of the class-level code obtained in step S2, use the entity dependency detection tool ENRE to scan the generated code, perform static analysis on the class-level code in step S2, construct the corresponding abstract syntax tree, obtain variable declarations, method declarations, method calls, etc., and further obtain the entities and dependencies in the code to determine whether there are any incorrect call relationships in the code.

[0051] like Figure 3As shown, the ENRE entity dependency calling tool is used to perform an AST scan of the code, construct an entity dependency graph, and specifically determine erroneous calls. For checking erroneous calls, scope verification is first performed to obtain entities after static analysis. An entity can be a method, function, or other code block. Next, it is checked whether the entity is of type `scope`, i.e., whether it belongs to a specific scope. If the entity is of type `scope`, existence verification is further performed to check whether the entity contains `call` calls. This requires identifying whether there are calls to other methods or functions within the entity. Then, all `call` calls contained in the entity are obtained. Next, parameter and return value compatibility verification is performed to determine whether the entity called by the entity exists, ensuring that the called method or function is valid in the codebase, and recording the erroneous call. If the called entity does not exist, the erroneous call is recorded. Finally, all erroneous calls are recorded. This facilitates subsequent interactive repair of syntax errors and code correctness by building a prompt word project and a large language model.

[0052] The fourth step involves the large language model optimizing the code based on feedback. Figure 1 S4 section:

[0053] S4. Based on the feedback from static analysis, and in conjunction with the large language model, continuously interact and iterate multiple times to improve the code.

[0054] Specifically, based on the error call analysis data obtained in step S3, prompt words for interaction with the large language model are further constructed. For example... Figure 4 As shown, for the prompt word project at this stage, the process begins with "role-playing," assigning an identity to the large language model. The model learns the pre-existing information, interacts with it, and applies prompt word templates. Then, specific error-related code snippets are input to refine the prompt words. Finally, an output template is set for the large language model to facilitate subsequent automated processing and data storage. This process continues, performing entity dependency analysis to detect and scan for errors in the code, interacting with the large language model for automated verification to optimize and improve the code, until the number of error calls in step S3 reaches zero, at which point the interaction stops.

[0055] The fifth step is to conduct a code quality assessment and generate high-quality code. Figure 1 S5 section:

[0056] S5. Conduct a non-functional quality assessment on the improved code to determine the final high-quality code to be generated.

[0057] Specifically, non-functional metrics are evaluated on the final code, including analysis of maintainability, complexity, and other indicators, to improve the final code.

[0058] Specifically, such as Figure 5 The diagram illustrates the evaluation and analysis of code generation at three levels during this implementation: the class skeleton code, the large language model-optimized class skeleton code, and the code after static analysis to detect and optimize errors. CLOC, CM-LOC, WMC, RFC, and CBO refer to the number of lines of code, the number of lines of code without comments, the weighted number of methods per class, the number of responses per class, and the coupling between objects, respectively. The results show that the code skeleton has less code, fewer explanatory comments for the generated code, fewer methods, and moderate coupling. The final optimized code, however, significantly increases the amount of code and comments, as well as the number of methods and responses, while further reducing coupling.

[0059] Based on the example results, the method described in this application can successfully generate the target code corresponding to the requirements and functional descriptions.

[0060] The technical solution of the present invention has been described above with reference to the preferred embodiments shown in the accompanying drawings. However, it will be readily understood by those skilled in the art that the scope of protection of the present invention is obviously not limited to these specific embodiments. Without departing from the principles of the present invention, those skilled in the art can make equivalent changes or substitutions to the relevant technical features, and the technical solutions after such changes or substitutions will all fall within the scope of protection of the present invention.

Claims

1. A code generation and quality optimization method based on static analysis and large language models, characterized in that, Includes the following steps: S1. UML model definition, mapping and verification: Complete the initial code framework mapping and verification of the model based on the requirements and functional descriptions; The UML model definition, mapping, and validation include: Based on UML forward engineering, the UML model is mapped to the target implementation language, defining class structure, method responsibilities, relationships between classes, and basic code framework; Class diagrams are used to map class definitions, package structures, attributes, interfaces, enumerations, and basic methods, and the generated code is initially verified to ensure consistency. S2. Set up a refined prompt word project. Based on the large language model, set up a refined prompt word strategy, use natural language to interact with the model, and generate class-level code after preliminary completion. The refined prompt word setting project includes: Through various prompting strategies, including input semantics, output customization, error recognition, prompt improvement, interaction optimization, and context control, fine-grained guidance is provided for the code generation process; In the design of prompt words, optimization is carried out based on three dimensions: accuracy, security and generalization ability of code generation, in order to improve the class-level code generated in step S1 and achieve preliminary completion of the code structure; S3. Perform static code analysis on the obtained class-level code to complete the construction of the abstract syntax tree, entity and dependency analysis, and scan for erroneous call issues in the code; To check for erroneous calls, the scope is first validated to obtain the entity after static analysis. The entity is a method, function, or other code block in the code. Then, it is checked whether the entity is of type scope, i.e., whether it belongs to a specific scope. If the entity is of type scope, existence validation is further performed to check whether the entity contains call calls. This requires identifying whether there are calls to other methods or functions inside the entity. Then, all call calls contained in the entity are obtained. Next, parameter and return value compatibility validation is performed to determine whether the entity called by the entity exists. This ensures that the called method or function is valid in the code library and records the erroneous call. If the called entity does not exist, the erroneous call is recorded. S4. Iterative code improvement based on quality feedback; including: Based on the erroneous calls identified in step S3 and other static analysis feedback results, the code errors are repaired and optimized through multiple rounds of interaction with the large language model using a refined prompt word method. The generated modifications are applied to the initial code, and then the static analysis and error checking in step S3 are repeated until erroneous calls and other static check issues are eliminated; through continuous interaction and context control, the code is iteratively improved, ultimately generating high-quality code; S5. Non-functional quality assessment to determine the final high-quality code to be generated. The assessment includes maintainability, cyclomatic complexity, and performance metrics. If the assessment results do not meet the predetermined standards, the system continues to interact with the large language model for optimization, ultimately generating high-quality code that meets the quality requirements.

2. The code generation and quality optimization method based on static analysis and large language models as described in claim 1, characterized in that, In step S3, the static code analysis completes the construction of the abstract syntax tree, entity and dependency analysis, and scans for erroneous call issues in the code, including: Static analysis is performed on the preliminary class-level code generated in step S2 to construct the corresponding abstract syntax tree (AST) and parse the entities and dependencies in the code. Static analysis is used to determine whether entity calls in the code are valid. This involves identifying erroneous calls by checking whether the called entity exists in its class, thus ensuring the correctness of the code's structure and dependencies.

3. The code generation and quality optimization method based on static analysis and large language models as described in claim 1, characterized in that, In step S5, a non-functional quality assessment is performed on the improved code to determine the final high-quality code to be generated, including: After completing the error call check, a non-functional quality assessment is performed on the code, including an evaluation of the code's maintainability, cyclomatic complexity, performance, and security metrics. If the evaluation results do not meet the predetermined standards, further interaction is conducted through the large language model to optimize and improve the code, ultimately generating high-quality code that meets the predetermined non-functional quality requirements.

4. The code generation and quality optimization method based on static analysis and large language models according to claim 1, characterized in that, The large language model supports context management, which can adjust the generated results based on interaction records and problem feedback, improving the consistency and continuity of the generated code.

5. The code generation and quality optimization method based on static analysis and large language model according to claim 1, characterized in that, Use the code entity dependency analysis tool ENRE to customize analysis rules and error detection standards according to the characteristics of different languages.

6. The code generation and quality optimization method based on static analysis and large language model according to claim 1, characterized in that, The non-functional quality assessment supports user-customized configuration, including adjustments to the weights of performance and security metrics to meet specific scenario requirements.

Citation Information

Patent Citations

  • Code generation method and system based on large language model

    CN118210489A

  • Bidirectional intelligent question and answer code supplementing method and system based on large language model

    CN118426825A