Method and system for mixed retention of service file content and intelligent merging of code fragments
By parsing the microservice interface source data to generate an abstract syntax tree with annotated method names, and combining it with Swagger configuration details, the problems of low efficiency and poor accuracy in service file merging in the existing technology are solved, and efficient and accurate code merging and upgrading are achieved.
Patent Information
- Application Number
- CN202510984873.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-17
- Publication Date
- 2025-09-16
- Estimated Expiration
- 2045-07-17
AI Technical Summary
Existing technologies are inefficient and error-prone when merging different versions of service files. They are difficult to handle complex code structures, and automated tools find it difficult to accurately understand the semantics and contextual relationships of the code, resulting in semantic errors or logical vulnerabilities in the merged code.
By parsing the source data of the microservice interface, extracting structured information, generating an abstract syntax tree with annotated method names, combining Swagger configuration details, adding annotations, and efficiently merging new code snippets with the original content, using semantic analysis and conflict detection algorithms to ensure code accuracy and consistency.
It realizes the automatic generation and efficient merging of microservice interface codes, improves development efficiency, reduces the risk of errors, ensures the maintainability and documentation consistency of the code, and achieves smooth upgrades and iterations.
Smart Images

Figure CN120491979B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of software development, and in particular relates to a method and system for mixed retention of service file contents and intelligent merging of code fragments. Background Art
[0002] During software development, service files often need to be modified and updated, for example, to add new features, fix vulnerabilities, or optimize code structures. Traditional code merging methods often rely on manual operations, which are inefficient, error-prone, and difficult to handle complex code structures. For example, when merging different versions of code, developers need to manually compare code differences to determine which parts to retain and which to update. This is not only time-consuming and labor-intensive, but can also easily lead to code conflicts or functional anomalies due to human negligence.
[0003] While some automated code merging tools exist, these tools are typically based on simple text matching or syntax analysis, making it difficult to accurately understand the semantics and context of the code. This can lead to semantic errors or logical vulnerabilities in the merged code. For example, when processing abstract syntax trees, only simple node replacements can be performed, without intelligent adjustments based on the code's semantics and business logic, which affects the correctness and maintainability of the code. Therefore, how to provide an efficient and accurate method for preserving mixed service file content and intelligently merging code snippets has become a pressing issue in the current software development field. Summary of the Invention
[0004] In response to the shortcomings of the existing technology, the present invention proposes a method and system for mixed retention of service file content and intelligent merging of code snippets. By parsing the source data of the microservice interface, structured information is extracted; the structured information is converted into an abstract syntax tree, the syntax element type is identified and the method name is deduced and confirmed, and an abstract syntax tree with the method name annotated is generated; the API path and Swagger configuration details are generated according to the interface details, and annotations are added to the abstract syntax tree method nodes to fill in the Swagger configuration; a new compilable code snippet is generated and the target method node is located; the target node is replaced with a temporary abstract syntax tree of the new code snippet to generate a new service file text; the new code snippet is merged with the original content and written back to the service file; the automatic generation and efficient merging of microservice interface code is realized, thereby improving development efficiency and code consistency.
[0005] To achieve the above object, the present invention provides the following technical solutions:
[0006] Methods for preserving mixed service file content and intelligently merging code snippets include:
[0007] Parse the microservice interface source data and extract structured information; the structured information includes the microservice path prefix, group list data, and interface details;
[0008] Convert the structured information into an abstract syntax tree, traverse the abstract syntax tree nodes to identify the syntax element type corresponding to each node, deduce the method name, and generate an abstract syntax tree annotated with the method name;
[0009] The microservice path prefix and interface details generate the API path, and the Swagger configuration details are generated in combination with the Swagger specification. The annotation is added to the abstract syntax tree of the annotated method name to obtain the abstract syntax tree with annotations.
[0010] Convert the annotated abstract syntax tree into a new code snippet, parse it into a temporary abstract syntax tree, and then replace the target node in the abstract syntax tree of the service file to generate a new service file text;
[0011] Identify the content that needs to be retained in the new service file, merge the new and old code using semantic analysis and conflict detection algorithms, and write the merged code back to the service file;
[0012] The step of converting the structured information into an abstract syntax tree, traversing the abstract syntax tree nodes to identify the syntax element type corresponding to each node, and deducing the method name to generate an abstract syntax tree annotated with the method name includes:
[0013] Mapping the extracted structured information to a predefined abstract syntax tree node model;
[0014] Based on the mapped structured information, a corresponding abstract syntax tree node is created for each syntax element, and the abstract syntax tree nodes are connected according to the code logic and hierarchical relationship to form an initial abstract syntax tree; the abstract syntax tree nodes include class nodes, method nodes, parameter nodes, and variable nodes;
[0015] Perform syntax analysis on each node of the initial abstract syntax tree, check the attributes and structural features of the node, match them with predefined syntax element types, and identify the syntax element type corresponding to each node;
[0016] Generate a method name based on the interface name and function description in the interface details, combined with the syntax element type and context information, and annotate the generated method name to the corresponding method node to obtain an abstract syntax tree annotated with the method name;
[0017] The process of converting the annotated abstract syntax tree into a new code snippet, parsing the code into a temporary abstract syntax tree, and replacing the target node in the abstract syntax tree of the service file to generate a new service file text includes:
[0018] Determine a code generation template based on the structure of the abstract syntax tree carrying annotations and the Swagger annotations; the code generation template includes the structure of the code, the syntax format, and the insertion position of the annotations;
[0019] Based on the code generation template, the nodes and edges of the abstract syntax tree carrying annotations are converted into corresponding code statements to generate new code snippets;
[0020] Use the syntax parser to parse the generated new code snippet, generate a temporary abstract syntax tree, and perform syntax verification and semantic analysis on the temporary abstract syntax tree;
[0021] Locating a target node in the abstract syntax tree of the service file and replacing the target node with a corresponding node in the temporary abstract syntax tree to generate a new service file text; the target node is the code portion that needs to be updated or replaced;
[0022] The process of identifying the content to be retained in the new service file, merging the new and old codes using semantic analysis and conflict detection algorithms, and writing the merged code back into the service file includes:
[0023] Perform semantic analysis on the new service file text, identify retained content, and establish identifiers and indexes for the retained content; the retained content includes business logic code, Swagger configuration details, and Swagger annotations;
[0024] Perform semantic analysis on the new and old codes to understand the functions and logical relationships of the codes, and use conflict detection algorithms to detect conflict points between the new and old codes; the conflict points include syntactic conflicts, semantic conflicts, and logical conflicts;
[0025] Merge the new and old codes using conflict resolution strategies based on the conflict type and severity;
[0026] The merged code is formatted and optimized, and an update log is generated. At the same time, the time of code merging, operation content and operator information are recorded, and the merged code is written into the service file.
[0027] Specifically, the process of identifying the syntax element type corresponding to each node includes:
[0028] Obtain an initial abstract syntax tree and prepare a predefined syntax element type library, including definitions and characteristics of classes, methods, parameters, and variable types in the programming language;
[0029] Use a depth-first search algorithm to traverse each node in the initial abstract syntax tree;
[0030] For each node, extract its attributes and structural features;
[0031] Matching the extracted node attributes and structural features with a predefined library of grammatical element types; the matching process is implemented by a rule engine;
[0032] Identify the corresponding syntax element type for each node and attach the type information to the node.
[0033] Specifically, the microservice path prefix and interface details generate an API path, and Swagger configuration details are generated in combination with the Swagger specification. Annotations are added to the abstract syntax tree of the annotated method name to obtain an abstract syntax tree with annotations, including:
[0034] Combine the microservice path prefix with the interface name in the interface details to form the API path format, and parameterize the API path;
[0035] Generate a JSON configuration file based on the API path, the request method, parameter list, and return value type in the interface details, and follow the Swagger specification. Add the Swagger configuration details to the configuration file. The Swagger configuration details include the interface description, parameter description, and response example information.
[0036] Generate the corresponding Swagger annotation content based on the information in the Swagger configuration details, associate the Swagger annotation with the method node in the abstract syntax tree that annotates the method name, add the Swagger annotation to the corresponding method node, and generate an abstract syntax tree with the annotation.
[0037] Specifically, based on the API path, combined with the request method, parameter list, and return value type in the interface details, a corresponding JSON format configuration file is generated in accordance with the requirements of the Swagger specification, and Swagger configuration details are added to the configuration file, including:
[0038] Collect the API path of the microservice interface, which is a combination of the path prefix and the interface name, and organize the interface details;
[0039] Introduce Swagger related dependencies into the project;
[0040] Create a Swagger configuration class, enable Swagger support, and configure basic information for the Swagger document;
[0041] Use Swagger annotations to describe interface details on controller classes and methods;
[0042] After starting the project, Swagger automatically scans the interfaces with Swagger annotations in the project and generates corresponding configuration files in JSON format.
[0043] Specifically, the parsed source data is achieved through regular expression matching or semantic parsing technology; the group list data includes module grouping, function grouping and version grouping information of the microservice; the interface details include interface name, request method, parameter list and return value type; the parameter list includes path parameters, query parameters, and request body parameters.
[0044] Service file content mixed retention and code snippet intelligent merging system, including: information extraction module, abstract syntax tree annotation module, annotation addition module, code parsing module, replacement and conversion module, merging and writing back module;
[0045] The information extraction module is used to parse the microservice interface source data and extract structured information, including microservice path prefixes, group list data, and interface details;
[0046] The abstract syntax tree annotation module is used to convert the extracted structured information into an abstract syntax tree according to the grammatical rules of the target programming language, traverse the abstract syntax tree nodes to identify the syntax element types, and deduce and confirm the method name based on the interface details to obtain an abstract syntax tree annotated with the method name;
[0047] The annotation adding module is used to generate the API path according to the microservice path prefix and interface details, and generate the Swagger configuration details according to the interface details and Swagger specifications. At the same time, it adds method annotations to the method nodes in the abstract syntax tree of the annotated method name, and fills the Swagger configuration details into the annotation attributes in combination with the API path to obtain a complete method node abstract syntax tree with annotations;
[0048] The code parsing module is used to generate a compilable new code snippet text from the abstract syntax tree of the complete method node carrying the annotation according to the language rules, read the content of the compilable new code snippet text, parse it into the abstract syntax tree of the service file, and search for the target method node that matches the interface name;
[0049] The replacement and conversion module is used to parse the new code snippet into a temporary abstract syntax tree, replace the target node in the abstract syntax tree of the service file, and reversely convert the replaced abstract syntax tree into the new service file text;
[0050] The merging and writing back module is used to identify the original content that needs to be retained in the new service file and the location where the new code snippet text can be inserted, merge the new code snippet text with the original content in the service file, and write the merged code back to the service file.
[0051] The annotation adding module includes: an API path generating unit, a Swagger configuration generating unit, and an annotation adding unit;
[0052] The API path generation unit is used to generate an API path according to the microservice path prefix and interface details;
[0053] The Swagger configuration generation unit is used to generate Swagger configuration details according to the interface details and the Swagger specification;
[0054] The annotation adding unit is used to add method annotations to the method nodes in the abstract syntax tree that annotates the method name and fill in the Swagger configuration details.
[0055] Compared with the prior art, the present invention has the following beneficial effects:
[0056] 1. The present invention proposes a system for mixed retention of service file content and intelligent merging of code fragments, and optimizes and improves the architecture, operation steps and processes. The system has the advantages of simple process, low investment and operation costs, and low production work costs.
[0057] 2. The present invention proposes a method for mixed retention of service file content and intelligent merging of code snippets. By automatically parsing the microservice interface source data and extracting structured information, an abstract syntax tree with method names annotated is generated, which significantly simplifies the generation process of microservice interface code and improves development efficiency. At the same time, the API path and Swagger configuration details are generated based on the interface details, and annotations are added to the method nodes to ensure that the generated code complies with the Swagger specification, thereby improving the maintainability of the code and the consistency of the document.
[0058] 3. The present invention proposes a method for mixed retention of service file content and intelligent merging of code snippets, which efficiently merges newly generated code snippets with the original content in the service file, ensuring the accuracy and completeness of code updates. This process not only reduces manual intervention and reduces the risk of errors, but also retains the original content that needs to be retained in the service file, realizing the smooth upgrade and iteration of the microservice interface code. BRIEF DESCRIPTION OF THE DRAWINGS
[0059] Figure 1 Schematic diagram of the method for mixed retention of service file content and intelligent merging of code snippets of the present invention;
[0060] Figure 2 This is a flow chart showing the principle of the method for mixed retention of service file content and intelligent merging of code fragments of the present invention;
[0061] Figure 3 This is a system architecture diagram for the mixed retention of service file content and intelligent merging of code snippets in the present invention. DETAILED DESCRIPTION
[0062] Example 1
[0063] See also Figure 1 and Figure 2 The present invention provides an embodiment of a method for mixed retention of service file content and intelligent merging of code fragments, the method comprising steps S1 to S5, including the following steps:
[0064] S1: parse the source data of the microservice interface and extract structured information;
[0065] The structured information includes microservice path prefix, group list data, and interface details;
[0066] The group list data includes module grouping, function grouping and version grouping information of the microservice;
[0067] The interface details include interface name, request method, parameter list and return value type;
[0068] The parameter list includes path parameters, query parameters, and request body parameters;
[0069] The parsing of source data is achieved by regular expression matching or semantic parsing technology.
[0070] S2: Convert the structured information into an abstract syntax tree, traverse the abstract syntax tree nodes to identify the syntax element type corresponding to each node, deduce the method name, and generate an abstract syntax tree annotated with the method name;
[0071] S3: Generates the API path based on the microservice path prefix and interface details, generates Swagger configuration details based on the Swagger specification, and adds annotations to the abstract syntax tree of the annotated method name to obtain an abstract syntax tree with annotations.
[0072] S4: Convert the annotated abstract syntax tree into a new code snippet, parse it into a temporary abstract syntax tree, and then replace the target node in the abstract syntax tree of the service file to generate a new service file text;
[0073] S5: Identify the content that needs to be retained in the new service file, use semantic analysis and conflict detection algorithms to merge the new and old codes, and write the merged code back to the service file.
[0074] The specific steps of S2 include:
[0075] S2.1: Map the extracted structured information to a predefined abstract syntax tree node model;
[0076] S2.2: Based on the mapped structured information, create a corresponding abstract syntax tree node for each syntax element, and connect the abstract syntax tree nodes according to the code logic and hierarchical relationship to form an initial abstract syntax tree; the abstract syntax tree nodes include class nodes, method nodes, parameter nodes, and variable nodes;
[0077] Furthermore, the specific steps of S2.2 include:
[0078] (1) Obtaining structured information after parsing;
[0079] (2) Mapping the structured information to a predefined abstract syntax tree node model. This step involves mapping the interface name, parameters, return value, and other information in the interface details to the attributes of the abstract syntax tree node;
[0080] (3) Based on the mapped structured information, create a corresponding abstract syntax tree node for each syntax element. The abstract syntax tree nodes include but are not limited to:
[0081] The class node represents the class to which the interface belongs;
[0082] Method nodes represent specific methods of an interface;
[0083] Parameter nodes represent method parameters;
[0084] Variable nodes represent variables used in methods;
[0085] (4) According to the code logic and hierarchical relationship, connect the abstract syntax tree nodes to obtain the initial abstract syntax tree, in which the method node is connected to the class node to which it belongs; the parameter node is connected to the method node to which it belongs; and the variable node is connected to the method node or class node to which it belongs.
[0086] S2.3: Perform syntax analysis on each node of the initial abstract syntax tree. By checking the attributes and structural features of the node and matching them with predefined syntax element types, the syntax element type corresponding to each node is identified.
[0087] For example, by analyzing the keywords, identifiers and other information of the node, we can determine whether the node is a class node, a method node or another type of node. This step clarifies the grammatical properties of each node in the abstract syntax tree and provides information for generating method names.
[0088] Furthermore, the specific steps of S2.3 include:
[0089] (1) Obtain the initial abstract syntax tree and prepare a predefined syntax element type library, including the definitions and characteristics of classes, methods, parameters, and variable types in the programming language;
[0090] (2) traversing each node in the initial abstract syntax tree using a depth-first search algorithm. The depth-first search algorithm is a prior art in this field and is not an inventive solution of the present application, and is not described in detail here.
[0091] (3) For each node, extract its attributes and structural features, for example:
[0092] Class nodes include class name, inheritance relationship, and member variables;
[0093] The method node includes the method name, parameter list, return value type, and access modifier;
[0094] The parameter node includes parameter name, parameter type, and whether it is optional;
[0095] A variable node includes variable name, variable type, and scope.
[0096] (4) matching the extracted node attributes and structural features with a predefined library of grammatical element types; the matching process is implemented by a rule engine;
[0097] (5) Identify the corresponding syntax element type for each node and attach the type information to the node.
[0098] S2.4: Generate a method name based on the interface name and function description in the interface details, combined with the syntax element type and context information, and annotate the generated method name to the corresponding method node to obtain an abstract syntax tree annotated with the method name.
[0099] Furthermore, the specific steps of S2.4 include:
[0100] (1) Obtain interface details and parse the interface name and function description. For example, the interface name contains the operation type, and the function description further explains the function of the interface;
[0101] (2) Obtain the initial abstract syntax tree containing the identified syntax element types, and obtain the syntax element types and context information of the method nodes;
[0102] (3) Based on the interface name and function description, combined with the syntax element type and context information, a natural language processing method is used to generate a method name. The natural language processing method is a prior art in this field and is not an inventive solution of this application, so it will not be described in detail here;
[0103] (4) Mark the generated method name to the corresponding method node and verify whether the generated method name complies with the naming conventions and semantic consistency of the programming language.
[0104] The specific steps of S3 include:
[0105] S3.1: Combine the microservice path prefix with the interface name in the interface details to form the API path format, and parameterize the API path;
[0106] Furthermore, the specific steps of S3.1 include:
[0107] (1) Get the path prefix from the microservice interface configuration and obtain the interface details;
[0108] (2) Combine the microservice path prefix with the interface name to form a complete API path format;
[0109] (3) Parameterize the API path, identify dynamic parameters in the path, and replace them with placeholders;
[0110] (4) Verify whether the generated API path complies with the RESTful API design specifications and output the parameterized API path.
[0111] It should be noted that the RESTful API design specification is a set of architectural principles and constraints used to guide the design of interactions between clients and servers. Furthermore, the RESTful API design specification promotes a development model that separates the front-end and back-end. The back-end is responsible for providing data interfaces, while the front-end is responsible for data rendering and user interaction. This separation allows the front-end and back-end to be developed and deployed independently, improving the flexibility and scalability of the system.
[0112] S3.2: Based on the API path, combined with the request method, parameter list, and return value type in the interface details, generate a corresponding JSON configuration file in accordance with the Swagger specification requirements, and add the Swagger configuration details to the configuration file; the Swagger configuration details include the interface description, parameter description, and response example information;
[0113] Furthermore, the specific steps of S3.2 include:
[0114] (1) Collect the API path of the microservice interface after combining the path prefix and interface name, and organize the interface details;
[0115] (2) Introduce Swagger related dependencies into the project;
[0116] (3) Create a Swagger configuration class, enable Swagger support, and configure the basic information of the Swagger document;
[0117] (4) Use the annotations provided by Swagger to describe interface details on controller classes and methods;
[0118] (5) After starting the project, Swagger automatically scans the interfaces with Swagger annotations in the project and generates corresponding JSON format configuration files.
[0119] S3.3: Generate the corresponding Swagger annotation content based on the information in the Swagger configuration details, associate the Swagger annotation with the method node in the abstract syntax tree that annotates the method name, add the Swagger annotation to the corresponding method node, and generate an abstract syntax tree with the annotation.
[0120] Furthermore, the specific steps of S3.3 include:
[0121] (1) Obtain Swagger configuration details, parse the Swagger configuration details, and extract interface description information;
[0122] (2) Obtain the abstract syntax tree of the annotated method name, including the method node and its syntax element type information;
[0123] (3) Generate the corresponding Swagger annotation content according to the interface description information in the Swagger configuration details;
[0124] (4) Associate the generated Swagger annotations with the method nodes in the abstract syntax tree, and match the method nodes based on the interface path and method name;
[0125] (5) Attach the Swagger annotation to the corresponding method node and update the abstract syntax tree;
[0126] (6) Verify whether the generated abstract syntax tree with annotations meets expectations.
[0127] The specific steps of S4 include:
[0128] S4.1: Determine a code generation template based on the structure of the annotated abstract syntax tree and the Swagger annotation; the code generation template includes the code structure, syntax format, and annotation insertion location;
[0129] The code generation template defines how to convert the nodes and edges of the abstract syntax tree into specific code statements.
[0130] S4.2: Based on the code generation template, convert the nodes and edges of the annotated abstract syntax tree into corresponding code statements to generate new code snippets;
[0131] For example, method nodes are converted into method definition statements, parameter nodes are converted into parameter declaration statements, and Swagger annotations are inserted into corresponding positions. Through this process, new code snippets are generated that reflect the structure and content of the abstract syntax tree.
[0132] Furthermore, the specific steps of S4.2 include:
[0133] (1) Obtain the defined code generation template, which contains the basic structure of the code snippet and placeholders for replacing the actual content. At the same time, ensure that the template supports dynamic content insertion, such as method name, parameter list, return value type, and Swagger annotation;
[0134] (2) Obtain an annotated abstract syntax tree, including method nodes, syntax element type information, and additional Swagger annotations;
[0135] (3) Traverse the abstract syntax tree with annotations and extract detailed information for each method node;
[0136] (4) According to the code generation template, the nodes and edges in the abstract syntax tree are converted into specific code statements, including:
[0137] Method node: convert the method node into a method definition, including the method name, parameter list, and return value type;
[0138] Swagger annotations: insert Swagger annotations into method definitions, usually appearing as Java annotations on methods;
[0139] Edge: In an abstract syntax tree, an edge usually represents the relationship between nodes, such as the containment relationship between classes and methods. In code generation, an edge is represented by code structures such as indentation and braces.
[0140] (5) Use the extracted node information and annotation content to fill the placeholders in the code generation template. For example, insert the method name into the method definition position in the template and insert the Swagger annotation before the method definition.
[0141] (6) Merge the filled template contents to generate a complete code snippet;
[0142] (7) Verify whether the generated code snippet complies with the grammatical specifications and logical requirements, and output a new code snippet.
[0143] S4.3: Use a parser to parse the generated new code snippet, generate a temporary abstract syntax tree, and perform syntax verification and semantic analysis on the temporary abstract syntax tree to ensure that the generated code snippet conforms to the grammatical rules and semantic requirements of the programming language and avoids syntactical or logical errors.
[0144] Furthermore, the specific steps of S4.3 include:
[0145] (1) Select the ANTLR parser based on the programming language and project requirements. At the same time, make sure that the parser supports the grammatical rules of the target programming language.
[0146] (2) Obtain the generated new code snippet, which is usually a source code in the form of a string, and parse the code snippet using a syntax parser to generate a temporary abstract syntax tree containing the syntax structure information of the code snippet;
[0147] (3) Perform syntax checking on the temporary abstract syntax tree to ensure that the code snippet complies with the syntax rules of the programming language, mainly checking for syntax errors such as mismatched brackets and missing statement terminators;
[0148] (4) Perform semantic analysis on the temporary abstract syntax tree to ensure that the code snippet is semantically correct, mainly checking the consistency between variable declaration and usage, type matching, and scoping rules;
[0149] (5) Output the results of syntax verification and semantic analysis, including error messages and confirmation of verification pass.
[0150] S4.4: Locate the target node in the service file abstract syntax tree, and replace the target node with the corresponding node in the temporary abstract syntax tree to generate a new service file text; the target node is the code portion that needs to be updated or replaced.
[0151] The specific steps of S5 include:
[0152] S5.1: Perform semantic analysis on the new service file text, identify retained content, and establish identifiers and indexes for the retained content; the retained content includes business logic code, Swagger configuration details, and Swagger annotations;
[0153] Furthermore, the identifier is a tag or number, and the index records the location information of the retained content in the new service file, so that the retained content can be quickly located and referenced during the merging process.
[0154] S5.2: Perform semantic analysis on the new and old code to understand the code's functions and logical relationships, and use conflict detection algorithms to detect conflicts between the new and old code. These conflicts include syntactic conflicts, semantic conflicts, and logical conflicts.
[0155] Furthermore, grammatical conflicts include duplicate variable names and incompatible grammatical structures; semantic conflicts include the same function but different implementation methods leading to inconsistent results; logical conflicts include contradictory business logic processes.
[0156] Furthermore, the specific steps of S5.2 include:
[0157] (1) Obtain new and old code files or abstract syntax trees, programming language syntax rules, and semantic analysis rule libraries;
[0158] (2) Use the syntax parser to generate an abstract syntax tree, and combine the symbol table and type system for semantic analysis;
[0159] (3) Entering the semantic analysis stage:
[0160] Traverse the abstract syntax tree of new and old codes, collect the definition and usage information of symbols such as variables, functions, and classes, and record the scope, type, life cycle and other attributes of the symbols;
[0161] Verify that the symbol types match, for example, whether the parameter types are consistent with the declaration when calling a function. Analyze the control flow of the code, identify conditional branches and execution paths, and finally, track the definition and use of variables to identify problems such as uninitialized variables and dead code.
[0162] (4) Entering the conflict detection phase:
[0163] Check whether the grammatical structure of the new and old codes is compatible, for example, whether the added or deleted statements destroy the original structure;
[0164] Check whether symbol definitions conflict, for example, whether there are variables or functions with the same name but inconsistent types in the new and old codes;
[0165] Check whether the types are compatible, for example, whether the function return types are consistent;
[0166] (5) Analyze the control flow and data flow to check whether the logic of the new and old codes conflicts, for example: whether the newly added conditional branch covers the original logic; whether the modified variable value affects the correctness of the original logic;
[0167] (6) List all detected conflict points, including conflict type, location, and detailed description;
[0168] (7) Provide repair solutions based on the conflict type, such as renaming variables, adjusting type declarations, and modifying control flow.
[0169] S5.3: Merge the old and new code using conflict resolution strategies based on the conflict type and severity.
[0170] Furthermore, for grammatical conflicts, they can be resolved by renaming variables and adjusting the grammatical structure; for semantic conflicts, it is necessary to deeply analyze the code functions for implementation, or integrate the implementation methods; for logical conflicts, it is necessary to re-organize the business logic to ensure that the merged code logic is correct and consistent.
[0171] Furthermore, the conflict type and severity are determined based on whether variables with the same name exist in the new and old codes but have the same types, whether function return types match, and whether newly added conditional branches override the original logic.
[0172] Furthermore, conflict resolution strategies include:
[0173] (1) When there is a syntax conflict, symbols are forced to be modified or renamed;
[0174] (2) When there is a semantic conflict, adjust the type or interface declaration;
[0175] (3) When there is a logic conflict, merge the logic branches or retain the original logic.
[0176] Furthermore, the conflict resolution process includes:
[0177] (1) Obtain conflict report, including conflict type, location, and description;
[0178] (2) Analyze conflict reports and extract conflict information;
[0179] (3) Classification according to conflict type and severity;
[0180] (4) Select appropriate resolution strategies for each conflict type;
[0181] (5) Modify new or old code to resolve conflicts;
[0182] (6) Re-perform semantic analysis and conflict detection to ensure that the conflict has been resolved.
[0183] Furthermore, the process of merging the old and new codes includes:
[0184] For conflicts of low severity, such as logic conflicts, retain the original code logic;
[0185] For high-severity conflicts, such as syntax conflicts, replace or add new code to resolve the conflict.
[0186] S5.4: Beautify and optimize the format of the merged code, generate an update log, record the time of code merging, operation content and operator information, and write the merged code into the service file.
[0187] Furthermore, the merged code is formatted and optimized, including: adjusting the code indentation, line breaks, spaces and other formats according to the programming language specifications and the team's code style requirements to make the code more readable. At the same time, the code is optimized, such as removing redundant code, simplifying complex logic, etc., to improve the performance and maintainability of the code.
[0188] The operations include which code parts were merged and which conflicts were resolved.
[0189] Example 2
[0190] See also Figure 3 Another embodiment provided by the present invention is a system for mixed retention of service file content and intelligent merging of code fragments, comprising:
[0191] Information extraction module, abstract syntax tree annotation module, annotation adding module, code parsing module, replacement and conversion module, merging and writing back module;
[0192] The information extraction module is used to parse the microservice interface source data and extract structured information, including microservice path prefixes, group list data, and interface details;
[0193] The abstract syntax tree annotation module is used to convert the extracted structured information into an abstract syntax tree according to the grammatical rules of the target programming language, traverse the abstract syntax tree nodes to identify the syntax element types, and deduce and confirm the method names based on the interface details to obtain an abstract syntax tree annotated with the method names;
[0194] The annotation adding module is used to generate the API path based on the microservice path prefix and interface details, and generate the Swagger configuration details based on the interface details and Swagger specifications. At the same time, it adds method annotations to the method nodes in the abstract syntax tree of the annotated method name, and fills the Swagger configuration details into the annotation attributes in combination with the API path to obtain a complete method node abstract syntax tree with annotations;
[0195] The code parsing module is used to generate a compilable new code snippet text from the abstract syntax tree of the complete method node carrying annotations according to language rules, read the content of the compilable new code snippet text, parse it into the abstract syntax tree of the service file, and find the target method node that matches the interface name;
[0196] A replacement and conversion module, configured to parse the new code snippet into a temporary abstract syntax tree, replace the target node in the abstract syntax tree of the service file, and reversely convert the replaced abstract syntax tree into a new service file text;
[0197] The merging and writing back module is used to identify the original content that needs to be retained in the new service file and the location where the new code snippet text can be inserted, merge the new code snippet text with the original content in the service file, and write the merged code back to the service file.
[0198] The abstract syntax tree annotation module includes: syntax tree generation unit, syntax element recognition unit, and method name annotation unit;
[0199] A syntax tree generation unit, configured to convert structured information into an abstract syntax tree according to the syntax rules of the target programming language;
[0200] Syntax element identification unit, which identifies the type of syntax element by traversing the abstract syntax tree nodes;
[0201] The method name annotation unit is used to deduce and confirm the method name based on the interface details and annotate it to the method node of the abstract syntax tree.
[0202] The annotation adding module includes: API path generation unit, Swagger configuration generation unit, and annotation adding unit;
[0203] API path generation unit, used to generate API path based on microservice path prefix and interface details;
[0204] Swagger configuration generation unit, used to generate Swagger configuration details based on interface details and Swagger specifications;
[0205] The annotation adding unit is used to add method annotations to the method nodes in the abstract syntax tree that annotates the method name and fill in the Swagger configuration details.
[0206] The code parsing module includes: generation unit, parsing unit, and search unit;
[0207] A generation unit is used to generate a compilable new code snippet text according to language rules from the abstract syntax tree of the complete method node carrying annotations;
[0208] The parsing unit is used to read and parse the new code snippet text and generate the corresponding abstract syntax tree;
[0209] A search unit is used to search for a target method node that matches the interface name in the abstract syntax tree of the service file.
[0210] The replacement and conversion module includes: a temporary syntax tree generation unit, a node replacement unit, and a reverse conversion unit;
[0211] A temporary syntax tree generation unit, used to parse a new code snippet into a temporary abstract syntax tree;
[0212] a node replacement unit, configured to replace a target node in the abstract syntax tree of the service file with a node in the temporary abstract syntax tree;
[0213] The reverse conversion unit is used to reversely convert the replaced abstract syntax tree into the new service file text.
[0214] The merging and writing back module includes: content recognition unit, intelligent merging unit, and code writing back unit;
[0215] A content identification unit, used to identify the original content that needs to be retained in the new service file and the location where the new code snippet text can be inserted;
[0216] An intelligent merging unit, used to merge the new code snippet text with the original content in the service file using an intelligent merging algorithm;
[0217] The code writing unit is used to write the merged code back to the service file.
[0218] The embodiments of the present invention are described above in conjunction with the accompanying drawings, but the present invention is not limited to the above-mentioned specific embodiments. The above-mentioned specific embodiments are merely illustrative and not restrictive. Under the guidance of the present invention, ordinary technicians in this field can also change, modify, replace and modify the above-mentioned embodiments without departing from the purpose and scope of protection of the present invention. These are all protected by the present invention.
Claims
1. A method for mixed retention of service file content and intelligent merging of code fragments, characterized in that: include: Parse microservice interface source data and extract structured information; The structured information includes microservice path prefix, group list data, and interface details; Convert the structured information into an abstract syntax tree, traverse the abstract syntax tree nodes to identify the syntax element type corresponding to each node, deduce the method name, and generate an abstract syntax tree annotated with the method name; The microservice path prefix and interface details generate the API path, and the Swagger configuration details are generated in combination with the Swagger specification. The annotation is added to the abstract syntax tree of the annotated method name to obtain the abstract syntax tree with annotations. Convert the annotated abstract syntax tree into a new code snippet, parse it into a temporary abstract syntax tree, and then replace the target node in the abstract syntax tree of the service file to generate a new service file text; Identify the content that needs to be retained in the new service file, merge the new and old code using semantic analysis and conflict detection algorithms, and write the merged code back to the service file; The step of converting the structured information into an abstract syntax tree, traversing the abstract syntax tree nodes to identify the syntax element type corresponding to each node, and deducing the method name to generate an abstract syntax tree annotated with the method name includes: Mapping the extracted structured information to a predefined abstract syntax tree node model; Based on the mapped structured information, a corresponding abstract syntax tree node is created for each syntax element, and the abstract syntax tree nodes are connected according to the code logic and hierarchical relationship to form an initial abstract syntax tree; the abstract syntax tree nodes include class nodes, method nodes, parameter nodes, and variable nodes; Perform syntax analysis on each node of the initial abstract syntax tree, check the attributes and structural features of the node, match them with predefined syntax element types, and identify the syntax element type corresponding to each node; Generate a method name based on the interface name and function description in the interface details, combined with the syntax element type and context information, and annotate the generated method name to the corresponding method node to obtain an abstract syntax tree annotated with the method name; The process of converting the annotated abstract syntax tree into a new code snippet, parsing the code into a temporary abstract syntax tree, and replacing the target node in the abstract syntax tree of the service file to generate a new service file text includes: Determine a code generation template based on the structure of the abstract syntax tree carrying annotations and the Swagger annotations; the code generation template includes the structure of the code, the syntax format, and the insertion position of the annotations; Based on the code generation template, the nodes and edges of the abstract syntax tree carrying annotations are converted into corresponding code statements to generate new code snippets; Use the syntax parser to parse the generated new code snippet, generate a temporary abstract syntax tree, and perform syntax verification and semantic analysis on the temporary abstract syntax tree; Locating a target node in the abstract syntax tree of the service file and replacing the target node with a corresponding node in the temporary abstract syntax tree to generate a new service file text; the target node is the code portion that needs to be updated or replaced; The process of identifying the content to be retained in the new service file, merging the new and old codes using semantic analysis and conflict detection algorithms, and writing the merged code back into the service file includes: Perform semantic analysis on the new service file text, identify retained content, and establish identifiers and indexes for the retained content; the retained content includes business logic code, Swagger configuration details, and Swagger annotations; Perform semantic analysis on the new and old codes to understand the functions and logical relationships of the codes, and use conflict detection algorithms to detect conflict points between the new and old codes; the conflict points include syntactic conflicts, semantic conflicts, and logical conflicts; Merge the new and old codes using conflict resolution strategies based on the conflict type and severity; The merged code is formatted and optimized, and an update log is generated. At the same time, the time of code merging, operation content and operator information are recorded, and the merged code is written into the service file.
2. The method for mixed retention of service file content and intelligent merging of code fragments according to claim 1, characterized in that: The process of identifying the syntax element type corresponding to each node includes: Obtain an initial abstract syntax tree and prepare a predefined syntax element type library, including definitions and characteristics of classes, methods, parameters, and variable types in the programming language; Use a depth-first search algorithm to traverse each node in the initial abstract syntax tree; For each node, extract its attributes and structural features; Matching the extracted node attributes and structural features with a predefined library of grammatical element types; the matching process is implemented by a rule engine; Identify the corresponding syntax element type for each node and attach the type information to the node.
3. The method for mixed retention of service file content and intelligent merging of code fragments according to claim 2, characterized in that: The microservice path prefix and interface details generate the API path, and the Swagger configuration details are generated in combination with the Swagger specification. The annotation is added to the abstract syntax tree of the annotated method name to obtain the abstract syntax tree with annotations, including: Combine the microservice path prefix with the interface name in the interface details to form the API path format, and parameterize the API path; Generate a JSON configuration file based on the API path, the request method, parameter list, and return value type in the interface details, and follow the Swagger specification. Add the Swagger configuration details to the configuration file. The Swagger configuration details include the interface description, parameter description, and response example information. Generate the corresponding Swagger annotation content based on the information in the Swagger configuration details, associate the Swagger annotation with the method node in the abstract syntax tree that annotates the method name, add the Swagger annotation to the corresponding method node, and generate an abstract syntax tree with the annotation.
4. The method for mixed retention of service file content and intelligent merging of code fragments according to claim 3, characterized in that: According to the API path, combined with the request method, parameter list, and return value type in the interface details, a corresponding JSON format configuration file is generated in accordance with the requirements of the Swagger specification, and Swagger configuration details are added to the configuration file, including: Collect the API path of the microservice interface, which is a combination of the path prefix and the interface name, and organize the interface details; Introduce Swagger related dependencies into the project; Create a Swagger configuration class, enable Swagger support, and configure basic information for the Swagger document; Use Swagger annotations to describe interface details on controller classes and methods; After starting the project, Swagger automatically scans the interfaces with Swagger annotations in the project and generates corresponding configuration files in JSON format.
5. The method for mixed retention of service file content and intelligent merging of code fragments according to claim 4, characterized in that: The parsed source data is achieved through regular expression matching or semantic parsing technology; the group list data includes module grouping, function grouping and version grouping information of the microservice; the interface details include interface name, request method, parameter list and return value type; the parameter list includes path parameters, query parameters, and request body parameters.
6. A system for preserving mixed service file content and intelligently merging code snippets, which is used to implement the method for preserving mixed service file content and intelligently merging code snippets according to any one of claims 1 to 5, characterized in that: include: Information extraction module, abstract syntax tree annotation module, annotation adding module, code parsing module, replacement and conversion module, merging and writing back module; The information extraction module is used to parse the microservice interface source data and extract structured information, including microservice path prefixes, group list data, and interface details; The abstract syntax tree annotation module is used to convert the extracted structured information into an abstract syntax tree according to the grammatical rules of the target programming language, traverse the abstract syntax tree nodes to identify the syntax element types, and deduce and confirm the method name based on the interface details to obtain an abstract syntax tree annotated with the method name; The annotation adding module is used to generate the API path according to the microservice path prefix and interface details, and generate the Swagger configuration details according to the interface details and Swagger specifications. At the same time, it adds method annotations to the method nodes in the abstract syntax tree of the annotated method name, and fills the Swagger configuration details into the annotation attributes in combination with the API path to obtain a complete method node abstract syntax tree with annotations; The code parsing module is used to generate a compilable new code snippet text from the abstract syntax tree of the complete method node carrying the annotation according to the language rules, read the content of the compilable new code snippet text, parse it into the abstract syntax tree of the service file, and search for the target method node that matches the interface name; The replacement and conversion module is used to parse the new code snippet into a temporary abstract syntax tree, replace the target node in the abstract syntax tree of the service file, and reversely convert the replaced abstract syntax tree into the new service file text; The merging and writing back module is used to identify the original content that needs to be retained in the new service file and the location where the new code snippet text can be inserted, merge the new code snippet text with the original content in the service file, and write the merged code back to the service file.
7. The service file content mixed retention and code fragment intelligent merging system according to claim 6, characterized in that: The annotation adding module includes: an API path generating unit, a Swagger configuration generating unit, and an annotation adding unit; The API path generation unit is used to generate an API path according to the microservice path prefix and interface details; The Swagger configuration generation unit is used to generate Swagger configuration details according to the interface details and the Swagger specification; The annotation adding unit is used to add method annotations to the method nodes in the abstract syntax tree that annotates the method name and fill in the Swagger configuration details.
Citation Information
Patent Citations
Interface generation method and device and terminal equipment
CN110262783A
Template code automatic generation system, method and equipment based on Spring micro-service framework and storage medium
CN117992043A