Code generation method and device, equipment and storage medium
Patent Information
- Application Number
- CN202610993014.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-07-03
- Publication Date
- 2026-10-30
AI Technical Summary
[0004]本发明的主要目的在于提供了一种代码生成方法、装置、设备及存储介质,旨在解决现有人工手动编码开发方案,因依赖人工编写全部代码且编码风格不统一,而导致的框架结构缺乏一致性、框架代码与业务逻辑代码边界模糊相互耦合难以分离维护,以及面对复杂业务场景时编码出错概率高、无法兼顾框架稳定性与业务灵活性的问题
在本发明实施例中,首先,基于目标代码工程的描述数据和预设代码模板,通过预设代码生成器生成目标代码工程的第一代码。其中,描述数据用于定义目标代码工程的代码结构和设计资产,设计资产用于生成实现代码结构所定义的代码框架的代码,预设代码生成器用于基于预设代码模板和预设填充规则生成代码,预设填充规则用于将描述数据映射至预设代码模板中对应的占位符。基于预设的代码编写规范和描述数据生成提示词,并将提示词输入目标模型,得到目标模型输出的目标代码工程的第二代码。目标模型用于基于对提示词的语义理解,生成实现描述数据所描述的业务功能的代码。在生成第一代码和第二代码后,通过抽象语法树分别解析第一代码和第二代码,得到第一抽象语法树和第二抽象语法树,并基于第一抽象语法树和第二抽象语法树,将第二代码中用于实现业务逻辑的代码拼装到第一代码中,得到目标代码。
Smart Images

