Code refactoring method, system, computing device, storage medium, and program product
By performing static and dynamic analysis on large code files, target refactoring schemes are generated and sorted according to functional dependencies. This solves the problem of insufficient context understanding in the refactoring of large code files, realizes an efficient and reliable code refactoring process, and generates code fragments that conform to logical dependencies.
Patent Information
- Application Number
- CN202510810400.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-17
- Publication Date
- 2026-01-16
- Estimated Expiration
- 2045-06-17
AI Technical Summary
Existing technologies suffer from insufficient context understanding and loss of long dependencies in large file code refactoring, leading to a lack of coherence in the refactored code or the introduction of potential errors. They are particularly inefficient when dealing with complex logic and dependencies.
By performing static and dynamic analysis on the code to be refactored, a target refactoring plan is generated, including a code refactoring plan and a test plan. Code refactoring tasks are generated based on the sorting of functional dependencies, and code snippets are iteratively optimized through static analysis and dynamic verification to ensure compliance with functional dependencies.
It improves the efficiency and reliability of code refactoring, the generated code snippets conform to logical dependencies, ensures code quality and stability, and reduces the risks in the refactoring process.
Smart Images

Figure CN120315754B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] Embodiments of the present application relate to the technical field of computer technology, and in particular, to a code refactoring method and system, a computing device, a storage medium, and a program product. BACKGROUND
[0002] In a software development ecosystem, code refactoring is a key link to improve software quality and development efficiency. Code refactoring aims to convert code in one programming language into another programming language, thereby enabling code reuse and porting in different systems and environments. It not only helps to improve the structure of existing code, but also improves the readability and maintainability of the code, while not changing its external behavior.
[0003] In related technologies, large language models have shown great potential in code understanding and generation. However, large language models still face challenges when processing large file code, such as insufficient context understanding and loss of long dependencies, which can result in a lack of coherence or introduction of potential errors in the refactored code.
[0004] Therefore, there is an urgent need for a code refactoring method that can refactor large files. SUMMARY
[0005] In view of this, embodiments of the present application provide a code refactoring method. One or more embodiments of the present application also relate to a code refactoring system, a computing device, a computer-readable storage medium, and a computer program product to address the technical deficiencies in the prior art.
[0006] According to a first aspect of an embodiment of the present application, a code method is provided, comprising:
[0007] performing code analysis on the initial code to be refactored to generate a target refactoring scheme, wherein the target refactoring scheme includes a code refactoring plan and a code testing plan, and the code refactoring plan includes function dependency relationships of the target code;
[0008] analyzing the function dependency relationships in the code refactoring plan to generate at least two code refactoring tasks corresponding to the initial code, wherein the at least two code refactoring tasks are sorted according to the function dependency relationships;
[0009] sequentially executing the at least two code refactoring tasks to obtain each initial code segment, and iteratively optimizing each initial code segment through static analysis and dynamic verification until a candidate code segment that meets the function dependency relationships is generated;
[0010] writing test cases for each candidate code segment based on the code testing plan, testing each candidate code segment based on each test case, and obtaining a plurality of target code segments that meet the function dependency relationships.
[0011] According to a second aspect of the embodiments of the present application, a code refactoring system is provided, comprising:
[0012] a code analysis module configured to perform code analysis on the initial code to be refactored to generate a target refactoring scheme, wherein the target refactoring scheme comprises a code refactoring plan and a code testing plan, and the code refactoring plan comprises function dependency relationships of the target code;
[0013] a task arrangement module configured to analyze the function dependency relationships in the code refactoring plan to generate at least two code refactoring tasks corresponding to the initial code, wherein the at least two code refactoring tasks are sorted according to the function dependency relationships;
[0014] a code generation module configured to execute the at least two code refactoring tasks in sequence to obtain initial code segments, and iteratively optimize the initial code segments through static analysis and dynamic verification until candidate code segments conforming to the function dependency relationships are generated;
[0015] a code testing module configured to write test cases for the candidate code segments based on the code testing plan, test the candidate code segments based on the test cases, and obtain multiple target code segments conforming to the function dependency relationships.
[0016] According to a third aspect of the embodiments of the present application, a computing device is provided, comprising:
[0017] a memory and a processor;
[0018] the memory is configured to store computer programs / instructions, and the processor is configured to execute the computer programs / instructions, which implement the steps of the code refactoring method when executed by the processor.
[0019] According to a fourth aspect of the embodiments of the present application, a computer readable storage medium is provided, which stores computer programs / instructions, which implement the steps of the code refactoring method when executed by the processor.
[0020] According to a fifth aspect of the embodiments of the present application, a computer program product is provided, comprising computer programs / instructions, which implement the steps of the code refactoring method when executed by the processor.
[0021] One embodiment of the present application realizes that the initial code to be reconstructed is analyzed to generate a target reconstruction scheme, wherein the target reconstruction scheme includes a code reconstruction plan and a code test plan, the code reconstruction plan includes function dependency of the target code; the function dependency in the code reconstruction plan is analyzed to generate at least two code reconstruction tasks corresponding to the initial code, wherein the at least two code reconstruction tasks are sorted according to the function dependency; the at least two code reconstruction tasks are executed in sequence to obtain each initial code segment, and each initial code segment is iteratively optimized through static analysis and dynamic verification until a candidate code segment conforming to the function dependency is generated; test cases of each candidate code segment are written based on the code test plan, each candidate code segment is tested based on each test case, and multiple target code segments conforming to the function dependency are obtained. Through systematic code analysis and function dependency sorting, the initial code reconstruction task is decomposed into ordered subtasks, ensuring that the generated candidate code segments conform to the logical dependency relationship, and each segment is verified based on the test plan, finally obtaining multiple high-quality, functionally correct and structurally clear target code segments, effectively improving the efficiency and reliability of code reconstruction. BRIEF DESCRIPTION OF DRAWINGS
[0022] Figure 1 is a flowchart of a code reconstruction method provided by one embodiment of the present application;
[0023] Figure 2 is a data interaction schematic diagram of a first code reconstruction system provided by one embodiment of the present application;
[0024] Figure 3 is a data interaction schematic diagram of a second code reconstruction system provided by one embodiment of the present application;
[0025] Figure 4 is a data interaction schematic diagram of a third code reconstruction system provided by one embodiment of the present application;
[0026] Figure 5 is a data interaction schematic diagram of a fourth code reconstruction system provided by one embodiment of the present application;
[0027] Figure 6 is a structural schematic diagram of a code reconstruction system provided by one embodiment of the present application;
[0028] Figure 7 is a structural block diagram of a computing device provided by one embodiment of the present application. DETAILED DESCRIPTION
[0029] In the following description, numerous specific details are set forth in order to provide a thorough understanding of the present application. However, the present application can be practiced without the specific details. In other instances, well-known methods have not been described in detail in order not to unnecessarily obscure aspects of the present application.
[0030] The terminology used in this disclosure of one or more embodiments of the application is for the purpose of describing particular embodiments only and is not intended to be limiting of one or more embodiments of the application. As used in this disclosure and the appended claims, the singular forms "a," "an" and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise. It will also be understood that the term "and / or" as used herein refers to and encompasses any and all possible combinations of one or more of the associated listed items.
[0031] It will be understood that, although the terms first, second, etc. can be used herein to describe various information, these terms are not intended to denote a temporal or chronological order. Rather, these terms are used solely to distinguish one from another only. For example, a first entity discussed below could later be discussed as a second entity, and similarly, a second entity discussed below could later be discussed as a first entity. As used herein, the term "if' can be construed to mean "when" or "upon" or "in response to determining" terms that indicate a logical or conditional relationship between the events or circumstances.
[0032] In addition, it should be noted that the user information (including but not limited to user equipment information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in one or more embodiments of the application are all information and data authorized by the user or authorized by all parties, and the collection, use and processing of related data need to comply with relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation portal for user to choose authorization or refusal.
[0033] First, the terms involved in one or more embodiments of the application are explained.
[0034] Agent (Intelligent Agent): An intelligent agent is an entity with autonomy, perception, decision-making and action ability. Through the perception of environmental information, it makes decisions and performs corresponding operations. In a multi-agent system, multiple agents work together or independently to achieve goals.
[0035] Characteristics of intelligent agents:
[0036] Autonomy: Intelligent agents can independently perceive, make decisions and act without continuous human intervention.
[0037] Perception: The ability of an agent to perceive its environment through sensors or data inputs, understanding the current state.
[0038] Decision-making: The ability of an agent to make decisions based on perceived information to achieve predetermined goals.
[0039] Action: The ability of an agent to change the environment or interact with other agents by performing actions.
[0040] Watch: The process by which an agent perceives the environment or the behavior of other agents, obtaining information about the current environment or task that helps the agent adjust its decisions and actions.
[0041] Action: The specific operation performed by an agent based on decisions, which affects the environment, other agents, or the agent's own state, and the agent adjusts subsequent behavior based on feedback to achieve goals.
[0042] Large language model: A language model with strong language understanding and generation capabilities, capable of understanding initial code and generating code in the target programming language.
[0043] Multi-agent system: A system composed of multiple agents that collaborate or compete in a common environment, often through communication and cooperation to achieve more complex tasks.
[0044] Characteristics of multi-agent systems:
[0045] Collaboration: Different agents work together in tasks by sharing information and coordinating behavior to achieve common goals. For example, in a team, agents can work together to improve overall efficiency.
[0046] Distribution: Each agent in a multi-agent system is usually independent, and they can be distributed in different physical locations or virtual environments, and may not have global control over each other.
[0047] Autonomy: Each agent has its own goals and behavior rules, and can make decisions independently, but also needs to coordinate with other agents.
[0048] Communication and coordination: Agents communicate with each other through message passing or data sharing to achieve cooperation or coordination, ensuring the smooth progress of the overall task.
[0049] As a key technology in software development, cross-language code refactoring aims to convert code in one programming language into another to achieve code reuse and portability in different systems and environments. With the increasing diversity of programming languages, cross-language conversion has gradually become an essential tool in large software projects. However, traditional manual refactoring methods require developers to master both the initial language and the target language, which is extremely demanding for technical personnel. Moreover, it is labor-intensive and inefficient, often requiring refactoring in man-months or even man-years. The huge time and labor costs hinder the advancement of large-scale cross-language code refactoring. In recent years, automated code conversion technology based on large language models has made significant progress, enabling rapid generation of code that meets target language specifications, especially for conversions between common programming languages. However, existing technologies still have obvious shortcomings: limited processing capacity for large files (such as code exceeding 3000 lines), difficulty in dealing with complex logic and dependency relationships; generated target code has deviations in programming style, naming conventions, and overall integrity, lacking maintainability; in addition, large models are prone to "hallucination" problems, resulting in insufficient reliability of output results.
[0050] In the present application, a code refactoring method is provided, and the present application also relates to a code refactoring system, a computing device, a computer-readable storage medium, and a computer program product, which are described in detail one by one in the following embodiments.
[0051] Referring to Figure 1 , Figure 1 A flowchart of a code refactoring method according to an embodiment of the present application is shown, which specifically includes the following steps.
[0052] Step 102: Perform code analysis on the initial code to be refactored to generate a target refactoring scheme, wherein the target refactoring scheme includes a code refactoring plan and a code testing plan, and the code refactoring plan includes the function dependency relationship of the target code.
[0053] It should be noted that code analysis refers to static and dynamic analysis of the initial code to be refactored to extract function dependency relationships, module structures, and potential problems in the code; the target refactoring scheme is a detailed plan generated based on the analysis results, used to guide subsequent code refactoring and testing work.
[0054] In actual implementation, the system first performs static analysis on the initial code to be refactored, parses the abstract syntax tree of the code to identify the calling relationship and dependency relationship between functions; at the same time, the system can also combine dynamic analysis technology to capture the actual function call path and dependency by running the code. For the code refactoring plan, one implementation is to design a phased refactoring strategy according to the function dependency relationship, and preferentially process the modules with higher independence; another implementation is to divide the refactoring priority according to the code complexity and dependency degree, and gradually complete the decoupling of the high-coupling modules. For the code test plan, the unit test and integration test scheme can be designed based on the function dependency relationship to ensure that the refactored code is logically correct and functionally complete.
[0055] Specifically, in actual scenarios, assuming that the initial code contains multiple interdependent function modules, the system discovers through static analysis that there is a strong dependency relationship between certain functions, while other functions are relatively independent. In view of these findings, the system can formulate a code refactoring plan, first refactor the functions with higher independence to reduce the impact on other modules; then gradually process the high-coupling modules, and design the corresponding test cases after each stage is completed. For example, when refactoring a core function, the system will write unit test cases for it, and design integration test cases for its dependent downstream functions to verify whether the behavior of the refactored code meets the expectations.
[0056] In the embodiments of the present specification, the target refactoring scheme is generated by combining static and dynamic analysis, which can comprehensively identify the dependency relationship in the code, ensure the scientificity and feasibility of the refactoring plan, and thus reduce the risk in the refactoring process. Based on the function dependency relationship, the code test plan is formulated, which can effectively cover the logic of the refactored code, improve the pertinence and coverage rate of the test, and guarantee the quality and stability of the refactored code.
[0057] In an optional implementation of the present embodiment, the code refactoring plan includes a target code structure design and a function dependency relationship; the initial code to be refactored is analyzed to generate a target refactoring scheme, including:
[0058] The function elements in the initial code are analyzed by the code analysis model to obtain a function variable list of the initial code;
[0059] The function variable list of the initial code is analyzed by the structure design model to generate a target code structure design under a target language;
[0060] The function call of the target code in the target code structure design is identified by the first dependency analysis model to obtain the function dependency relationship of the target code;
[0061] Based on the target code structure design, a corresponding code test plan is generated by the test generation model.
[0062] It should be noted that the target code structure design refers to the target code framework and organizational form required when converting the initial code into the target language; the function dependency relationship refers to the calling and logical dependency between functions in the target code; the target refactoring scheme is generated through multiple model analysis and contains code refactoring plan and code testing plan, which is used to guide the subsequent code refactoring and testing work.
[0063] In actual implementation, the system first parses the function elements in the initial code through the code analysis model, and extracts the function variable list. The list includes information such as input parameters, local variables and return values of the function, providing a basis for subsequent target code structure design. Then, the system analyzes the function variable list using the structure design model, generates the target code structure design in combination with the syntax rules and programming specifications of the target language. Next, the system identifies the function call relationship in the target code structure design through the first dependency analysis model, constructs the function dependency graph, and clearly defines the calling logic and dependency order between functions in the target code. Finally, based on the target code structure design, the system designs the corresponding code testing plan through the test generation model, ensuring that the subsequent testing can fully cover the functions and logic of the target code.
[0064] Specifically, in actual scenarios, assuming that the initial code contains multiple function modules, the system extracts the variable list of these functions through the code analysis model, for example, the input parameters of function A are X and Y, and the return value is Z. Then, the structure design model generates the target code structure design according to the characteristics of the target language, for example, maps function A to a class method in the target language, and adjusts the variable types to conform to the specifications of the target language. Next, the first dependency analysis model identifies that function A calls functions B and C in the target code, thereby constructing the function dependency relationship. Finally, the test generation model designs unit test cases for functions A, B and C based on the target code structure design, and designs integration test cases for the calling relationship between functions, forming a complete code testing plan.
[0065] In the embodiments of the present specification, the function variable list is extracted through the code analysis model, which can provide accurate basic data for subsequent target code structure design, improving the accuracy of design. The target code structure design is generated using the structure design model, which can ensure that the target code conforms to the syntax and specifications of the target language, improving the compatibility of code conversion. The function dependency relationship is identified through the first dependency analysis model, which can clearly define the calling logic of functions in the target code and provide a basis for subsequent code refactoring task sequencing. The code testing plan is designed based on the test generation model, which can fully cover the functions and logic of the target code, ensuring the quality and stability of the refactored code.
[0066] In an optional implementation of the embodiment, after the function element in the initial code is analyzed by the code analysis model to obtain the function variable list of the initial code, the method further includes:
[0067] The function variable list of the initial code is checked by the second dependency analysis model; if there is a missing function variable, the function element in the initial code is analyzed by the code analysis model to obtain the function variable list of the initial code until the function variable checking condition is reached.
[0068] After the function variable list of the initial code is analyzed by the structure design model to generate the target code structure design in the target language, the method further includes:
[0069] The target code structure design is checked by the structure evaluation model; if there is a structure defect, the function variable list of the initial code is analyzed by the structure design model to generate the target code structure design in the target language until the code structure checking condition is reached.
[0070] After the function call of the target code in the target code structure design is identified by the first dependency analysis model to obtain the function dependency relationship of the target code, the method further includes:
[0071] The function dependency relationship of the target code is checked by the dependency verification model; if there is an error dependency, the function call of the target code in the target code structure design is identified by the first dependency analysis model to obtain the function dependency relationship of the target code until the dependency relationship checking condition is reached.
[0072] After the corresponding code test plan is generated by the test generation model based on the target code structure design, the method further includes:
[0073] The code test plan is checked by the test verification model; if there is uncovered test logic, the corresponding code test plan is generated by the test generation model based on the target code structure design until the test verification condition is reached.
[0074] It should be noted that the checking process refers to introducing a verification mechanism at each key stage of the code refactoring scheme generation to ensure the accuracy and integrity of the function variable list, the target code structure design, the function dependency relationship, and the code test plan. Through multiple iterations of checking, missing and errors can be effectively avoided, and the quality of the final refactoring scheme can be improved.
[0075] In actual implementation, after obtaining the function variable list of the initial code, the system checks the function variable list through the second dependency analysis model. If a missing function variable is found, the function variable list is extracted again through the code analysis model until the check passes. After generating the target code structure design, the system checks the target code structure through the structure evaluation model. If there is a structure defect, such as not meeting the target language specification or incomplete logic, the target code structure design is generated again through the structure design model until the check passes. After identifying the function dependency relationship of the target code, the system checks the function dependency relationship through the dependency verification model. If an incorrect dependency is found, such as an incomplete call chain or a conflict dependency relationship, the function dependency relationship is identified again through the first dependency analysis model until the check passes. Finally, after generating the code test plan, the system checks the test plan through the test verification model. If there is uncovered test logic or insufficient test cases, the code test plan is generated again through the test generation model until the check passes.
[0076] Specifically, in actual scenarios, assuming that the system misses a local variable when extracting the function variable list of the initial code, the second dependency analysis model detects the missing variable and triggers the re-extraction process until all variables are correctly identified. Subsequently, when generating the target code structure design, if the structure evaluation model finds that the parameter type of a function does not meet the target language specification, the system adjusts the structure design to meet the requirements. Then, when identifying the function dependency relationship, if the dependency verification model finds that a function calls a non-existent dependent function, the system reanalyzes the call relationship to correct the problem. Finally, when generating the code test plan, if the test verification model finds that some boundary conditions are not covered, the system supplements the corresponding test cases until the test plan fully covers all logic.
[0077] In the embodiments of the present specification, checking the function variable list through the second dependency analysis model can ensure the completeness of variable extraction and provide a reliable data foundation for subsequent code analysis. Checking the target code structure design through the structure evaluation model can timely discover and fix structure defects, improving the standardization and logic of the target code. Checking the function dependency relationship through the dependency verification model can avoid logic problems caused by incorrect dependencies and ensure the completeness of the call chain of the target code. Checking the code test plan based on the test verification model can ensure comprehensive coverage of test cases, reduce functional risks caused by test omissions, and improve code quality.
[0078] In an optional implementation of the present embodiment, after analyzing the function elements in the initial code through the code analysis model to obtain the function variable list of the initial code, the following steps are further included:
[0079] In the case of detecting that the upstream node outputs the function variable list of the initial code, the function variable list of the initial code is checked by the downstream node corresponding to the upstream node through the second dependency analysis model.
[0080] If there is a missing function variable, the upstream node is returned until the function variable checking pass condition is reached.
[0081] It should be noted that the upstream node and the downstream node refer to the task execution units at different stages in the code refactoring process, wherein the upstream node is responsible for outputting the initial data (such as the function variable list), and the downstream node performs further processing or checking based on the data.
[0082] In actual implementation, in the case of detecting that the upstream node outputs the function variable list of the initial code, the system checks the function variable list of the initial code by the downstream node corresponding to the upstream node through the second dependency analysis model. If there is a missing function variable, the system will return to the upstream node to require re-extraction of the function variable list until the checking pass condition is reached. In this way, it can be ensured that all necessary function variables are correctly identified to provide accurate data support for subsequent target code structure design.
[0083] Specifically, in actual scenarios, it is assumed that the upstream node has completed the function element analysis of the initial code and generated a preliminary function variable list. The downstream node receives the list and checks it using the second dependency analysis model. If it is found that some key variables are not included in the list, the system sends feedback information back to the upstream node, prompting the upstream node to re-analyze the code until all necessary variables are identified and added to the list.
[0084] In the embodiments of the present specification, through the iterative checking mechanism between the upstream and downstream nodes, subsequent problems caused by missing initial data can be effectively reduced, and the reliability and efficiency of the entire code refactoring process are improved.
[0085] Further, the upstream and downstream relationship of the nodes can be arranged as follows.
[0086] The first node, the code analysis model, is responsible for analyzing the function elements in the initial code, outputting the function variable list and sending it to the second node.
[0087] The second node, the second dependency analysis model, receives the function variable list from the first node and checks it. If it is found to be missing, it is fed back to the first node to require re-analysis, to ensure the integrity of the function variable list, and if it is confirmed to be complete, it is sent to the third node.
[0088] The third node, a structure design model, generates a target code structure design according to the target language specification after obtaining the complete function variable list sent by the second node, and sends it to the fourth node.
[0089] The fourth node, a structure evaluation model, checks the target code structure design generated by the third node for structural defects. If problems are found, it returns to the third node for correction to ensure the rationality and compliance of the structure design. If there are no structural defects, it is sent to the fifth node.
[0090] The fifth node, a first dependency analysis model, identifies function call relationships based on the target code structure design sent by the fourth node, constructs a function dependency graph, and clearly defines the logical dependencies between functions, and sends it to the sixth node.
[0091] The sixth node, a dependency verification model, checks the accuracy of function dependency relationships to avoid false dependencies. If there are errors, it returns to the fifth node for adjustment to ensure the correctness of the dependency relationship.
[0092] The seventh node, a test generation model, generates a comprehensive code test plan based on the target code structure design and function dependency relationship determined by the fourth node, covering all possible test logic, and sends it to the eighth node.
[0093] The eighth node, a test verification model, fully checks the test plan to ensure that no test logic is missed. If deficiencies are found, it returns to the seventh node to supplement test cases to ensure the completeness of test coverage.
[0094] Step 104: Analyze the function dependency relationship in the code refactoring plan and generate at least two code refactoring tasks corresponding to the initial code, wherein the at least two code refactoring tasks are sorted according to the function dependency relationship.
[0095] It should be noted that the function dependency relationship refers to the call and dependency logic between functions in the code, and analyzing these relationships helps determine the execution order of refactoring tasks; code refactoring tasks are subtasks that decompose the initial code into multiple independent or step-by-step tasks to achieve more efficient code refactoring.
[0096] In actual implementation, the system first analyzes the function dependency relationship in the code refactoring plan, and determines the calling chain among functions by constructing a function dependency graph. Based on the dependency graph, the system can identify functions with high independence and core functions that depend on other functions. For generating code refactoring tasks, one implementation is to generate refactoring tasks for independent functions in topological order of the dependency relationship; another implementation is to divide high-coupling modules into multiple sub-tasks according to the complexity and influence range of functions, and assign them to different refactoring stages. Finally, the system generates at least two code refactoring tasks and ensures that these tasks are sorted according to the function dependency relationship.
[0097] Specifically, in an actual scenario, it is assumed that the initial code contains three functions A, B and C, where function A depends on function B, and function B depends on function C. After analyzing the function dependency relationship, the system will generate a refactoring task for function C first, because it is the starting point of the entire dependency chain; then generate a refactoring task for function B, and finally generate a refactoring task for function A. In this way, it can be ensured that when each refactoring task is executed, the dependent functions have been refactored, avoiding errors caused by unresolved dependencies. In addition, the system can also combine code complexity to split complex functions into multiple sub-tasks, further refining the refactoring process.
[0098] In the embodiments of the present specification, by generating sorted refactoring tasks based on function dependency relationships, the coupling between tasks can be effectively reduced, the conflicts and errors in the refactoring process can be reduced, and the refactoring efficiency can be improved. Generating at least two code refactoring tasks and sorting them according to the dependency relationship can ensure the logicality and continuity of the refactoring process, thereby improving the controllability and success rate of code refactoring.
[0099] In one optional implementation of the present embodiment, the function dependency relationship in the code refactoring plan is analyzed, and at least two code refactoring tasks corresponding to the initial code are generated, including:
[0100] The dependency relationship analysis is performed based on the function dependency relationship by a topological sorting model to obtain a function mapping sequence list arranged in calling order;
[0101] The task splitting operation is performed based on the function mapping sequence list by a task decomposition model to obtain at least two code refactoring tasks sorted according to the function dependency relationship;
[0102] The header file analysis task is performed based on the at least two code refactoring tasks by a dependency analysis model to obtain a complete import declaration set of each target code segment;
[0103] The initial code is segmented into a set of modularized code segments divided according to function boundaries.
[0104] It should be noted that the function dependency analysis refers to analyzing the calling logic and dependency order of functions in the code to determine the execution relationship between functions; the code refactoring task is to divide the initial code into multiple independent or step-by-step completion task modules according to the dependency relationship; and the header file analysis task is used to identify the complete import declaration set required by each target code segment to ensure that the modularized code segment can correctly reference the dependencies during the refactoring process.
[0105] In actual implementation, the system first analyzes the function dependency relationship through the topological sorting model to generate a function mapping sequence list arranged in calling order. The sequence list clearly defines the position of each function in the calling chain, providing a basis for subsequent task splitting. Subsequently, the system uses the task decomposition model to perform task splitting operation based on the function mapping sequence list, divides the initial code into at least two code refactoring tasks, and sorts them according to the function dependency relationship to ensure that the execution order of the tasks conforms to the logical dependency. Then, the system performs the header file analysis task on the generated code refactoring tasks through the dependency analysis model to extract the complete import declaration set of each target code segment, avoiding compilation errors caused by missing dependency declarations. Finally, the system segments the initial code into a set of modularized code segments divided by function boundaries, each segment corresponding to one or more functions, facilitating subsequent phased reconstruction.
[0106] Specifically, in actual scenarios, assume that the initial code contains functions A, B, and C, where A depends on B and B depends on C. The system generates a function mapping sequence list through the topological sorting model, resulting in [C, B, A], indicating that C is the most basic function and A is the most top-level function. Subsequently, the task decomposition model generates two code refactoring tasks according to the sequence list: task 1 for function C and task 2 for functions B and A, ensuring that task 1 is executed before task 2. Then, the dependency analysis model analyzes the target files of task 1 and task 2 to determine the complete import declaration set required by each file, such as task 1 requiring the import of certain library files and task 2 requiring the additional import of the output declaration of task 1. Finally, the system segments the initial code into three modularized code segments corresponding to functions C, B, and A, each containing its complete logic and dependency declaration.
[0107] In the embodiments of the present specification, the function dependency relationship is analyzed by the topological sorting model, which can clearly define the function calling order and provide a scientific basis for task splitting, reducing the risk of task conflicts. The task decomposition model generates sorted code refactoring tasks, which can effectively reduce the dependency problems between high-coupling modules and improve the controllability of the refactoring process. The dependency analysis model performs the header file analysis task, which can ensure that the dependency declarations of each target code segment are complete and accurate, avoiding compilation failures caused by missing declarations. The initial code is segmented into a set of modularized code segments, which can improve the maintainability and reusability of the code and lay the foundation for subsequent phased reconstruction.
[0108] In an optional implementation of the embodiment, after the function mapping sequence list arranged in the calling order is obtained by the dependency analysis task based on the function dependency relationship through the topological sorting model, the method further includes:
[0109] The function mapping sequence list is verified through the mapping verification model. If there is an abnormal mapping, the function mapping sequence list arranged in the calling order is obtained by the dependency analysis task based on the function dependency relationship through the topological sorting model until the function mapping verification pass condition is reached.
[0110] In an optional implementation of the embodiment, after the function mapping sequence list arranged in the calling order is obtained by the dependency analysis task based on the function dependency relationship through the topological sorting model, the method further includes:
[0111] The function mapping sequence list is verified through the mapping verification model. If there is an abnormal mapping, the function mapping sequence list arranged in the calling order is obtained by the dependency analysis task based on the function dependency relationship through the topological sorting model until the function mapping verification pass condition is reached.
[0112] In an optional implementation of the embodiment, after the function mapping sequence list arranged in the calling order is obtained by the dependency analysis task based on the function dependency relationship through the topological sorting model, the method further includes:
[0113] The function mapping sequence list is verified through the mapping verification model. If there is an abnormal mapping, the function mapping sequence list arranged in the calling order is obtained by the dependency analysis task based on the function dependency relationship through the topological sorting model until the function mapping verification pass condition is reached.
[0114] It should be noted that the verification process refers to introducing a verification mechanism in the key stage of generating the code refactoring task to ensure the accuracy and integrity of the function mapping sequence list, the code refactoring task, and the header file import declaration set. Through multiple iterations of verification, problems such as abnormal mapping, task omission, and redundant import can be effectively avoided, and the quality of the final refactoring scheme can be improved.
[0115] In actual implementation, after obtaining the function mapping sequence list arranged in calling order, the system checks the sequence list through the mapping verification model. If abnormal mapping (such as circular dependency or missing dependency) is found, the topological sorting model is returned to reanalyze the dependency relationship until the check passes. Subsequently, after generating at least two code refactoring tasks, the system checks the task coverage through the integrity detection model. If an uncovered code refactoring task is found, the task decomposition model is returned to re-split the task to ensure that all source functions and target functions are correctly allocated to the task until the task coverage condition is met. Finally, after generating the complete import declaration set of each target code fragment, the system checks the declaration set through the header file optimization model. If redundant imports (such as repeated or unnecessary declarations) are found, the dependency resolution model is returned to reanalyze the header file until the import declaration set reaches a state of refinement and completeness.
[0116] Specifically, in actual scenarios, assuming that the system generates a function mapping sequence list [C, B, A] through the topological sorting model, but the mapping verification model finds that there is a circular dependency problem between A and C, the system will reanalyze the dependency relationship to correct the error. Next, when generating code refactoring tasks, if the integrity detection model finds that some functions are not allocated to any task, the system will re-split the task to ensure that all functions are covered. Finally, when generating the header file import declaration set, if the header file optimization model finds that there are repeated import declarations in a target file, the system will reanalyze the dependency relationship and optimize the declaration set to ensure that the final result is complete and has no redundancy.
[0117] In the embodiments of the present specification, the function mapping sequence list is checked through the mapping verification model, which can timely discover and repair abnormal mapping problems and ensure the logical consistency of the dependency relationship. The code refactoring task is checked through the integrity detection model, which can avoid task omission and ensure that all source functions and target functions are correctly allocated to the refactoring task. The complete import declaration set is checked through the header file optimization model, which can remove redundant imports, improve the readability and compilation efficiency of the code, and at the same time ensure the integrity of the dependency declaration.
[0118] In an optional implementation of the present embodiment, after the topological sorting model performs dependency relationship resolution based on the function dependency relationship to obtain a function mapping sequence list arranged in calling order, the following steps are further included:
[0119] In the case of detecting that the upstream node outputs the function mapping sequence list, the function mapping sequence list is checked by the downstream node corresponding to the upstream node through the mapping verification model;
[0120] If there is abnormal mapping, the upstream node is returned until the function mapping check passes.
[0121] In actual implementation, after detecting the function mapping sequence list arranged in calling order output by the upstream node, the system checks the sequence list through the mapping verification model of the downstream node corresponding to the upstream node. If abnormal mapping (such as incomplete function call chain or circular dependency) is found, the system sends feedback information back to the upstream node, requiring re-execution of the topological sorting model to correct the function mapping sequence list until the pass condition is met. This iterative checking mechanism ensures the accuracy and logical consistency of the function mapping sequence list, laying a reliable foundation for subsequent task decomposition and code refactoring.
[0122] Specifically, in an actual scenario, assume that the upstream node generates a function mapping sequence list [C, B, A] through the topological sorting model and passes it to the downstream node. When the downstream node checks the sequence list using the mapping verification model, it finds that there is a circular dependency problem between functions A and C. At this time, the system will return to the upstream node, triggering the topological sorting model to re-analyze the function dependency relationship and generate a new function mapping sequence list. This process will be repeated until the mapping verification model confirms that there is no abnormal mapping in the sequence list.
[0123] In the embodiments of the present specification, through the iterative checking mechanism between the upstream and downstream nodes, abnormal mapping problems in the function mapping sequence list can be effectively identified and repaired, improving the accuracy of dependency relationship analysis, thereby ensuring the logical correctness and execution efficiency of subsequent code refactoring tasks.
[0124] Further, the upstream and downstream relationships of the nodes can be arranged as follows:
[0125] The ninth node, the topological sorting model, inputs the function dependency relationship from the target refactoring plan. The ninth node is responsible for performing dependency relationship analysis based on these dependency relationships, generating a function mapping sequence list arranged in calling order, and sending the sequence list to the tenth node.
[0126] The tenth node, the mapping verification model, inputs the function mapping sequence list output by the ninth node. The tenth node checks the function mapping sequence list to confirm whether there is abnormal mapping. If any problem is found, such as circular dependency or missing dependency, it will return information to the ninth node to require re-analysis; if the check passes, it will pass the correct function mapping sequence list to the eleventh node as its input.
[0127] The eleventh node, the task decomposition model, inputs the checked function mapping sequence list from the tenth node. The eleventh node performs task splitting operations based on the sequence list, generates at least two code refactoring tasks sorted according to the function dependency relationship, and sends the results to the twelfth node.
[0128] The twelfth node is an integrity detection model, which inputs at least two code refactoring tasks output by the eleventh node. The twelfth node is responsible for checking whether these tasks completely cover all necessary source function and target function correspondence. If problems are found, for example, there are unassigned tasks, the eleventh node will be fed back for correction; once confirmed to be correct, these tasks will be sent to the thirteenth node and the fifteenth node.
[0129] The thirteenth node is a dependency analysis model, which inputs the code refactoring tasks output by the twelfth node. The thirteenth node performs header file analysis tasks based on these tasks, determines the complete import declaration set required by each target code segment, and sends these declaration sets to the fourteenth node.
[0130] The fourteenth node is a header file optimization model, which inputs the complete import declaration set of each target code segment provided by the thirteenth node. The task of the fourteenth node is to check these declaration sets and remove any redundant imports to ensure that each declaration set is complete and has no redundant content. If problems are found, the thirteenth node will be notified to re-analyze; once confirmed to be correct, the header file statement dictionary will be generated.
[0131] The fifteenth node is a modular segmentation tool, which inputs the output of the twelfth node. The fifteenth node is responsible for dividing the initial code into a set of modular code segments according to function boundaries, so that each segment can be independently or step-by-step refactored.
[0132] Step 106: Perform at least two code refactoring tasks in sequence to obtain each initial code segment, and iteratively optimize each initial code segment through static analysis and dynamic verification until a candidate code segment that meets the function dependency relationship is generated.
[0133] It should be noted that the candidate code segment refers to the intermediate result or partially refactored code generated during the execution of the code refactoring task. These segments need to meet the function dependency relationship to ensure logical consistency; performing in sequence means completing each task in order according to the order of the code refactoring task, avoiding errors caused by unresolved dependencies.
[0134] Static analysis refers to discovering potential errors and dependency problems through syntax parsing and logical checking without executing the code. Dynamic verification refers to capturing code execution results and comparing them with expected outputs to verify functionality correctness by actually running test cases.
[0135] In actual implementation, the system executes each task in the order of the previously generated code refactoring tasks. For each task, the system first locates the target function or module, refactors it, and ensures that the functions or modules on which it depends have been refactored. If the current task involves multiple functions, the system processes the functions upstream of the dependency chain first, and then processes the functions downstream step by step. During the execution of the task, the system verifies whether the generated candidate code snippets conform to the function dependency relationship through static analysis, and records the intermediate results for use by subsequent tasks. For complex refactoring tasks, one implementation is to split the task into smaller subtasks for step-by-step execution; another implementation is to use an automated tool to assist in code modification to ensure the consistency of refactoring.
[0136] Specifically, in an actual scenario, assume that the system needs to refactor a group of code containing functions A, B, and C, where function A depends on function B, and function B depends on function C. The system first executes the refactoring task for function C, generates a candidate code snippet that conforms to the expectation; then executes the refactoring task for function B, uses the refactored function C as a dependency to generate a new candidate code snippet; and finally executes the refactoring task for function A, generates a final candidate code snippet based on the refactored function B. During the entire process, the system continuously verifies the correctness of the candidate code snippets to ensure that they meet the function dependency relationship.
[0137] In the embodiments of the present specification, by sequentially executing the code refactoring tasks, dependency conflicts can be effectively avoided, the logical consistency of the generated candidate code snippets can be ensured, and the reliability of the refactoring process can be improved. The generation of candidate code snippets that conform to the function dependency relationship can lay a foundation for subsequent code integration and testing, and reduce the risk of rework caused by dependency problems.
[0138] In an optional implementation of the present embodiment, at least two code refactoring tasks are sequentially executed to obtain initial code snippets, and the initial code snippets are iteratively optimized through static analysis and dynamic verification until a candidate code snippet that conforms to the function dependency relationship is generated, comprising:
[0139] A task is constructed based on the execution information of at least two code refactoring tasks to obtain prompt information for prompting the code generation model to generate a code snippet, wherein the prompt information includes code dependency prompt words and header file reference prompt words, the code dependency prompt words are used to prompt the code generation model to import the dependent library required for the execution of the target code, and the header file reference prompt words are used to prompt the code generation model to add the header file of the target code;
[0140] Each initial code snippet that conforms to the function dependency relationship is generated based on the prompt information through the code generation model;
[0141] Each initial code snippet is subjected to static analysis and dynamic verification;
[0142] in the case that the static analysis or the dynamic verification fails, inputting the initial code segments into an iterative optimization model to obtain optimized code segments that pass the verification, and returning the static analysis and the dynamic verification on the initial code segments;
[0143] in the case that the static analysis and the dynamic verification pass, executing the optimized code segments, and in the case that the execution of the optimized code segments succeeds, obtaining candidate code segments.
[0144] It should be noted that the sequential execution of the code refactoring task refers to processing each refactoring unit in sequence according to the function dependency relationship, to ensure that the preceding dependency has been completed in the previous task; the static analysis is used to check the syntax structure, variable usage, and dependency integrity without running the code; and the dynamic verification is used to further confirm whether the code behavior meets the expectation by executing the test case.
[0145] In actual implementation, the system executes at least two code refactoring tasks in sequence according to the function dependency order to generate initial code segments. Subsequently, the static analysis is performed on each initial code segment to identify syntax errors, logical defects, or missing dependency declarations. If a problem is found, the iterative optimization model is used to modify the code segment, and the static analysis is performed again until the verification passes. Then, the code segment that passes the static analysis is sent to the dynamic verification stage, the test case is executed, and the actual output result is captured and compared with the expected result. If the verification fails, the iterative optimization process is entered again for repair. This process is repeated until all code segments pass the static analysis and the dynamic verification, and finally the candidate code segments that meet the function dependency relationship are generated.
[0146] Specifically, in an actual scenario, a code refactoring task generates a code segment that calls an external library function. The static analysis finds that the corresponding header file reference is missing, and the system triggers the optimization process to complete the reference. After optimization, the static analysis is performed again and no error is found, and then the dynamic verification stage is entered to execute the test case to verify whether the function performance meets the expectation. Only in the case that all verification links pass, the code segment is identified as a qualified candidate code segment.
[0147] In the embodiments of the present specification, structural problems in the code can be found early through static analysis, invalid execution is reduced, and the code quality is improved.
[0148] In the embodiments of the present specification, the dynamic verification can be combined to further ensure the correctness of the code in the real running environment, and the reliability and stability of the refactoring result are enhanced.
[0149] In an optional implementation of the embodiment, after the task is constructed based on the at least two code refactoring task execution information by the prompt information construction model to obtain the prompt information for prompting the code generation model to generate the code snippet, the method further includes:
[0150] In a case where it is detected that the upstream node outputs the prompt information, each initial code snippet conforming to the function dependency relationship is generated by the code generation model using the downstream node corresponding to the upstream node.
[0151] It should be noted that the upstream node and the downstream node refer to task execution units at different stages in the code generation process, wherein the upstream node is responsible for outputting the prompt information, and the downstream node generates the initial code snippet based on the prompt information; the prompt information includes a code dependency prompt word and a header file reference prompt word, and is used to guide the code generation model to generate the code conforming to the function dependency relationship.
[0152] In actual implementation, after it is detected that the upstream node (the prompt information construction model) outputs the prompt information, the system generates the initial code snippet based on the prompt information using the downstream node (the code generation model) corresponding to the upstream node. The code dependency prompt word in the prompt information ensures that the generated code snippet can correctly import the dependent library required for target code running, and the header file reference prompt word ensures that the generated code snippet contains the necessary header file declaration. This upstream and downstream cooperation mechanism can ensure that the prompt information is accurately transmitted and applied to the code generation process, thereby improving the quality and standardization of the generated code.
[0153] Specifically, in actual scenarios, it is assumed that the upstream node generates the prompt information, for example, "import math library" as the code dependency prompt word and "include stdio.h" as the header file reference prompt word. After the downstream node receives the prompt information, the code generation model generates the initial code snippet according to the prompt. For example, for a certain refactoring task, the code generation model can generate a code snippet containing #include<stdio.h> and import math, while ensuring that the snippet conforms to the function dependency relationship. In this way, the prompt information can effectively guide the code generation process and avoid missing necessary dependencies or header files.
[0154] In the embodiments of the present specification, through the cooperative work between the upstream and downstream nodes, it can be ensured that the prompt information is accurately applied to the code generation process, and the standardization and logical consistency of the generated code are improved.
[0155] Further, the upstream and downstream relationship of the nodes can be arranged as follows:
[0156] The sixteenth node is a hint information construction model, and the input is at least two code refactoring tasks. This node is responsible for generating hint information for guiding code generation based on these tasks, including code dependency hint words and header file reference hint words. If header file reference hint words are needed, they are obtained through the seventeenth node; if dependency import hint words are needed, they are extracted through the eighteenth node. The generated hint information is then sent to the nineteenth node as its input.
[0157] The seventeenth node is a header file dictionary query model, and the input is information from the sixteenth node requesting header file reference hint words. This node extracts relevant header file hint words from a predefined header file dictionary and returns them to the sixteenth node to construct complete hint information.
[0158] The eighteenth node is a dependency code extraction model, and the input is information from the sixteenth node requesting dependency import hint words. This node analyzes the target code to extract necessary dependency code as dependency hint words and returns these hint words to the sixteenth node for constructing hint information.
[0159] The nineteenth node is a code generation model, and the input is the hint information output by the sixteenth node. This node generates individual initial code snippets that conform to function dependency relationships based on the hint information and sends these initial code snippets to the twentieth node (static analysis model) for further checking.
[0160] The twentieth node is a static analysis model, and the input is the initial code snippets from the nineteenth node. This node performs static analysis on these code snippets to verify that the syntax, logic, and dependency relationships are correct. In the case of static analysis failure, the code snippets are sent to the twenty-second node for correction. If static analysis passes, the code snippets are sent to the twenty-first node for actual execution testing.
[0161] The twenty-first node is a code execution model, and the input is the code snippets that pass static analysis output by the twentieth node. This node is responsible for executing these code snippets, and in the case of successful execution, generates candidate code snippets; if execution fails, the code snippets are fed back to the twenty-second node for further optimization.
[0162] The twenty-second node is an iterative optimization model, and the input is code snippets that fail static analysis from the twentieth node or code snippets that fail execution from the twenty-first node. This node performs multiple iterations of optimization on these code snippets until the requirements are met. The optimized code snippets are sent back to the twentieth node for static analysis until they pass all checks and ultimately become candidate code snippets.
[0163] Step 108: Write test cases for each candidate code snippet based on the code test plan, test each candidate code snippet based on each test case, and obtain multiple target code snippets that meet the function dependency relationship.
[0164] It should be noted that the test case is a specific test scheme for verifying the correctness of the function of the candidate code snippet and the logical consistency. The target code snippet refers to the code part that meets the function dependency relationship and satisfies the expected function after testing and verification, which will serve as the basis for the final refactoring code.
[0165] In actual implementation, the system writes test cases for each candidate code snippet based on the code test plan. The design of the test case needs to cover the core function, boundary condition, and interaction logic of the candidate code snippet with the dependent function. For unit testing, the test case can verify the function of a single candidate code snippet; for integration testing, the test case needs to simulate the calling scenario of the candidate code snippet with its dependent function to ensure the correctness of the function dependency. During the testing process, the system executes each test case, records the test results, and locates and repairs the problems of the candidate code snippet that fails the test. For complex test scenarios, one implementation way is to use an automated testing tool to generate test data and perform testing; another implementation way is to combine manual analysis to design test cases for specific scenarios to improve test coverage.
[0166] Specifically, in an actual scenario, assume that the system has generated candidate code snippets A, B, and C, where A depends on B, and B depends on C. The system first writes unit test cases for C to verify the correctness of its independent function; then writes integration test cases for B to verify whether the interaction logic between B and C meets the expectations; finally, writes test cases for A to verify whether the cooperation of A and B meets the requirements. During the testing process, if a candidate code snippet fails the test, the system will locate the problem and perform repair according to the test results, until all candidate code snippets pass the test, forming multiple target code snippets that meet the function dependency relationship.
[0167] In the embodiments of the present specification, by writing test cases based on the code test plan and performing testing, the correctness of the candidate code snippet can be comprehensively verified to ensure the functional integrity of the target code snippet. Obtaining target code snippets that meet the function dependency relationship can provide a high-quality code basis for subsequent code integration and reduce the rework cost caused by logical errors.
[0168] In one optional implementation of the present embodiment, test cases for each candidate code snippet are written based on the code test plan, each candidate code snippet is tested based on each test case, and multiple target code snippets that meet the function dependency relationship are obtained, including:
[0169] The test generation model generates test cases for each candidate code segment based on the code test plan, the test cases including expected output results;
[0170] The test execution model runs the test case set and captures actual output results;
[0171] The actual output results are compared with the expected output results to determine failed test cases;
[0172] The test repair model performs parameter adjustment and logic correction operations on the candidate code segment corresponding to the failed test case to obtain an optimized code segment;
[0173] The candidate code segments are updated based on the optimized code segment to obtain a plurality of target code segments that meet the function dependency relationship.
[0174] It should be noted that the test case is a specific test scheme for verifying the correctness of the function and the logic consistency of the candidate code segment; the target code segment refers to a code part that meets the function dependency relationship and satisfies the expected function after being tested. The test generation model, the test execution model, and the test repair model jointly constitute a complete test and optimization process, ensuring that the candidate code segment reaches a high-quality standard after multiple iterations of optimization.
[0175] In actual implementation, the system first generates test cases for each candidate code segment based on the code test plan through the test generation model. These test cases include specific input data and expected output results, which are used to verify whether the function of the candidate code segment meets the expectation. Subsequently, the test execution model runs the test case set and captures the actual output results of each candidate code segment. The system identifies failed test cases and their corresponding candidate code segments by comparing the actual output results with the expected output results. For failed test cases, the system uses the test repair model to perform parameter adjustment and logic correction operations on the candidate code segment to eliminate errors and generate an optimized code segment. Finally, the system updates the candidate code segment set based on the optimized code segment to obtain a plurality of target code segments that meet the function dependency relationship.
[0176] Specifically, in an actual scenario, assume that the system generates three candidate code segments A, B, and C and designs corresponding test cases for them. After the test execution model runs these test cases, it finds that the actual output result of a certain test case of segment B does not match the expectation. The test repair model analyzes the failure reason and performs logic correction (such as adjusting conditional judgment or modifying variable assignment) on segment B to generate an optimized code segment B'. The system re-adds the optimized B' to the candidate code segment set and finally obtains a group of target code segments that meet the function dependency relationship.
[0177] In the embodiments of the present specification, the test generation model is used to generate test cases containing expected output results, which can provide clear verification standards for subsequent testing, and improve the pertinence and effectiveness of testing. The test execution model is used to run the test cases and capture the actual output results, which can comprehensively evaluate the functional correctness of the candidate code segments and discover potential problems in time. The test repair model is used to adjust the parameters and correct the logic of the failed test cases, which can effectively improve the quality of the candidate code segments and reduce the risk of rework caused by logical errors. The optimized code segments are used to update the candidate code segment set, which can ensure that the final generated target code segments meet the functional dependency relationship and functional requirements, and provide high-quality results for code refactoring.
[0178] In one embodiment of the present application, the initial code refactoring task is decomposed into ordered subtasks through systematic code analysis and function dependency relationship sorting, which ensures that the generated candidate code segments meet the logical dependency relationship, and each segment is verified based on the test plan. Finally, multiple high-quality, functionally correct and structurally clear target code segments are obtained, which effectively improves the efficiency and reliability of code refactoring.
[0179] The following describes the code refactoring method provided by the present application in conjunction with the accompanying drawings. Figures 2-5 The code refactoring method provided by the present application is further described by taking the application of the code refactoring method in a multi-agent system as an example.
[0180] Figure 2 is a data interaction schematic diagram of a first code refactoring system provided by one embodiment of the present application, which specifically includes:
[0181] Initial code: The leftmost side is the "initial code" module, representing the starting point of the entire development process. The initial code is the input data of the refactoring system, including initial code based on an initial language. These files will be processed subsequently to generate a final set of target codes.
[0182] Code analysis agent: The code analysis agent is responsible for preliminary analysis of the initial code, extracting code structure, function dependency relationship and other information, and generating a target refactoring scheme and a code test scheme. The target refactoring scheme is sent to the task arrangement agent, and the code test scheme is sent to the code test agent. The code analysis agent uses the code analysis capability of a large model to ensure the integrity and efficiency of the refactoring plan.
[0183] Task arrangement agent: The task arrangement agent splits the code refactoring task into an executable subtask sequence according to the target refactoring scheme provided by the code analysis agent. It clearly defines the specific input and output of each task, and assigns a corresponding code generation agent to each task. The task arrangement agent ensures that the dependency relationship between tasks is correctly handled, thereby ensuring the orderly progress of the code refactoring process.
[0184] According to the code refactoring task, the initial code is split and input into the code generation agent arranged by the code arrangement intelligent agent.
[0185] Code generation agent: The code generation agent is responsible for generating specific candidate code snippets according to the tasks assigned by the task arrangement intelligent agent. The code generation agent utilizes the code generation capabilities of large models to ensure that the generated code meets the target language specifications and is logically correct. The code generation agent is also responsible for automated debugging, locating and fixing defects and problems in the code.
[0186] Code testing agent: The code testing agent is responsible for quality assurance of the system, ensuring that the developed functions work as expected and discovering potential problems. The agent writes integration test cases based on the code testing scheme sent by the code analysis agent and runs these test cases to check whether the code meets expectations. The code testing agent helps generate test cases with the help of large model capabilities, automates the testing process, and automatically fixes some known problems during testing, thereby improving testing efficiency and quality.
[0187] Target code snippets: At the end of the entire process, after all development and testing work is completed, the final output is multiple target code snippets. The target code is the result of architecture design, development and testing in various stages, and is optimized and supported by AI agents, reaching high quality and reliable standards, ready for deployment.
[0188] Figure 3 is a data interaction schematic diagram of a second code refactoring system provided by an embodiment of the present application, and the code analysis intelligent agent specifically includes:
[0189] The initial code analysis agent parses the functions and variables in the initial code by calling a large model, extracts the functions of the initial code, extracts the structural information of the initial code, understands the functions and behaviors of the code, and especially pays attention to putting the code not in any function into a function named __others__ to ensure that subsequent steps can be based on accurate code structure information.
[0190] The target code structure agent uses a large model to design the target code structure and target functions, determines the overall architecture of the refactored code, including the design of the directory structure, and designs functions or variables that meet the target language specifications and are logically consistent. The agent also adjusts the code structure according to the requirements to ensure good maintainability and scalability, thereby generating a detailed target code structure design scheme.
[0191] The code test plan writing agent uses a large model to design a code test plan based on the functional requirements of the target code and ensures that all functional paths are fully tested. It not only focuses on creating a comprehensive test plan, including specific test inputs and expected outputs, but also strives to improve test coverage to ensure the correctness of the code in various scenarios.
[0192] The function dependency analysis agent uses the thought chain technology to identify and analyze the function dependencies in the target code, establishing a detailed function dependency list. It thoroughly checks the correctness of data flow and control flow, avoiding circular dependencies or unreasonable dependencies, and finally forms a clear priority function dependency list.
[0193] The verification of initial code analysis results agent verifies the output of the initial code analysis optimization agent to confirm that no critical code logic or structure is missed, ensuring the accuracy of subsequent work. It uses the thought chain technology to carefully review the analysis results and promptly feedback any issues found, ensuring that every detail is accurately captured.
[0194] The verification of target code structure design agent verifies whether the target code structure and target functions designed by the target code structure optimization agent are reasonable and efficient, by comparing the target code structure with the original code functionality to ensure that the migrated code can fully implement the functionality of the original code. It also conducts strict code reviews on the design scheme to ensure compliance with best practices and programming standards, laying the foundation for high-quality code.
[0195] The verification of code test plan agent verifies the code test plan output by the code test plan writing optimization agent to ensure that all critical functions and boundary conditions are covered. It uses the thought chain technology to verify the rationality of test data, ensuring the reliability of the testing process, and provides optimization suggestions when deficiencies are found to improve overall testing efficiency and quality.
[0196] The verification of function dependency agent verifies the function dependency relationship analysis agent's output by thoroughly checking the call relationship graph between functions to ensure that all dependencies can be correctly implemented, preventing unnecessary circular dependencies or undefined dependencies. It uses the thought chain technology to optimize dependencies to ensure smooth data and task execution, ensuring the coordinated operation of the code.
[0197] Finally, through the cooperation of various agents, a target refactoring scheme containing dependencies is generated. Table 1 is an example:
[0198] Table 1: Target refactoring scheme containing initial code and target code, source function and target function dependencies
[0199]
[0200] Figure 4 is a data interaction schematic diagram of a third code refactoring system provided by an embodiment of the present application, and the task arrangement intelligent agent specifically includes:
[0201] The function dependency sorting intelligent agent generates a source-target function mapping sequence table sorted by dependency according to the target refactoring scheme by calling the large model to perform the SortTargetFunctions operation, and the functions in the table are sorted by dependency and sent to the verification dependency sorting intelligent agent. Table 2 is an example:
[0202] Table 2: Function mapping sequence table example
[0203]
[0204] The task generation management intelligent agent performs task division based on the source-target function mapping table sorted by target function output by the verification dependency sorting intelligent agent, obtains at least two code refactoring tasks, ensures that each code refactoring task has an initial code, a source function and a corresponding target code, a target function, and verifies to the two code refactoring task intelligent agents. Table 3 is an example:
[0205] Table 3: Task list of at least two code refactoring tasks
[0206]
[0207] The header file import management intelligent agent writes import statements for each target code according to the source-target function mapping sequence table sorted by dependency, and verifies the completeness and necessity of the import statements.
[0208] The code splitting intelligent agent splits the initial code into multiple code segments according to the division of the source functions in the task list, and the code not contained in any function is written into one or more __others__ code segments according to length, and the split code segment set is verified.
[0209] The verification dependency sorting intelligent agent calls the large model through the thinking chain, verifies the dependency of the sorting result in the source table line by line, reviews the correctness of the dependency between functions, and provides optimization suggestions to the function dependency sorting intelligent agent or sends the source-target function mapping table sorted by target function to the task generation management intelligent agent in the case of confirming that the verification is passed.
[0210] The verification at least two code refactoring task intelligent agent calls the large model through the thinking chain, verifies the at least two code refactoring tasks line by line, ensures the accuracy of task division and sorting, and provides optimization suggestions to the task generation management intelligent agent or sends the at least two code refactoring tasks to the header file import management intelligent agent in the case of confirming that the verification is passed.
[0211] The verification import statement intelligent agent calls a large model through a thinking chain to verify and optimize the import statement written by the header file import management intelligent agent, verifies the integrity and optimization space of the import statement, and provides optimization suggestions to the header file import management intelligent agent, or generates an import statement dictionary in the case of verification.
[0212] Figure 5 The fourth code refactoring system provided in an embodiment of the present application is a data interaction schematic diagram, and the code generation intelligent agent specifically includes:
[0213] The context prompt word construction intelligent agent is responsible for constructing the context environment of code refactoring, does not depend on the output of other intelligent agents, directly starts working from the initial code segment, calls a large model, and selects a suitable model refactoring prompt word template according to the language and code dependency of the initial code and the target code.
[0214] In the case of requiring a reference header of the target code, the header file injection intelligent agent is triggered during context construction, and if the context needs, the reference header statement for the target code is prepared, and the necessary reference header statements are extracted from the reference header dictionary of each file prepared in advance by the task arrangement intelligent agent.
[0215] In the case of requiring target code extraction dependency code, the dependency import intelligent agent is also triggered during context construction, and if the context needs, the dependency code of the target code generated by the target code extraction is extracted, and all necessary dependencies are correctly imported.
[0216] The code generation intelligent agent is triggered after the context prompt word is generated, and a thinking chain calls a large model to generate code according to the context prompt word.
[0217] The static analysis intelligent agent uses a static analysis tool to perform static analysis on the code after the code is generated or optimized. The analysis tool is language-dependent. For example, the Pylint tool can be used to analyze the code quality and ensure that the code meets the preset coding standards. If not, feedback is given.
[0218] The code execution intelligent agent is triggered after the static analysis is successful, and is responsible for executing the code. It calls a tool to run the code and verifies the correctness of the code. When executing the code, necessary tools are used to arrange the running environment. For example, the root directory generated by the code is configured as the python code execution path, so that the file can be found when executing. The execution result is sent to the large model to judge whether there is an exception. If there is an exception, the code execution fails, otherwise the code execution succeeds and the target code is obtained.
[0219] The code optimization intelligent agent is triggered when the static analysis fails or the code execution fails. It feeds back to the large model to continuously optimize the generated code until it passes all the verifications.
[0220] By the above Figures 2-5 The code reconstruction system shown in the code reconstruction system ensures that the target code not only meets the target language specifications but also is logically correct and executable, and improves the efficiency and accuracy of code reconstruction through the collaborative work of the multi-agent system, significantly reduces manual intervention, realizes efficient and automated cross-language code conversion, and greatly reduces time and labor costs.
[0221] Corresponding to the method embodiments described above, the present application also provides code reconstruction system embodiments, Figure 6 The structure of a code reconstruction system provided by an embodiment of the present application is shown. As shown in Figure 6 The system includes:
[0222] The code analysis module 602 is configured to perform code analysis on the initial code to be reconstructed to generate a target reconstruction scheme, wherein the target reconstruction scheme includes a code reconstruction plan and a code test plan, and the code reconstruction plan includes the function dependency relationship of the target code.
[0223] Optionally, the code analysis module 602 is further configured to analyze the function elements in the initial code by a code analysis model to obtain a function variable list of the initial code; analyze the function variable list of the initial code by a structure design model to generate a target code structure design under the target language; identify the function calls of the target code in the target code structure design by a first dependency analysis model to obtain the function dependency relationship of the target code; and generate a corresponding code test plan based on the target code structure design by a test generation model.
[0224] Optionally, the code analysis module 602 is further configured to check the function variable list of the initial code by a second dependency analysis model; if there is a missing function variable, return to analyze the function elements in the initial code by the code analysis model to obtain the function variable list of the initial code until a function variable check pass condition is reached; check the structure design of the target code by a structure evaluation model; if there is a structure defect, return to analyze the function variable list of the initial code by the structure design model to generate the target code structure design under the target language until a code structure check pass condition is reached; check the function dependency relationship of the target code by a dependency verification model; if there is an error dependency, return to identify the function calls of the target code in the target code structure design by the first dependency analysis model to obtain the function dependency relationship of the target code until a dependency relationship check pass condition is reached; and check the code test plan by a test verification model; if there is a test logic that is not covered, return to generate the corresponding code test plan based on the target code structure design by the test generation model until a test verification pass condition is reached.
[0225] Optionally, the code analysis module 602 is further configured to, in the case of detecting that the upstream node outputs the function variable list of the initial code, verify the function variable list of the initial code by a second dependency analysis model with the downstream node corresponding to the upstream node; if there is a missing function variable, return to the upstream node until a function variable verification pass condition is reached.
[0226] The task arrangement module 604 is configured to analyze the function dependency relationship in the code refactoring plan, and generate at least two code refactoring tasks corresponding to the initial code, wherein the at least two code refactoring tasks are sorted according to the function dependency relationship.
[0227] Optionally, the task arrangement module 604 is further configured to perform dependency relationship analysis based on the function dependency relationship by a topological sorting model to obtain a function mapping sequence list arranged in calling order; perform task splitting operation based on the function mapping sequence list by a task decomposition model to obtain the at least two code refactoring tasks sorted according to the function dependency relationship; perform header file analysis task based on the at least two code refactoring tasks by a dependency analysis model to obtain a complete import declaration set of each target code segment; and segment the initial code into a modularized code segment set divided according to function boundaries.
[0228] Optionally, the task arrangement module 604 is further configured to verify the function mapping sequence list by a mapping verification model; if there is an abnormal mapping, return to perform dependency relationship analysis based on the function dependency relationship by the topological sorting model to obtain the function mapping sequence list arranged in calling order until a function mapping verification pass condition is reached; verify the at least two code refactoring tasks by an integrity detection model; if there is an uncovered code refactoring task, return to perform splitting task based on the function mapping sequence list by the task decomposition model to obtain the at least two code refactoring tasks containing source function and target function corresponding relationship until a task coverage condition is reached; verify the complete import declaration set by a header file optimization model; if there is redundant import, return to perform header file analysis task based on the at least two code refactoring tasks by the dependency analysis model to obtain the complete import declaration set of each target code segment until an import verification pass condition is reached.
[0229] The code generation module 606 is configured to execute the at least two code refactoring tasks in sequence to obtain each initial code segment, and iteratively optimize each initial code segment by static analysis and dynamic verification until a candidate code segment conforming to the function dependency relationship is generated.
[0230] Optionally, the code generation module 606 is further configured to construct a task based on at least two pieces of code refactoring task execution information, obtain prompt information for prompting the code generation model to generate a code snippet, wherein the prompt information includes code dependency prompt words and header file reference prompt words, the code dependency prompt words are used to prompt the code generation model to import a dependent library required for running the target code, and the header file reference prompt words are used to prompt the code generation model to add a header file of the target code; generate each initial code snippet in accordance with the function dependency relationship based on the code generation model and the prompt information; perform static analysis on each initial code snippet; in the case of failing the static analysis, input each code snippet into an iterative optimization model to obtain each optimized code snippet that passes the verification; execute each optimized code snippet, and in the case of successful execution of each optimized code snippet, obtain each candidate code snippet.
[0231] The code testing module 608 is configured to write test cases for each candidate code snippet based on a code testing plan, test each candidate code snippet based on each test case, and obtain a plurality of target code snippets in accordance with the function dependency relationship.
[0232] Optionally, the code testing module 608 is further configured to generate, based on a code testing plan, test cases for each candidate code snippet containing expected output results; run the test case set and capture actual output results by a test execution model; compare the actual output results with the expected output results to determine failed test cases; perform parameter adjustment and logic correction operations on the candidate code snippet corresponding to the failed test cases by a test repair model to obtain an optimized code snippet; update each candidate code snippet based on the optimized code snippet to obtain a plurality of target code snippets in accordance with the function dependency relationship.
[0233] The present embodiment provides a code refactoring system, which decomposes an initial code refactoring task into ordered subtasks through systematic code analysis and function dependency relationship sorting, ensures that the generated candidate code snippets conform to the logical dependency relationship, verifies each snippet based on a test plan, and finally obtains a plurality of target code snippets that are of high quality, functionally correct, and structurally clear, thereby effectively improving the efficiency and reliability of code refactoring.
[0234] The above is a schematic scheme of a code refactoring system according to the present embodiment. It should be noted that the technical scheme of the code refactoring system belongs to the same concept as the technical scheme of the code refactoring method described above, and the details of the technical scheme of the code refactoring system that are not described in detail can be referred to the description of the technical scheme of the code refactoring method.
[0235] Figure 7A structural block diagram of a computing device 700 is shown, which is provided according to one embodiment of the present application. The components of the computing device 700 include, but are not limited to, a memory 710 and a processor 720. The processor 720 is connected with the memory 710 through a bus 730, and a large model 750 is used to perform the related person of code refactoring.
[0236] The computing device 700 also includes an access device 740, which enables the computing device 700 to communicate via one or more networks 760. Examples of these networks include a public switched telephone network (PSTN), a local area network (LAN), a wide area network (WAN), a personal area network (PAN), or a combination of communication networks such as the Internet. The access device 740 can include one or more of any type of network interface (for example, a network interface card (NIC)), wired or wireless, such as an IEEE 802.11 wireless local area network (WLAN) wireless interface, a Worldwide Interoperability for Microwave Access (Wi-MAX) interface, an Ethernet interface, a Universal Serial Bus (USB) interface, a cellular network interface, a Bluetooth interface, a Near Field Communication (NFC).
[0237] In one embodiment of the present application, the above-mentioned components of the computing device 700 and other components not shown in the figure can be connected with each other, for example, through a bus. It should be understood that Figure 7 the above-mentioned components of the computing device 700 and other components not shown in the figure can be connected with each other, for example, through a bus. It should be understood that Figure 7 The structural block diagram of the computing device shown is only for the purpose of example, and is not a limitation on the scope of the present application. Other components can be added or replaced as needed by those skilled in the art.
[0238] The computing device 700 can be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, etc.), a mobile phone (e.g., a smartphone), a wearable computing device (e.g., a smart watch, smart glasses, etc.), or other type of mobile device, or a stationary computing device such as a desktop computer or a personal computer (PC). The computing device 700 can also be a mobile or stationary server.
[0239] The processor 720 is configured to execute instructions of a computer program to implement the steps of the code refactoring method.
[0240] Each of the embodiments of the present application is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other. Each of the embodiments mainly describes the difference from other embodiments. In particular, the computing device embodiment is basically similar to the code refactoring method embodiment, and thus the description is relatively simple, and the relevant parts can be referred to the description of the code refactoring method embodiment.
[0241] An embodiment of the present application further provides a computer readable storage medium storing a computer program / instruction, which is executed by a processor to implement the steps of the code refactoring method.
[0242] Each of the embodiments of the present application is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other. Each of the embodiments mainly describes the difference from other embodiments. In particular, the computer readable storage medium embodiment is basically similar to the code refactoring method embodiment, and thus the description is relatively simple, and the relevant parts can be referred to the description of the code refactoring method embodiment.
[0243] An embodiment of the present application further provides a computer program product comprising a computer program / instruction, which is executed by a processor to implement the steps of the code refactoring method.
[0244] The above is a schematic scheme of the computer program product of the embodiment. It should be noted that the technical scheme of the computer program product and the technical scheme of the code refactoring method belong to the same concept, and the details of the technical scheme of the computer program product which are not described in detail can be referred to the description of the technical scheme of the code refactoring method.
[0245] The above describes specific embodiments of the application. Other embodiments are within the scope of the following claims. In some cases, the acts or steps recited in the claims can be performed in a different order and still accomplish the desired results. Also, the order or sequence of those processes depicted in the diagrams is not necessarily to be construed as the only order or sequence in which the desired results can be achieved. In some embodiments, multitasking and parallel processing can be advantageous or necessary. The application has been described in terms of particular embodiments. Other embodiments are within the scope of the following claims. In some cases, the acts or steps recited in the claims can be performed in a different order and still accomplish the desired results. Also, the order or sequence of those processes depicted in the diagrams is not necessarily to be construed as the only order or sequence in which the desired results can be achieved. In some embodiments, multitasking and parallel processing can be advantageous or necessary.
[0246] The computer readable medium can include any entity or apparatus capable of carrying the computer program code, recording medium, U disk, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signal, telecommunication signal, software distribution medium, etc. It should be noted that the computer readable medium can include appropriate additions or subtractions according to the requirements of patent practice. For example, according to the patent practice in some regions, the computer readable medium does not include electrical carrier signals and telecommunication signals.
[0247] It should be noted that for the foregoing method embodiments, in order to facilitate description, they are all expressed as a combination of a series of actions, but those skilled in the art should know that the embodiments of the present application are not limited by the order of the described actions, because according to the embodiments of the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should know that the embodiments described in the specification all belong to preferred embodiments, and the actions and modules involved are not necessarily necessary for the embodiments of the present application.
[0248] In the above embodiments, the description of each embodiment has its own focus, and the parts not described in detail in a certain embodiment can be referred to the related description of other embodiments.
[0249] The above disclosed preferred embodiments of the present application are only used to help explain the present application. Alternative embodiments do not describe all the details and do not limit the application to the specific embodiments described. Obviously, according to the content of the embodiments of the present application, many modifications and changes can be made. The present application selects and describes these embodiments in order to better explain the principles and practical applications of the embodiments of the present application, so that those skilled in the art can well understand and utilize the present application. The present application is limited by the claims and their entire scope and equivalents.
Claims
1. A code refactoring method characterized by, The method comprises the following steps: code analysis is performed on the initial code to be reconstructed to generate a target reconstruction scheme, wherein the target reconstruction scheme comprises a code reconstruction plan and a code test plan, and the code reconstruction plan comprises function dependency relationships of target code; function dependency relationships in the code reconstruction plan are analyzed to generate at least two code reconstruction tasks corresponding to the initial code, wherein the at least two code reconstruction tasks are sorted according to the function dependency relationships; the at least two code reconstruction tasks are executed in sequence to obtain initial code segments, static analysis is performed on the initial code segments, in the case that the static analysis fails, the initial code segments are modified by an iterative optimization model until the static analysis passes, the initial code segments passing the static analysis are dynamically verified until verification passes, and candidate code segments meeting the function dependency relationships are generated; test cases of the candidate code segments are written based on the code test plan, the candidate code segments are tested based on the test cases, and a plurality of target code segments meeting the function dependency relationships are obtained.
2. The method of claim 1, wherein, The code reconstruction plan comprises target code structure design and function dependency relationships. The code analysis performed on the initial code to be reconstructed to generate a target reconstruction scheme comprises the following steps: function elements in the initial code are analyzed by a code analysis model to obtain a function variable list of the initial code; the function variable list of the initial code is analyzed by a structure design model to generate target code structure design under a target language; function calls of the target code in the target code structure design are identified by a first dependency analysis model to obtain function dependency relationships of the target code; a corresponding code test plan is generated by a test generation model based on the target code structure design.
3. The method of claim 2, wherein, After the function elements in the initial code are analyzed by the code analysis model to obtain the function variable list of the initial code, the following step is further included: the function variable list of the initial code is verified by a second dependency analysis model; if there is a missing function variable, the function elements in the initial code are analyzed by the code analysis model to obtain the function variable list of the initial code until a function variable verification pass condition is reached; After the function variable list of the initial code is analyzed by the structure design model to generate the target code structure design under the target language, the following step is further included: the structure design of the target code is verified by a structure evaluation model; if there is a structure defect, the function variable list of the initial code is analyzed by the structure design model to generate the target code structure design under the target language until a code structure verification pass condition is reached; After the function calls of the target code in the target code structure design are identified by the first dependency analysis model to obtain the function dependency relationships of the target code, the following step is further included: The function dependency of the target code is checked by a dependency verification model. If there is an error dependency, a first dependency analysis model is used to identify the function call of the target code in the target code structure design, to obtain the function dependency of the target code, until a dependency checking pass condition is reached. After the code test plan is generated based on the target code structure design by the test generation model, the following steps are further included: The code test plan is checked by a test verification model. If there is uncovered test logic, a code test plan is generated based on the target code structure design by the test generation model, until a test verification pass condition is reached.
4. The method according to any one of claims 2-3, characterized in that, After the function variable list of the initial code is analyzed by the code analysis model, the following steps are further included: If the upstream node outputs the function variable list of the initial code, the function variable list of the initial code is checked by the downstream node corresponding to the upstream node using a second dependency analysis model. If there is a missing function variable, the upstream node is returned until a function variable checking pass condition is reached.
5. The method of claim 1, wherein, The function dependency in the code refactoring plan is analyzed to generate at least two code refactoring tasks corresponding to the initial code, including: A function mapping sequence list is obtained by performing dependency resolution based on the function dependency by a topological sorting model; At least two code refactoring tasks are obtained by performing task splitting based on the function mapping sequence list by a task decomposition model; A complete import declaration set of each target code segment is obtained by performing a header file analysis task based on the at least two code refactoring tasks by a dependency resolution model; The initial code is segmented into a set of modular code segments divided by function boundaries.
6. The method of claim 5, wherein, After the dependency resolution task based on the function dependency by the topological sorting model is performed, the following steps are further included: The function mapping sequence list is checked by a mapping verification model. If there is an abnormal mapping, the dependency resolution task based on the function dependency by the topological sorting model is performed to obtain a function mapping sequence list arranged in call order, until a function mapping checking pass condition is reached. After the splitting task based on the function mapping sequence list by the task decomposition model is performed to obtain at least two code refactoring tasks containing the correspondence between the initial function and the target function, the following steps are further included: The at least two code refactoring tasks are checked by a completeness detection model. If there is an uncovered code refactoring task, the splitting task based on the function mapping sequence list by the task decomposition model is performed to obtain at least two code refactoring tasks containing the correspondence between the initial function and the target function, until a task coverage condition is reached. After the header file analysis task based on the at least two code refactoring tasks by the dependency resolution model is performed to obtain a complete import declaration set of each target code segment, the following steps are further included: The complete import declaration set is checked through the header file optimization model. If there is redundant import, a header file analysis task is performed based on the at least two code refactoring tasks through a dependency resolution model to obtain a complete import declaration set of each target code segment until an import check pass condition is reached.
7. The method of claim 1, wherein, The at least two code refactoring tasks are executed in sequence to obtain each initial code segment, and the initial code segments are statically analyzed. In the case of static analysis failure, the initial code segments are modified through an iterative optimization model until the static analysis passes. The initial code segments that pass the static analysis are dynamically verified until verification passes, and candidate code segments that meet the function dependency relationship are generated, including: An information construction task is performed based on the at least two code refactoring tasks through a prompt information construction model to obtain prompt information for prompting a code generation model to generate code segments. The prompt information includes code dependency prompt words and header file reference prompt words. The code dependency prompt words are used to prompt the code generation model to import dependent libraries required for target code execution. The header file reference prompt words are used to prompt the code generation model to add header files of the target code. Each initial code segment that meets the function dependency relationship is generated based on the prompt information through a code generation model. The initial code segments are statically analyzed and dynamically verified. In the case of static analysis or dynamic verification failure, the initial code segments are input into an iterative optimization model to obtain optimized code segments that pass verification, and the static analysis and dynamic verification of the initial code segments are returned. In the case of static analysis and dynamic verification success, the optimized code segments are executed. In the case of successful execution of the optimized code segments, candidate code segments are obtained.
8. The method of claim 1, wherein, Test cases for the candidate code segments are written based on the code test plan. The candidate code segments are tested based on the test cases to obtain multiple target code segments that meet the function dependency relationship, including: Test cases for the candidate code segments containing expected output results are generated based on the code test plan through a test generation model. The test case set is run through a test execution model and actual output results are captured. The actual output results are compared with the expected output results to determine failed test cases. Parameter adjustment and logic correction operations are performed on the candidate code segments corresponding to the failed test cases through a test repair model to obtain optimized code segments. The candidate code segments are updated based on the optimized code segments to obtain multiple target code segments that meet the function dependency relationship.
9. A code refactoring system characterized by, It includes: A code analysis module configured to perform code analysis on initial code to be refactored to generate a target refactoring scheme, wherein the target refactoring scheme includes a code refactoring plan and a code test plan, and the code refactoring plan includes a function dependency relationship of target code. The task scheduling module is configured to analyze function dependency in the code refactoring plan, and generate at least two code refactoring tasks corresponding to the initial code, wherein the at least two code refactoring tasks are sorted according to the function dependency; The code generation module is configured to execute the at least two code refactoring tasks in sequence to obtain each initial code segment, and perform static analysis on the each initial code segment, in the case that the static analysis fails, correct the each initial code segment by iteration optimization model until the static analysis passes, perform dynamic verification on the each initial code segment passing the static analysis until the verification passes, and generate a candidate code segment conforming to the function dependency; The code testing module is configured to write test cases of each candidate code segment based on the code testing plan, test each candidate code segment based on each test case, and obtain a plurality of target code segments conforming to the function dependency.
10. A computing device, comprising: Comprising: a memory and a processor; The memory is used to store computer programs / instructions, and the processor is used to execute the computer programs / instructions, which realize the steps of the code refactoring method in any one of claims 1-8 when executed by the processor.
11. A computer readable storage medium, characterized in that, The memory has stored computer programs / instructions, which realize the steps of the code refactoring method in any one of claims 1-8 when executed by the processor.
12. A computer program product, characterised in that, Comprising computer programs / instructions, which realize the steps of the code refactoring method in any one of claims 1-8 when executed by the processor.
Citation Information
Patent Citations
Software testing method and device, computer equipment and readable storage medium
CN118779234A
Parsing tool for optimizing code for deployment on a serverless platform
US20230083849A1