CN-121979511-A - Code generation method of application program, electronic device, medium and program product
Abstract
The invention discloses a code generation method of an application program, electronic equipment, a medium and a program product, and relates to the technical field of financial science and technology. The method comprises the steps of obtaining an output first code segment in the process of generating a target application program code in response to a code generation request containing service function description and compliance constraint conditions, carrying out static analysis on the first code segment based on preset code defect rules, identifying safety and precision defects existing in the first code segment, modifying the first code segment based on a preset repair template corresponding to the safety and precision defects to obtain a repaired first code segment, generating a second code segment based on the repaired first code segment, and continuously executing the operation of carrying out static analysis on the second code segment based on the preset code defect rules until the generated code does not detect the safety and precision defects, and outputting the application program code. The scheme of the invention can realize high-quality automatic generation of codes.
Inventors
- HE JIAJUN
- ZHENG ZELIN
Assignees
- 中国工商银行股份有限公司
Dates
- Publication Date
- 20260505
- Application Date
- 20260213
Claims (10)
- 1. A method for generating code for an application program, the method comprising: acquiring an output first code segment in the process of generating a target application program code in response to a code generation request containing a service function description and compliance constraints; Performing static analysis on the first code segment based on a preset code defect rule, identifying the safety and precision defects existing in the first code segment, and modifying the first code segment based on a preset repair template corresponding to the safety and precision defects to obtain a repaired first code segment; generating a second code segment based on the repaired first code segment; And continuously executing the operation of carrying out static analysis on the second code segment based on a preset code defect rule until no security and precision defects are detected on the generated code, and outputting the application program code.
- 2. The code generation method of an application program according to claim 1, wherein in generating the target application program code in response to the code generation request including the business function description and the compliance constraint condition, acquiring the outputted first code segment includes: Receiving code segments from a code output stream of a code generation agent; Upon detecting that the code segment constitutes a complete syntax element, determining the code segment as the first code segment; Wherein the complete syntax element comprises at least one of a function definition, a control structure block, and a transaction block.
- 3. The method for generating code for an application program according to claim 1, wherein said performing static analysis on said first code segment based on a preset code defect rule, identifying security and accuracy defects existing therein, comprises: Loading a preset lexical analyzer and a grammar analyzer according to a programming language adopted by the target application program, analyzing the first code segment and generating an abstract grammar tree; Traversing nodes of the abstract syntax tree, and matching syntax structures of the nodes with defect modes defined in a preset code defect rule; When the target node is matched with the defect mode, determining that the code corresponding to the target node has security and precision defects, and recording defect type and position information.
- 4. A method of generating code for an application according to claim 3, wherein traversing the nodes of the abstract syntax tree matches the syntax structure of each node with a defect pattern defined in a predetermined code defect rule, comprising: traversing each node of the abstract syntax tree to obtain node characteristics of each node; Comparing each node characteristic with a preset defect characteristic, and determining the target node as a node to be repaired under the condition that the target node is determined to be matched with the preset defect characteristic; Wherein the node characteristics include at least one of a node type, a textual representation of a current node in source code, an operator, and path information from an abstract syntax tree root node to the current node.
- 5. The method for generating a code for an application program according to claim 4, wherein said modifying the first code segment based on a preset repair template corresponding to the security and precision defect to obtain a repaired first code segment includes: obtaining a repair template associated with preset defect characteristics matched with the node to be repaired; determining the grammar node type required by the father node for the position of the child node according to the father node type of the node to be repaired and the position of the node to be repaired in the child node sequence; Generating a repaired node meeting the structural requirement of the abstract syntax tree based on the repair template and the syntax node type; replacing the node to be repaired with the repaired node to obtain a repaired abstract syntax tree; And calling a code generator corresponding to the application programming language, and converting the repaired abstract syntax tree into codes serving as a repaired first code segment.
- 6. The method for generating code for an application program according to claim 1, wherein said generating a second code segment based on said repaired first code segment comprises: Inputting the repaired first code segment into a code generation agent, and determining each associated code segment associated with the repaired first code segment; determining items to be modified of each associated code segment based on modification content in the repaired first code segment, and modifying the items to be modified to obtain repaired associated code segments; And generating a second code segment based on the repaired first code segment and the repaired associated code segment.
- 7. The method for generating a code of an application program according to claim 1, wherein the operation of continuing to perform static analysis of the second code segment based on a preset code defect rule until no security and accuracy defect is detected for the generated code, and outputting the application program code, comprises: performing static analysis on the second code segment based on a preset code defect rule; When the safety and precision defects are identified, modifying the second code segment based on a preset repair template associated with preset defect characteristics matched with the defects, and generating a next code segment based on the modified second code segment; Generating a next code segment based on the second code segment when no security and accuracy defects are identified; When the newly generated code segment does not detect the security and precision defect and no subsequent code needs to be generated, the application program code composed of all the generated code segments is output.
- 8. An electronic device, the electronic device comprising: At least one processor; and a memory communicatively coupled to the at least one processor; Wherein the memory stores a computer program executable by the at least one processor to enable the at least one processor to perform the code generation method of the application of any one of claims 1-7.
- 9. A computer readable storage medium storing computer instructions for causing a processor to execute the code generation method of the application program of any one of claims 1-7.
- 10. A computer program product comprising a computer program which, when executed by a processor, implements a code generation method of an application program according to any of claims 1-7.
Description
Code generation method of application program, electronic device, medium and program product Technical Field The present invention relates to the technical field of financial science and technology, and in particular, to a code generation method, an electronic device, a medium, and a program product for an application program. Background With the popularity of artificial intelligence (ARTIFICIAL INTELLIGENCE, AI) assisted programming techniques, developers are increasingly using AI tools to automatically generate code fragments. However, AI-generated codes are not deterministic in quality, often do not meet the coding specifications, security requirements and performance standards of the project, resulting in a significant amount of time being devoted to subsequent inspection and modification. At present, code generation and quality inspection are two independent serial links, namely, firstly, an AI code generation tool outputs codes according to functional requirements, then the generated codes are input into a static code analysis tool to detect defects based on preset rules, and if problems are detected, the problems are repaired manually. The serial flow has the problems of long feedback period, low automation degree, serious dependence on manual intervention and the like, and high-quality automatic generation of codes cannot be realized. Disclosure of Invention The invention provides a code generation method, electronic equipment, medium and program product of an application program, which are used for solving the problems of long feedback period, low automation degree and serious dependence on manual intervention of a serial flow which is generated before checking, and can realize high-quality automatic generation of codes. According to an aspect of the present invention, there is provided a code generation method of an application program, the method including: acquiring an output first code segment in the process of generating a target application program code in response to a code generation request containing a service function description and compliance constraints; Performing static analysis on the first code segment based on a preset code defect rule, identifying the safety and precision defects existing in the first code segment, and modifying the first code segment based on a preset repair template corresponding to the safety and precision defects to obtain a repaired first code segment; generating a second code segment based on the repaired first code segment; And continuously executing the operation of carrying out static analysis on the second code segment based on a preset code defect rule until no security and precision defects are detected on the generated code, and outputting the application program code. According to another aspect of the present invention, there is provided a code generation apparatus of an application program, the apparatus including: The first code segment output module is used for acquiring the output first code segment in the process of generating the target application program code in response to the code generation request containing the service function description and the compliance constraint condition; the first code segment repairing module is used for carrying out static analysis on the first code segment based on a preset code defect rule, identifying the safety and precision defects existing in the first code segment, and modifying the first code segment based on a preset repairing template corresponding to the safety and precision defects to obtain a repaired first code segment; a second code segment generation module for generating a second code segment based on the repaired first code segment; And the application program code output module is used for continuously executing the operation of carrying out static analysis on the second code segment based on the preset code defect rule until the generated code does not detect the safety and precision defect, and outputting the application program code. According to another aspect of the present invention, there is provided an electronic apparatus including: At least one processor; and a memory communicatively coupled to the at least one processor; Wherein the memory stores a computer program executable by the at least one processor to enable the at least one processor to perform the code generation method of the application program according to any one of the embodiments of the present invention. According to another aspect of the present invention, there is provided a computer readable storage medium storing computer instructions for causing a processor to execute a code generation method of an application program according to any embodiment of the present invention. According to another aspect of the present invention, there is provided a computer program product comprising a computer program which, when executed by a processor, implements a code generation method of an application program according to