Figure CN123011203A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a code generation method, apparatus, device, and storage medium. Background Technology
[0002] Currently, software engineering code is typically developed manually. Specifically, developers manually write all the code for the entire project, including the framework code and the logic implementation code for each business function module, based on the project's design documents.
[0003] However, different developers have different coding styles, leading to a lack of consistency in the code project's framework structure. Furthermore, the boundaries between the framework code and the logic implementation code for business functions become blurred and tightly coupled during manual writing, making reliable separation and maintenance difficult later on. In addition, manually writing all the code is inefficient, has a high error rate when facing complex business scenarios, and cannot flexibly adapt to the logic implementation requirements of different business scenarios while ensuring the stability and reliability of the framework structure. Summary of the Invention
[0004] The main objective of this invention is to provide a code generation method, apparatus, device, and storage medium, which aims to solve the problems of existing manual coding development schemes, which rely on manual writing of all code and inconsistent coding styles, resulting in inconsistent framework structure, blurred boundaries between framework code and business logic code, difficulty in separation and maintenance, high probability of coding errors in complex business scenarios, and inability to balance framework stability and business flexibility.
[0005] In a first aspect, embodiments of this disclosure provide a code generation method, including: Based on the description data and preset code template of the target code project, a first code for the target code project is generated through a preset code generator; wherein, the description data is used to define the code structure and design assets of the target code project; the design assets are used to generate code that implements the code framework defined by the code structure; the preset code generator is used to generate code based on the preset code template and preset fill rules; the preset fill rules are used to map the description data to the corresponding placeholders in the preset code template; Based on preset coding standards and the description data, prompt words are generated, and the prompt words are input into the target model to obtain the second code of the target code project output by the target model; the target model is used to generate code that implements the business function described by the description data based on the semantic understanding of the prompt words. The first code and the second code are parsed using abstract syntax trees to obtain a first abstract syntax tree and a second abstract syntax tree. Based on the first abstract syntax tree and the second abstract syntax tree, the code in the second code used to implement the business logic is assembled into the first code to obtain the target code.
[0006] Secondly, embodiments of this disclosure provide a code generation apparatus, the apparatus comprising: The first generation module is used to generate the first code of the target code project based on the description data and preset code template of the target code project, through a preset code generator; wherein, the description data is used to define the code structure and design assets of the target code project; the design assets are used to generate code that implements the code framework defined by the code structure; the preset code generator is used to generate code based on the preset code template and preset filling rules; the preset filling rules are used to map the description data to the corresponding placeholders in the preset code template; The second generation module is used to generate prompt words based on preset code writing specifications and the description data, and input the prompt words into the target model to obtain the second code of the target code project output by the target model; the target model is used to generate code that implements the business function described by the description data based on the semantic understanding of the prompt words. The third generation module is used to parse the first code and the second code respectively through the abstract syntax tree to obtain the first abstract syntax tree and the second abstract syntax tree, and based on the first abstract syntax tree and the second abstract syntax tree, to assemble the code in the second code used to implement the business logic into the first code to obtain the target code.
[0007] Thirdly, embodiments of this disclosure provide an electronic device, including: a processor; and a memory configured to store computer-executable instructions, which, when executed, cause the processor to perform the steps of the method described in the first aspect above.
[0008] Fourthly, embodiments of this disclosure provide a computer-readable storage medium for storing computer-executable instructions that, when executed by a processor, implement the steps of the method described in the first aspect.
[0009] Fifthly, embodiments of this disclosure provide a computer program product, the computer program product including a computer program, which, when executed by a processor, implements the steps of the method described in the first aspect above.
[0010] The at least one technical solution provided by the embodiments of the present invention can achieve the following technical effects: In this embodiment of the invention, firstly, based on the description data and preset code template of the target code project, a first code for the target code project is generated using a preset code generator. The description data defines the code structure and design assets of the target code project; the design assets generate code that implements the code framework defined by the code structure; and the preset code generator generates code based on the preset code template and preset fill rules, which map the description data to corresponding placeholders in the preset code template. Based on preset code writing specifications and the description data, prompt words are generated and input into the target model to obtain the second code for the target code project output by the target model. The target model generates code that implements the business function described by the description data based on the semantic understanding of the prompt words. After generating the first and second codes, the first and second codes are parsed using abstract syntax trees (ASTs) to obtain a first AST and a second AST. Based on the first and second ASTs, the code in the second code used to implement the business logic is assembled into the first code to obtain the target code.
[0011] This invention, through a pre-defined code generator based on pre-defined filling rules, uniformly generates framework structure code, ensuring consistency of the framework structure at the generation mechanism level. This ensures clear separation of the framework structure code and business logic code during the generation stage, effectively overcoming the technical problems of inconsistencies in framework structure, blurred boundaries and mutual coupling between the two types of code caused by manual writing in existing solutions, and difficulties in reliable separation and maintenance later. Furthermore, by generating business logic code based on semantic understanding using a target model, while maintaining a stable and reliable framework structure, it flexibly adapts to the logic implementation requirements of different business scenarios, effectively solving the technical problems of low efficiency and high probability of coding errors when manually writing all code in complex business scenarios. In addition, by parsing the first and second codes using an abstract syntax tree and assembling the business logic code into the framework structure code, the separately generated framework structure code and business logic code can be automatically and reliably integrated, thereby generating a complete, executable code. Attached Figure Description
[0012] Figure 1 This is one of the flowcharts illustrating the code generation method provided in an embodiment of the present invention; Figure 2 This is a schematic diagram of a scenario for the code generation method provided in an embodiment of the present invention; Figure 3 This is a schematic diagram of the AST architecture of the code generation method provided in the embodiments of the present invention; Figure 4 A second schematic flowchart of the code generation method provided in an embodiment of the present invention; Figure 5A schematic diagram of the module composition of the code generation device 500 provided in an embodiment of the present invention; Figure 6 This is a schematic diagram of the hardware structure of an electronic device provided in one embodiment of the present invention. Detailed Implementation
[0013] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below in conjunction with specific embodiments and corresponding drawings. Obviously, the described embodiments are only a part of the embodiments of this invention, and not all of them. Based on the embodiments of this invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this invention.
[0014] The technical solutions provided by the various embodiments of the present invention will be described in detail below with reference to the accompanying drawings.
[0015] Please see Figure 1 , Figure 1 This is one of the flowcharts illustrating the simulation test environment construction method provided in this embodiment of the invention, such as... Figure 1 As shown, the method includes the following steps: Step 102: Based on the description data and preset code template of the target code project, generate the first code of the target code project through the preset code generator; wherein, the description data is used to define the code structure and design assets of the target code project; the design assets are used to generate code that implements the code framework defined by the code structure; the preset code generator is used to generate code based on the preset code template and preset filling rules; the preset filling rules are used to map the description data to the corresponding placeholders in the preset code template.
[0016] Step 104: Generate prompt words based on preset code writing specifications and description data, and input the prompt words into the target model to obtain the second code of the target code project output by the target model; the target model is used to generate code that implements the business functions described in the description data based on the semantic understanding of the prompt words.
[0017] Step 106: Parse the first code and the second code using the abstract syntax tree to obtain the first abstract syntax tree and the second abstract syntax tree. Based on the first abstract syntax tree and the second abstract syntax tree, assemble the code in the second code used to implement the business logic into the first code to obtain the target code.
[0018] In this embodiment of the invention, a first code for the target code project can be generated using a preset code generator based on the target code project's description data and a preset code template. The description data can be used to define the code structure and design assets of the target code project. The design assets can be used to generate code that implements the code framework defined by the code structure. The preset code generator can be used to generate code based on the preset code template and preset fill rules. The preset fill rules can be used to map the description data to corresponding placeholders in the preset code template.
[0019] Specifically, descriptive data can be defined by the user during detailed design on the platform. For example, code structure can include the scope and service interfaces of microservices: the scope of a microservice defines the boundaries of the business functions it covers; the service interface can define its input and output parameters: input parameters define the data types, formats, and meanings received by the service interface; output parameters define the data types, formats, and meanings returned by the service interface. Design assets can include request message structures, error codes, data standards, data models, and system configurations. The request message structure defines the data message format for interaction between the service interface and external systems; error codes define the encoding and meaning of various exceptions and error states in the microservice project; data standards can include global reusable objects, sets, constants, and enumerations: global reusable objects refer to data structures shared by multiple service interfaces; sets refer to aggregate structures composed of multiple data items of the same type; constants refer to immutable data values used globally in the microservice project; enumerations refer to types composed of a set of fixed named values; the data model can include table structures and business entities: table structures define the field names, field types, and field constraints of data tables in the database; business entities define the attributes and relationships of core business objects in the business domain; system configuration can include communication protocols, technical components, and response parameters: communication protocols define the protocol types and parameters used for external communication by the microservice; technical components define the technical frameworks and common components on which the microservice project depends; response parameters define the unified response format and structure of the service interface. These descriptive data can collectively constitute the basic input for subsequent code generation.
[0020] In one example, the pre-defined code generator can be a set of Java components that generate code based on its own data model. The pre-defined code templates can be implemented using FreeMarker template technology, and the templates can pre-define the structural framework of various code files in a microservice project. Specifically, the pre-defined code templates consist of multiple template files, which are organized according to the standard directory structure and code layering specifications of microservice projects. Each template file can correspond to a specific code file in the project, such as a service interface class template, a data model class template, a configuration file template, etc. Each template file contains placeholders. These placeholders are markers in the template that indicate where descriptive data needs to be filled in. Pre-defined filling rules can be used to map descriptive data to the corresponding placeholders in the pre-defined code templates.
[0021] During execution, the code generator can query the asset data designed by the user, including microservice scope, service interface definitions, input / output parameters, request message structure, error codes, data standards, data models, and system configurations. Then, according to preset filling rules, it maps and fills these descriptive data into the corresponding placeholder positions in the code template. The filling rules define the correspondence between each type of descriptive data and the template placeholders. For example, the interface name encoding corresponds to the class name placeholder in the service interface class file of the template, the input parameter definition corresponds to the method parameter list placeholder in the template, and the field definition in the data model corresponds to the attribute placeholder in the entity class of the template. The code generator can then map the descriptive data to the corresponding placeholders sequentially according to the filling rules, generating the first code of the target code project. This first code is the framework code or scaffolding code of the microservice, which can contain class definitions and method definitions for various asset classes. Class definitions can include declarations such as class name, package path, inheritance relationship, and interface implementation. Method definitions can include declarations such as method name, parameter list, return type, and method annotations.
[0022] For example, when generating code for a service interface class, the code generator can read the interface name code and the definition data of input and output parameters from the service interface design. Then, it can fill the interface name code into the class name placeholder in the service interface class file of the template, fill the input parameter definition data into the method parameter list placeholder in the template, and fill the output parameter definition data into the method return type placeholder in the template. This will generate service interface class code containing the correct class name, method signature, and parameter definitions.
[0023] In the first code snippet, the business logic implementation within the method body is blank. The method body may only contain placeholder code, such as statements to return a null value or throw unimplemented exceptions. The actual business logic processing statements within the method body are not yet filled in. In other words, the first code snippet provides a complete, runnable project skeleton, but the business logic method bodies within the skeleton are empty and need to be filled in later. The role of design assets is to generate the code framework defined by the implementation code structure, that is, the infrastructure code that supports the entire microservice project.
[0024] In this embodiment of the invention, prompt words can be generated based on preset code writing specifications and descriptive data. These prompt words are then input into a target model to obtain the second code of the target code project output by the target model. The target model can be used to generate code that implements the business functions described in the descriptive data based on the semantic understanding of the prompt words.
[0025] Specifically, users can perform detailed design and define related assets on the platform, including microservices, service interfaces, request message structures, error codes, data standards, data models, and system configurations. For example, when designing a service interface, users can input a detailed description of the business service function or draw a logical flow diagram. The detailed description of the business service function can be a natural language description of the business logic that the service interface needs to implement, which may include business rules, processing flow, data validation requirements, exception handling strategies, etc. The logical flow diagram can be a graphical description of the execution flow of the business logic, including decision branches, loop processing, and exception paths. It should be noted that the above-mentioned detailed description of the business service function or the drawing of the logical flow diagram is not mandatory, but the more detailed the input, the more accurate the generated code will be.
[0026] In one example, when a user selects a microservice and triggers the generation and download of its business logic code, the platform's AI (Artificial Intelligence) coding assistant can begin working. The AI coding assistant first reads preset coding standards. These standards can include general coding standards and business coding standards. General coding standards, such as Java coding standards and microservice coding standards, can uniformly constrain Java code naming rules, comment formats, exception handling patterns, logging methods, etc. Business coding standards, such as the development standards for a company's financial system, can specifically constrain coding styles, object conversion methods, data validation patterns, etc., for specific business domains. General coding standards and business coding standards can have a hierarchical relationship; business coding standards can superimpose enterprise- or project-level specific constraints on top of general coding standards. When there is a conflict between the two, the constraints of the business coding standards take precedence.
[0027] In one example, prompts can be generated based on preset coding standards and descriptive data. Specifically, the target design asset for generating the second code can be determined from the design assets defined in the descriptive data. The AI coding assistant can query the design definition data corresponding to the asset based on the asset data placeholders required in the context. The placeholders can be predefined markers in the code template. The AI coding assistant can parse the placeholder markers in the current context to determine the scope of assets involved in generating the business logic code, thereby identifying the target design asset. Then, the target descriptive data corresponding to the target design asset is extracted from the descriptive data.
[0028] For example, if business logic code needs to be generated for a certain service interface, the target design asset is the definition related to that service interface. The target description data includes the interface name code, the types and meanings of input parameters, and the types and meanings of output parameters. Input parameter types can include basic data types and custom object types, and the meaning of input parameters can be a textual description of what information the parameter represents in the business context. The types and meanings of output parameters are similar. Assembling the target description data with preset coding standards yields prompt words. The assembly method can be to use the coding constraints in the coding standards as the first part of the prompt words, and the target description data as the second part, organizing the two parts together according to a predetermined structure to form structured prompt words. The prompt words contain both the constraints of the coding standards on code style, naming, structure, exception handling, logging, etc., and specific design data for the large model to understand the business context.
[0029] In this embodiment of the invention, after assembling the prompt words, the prompt words can be input into the target model. After receiving the prompt words, the target model can parse the coding specification requirements and business context information in the prompt words based on its semantic understanding capabilities.
[0030] It should be noted that the target model is not a necessary component of the embodiments of this invention. The embodiments of this invention do not include a large model by default, nor do they depend on a large model from a specific brand or manufacturer. The embodiments of this invention send structured prompts to the target model, which then generates code based on its semantic understanding of the prompts and returns it. This interaction process only involves text input and output, and the interface is generic, making no assumptions about the internal implementation of the target model. Therefore, the target model can be any large language model provided by any vendor, such as any commercial or open-source large model, as long as the model has the ability to generate code based on the semantic understanding of the prompts. The embodiments of this invention do not limit the specific type, brand, version, or internal architecture of the target model; users can flexibly choose the large model to access according to their actual needs.
[0031] In one example, the target model can first parse the coding convention constraints in the prompt words to understand the naming rules, code structure requirements, exception handling patterns, etc. Then, it can parse the design data to understand the business definition of the service interface, the meaning and relationships of input and output parameters. Finally, it can combine the results of the two parts to generate business logic code that conforms to the coding conventions and implements the corresponding business functions. This code is then returned as the second code to the AI coding assistant.
[0032] In one example, see Figure 2 This illustrates one of the processes for generating the second code described above. Figure 2 In the "Deposits" microservice, the user defines a service interface named "Query User," coded as `queryUser`. The input field is an `id` parameter of type `java.lang.long`, marked as nullable. This design data corresponds to the aforementioned target asset design. After the user triggers code generation, the AI coding assistant generates prompts based on the service interface's design data and pre-configured coding standards, and inputs these prompts into the main model. The main model returns the `QueryUserImpl` service implementation class, which implements the `QueryUser` interface and is annotated with `@Service`. Database operations are performed by injecting the `BaseDao` component, generating complete business logic in the `execute` method, encompassing three steps: constructing query conditions, executing the query, and transforming the results. Figure 2 The input box at the bottom center displays the multi-turn dialogue entry point mentioned above. Users can enter adjustment commands at the dialogue entry point to iteratively optimize the generated code.
[0033] In one example, after obtaining the second code output by the target model, user feedback on the second code can be received. Specific types of feedback can include modification suggestions, supplementary explanations, and error correction instructions. Modification suggestions can be user requests to adjust certain parts of the generated code, such as changing the name of a variable or adjusting the order of certain processing logic; supplementary explanations can be user elaborations on business logic, such as adding boundary conditions or handling for exceptions; error correction instructions can be user-pointed errors in the generated code and their correct implementation methods. Then, updated prompts can be generated based on the feedback, descriptive data, and generated prompts. The updated prompts can use the user's feedback as new contextual information and be integrated with the original descriptive data and prompts. During integration, modification suggestions, supplementary explanations, or error correction instructions from the feedback can be appended to or replace corresponding parts of the original prompts, forming updated prompts that include the user's feedback. The updated prompts can then be input back into the target model to obtain the updated second code output by the target model. Since the updated second code is regenerated from the updated prompts, it better reflects the user's feedback requirements. Through multiple rounds of interaction, the generated business logic code can be gradually optimized to better meet user expectations and business needs. The multi-round interaction can terminate once the user confirms satisfaction with the code or subsequent syntax validation passes.
[0034] In this embodiment of the invention, after generating the first code and the second code, the first code and the second code can be parsed using an abstract syntax tree (AST) to obtain a first AST and a second AST. Then, based on the first AST and the second AST, the code in the second code used to implement business logic can be assembled into the first code to obtain the target code.
[0035] Due to the inherent instability of AI-generated code, it can be either complete code (including class definitions and method declarations) or purely logic-based code (consisting only of business logic statements within method bodies, excluding class definitions and method declarations). The structure of complete code in an Abstract Syntax Tree (AST) can include a root class definition node, which can contain method declaration nodes, which can then contain method body nodes, and finally, business logic statement nodes. In contrast, logic-based code in an AST only contains method body nodes and their internal business logic statement nodes, lacking outer class definition and method declaration nodes. Furthermore, logic-based code may lack fixed templates or anchor points, making it impossible to assemble using text truncation or similar methods. Therefore, this example uses an AST to parse both types of code before assembling them. Regardless of the AI-generated code format, the structured parsing capabilities of the AST accurately identify and extract business logic code fragments at the AST level, perform syntax verification after integration, and ultimately produce complete code.
[0036] In one example, code assembly can be performed based on a first abstract syntax tree (BLS) and a second BLS. Specifically, based on the first BLS, the first method node in the first code with an empty method body can be identified. This identification can be done by traversing the nodes of the first BLS and checking if each method node's child nodes contain business logic statement nodes. If a method node's method body child nodes only contain placeholder code for a return statement or placeholder code for throwing an unimplemented exception, without actual business logic statement nodes, then that method node can be identified as the first method node. The first code is framework code generated by the code generator based on a template, where all method bodies are blank. Therefore, in the first BLS, all method nodes with empty method bodies can be found, and business logic code can be subsequently filled into the method bodies corresponding to these method nodes. Based on the second BLS, second method nodes in the second code whose method bodies contain business logic implementation statements can be identified. The identification method can be achieved by traversing the nodes of the second abstract syntax tree and checking whether the child nodes of each method node's method body contain business logic statement nodes, such as variable assignment statement nodes, conditional statement nodes, loop statement nodes, method call statement nodes, etc. If these nodes are present, then the method node can be identified as a second method node. The second code is the business logic code generated by the large code model, and its method bodies contain specific business logic implementation statements.
[0037] In one example, the correspondence between the first method node and the second method node can be determined based on the description data, and node pairs can be constructed based on this correspondence. The description data can record the method name and the service interface information to which each service interface belongs. This information allows the establishment of a mapping relationship between the first method node and the second method node. Specifically, the class name and method name of the first method node and the method name of the second method node can be extracted. These are then matched with the service interface definitions recorded in the description data. When the method names of the first and second method nodes are the same and both belong to the same method of the same service interface defined in the description data, a correspondence is formed, and thus a node pair can be constructed. A node pair can include the first method node and the second method node corresponding to the same design asset defined in the description data. For any node pair, the method body code of the second method node in the node pair can be assembled into the method body of the first method node in the node pair, resulting in an assembled abstract syntax tree (AST). The assembly operation can be a node replacement operation performed at the AST level, replacing empty method body child nodes in the first method node with method body child nodes in the second method node. After the replacement, the AST subtree of the first method node contains the complete business logic implementation. The essence of this operation is to fill the corresponding blank positions in the framework code generated by the code generator with the business logic code fragments generated by the AI, so as to achieve the structured fusion of the two codes at the AST level. The assembled abstract syntax tree maintains a complete syntax structure.
[0038] In one example, the second code can be complete code, meaning it includes not only the business logic statements within the method body but also the complete class definition structure and method declaration structure. When the second code contains both class definition and method declaration structures, class definition nodes and method declaration nodes can be discarded based on the second abstract syntax tree (AST). This discarding operation can be performed at the AST level, removing these nodes so they no longer participate in subsequent assembly. Method nodes within the second code that contain business logic implementation statements are then identified as second method nodes. After this processing, regardless of whether the AI model generates complete code or only logic code, the business logic code fragments at the method body level can be uniformly extracted, providing consistent input for subsequent assembly operations.
[0039] Please see Figure 3 , Figure 3 This is a schematic diagram of the AST assembly architecture provided in an embodiment of the present invention. Figure 3As shown in the diagram, the architecture diagram illustrates the complete process of parsing and assembling the scaffolding code and AI logic code using an AST (Abstract Syntax Tree). On the left is the scaffolding service class code. After parsing by the AST parser, this code structure includes package information, framework import information, class definitions, and method definitions. The class definition includes the name, inheritance or implementation relationship, and public parameters, while the method definition includes the method name and input / output parameter information. On the right is the AI logic class code. After parsing by the AST parser, this code structure includes logic import information, class definitions, method definitions, and method implementations. The class definition includes the name, inheritance or implementation relationship, and public parameters, while the method definition includes the method name and input / output parameter information. The method implementation contains the business logic code generated by the AI. The assembling of the syntax block selectively merges the AST nodes on both sides and allocates them according to their source: package information, framework import information, class definitions, and method definitions are taken from the scaffolding code side; logic import information and method implementations are taken from the AI logic code side. The assembled merged class code contains a complete code structure, including package information, framework import information, and logic import information, as well as class definitions, method definitions, and method implementations, achieving a complete code combination encompassing the framework structure and business logic. After assembly, the merged class code can be further validated to ensure the syntactic correctness of the assembled result.
[0040] In one example, after obtaining the assembled Abstract Syntax Tree (AST) and before generating target code based on it, a syntax check can be performed on the AST. If the syntax check passes, target code can be generated based on the AST. The process of restoring Java source code from the assembled AST can be as follows: First, perform a depth-first traversal of the AST, serializing each AST node into the corresponding source code text according to Java syntax rules. If the syntax check fails, an error message is generated to prompt the user to adjust the second code. The error message can include the location of the syntax error, the error type, and the error description. The user can locate the specific syntax error based on the error message, locate the corresponding code segment in the second code, modify the second code through a multi-turn dialogue mode, and then reassemble and check until the syntax check passes. A closed loop is formed between syntax check, multi-turn dialogue modification, and reassembly. The user gradually corrects the problems in the second code based on the error messages of each check, and reassembles and checks after each correction until the syntax check finally passes. After passing syntax validation, target code can be generated based on the assembled abstract syntax tree. The target code is the final code containing the complete project framework and business logic implementation. Users can directly deploy the target code to the development environment for use, or debug and customize it based on it.
[0041] In one example, see Figure 4 , Figure 4 This is a flowchart illustrating the code generation method provided in an embodiment of the present invention. Figure 4 As shown, the specific steps of this process are as follows: In step 1, users can perform detailed design through the designer, defining microservices, service interfaces (including service logic descriptions), request message structures, error codes, data standards, data models, and system configurations. These design assets together constitute the description data and serve as the deliverables of the detailed design phase. After entering the low-code phase, the process is divided into two parallel paths. The first path is the scaffolding code generation path, starting from step 2: the code generator reads the design data defined by the user in the detailed design phase. In step 3, the code generator, based on preset code templates and preset filling rules, fills the asset data into the placeholders of the corresponding templates to generate scaffolding code. The scaffolding code is organized into four layers according to the standard layered structure of microservice engineering: the interface layer contains interface definition code and message definition code; the service layer contains business model code and logic merging code; the infrastructure layer contains technical framework code and persistence layer code; and the common configuration layer contains data standard code and error code code. Among them, the logic merging code of the service layer reserves blank spaces in the method body for subsequent reception of assembled business logic code. The second path is the business logic code generation path. In step 4, the platform's AI coding assistant reads prompt words from the built-in prompt context, which includes general coding standards and business coding standards. Simultaneously, in step 5, the platform's AI coding assistant reads design data and queries the corresponding design definition data based on the asset data placeholders in the context. Steps 4 and 5 can be parallel operations. In step 6, the platform's AI coding assistant assembles the read development specification data and the queried design data into a complete prompt word and calls the large code model. In step 7, the large code model completes the logic code generation and outputs the business logic code. In step 8, code integration is performed, assembling the business logic code generated in step 7 into the scaffolding code generated in step 3. The target assembly location is the blank space in the method body of the logic merging code in the service layer. After integration, the assembly result is syntax-validated, and finally, the complete target code containing the engineering framework and business logic is output.
[0042] In this embodiment of the invention, firstly, based on the description data and preset code template of the target code project, a first code for the target code project is generated using a preset code generator. The description data defines the code structure and design assets of the target code project; the design assets generate code that implements the code framework defined by the code structure; and the preset code generator generates code based on the preset code template and preset fill rules, which map the description data to corresponding placeholders in the preset code template. Based on preset code writing specifications and the description data, prompt words are generated and input into the target model to obtain the second code for the target code project output by the target model. The target model generates code that implements the business function described by the description data based on the semantic understanding of the prompt words. After generating the first and second codes, the first and second codes are parsed using abstract syntax trees (ASTs) to obtain a first AST and a second AST. Based on the first and second ASTs, the code in the second code used to implement the business logic is assembled into the first code to obtain the target code.
[0043] This invention, through a pre-defined code generator based on pre-defined filling rules, uniformly generates framework structure code, ensuring consistency of the framework structure at the generation mechanism level. This ensures clear separation of the framework structure code and business logic code during the generation stage, effectively overcoming the technical problems of inconsistencies in framework structure, blurred boundaries and mutual coupling between the two types of code caused by manual writing in existing solutions, and difficulties in reliable separation and maintenance later. Furthermore, by generating business logic code based on semantic understanding using a target model, while maintaining a stable and reliable framework structure, it flexibly adapts to the logic implementation requirements of different business scenarios, effectively solving the technical problems of low efficiency and high probability of coding errors when manually writing all code in complex business scenarios. In addition, by parsing the first and second codes using an abstract syntax tree and assembling the business logic code into the framework structure code, the separately generated framework structure code and business logic code can be automatically and reliably integrated, thereby generating a complete, executable code.
[0044] Figure 5 The code generation device 500 shown can achieve Figure 1 The method described in the embodiment achieves the same technical effect, and can be specifically referred to in the above description. Figure 1 The code generation method of the illustrated embodiment will not be described in detail here. The code generation apparatus 500 includes: The first generation module 501 is used to generate the first code of the target code project based on the description data and preset code template of the target code project, through a preset code generator; wherein, the description data is used to define the code structure and design assets of the target code project; the design assets are used to generate code that implements the code framework defined by the code structure; the preset code generator is used to generate code based on the preset code template and preset filling rules; the preset filling rules are used to map the description data to the corresponding placeholders in the preset code template; The second generation module 502 is used to generate prompt words based on preset code writing specifications and the description data, and input the prompt words into the target model to obtain the second code of the target code project output by the target model; the target model is used to generate code that implements the business function described by the description data based on the semantic understanding of the prompt words. The third generation module 503 is used to parse the first code and the second code respectively through the abstract syntax tree to obtain the first abstract syntax tree and the second abstract syntax tree, and based on the first abstract syntax tree and the second abstract syntax tree, to assemble the code in the second code used to implement the business logic into the first code to obtain the target code.
[0045] Optionally, the third generation module 503 is used for: Based on the first abstract syntax tree, identify the first method node in the first code whose method body is empty; Based on the second abstract syntax tree, identify the second method node in the second code that contains business logic implementation statements within the method body; Based on the description data, the correspondence between the first method node and the second method node is determined, and node pairs are constructed based on the correspondence; wherein, the node pair includes a first method node and a second method node corresponding to the same design asset defined by the description data; For any node pair, the method body code of the second method node in the node pair is assembled into the method body of the first method node in the node pair to obtain the assembled abstract syntax tree. The target code is generated based on the assembled abstract syntax tree.
[0046] Optionally, the device further includes ( Figure 5 (not shown in the image) The receiving module 504 is used to receive user feedback information on the second code before the code for implementing business logic in the second code is assembled into the first code based on the first abstract syntax tree and the second abstract syntax tree to obtain the target code. The fourth generation module 505 is used to generate updated prompt words based on the feedback information, the description data, and the prompt words; The acquisition module 506 is used to input the updated prompt words into the target model and obtain the updated second code output by the target model; The third generation module 503 is used for: Based on the first abstract syntax tree and the second abstract syntax tree, the code used to implement business logic in the updated second code is assembled into the first code to obtain the target code.
[0047] Optionally, the third generation module 503 is used for: In the case where the second code contains a class definition structure and a method declaration structure, based on the second abstract syntax tree, the class definition nodes and method declaration nodes in the second code are discarded, and the method nodes in the method body of the second code that contain business logic implementation statements are identified as the second method nodes.
[0048] Optionally, the device further includes ( Figure 5 (not shown in the image) Verification module 507 is used to perform syntax verification on the assembled abstract syntax tree before generating the target code based on the assembled abstract syntax tree; The fifth generation module 508 is used to generate an error message when the syntax check fails; the error message is used to prompt the user to adjust the second code.
[0049] Optionally, the first generation module 502 is used for: From the design assets defined by the description data, determine the target design asset for generating the second code; Extract the target description data corresponding to the target design asset from the description data; The target description data is assembled with the preset code writing specifications to obtain the prompt words.
[0050] In this embodiment of the invention, firstly, based on the description data and preset code template of the target code project, a first code for the target code project is generated using a preset code generator. The description data defines the code structure and design assets of the target code project; the design assets generate code that implements the code framework defined by the code structure; and the preset code generator generates code based on the preset code template and preset fill rules, which map the description data to corresponding placeholders in the preset code template. Based on preset code writing specifications and the description data, prompt words are generated and input into the target model to obtain the second code for the target code project output by the target model. The target model generates code that implements the business function described by the description data based on the semantic understanding of the prompt words. After generating the first and second codes, the first and second codes are parsed using abstract syntax trees (ASTs) to obtain a first AST and a second AST. Based on the first and second ASTs, the code in the second code used to implement the business logic is assembled into the first code to obtain the target code.
[0051] This invention, through a pre-defined code generator based on pre-defined filling rules, uniformly generates framework structure code, ensuring consistency of the framework structure at the generation mechanism level. This ensures clear separation of the framework structure code and business logic code during the generation stage, effectively overcoming the technical problems of inconsistencies in framework structure, blurred boundaries and mutual coupling between the two types of code caused by manual writing in existing solutions, and difficulties in reliable separation and maintenance later. Furthermore, by generating business logic code based on semantic understanding using a target model, while maintaining a stable and reliable framework structure, it flexibly adapts to the logic implementation requirements of different business scenarios, effectively solving the technical problems of low efficiency and high probability of coding errors when manually writing all code in complex business scenarios. In addition, by parsing the first and second codes using an abstract syntax tree and assembling the business logic code into the framework structure code, the separately generated framework structure code and business logic code can be automatically and reliably integrated, thereby generating a complete, executable code.
[0052] Figure 6 This is a schematic diagram of the structure of an electronic device provided in one embodiment of the present invention. Please refer to it. Figure 6 At the hardware level, the electronic device includes a processor, and optionally also includes an internal bus, a network interface, and memory. The memory may include main memory, such as high-speed random-access memory (RAM), or non-volatile memory, such as at least one disk drive. Of course, the electronic device may also include other hardware required for other business operations.
[0053] The processor, network interface, and memory can be interconnected via an internal bus, which can be an ISA (Industry Standard Architecture) bus, a PCI (Peripheral Component Interconnect) bus, or an EISA (Extended Industry Standard Architecture) bus, etc. This bus can be divided into address bus, data bus, control bus, etc. For ease of representation, Figure 6 The symbol is represented by a single double-headed arrow, but this does not mean that there is only one bus or one type of bus.
[0054] Memory is used to store programs. Specifically, programs may include program code, which includes computer operation instructions. Memory may include main memory and non-volatile memory, and provides instructions and data to the processor.
[0055] The processor reads the corresponding computer program from non-volatile memory into main memory and then runs it, forming a code generation device at the logical level. The processor executes the program stored in memory and specifically performs the following operations: Based on the description data and preset code template of the target code project, a first code for the target code project is generated through a preset code generator; wherein, the description data is used to define the code structure and design assets of the target code project; the design assets are used to generate code that implements the code framework defined by the code structure; the preset code generator is used to generate code based on the preset code template and preset fill rules; the preset fill rules are used to map the description data to the corresponding placeholders in the preset code template; Based on preset coding standards and the description data, prompt words are generated, and the prompt words are input into the target model to obtain the second code of the target code project output by the target model; the target model is used to generate code that implements the business function described by the description data based on the semantic understanding of the prompt words. The first code and the second code are parsed using abstract syntax trees to obtain a first abstract syntax tree and a second abstract syntax tree. Based on the first abstract syntax tree and the second abstract syntax tree, the code in the second code used to implement the business logic is assembled into the first code to obtain the target code.
[0056] The above is as described in the present invention. Figure 1The code generation method disclosed in the embodiments described above can be applied to a processor or implemented by a processor. The processor may be an integrated circuit chip with signal processing capabilities. During implementation, each step of the above method can be completed by integrated logic circuits in the processor's hardware or by instructions in software form. The processor can be a general-purpose processor, including a Central Processing Unit (CPU), a Network Processor (NP), etc.; it can also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field-Programmable Gate Array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the methods, steps, and logic block diagrams disclosed in one or more embodiments of the present invention. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the methods disclosed in one or more embodiments of the present invention can be directly embodied in the execution of a hardware decoding processor, or executed by a combination of hardware and software modules in the decoding processor. The software module can reside in a mature storage medium in the field, such as random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, or registers. This storage medium is located in memory, and the processor reads information from the memory and, in conjunction with its hardware, completes the steps of the above method.
[0057] The electronic device can also perform Figure 1 The code generation method described herein will not be elaborated further here.
[0058] This invention also provides a computer-readable storage medium that stores one or more programs, the programs including instructions that, when executed by a portable electronic device including multiple applications, enable the portable electronic device to perform... Figure 1 The method of the illustrated embodiment will not be described in detail here.
[0059] This invention also provides a computer program product stored in a storage medium and executed by at least one processor to implement... Figure 1 The method of the illustrated embodiment will not be described in detail here.
[0060] Of course, in addition to the software implementation, the electronic device of the present invention does not exclude other implementation methods, such as logic devices or a combination of software and hardware, etc. That is to say, the execution subject of the following processing flow is not limited to each logic unit, but can also be hardware or logic devices.
[0061] In summary, the above description is merely a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of one or more embodiments of the present invention should be included within the scope of protection of one or more embodiments of the present invention.
[0062] The systems, devices, modules, or units described in the above embodiments can be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer. Specifically, a computer can be, for example, a personal computer, laptop computer, cellular phone, camera phone, smartphone, personal digital assistant, media player, navigation device, email device, game console, tablet computer, wearable device, or any combination of these devices.
[0063] Computer-readable media, including both permanent and non-permanent, removable and non-removable media, can store information using any method or technology. Information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined in this embodiment of the invention, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.
[0064] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0065] The various embodiments in this invention are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the system embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions in the method embodiments.
Claims
1. A code generation method, characterized in that, The method includes: Based on the description data and preset code template of the target code project, a first code for the target code project is generated through a preset code generator; wherein, the description data is used to define the code structure and design assets of the target code project; the design assets are used to generate code that implements the code framework defined by the code structure; the preset code generator is used to generate code based on the preset code template and preset fill rules; the preset fill rules are used to map the description data to the corresponding placeholders in the preset code template; Based on preset coding standards and the description data, prompt words are generated, and the prompt words are input into the target model to obtain the second code of the target code project output by the target model; the target model is used to generate code that implements the business function described by the description data based on the semantic understanding of the prompt words. The first code and the second code are parsed using abstract syntax trees to obtain a first abstract syntax tree and a second abstract syntax tree. Based on the first abstract syntax tree and the second abstract syntax tree, the code in the second code used to implement the business logic is assembled into the first code to obtain the target code.
2. The method according to claim 1, characterized in that, The step of assembling the code used to implement business logic in the second code into the first code based on the first abstract syntax tree and the second abstract syntax tree includes: Based on the first abstract syntax tree, identify the first method node in the first code whose method body is empty; Based on the second abstract syntax tree, identify the second method node in the second code that contains business logic implementation statements within the method body; Based on the description data, the correspondence between the first method node and the second method node is determined, and node pairs are constructed based on the correspondence; wherein, the node pair includes a first method node and a second method node corresponding to the same design asset defined by the description data; For any node pair, the method body code of the second method node in the node pair is assembled into the method body of the first method node in the node pair to obtain the assembled abstract syntax tree. The target code is generated based on the assembled abstract syntax tree.
3. The method according to claim 1, characterized in that, Before assembling the code for implementing business logic in the second code into the first code based on the first abstract syntax tree and the second abstract syntax tree to obtain the target code, the method further includes: Receive user feedback regarding the second code; Based on the feedback information, the description data, and the prompt words, an updated prompt word is generated; The updated prompt word is input into the target model to obtain the updated second code output by the target model; The step of assembling the code used to implement business logic in the second code into the first code based on the first abstract syntax tree and the second abstract syntax tree to obtain the target code includes: Based on the first abstract syntax tree and the second abstract syntax tree, the code used to implement business logic in the updated second code is assembled into the first code to obtain the target code.
4. The method according to claim 2, characterized in that, The step of identifying second method nodes in the second code that contain business logic implementation statements within their method bodies, based on the second abstract syntax tree, includes: In the case where the second code contains a class definition structure and a method declaration structure, based on the second abstract syntax tree, the class definition nodes and method declaration nodes in the second code are discarded, and the method nodes in the method body of the second code that contain business logic implementation statements are identified as the second method nodes.
5. The method according to claim 2, characterized in that, Before generating the target code based on the assembled abstract syntax tree, the method further includes: Perform syntax validation on the assembled abstract syntax tree; If the syntax check fails, an error message is generated; the error message is used to prompt the user to adjust the second code.
6. The method according to claim 1, characterized in that, The generation of prompt words based on preset code writing standards and the description data includes: From the design assets defined by the description data, determine the target design asset for generating the second code; Extract the target description data corresponding to the target design asset from the description data; The target description data is assembled with the preset code writing specifications to obtain the prompt words.
7. A code generation device, characterized in that, The device includes: The first generation module is used to generate the first code of the target code project based on the description data and preset code template of the target code project, through a preset code generator; wherein, the description data is used to define the code structure and design assets of the target code project; the design assets are used to generate code that implements the code framework defined by the code structure; the preset code generator is used to generate code based on the preset code template and preset filling rules; the preset filling rules are used to map the description data to the corresponding placeholders in the preset code template; The second generation module is used to generate prompt words based on preset code writing specifications and the description data, and input the prompt words into the target model to obtain the second code of the target code project output by the target model; the target model is used to generate code that implements the business function described by the description data based on the semantic understanding of the prompt words. The third generation module is used to parse the first code and the second code respectively through the abstract syntax tree to obtain the first abstract syntax tree and the second abstract syntax tree, and based on the first abstract syntax tree and the second abstract syntax tree, to assemble the code in the second code used to implement the business logic into the first code to obtain the target code.
8. An electronic device, characterized in that, include: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program, when executed by the processor, implements the steps of the method as described in any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium is used to store computer-executable instructions that, when executed by a processor, implement the steps of the method described in any one of claims 1 to 6.
10. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, implements the steps of the method described in any one of claims 1 to 6.
