Code reconstruction method and system, computing device, storage medium and program product
A systematic code restructuring method addresses context and dependency issues in large files by analyzing function dependencies and iteratively optimizing code, resulting in high-quality, reliable, and maintainable code.
Patent Information
- Application Number
- CN202510810400.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-17
- Publication Date
- 2025-07-15
- Estimated Expiration
- 2045-06-17
AI Technical Summary
The prior art has problems in the reconstruction of large files with insufficient context understanding, long dependencies, and lack of coherence and reliability of codes, resulting in inefficient reconstruction and easy introduction of errors.
By performing static and dynamic analysis of the initial code, a target reconstruction scheme is generated, including code reconstruction plan and test plan, the reconstruction task is executed by sorting function dependencies, and the code snippets are iteratively optimized through static analysis and dynamic verification, and the final result is verified based on test cases.
Improve the efficiency and reliability of code refactoring, ensure that the generated code snippets conform to logical dependencies, improve code quality and stability, and reduce manual intervention and time costs.
Smart Images

Figure CN120315754A_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present invention relate to the field of computer technologies, and in particular, to a code refactoring method, system, computing device, storage medium, and program product. Background Art
[0002] In the software development ecosystem, code refactoring is a key link to improve software quality and development efficiency. Code refactoring aims to transform the code of one programming language into that of another programming language, so as to achieve code reuse and transplantation in different systems and environments. It not only helps to improve the structure of the existing code, but also enhances 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 problems when dealing with large file codes, such as insufficient context understanding and loss of long dependencies, resulting in the refactored code may lack coherence or introduce potential errors.
[0004] Therefore, there is an urgent need for a code refactoring method that can refactor large files. Summary of the Invention
[0005] In view of this, embodiments of the present invention provide a code refactoring method. One or more embodiments of the present invention also relate to a code refactoring system, a computing device, a computer-readable storage medium, and a computer program product to solve the technical defects existing in the prior art.
[0006] According to the first aspect of the embodiments of the present invention, a code method is provided, including: Performing code analysis on the initial code to be refactored to generate a target refactoring plan, where the target refactoring plan includes a code refactoring plan and a code testing plan, and the code refactoring plan includes the function dependency relationship of the target code; Analyzing the function dependency relationship in the code refactoring plan to generate at least two code refactoring tasks corresponding to the initial code, where the at least two code refactoring tasks are sorted according to the function dependency relationship; 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 conforms to the function dependency relationship is generated; Writing test cases for each candidate code segment based on the code testing plan, and testing each candidate code segment based on each test case to obtain multiple target code segments that conform to the function dependency relationship.
[0007] According to the second aspect of the embodiments of the present invention, a code refactoring system is provided, including: A code analysis module, configured to perform code analysis on the initial code to be refactored and generate a target refactoring solution, where the target refactoring solution includes a code refactoring plan and a code testing plan, and the code refactoring plan includes the function dependency relationship of the target code; A task scheduling module, 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, where the at least two code refactoring tasks are sorted according to the function dependency relationship; A code generation module, configured to sequentially execute at least two code refactoring tasks 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 conforms to the function dependency relationship is generated; A code testing module, configured to write test cases for each candidate code segment based on the code testing plan, and test each candidate code segment based on each test case to obtain multiple target code segments that conform to the function dependency relationship.
[0008] According to a third aspect of an embodiment of the present invention, a computing device is provided, including: 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. When the computer programs / instructions are executed by the processor, the steps of the above code refactoring method are implemented.
[0009] According to a fourth aspect of an embodiment of the present invention, a computer-readable storage medium is provided, which stores computer programs / instructions. When the computer programs / instructions are executed by a processor, the steps of the above code refactoring method are implemented.
[0010] According to a fifth aspect of an embodiment of the present invention, a computer program product is provided, including computer programs / instructions. When the computer programs / instructions are executed by a processor, the steps of the above code refactoring method are implemented.
[0011] An embodiment of the present invention realizes code analysis on the initial code to be reconstructed to generate a target reconstruction scheme. The target reconstruction scheme includes a code reconstruction plan and a code testing plan. The code reconstruction plan includes the function dependency relationships of the target code. Analyze the function dependency relationships in the code reconstruction plan to generate at least two code reconstruction tasks corresponding to the initial code. The at least two code reconstruction tasks are sorted according to the function dependency relationships. Execute the at least two code reconstruction tasks in sequence to obtain each initial code segment, and iteratively optimize each initial code segment through static analysis and dynamic verification until candidate code segments that conform to the function dependency relationships are generated. Write test cases for each candidate code segment based on the code testing plan, and test each candidate code segment based on each test case to obtain multiple target code segments that conform to the function dependency relationships. Through systematic code analysis and function dependency relationship sorting, the initial code reconstruction tasks are decomposed into ordered subtasks, ensuring that the generated candidate code segments conform to the logical dependency relationships. At the same time, each segment is verified based on the testing plan, and finally multiple high-quality, functionally correct, and structurally clear target code segments are obtained, effectively improving the efficiency and reliability of code reconstruction. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] Figure 1 is a flowchart of a code reconstruction method provided by an embodiment of the present invention; Figure 2 is a data interaction schematic diagram of a first code reconstruction system provided by an embodiment of the present invention; Figure 3 is a data interaction schematic diagram of a second code reconstruction system provided by an embodiment of the present invention; Figure 4 is a data interaction schematic diagram of a third code reconstruction system provided by an embodiment of the present invention; Figure 5 is a data interaction schematic diagram of a fourth code reconstruction system provided by an embodiment of the present invention; Figure 6 is a structural schematic diagram of a code reconstruction system provided by an embodiment of the present invention; Figure 7 is a structural block diagram of a computing device provided by an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0013] Many specific details are set forth in the following description in order to provide a thorough understanding of the present invention. However, the present invention can be implemented in many other ways different from those described herein, and those skilled in the art can make similar extensions without departing from the connotation of the present invention. Therefore, the present invention is not limited by the specific implementations disclosed below.
[0014] The terms used in one or more embodiments of the present invention are for the purpose of describing specific embodiments only and are not intended to limit one or more embodiments of the present invention. The singular forms "a", "the", and "said" used in one or more embodiments of the present invention and the appended claims are also intended to include the plural forms unless the context clearly dictates otherwise. It should also be understood that the term "and / or" used in one or more embodiments of the present invention refers to and encompasses any and all possible combinations of one or more of the associated listed items.
[0015] It should be understood that although the terms first, second, etc. may be used in one or more embodiments of the present invention to describe various information, such information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of one or more embodiments of the present invention, the first may also be referred to as the second, and similarly, the second may also be referred to as the first. Depending on the context, the word "if" as used herein may be interpreted as "when" or "while" or "in response to a determination".
[0016] In addition, it should be noted that the user information (including but not limited to user device 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 present invention are all information and data that have been authorized by the user or fully authorized by all parties, and the collection, use, and processing of the relevant data need to comply with the relevant laws, regulations, and standards of the relevant countries and regions, and corresponding operation entrances are provided for the user to choose to authorize or reject.
[0017] First, the noun terms involved in one or more embodiments of the present invention are explained.
[0018] Agent: An agent is an entity with autonomy, perception, decision-making, and action capabilities. By perceiving environmental information, making decisions, and performing corresponding operations, in a multi-agent system, multiple agents cooperate or work independently to achieve goals.
[0019] Characteristics of an agent: Autonomy: An agent can independently perform perception, decision-making, and action without continuous human intervention.
[0020] Perception ability: An agent perceives the surrounding environment through sensors or data input to understand the current state.
[0021] Decision-making ability: Based on the perceived information, an agent can make decisions to achieve a predetermined goal.
[0022] Action ability: An agent changes the environment or interacts with other agents by performing actions.
[0023] Watch: The process by which an agent perceives the environment or the behavior of other agents. An agent obtains information about the current environment or task through observation, and this information helps the agent adjust its decisions and actions.
[0024] Action: An action is a specific operation executed by an agent based on its decision. Actions affect the state of the environment, other agents, or itself, and the agent adjusts its subsequent behavior according to the feedback to achieve the goal.
[0025] Large language model: A language model with powerful language understanding and generation capabilities, capable of understanding the initial code and generating code in the target programming language.
[0026] Multi-agent system: A system composed of multiple agents that collaborate or compete in a common environment, usually achieving more complex tasks through communication and cooperation.
[0027] Characteristics of multi-agent systems: Collaboration: Different agents cooperate to achieve common goals by sharing information and coordinating behaviors in a task. For example, in a team, agents can divide labor and cooperate to improve overall efficiency.
[0028] Distributivity: Each agent in a multi-agent system is usually independent, and they can be distributed in different physical locations or virtual environments, and there may be no global control among them.
[0029] Autonomy: Each agent has its own goals and behavior rules, and they can make decisions autonomously, but they also need to coordinate with other agents.
[0030] Communication and coordination: Agents communicate with each other by message passing or sharing data to achieve cooperation or coordination, ensuring the smooth progress of the overall task.
[0031] Cross - language code refactoring, as a key technology in software development, aims to transform the code of one programming language into that of another to achieve code reuse and portability across different systems and environments. With the increasing diversity of programming languages, cross - language conversion has gradually become an essential tool in large - scale software projects. However, traditional manual refactoring methods rely on developers' simultaneous mastery of the initial language and the target language, which requires extremely high technical skills, involves high labor intensity and low efficiency. Usually, refactoring is carried out in units of person - months or even person - years. The huge time and labor costs have hindered the advancement of large - scale cross - language code refactoring. In recent years, automated code conversion technologies based on large language models have made remarkable progress, being able to quickly generate code that conforms to the target language specifications, especially suitable for conversions between common programming languages. However, the existing technologies still have obvious deficiencies: they have limited processing capabilities for large files (such as code with more than 3000 lines), making it difficult to handle complex logics and dependencies; the generated target code has deviations in programming style, naming conventions, and integrity, lacking maintainability; in addition, large models are prone to the "hallucination" problem, resulting in insufficient reliability of the output results.
[0032] In the present invention, a code refactoring method is provided. The present invention also relates to a code refactoring system, a computing device, a computer - readable storage medium, and a computer program product, which will be described in detail one by one in the following embodiments.
[0033] See Figure 1 , Figure 1 which shows a flowchart of a code refactoring method provided according to an embodiment of the present invention, specifically including the following steps.
[0034] Step 102: Perform code analysis on the initial code to be refactored to generate a target refactoring plan, where the target refactoring plan includes a code refactoring plan and a code testing plan, and the code refactoring plan includes the function dependency relationships of the target code.
[0035] It should be noted that code analysis refers to performing static and dynamic analysis on the initial code to be refactored to extract function dependency relationships, module structures, and potential problems in the code; the target refactoring plan is a detailed plan generated based on the analysis results, used to guide subsequent code refactoring and testing work.
[0036] 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 call relationships and dependency relationships between functions. At the same time, the system can also combine dynamic analysis techniques to capture the actual function call paths and dependency situations by running the code. For the code refactoring plan, one implementation method is to design a phased refactoring strategy based on the function dependency relationships and give priority to processing modules with higher independence. Another implementation method is to divide the refactoring priorities according to the code complexity and dependency degree and gradually decouple the highly coupled modules. For the code testing plan, unit test and integration test schemes can be designed based on the function dependency relationships to ensure that the logic of the refactored code is correct and the functions are complete.
[0037] Specifically, in an actual scenario, assume that the initial code contains multiple mutually dependent function modules. The system discovers through static analysis that there are strong dependency relationships between some functions, while other functions are relatively independent. Based on these findings, the system can formulate a code refactoring plan, first refactor the functions with higher independence to reduce the impact on other modules, and then gradually process the highly coupled modules. Corresponding test cases are designed after each stage is completed. For example, when refactoring a certain core function, the system will write unit test cases for it and design integration test cases for the downstream functions it depends on to verify whether the behavior of the refactored code meets the expectations.
[0038] In the embodiments of this specification, by combining static and dynamic analysis to generate a target refactoring plan, it is possible to comprehensively identify the dependency relationships in the code, ensure the scientificity and feasibility of the refactoring plan, and thus reduce the risks during the refactoring process. Formulating a code testing plan based on the function dependency relationships can effectively cover the logic of the refactored code, improve the pertinence and coverage rate of testing, and ensure the quality and stability of the refactored code.
[0039] In an optional implementation manner of this embodiment, the code refactoring plan includes the target code structure design and function dependency relationships. Code analysis is performed on the initial code to be refactored to generate a target refactoring plan, including: Analyze the function elements in the initial code through a code analysis model to obtain the function variable list of the initial code; Analyze the function variable list of the initial code through a structure design model to generate the target code structure design in the target language; Through a first dependency analysis model, identify the function calls of the target code in the target code structure design to obtain the function dependency relationships of the target code; Based on the target code structure design, generate a corresponding code testing plan through a test generation model.
[0040] 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 call and logical dependencies between functions in the target code; the target refactoring plan is generated through various model analyses and includes a code refactoring plan and a code testing plan, which are used to guide subsequent code refactoring and testing work.
[0041] In actual implementation, the system first parses the function elements in the initial code through a code analysis model and extracts a list of function variables. This list includes information such as the input parameters, local variables, and return values of the functions, providing a basis for subsequent target code structure design. Subsequently, the system uses a structure design model to analyze the list of function variables, combines the syntax rules and programming specifications of the target language, and generates a target code structure design. Then, the system uses a first dependency analysis model to identify the function call relationships in the target code structure design, constructs a function dependency graph, and clarifies the call logic and dependency order between functions in the target code. Finally, based on the target code structure design, the system designs a corresponding code testing plan through a test generation model to ensure that subsequent tests can comprehensively cover the functions and logic of the target code.
[0042] Specifically, in an actual scenario, assume that the initial code contains multiple function modules. The system extracts a list of variables of these functions through a code analysis model. For example, the input parameters of function A are X and Y, and the return value is Z. Subsequently, the structure design model generates a target code structure design according to the characteristics of the target language. For example, function A is mapped to a class method in the target language, and the variable types are adjusted to conform to the specifications of the target language. Then, the first dependency analysis model identifies that function A calls function B and function C in the target code, thus constructing a function dependency relationship. Finally, based on the target code structure design, the test generation model designs unit test cases for functions A, B, and C respectively, and designs integration test cases for the call relationships between functions, forming a complete code testing plan.
[0043] In the embodiments of this specification, extracting a list of function variables through a code analysis model can provide accurate basic data for subsequent target code structure design and improve the accuracy of the design. Using a structure design model to generate a target code structure design can ensure that the target code conforms to the syntax and specifications of the target language and improve the compatibility of code conversion. Identifying function dependency relationships through a first dependency analysis model can clearly define the call logic of functions in the target code and provide a basis for sorting subsequent code refactoring tasks. Designing a code testing plan based on a test generation model can comprehensively cover the functions and logic of the target code and ensure the quality and stability of the code after refactoring.
[0044] In an optional implementation of this embodiment, after analyzing the function elements in the initial code through a code analysis model to obtain the function variable list of the initial code, the following steps are further included: Verify the function variable list of the initial code through a second dependency analysis model; if there are missing function variables, return to analyze the function elements in the initial code through the code analysis model to obtain the function variable list of the initial code until the function variable verification passes condition is met; After analyzing the function variable list of the initial code through a structure design model to generate the target code structure design in the target language, the following steps are further included: Verify the structure design of the target code through a structure evaluation model; if there are structural defects, return to analyze the function variable list of the initial code through the structure design model to generate the target code structure design in the target language until the code structure verification passes condition is met; After identifying the function calls of the target code in the target code structure design through a first dependency analysis model to obtain the function dependency relationship of the target code, the following steps are further included: Verify the function dependency relationship of the target code through a dependency verification model; if there are incorrect dependencies, return to identify the function calls of the target code in the target code structure design through the first dependency analysis model to obtain the function dependency relationship of the target code until the dependency relationship verification passes condition is met; After generating the corresponding code test plan through a test generation model based on the target code structure design, the following steps are further included: Verify the code test plan through a test verification model; if there are un-covered test logics, return to generate the corresponding code test plan through the test generation model based on the target code structure design until the test verification passes condition is met.
[0045] It should be noted that the verification process refers to introducing a verification mechanism at each key stage of the code refactoring plan 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 iterative verifications, omissions and errors can be effectively avoided, and the quality of the final refactoring plan can be improved.
[0046] In actual implementation, after obtaining the function variable list of the initial code, the system verifies the function variable list through the second dependency analysis model. If missing function variables are found, the code analysis model is returned to re-extract the function variable list until the verification passes. After generating the target code structure design, the system verifies the target code structure through the structure evaluation model. If there are structural defects, such as not conforming to the target language specification or having incomplete logic, the structure design model is returned to regenerate the target code structure design until the verification passes. After identifying the function dependency relationships of the target code, the system verifies the function dependency relationships through the dependency verification model. If incorrect dependencies are found, such as an incomplete call chain or conflicting dependency relationships, the first dependency analysis model is returned to re-identify the function dependency relationships until the verification passes. Finally, after generating the code test plan, the system verifies the test plan through the test verification model. If uncovered test logic or insufficient test cases are found, the test generation model is returned to regenerate the code test plan until the verification passes.
[0047] Specifically, in an actual scenario, assume that the system misses a local variable when extracting the function variable list of the initial code. The second dependency analysis model will detect this omission and trigger 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 conform to the target language specification, the system will readjust the structure design to meet the requirements. Then, when identifying the function dependency relationships, if the dependency verification model finds that a function calls a non-existent dependent function, the system will re-analyze the call relationships 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 will supplement the corresponding test cases until the test plan comprehensively covers all logic.
[0048] In the embodiments of this specification, by verifying the function variable list through the second dependency analysis model, the integrity of variable extraction can be ensured, providing a reliable data basis for subsequent code analysis. By using the structure evaluation model to verify the target code structure design, structural defects can be discovered and repaired in a timely manner, improving the normativity and logic of the target code. By verifying the function dependency relationships through the dependency verification model, logical problems caused by incorrect dependencies can be avoided, ensuring the integrity of the call chain of the target code. Based on the test verification model to verify the code test plan, it can be ensured that the test cases are comprehensively covered, reducing functional hidden dangers caused by test omissions and improving the code quality.
[0049] In an optional implementation manner of this embodiment, after analyzing the function elements in the initial code through the code analysis model and obtaining the function variable list of the initial code, it further includes: In the case of detecting the function variable list of the initial code output by the upstream node, the downstream node corresponding to the upstream node is used to verify the function variable list of the initial code through the second dependency analysis model; If there are missing function variables, return to the upstream node until the condition for passing the function variable verification is met.
[0050] 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. The upstream node is responsible for outputting the initial data (such as the function variable list), while the downstream node further processes or verifies based on this data.
[0051] In actual implementation, in the case of detecting the function variable list of the initial code output by the upstream node, the system uses the downstream node corresponding to the upstream node to verify the function variable list of the initial code through the second dependency analysis model. If there are missing function variables, the system will return to the upstream node and require re-extracting the function variable list until the verification passing condition is achieved. This can ensure that all necessary function variables are correctly identified and provide accurate data support for the subsequent target code structure design.
[0052] Specifically, in an actual scenario, assume that the upstream node has completed the analysis of the function elements of the initial code and generated a preliminary function variable list. After receiving this list, the downstream node uses the second dependency analysis model to verify it. If it is found that some key variables are not included in the list, the system will send 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.
[0053] In the embodiments of this specification, through the iterative verification mechanism between the upstream and downstream nodes, it is possible to effectively reduce subsequent problems caused by missing initial data and improve the reliability and efficiency of the entire code refactoring process.
[0054] Furthermore, the upstream and downstream relationships of the nodes can be arranged as follows.
[0055] 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.
[0056] The second node, the second dependency analysis model, receives the function variable list from the first node and conducts verification. If any omission is found, it feeds back to the first node and requests re-analysis to ensure the integrity of the function variable list. If it is confirmed to be complete, it sends it to the third node.
[0057] The third node, the structure design model, after obtaining the complete function variable list sent by the second node, generates the target code structure design according to the target language specification and sends it to the fourth node.
[0058] The fourth node, the structure evaluation model, verifies the target code structure design generated by the third node to check 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 sends the design to the fifth node.
[0059] The fifth node, the first dependency analysis model, based on the target code structure design sent by the fourth node, identifies function call relationships, constructs a function dependency graph, clarifies the logical dependencies between functions, and sends it to the sixth node.
[0060] The sixth node, the dependency verification model, checks the accuracy of the function dependency relationships to avoid incorrect dependencies. If there are errors, it returns to the fifth node for adjustment to ensure the correctness of the dependency relationships.
[0061] The seventh node, the test generation model, generates a comprehensive code test plan according to the target code structure design and function dependency relationships determined by the fourth node, covering all possible test logics, and sends it to the eighth node.
[0062] The eighth node, the test verification model, conducts a comprehensive verification of 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 integrity of test coverage.
[0063] Step 104: Analyze the function dependency relationships in the code refactoring plan to generate at least two code refactoring tasks corresponding to the initial code. Among them, at least two code refactoring tasks are sorted according to the function dependency relationships.
[0064] It should be noted that the function dependency relationship refers to the call and dependency logic between functions in the code. Analyzing these relationships helps to determine the execution order of refactoring tasks; the code refactoring task is to decompose the initial code into multiple sub-tasks that can be completed independently or step by step to more efficiently implement code refactoring.
[0065] In actual implementation, the system first parses the function dependency relationships in the code refactoring plan and clarifies the call chain between functions by constructing a function dependency graph. Based on this 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 method is to perform a topological sort according to the dependency relationships and preferentially generate refactoring tasks for independent functions; another implementation method is to divide high-coupling modules into multiple sub-tasks according to the complexity and influence scope of functions and allocate 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 relationships.
[0066] Specifically, in an actual scenario, assume 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 dependencies, the system will preferentially generate a refactoring task for function C 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. This can ensure that when each refactoring task is executed, the functions it depends on have been refactored, avoiding errors caused by unresolved dependencies. In addition, the system can also combine code complexity to split complex functions into multiple subtasks to further refine the refactoring process.
[0067] In the embodiments of this specification, by generating sorted refactoring tasks based on function dependencies, the coupling between tasks can be effectively reduced, conflicts and errors during 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 logic and coherence of the refactoring process, thereby enhancing the controllability and success rate of code refactoring.
[0068] In an optional implementation manner of this embodiment, analyzing the function dependencies in the code refactoring plan and generating at least two code refactoring tasks corresponding to the initial code includes: Performing dependency relationship parsing through a topological sorting model based on the function dependencies to obtain a function mapping sequence list arranged in the call order; Performing task splitting operations through a task decomposition model based on the function mapping sequence list to obtain at least two code refactoring tasks sorted according to the function dependencies; Performing header file analysis tasks through a dependency parsing model based on at least two code refactoring tasks to obtain a complete import declaration set for each target code segment; Segmenting the initial code into a set of modular code segments divided by function boundaries.
[0069] It should be noted that function dependency parsing refers to clarifying the execution relationship between functions by analyzing the call logic and dependency order of functions in the code; code refactoring tasks are to decompose the initial code into multiple task modules that can be completed independently or step by step according to the dependency relationship; the header file analysis task is used to identify the complete import declaration set required for each target code segment to ensure that the modular code segments can correctly reference dependencies during the refactoring process.
[0070] In actual implementation, the system first parses the function dependency relationship through a topological sorting model to generate a function mapping sequence list arranged in the call order. This sequence list clarifies the position of each function in the call chain and provides a basis for subsequent task splitting. Subsequently, the system uses a task decomposition model to perform task splitting operations 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 dependencies. Then, the system performs a header file analysis task on the generated code refactoring tasks through a dependency parsing model, extracts the complete import declaration set of each target code segment, and avoids compilation errors caused by missing dependency declarations. Finally, the system segments the initial code into a modular code segment set divided by function boundaries, with each segment corresponding to one or more functions, facilitating subsequent phased refactoring.
[0071] Specifically, in an actual scenario, assume that the initial code includes functions A, B, and C, where A depends on B and B depends on C. The system generates a function mapping sequence list through a topological sorting model, and the result is [C, B, A], indicating that C is the most basic function and A is the top-level function. Subsequently, the task decomposition model generates two code refactoring tasks based on this sequence list: Task 1 for function C, and Task 2 for functions B and A, and ensures that Task 1 is executed prior to Task 2. Then, the dependency parsing model analyzes the target files of Task 1 and Task 2 to determine the complete import declaration set required for each file. For example, Task 1 needs to import certain library files, while Task 2 needs to additionally import the output declarations of Task 1. Finally, the system segments the initial code into three modular code segments, corresponding to functions C, B, and A respectively, and each segment contains its complete logic and dependency declarations.
[0072] In the embodiments of this specification, by parsing the function dependency relationship through a topological sorting model, the function call order can be clearly defined, providing a scientific basis for task splitting and reducing the risk of task conflicts. Using the task decomposition model to generate sorted code refactoring tasks can effectively reduce the dependency problems between highly coupled modules and improve the controllability of the refactoring process. By performing a header file analysis task through the dependency parsing model, it can be ensured that the dependency declarations of each target code segment are complete and error-free, avoiding compilation failures caused by missing declarations. Segmenting the initial code into a modular code segment set can improve the maintainability and reusability of the code and lay a foundation for subsequent phased refactoring.
[0073] In an optional implementation manner of this embodiment, after performing the dependency relationship parsing task based on the function dependency relationship through the topological sorting model to obtain a function mapping sequence list arranged in the call order, it further includes: Verify the model through mapping and check the function mapping sequence list; if there is an abnormal mapping, return to perform the dependency parsing task based on the function dependency relationship through the topological sorting model to obtain the function mapping sequence list arranged in the call order until the function mapping verification pass condition is reached; After performing the splitting task based on the function mapping sequence list through the task decomposition model to obtain at least two code refactoring tasks including the corresponding relationship between the source function and the target function, it further includes: Verify at least two code refactoring tasks through the integrity detection model; if there are uncovered code refactoring tasks, return to perform the splitting task based on the function mapping sequence list through the task decomposition model to obtain at least two code refactoring tasks including the corresponding relationship between the source function and the target function until the task coverage condition is reached; After performing the header file analysis task based on at least two code refactoring tasks through the dependency parsing model to obtain the complete import statement set of each target code segment, it further includes: Verify the complete import statement set through the header file optimization model; if there are redundant imports, return to perform the header file analysis task based on at least two code refactoring tasks through the dependency parsing model to obtain the complete import statement set of each target code segment until the import verification pass condition is reached.
[0074] It should be noted that the verification process refers to introducing a verification mechanism at the key stage of code refactoring task generation to ensure the accuracy and integrity of the function mapping sequence list, code refactoring tasks, and header file import statement set; through multiple iterative verifications, problems such as abnormal mapping, task omission, and redundant imports can be effectively avoided, improving the quality of the final refactoring plan.
[0075] In actual implementation, after obtaining the function mapping sequence list arranged in the call order, the system verifies the sequence list through the mapping verification model. If an abnormal mapping (such as circular dependency or missing dependency) is found, it returns to the topological sorting model to re-parse the dependency relationship until the verification passes. Subsequently, after generating at least two code refactoring tasks, the system verifies the task coverage through the integrity detection model. If an uncovered code refactoring task is found, it returns to the task decomposition model to re-split the task to ensure that all source functions and target functions are correctly assigned to the tasks until the task coverage condition is met. Finally, after generating the complete import statement set of each target code segment, the system verifies the statement set through the header file optimization model. If redundant imports (such as duplicate or unnecessary declarations) are found, it returns to the dependency parsing model to re-analyze the header file until the import statement set reaches a concise and complete state.
[0076] Specifically, in an actual scenario, assume that the system generates a function mapping sequence list [C, B, A] through a topological sorting model. However, the mapping verification model discovers a circular dependency problem between A and C. In this case, the system will re-parse the dependency relationship to correct the error. Then, when generating the code refactoring task, if the integrity detection model finds that some functions are not assigned to any tasks, the system will re-split the tasks to ensure that all functions are covered. Finally, when generating the header file import declaration set, if the header file optimization model finds duplicate import declarations in a target file, the system will re-analyze the dependency relationship and optimize the declaration set to ensure that the final result is both complete and free of redundancy.
[0077] In the embodiments of this specification, by using the mapping verification model to verify the function mapping sequence list, abnormal mapping problems can be promptly discovered and fixed, ensuring the logical consistency of the dependency relationship. By using the integrity detection model to verify the code refactoring task, task omissions can be avoided, ensuring that all source functions and target functions are correctly assigned to the refactoring tasks. By using the header file optimization model to verify the complete import declaration set, redundant imports can be removed, improving the readability and compilation efficiency of the code, while ensuring the integrity of the dependency declarations.
[0078] In an optional implementation manner of this embodiment, after the topological sorting model performs dependency relationship parsing based on the function dependency relationship to obtain a function mapping sequence list arranged in the call order, the following steps are further included: In the case of detecting that the upstream node outputs a function mapping sequence list, use the downstream node corresponding to the upstream node to verify the function mapping sequence list through the mapping verification model; If there is an abnormal mapping, return to the upstream node until the function mapping verification pass condition is reached.
[0079] In actual implementation, after detecting that the upstream node outputs a function mapping sequence list arranged in the call order, the system uses the downstream node corresponding to the upstream node to verify the sequence list through the mapping verification model. If an abnormal mapping is found (such as an incomplete function call chain or a circular dependency), the system will send the feedback information back to the upstream node, requesting to re-execute the topological sorting model to correct the function mapping sequence list until the verification pass condition is achieved. This iterative verification mechanism ensures the accuracy and logical consistency of the function mapping sequence list, laying a reliable foundation for subsequent task decomposition and code refactoring.
[0080] Specifically, in an actual scenario, assume that the upstream node generates a function mapping sequence list [C, B, A] through a topological sorting model and passes it to the downstream node. When the downstream node uses the mapping verification model to check the sequence list, it is found that there is a circular dependency problem between function A and C. At this time, the system will return to the upstream node, trigger the topological sorting model to re-parse the function dependencies, and generate a new function mapping sequence list. This process will be repeated continuously until the mapping verification model confirms that there are no abnormal mappings in the sequence list.
[0081] In the embodiments of this specification, through the iterative verification mechanism between the upstream and downstream nodes, it is possible to effectively identify and repair abnormal mapping problems in the function mapping sequence list, improve the accuracy of dependency relationship parsing, and thus ensure the logical correctness and execution efficiency of subsequent code refactoring tasks.
[0082] Furthermore, the upstream and downstream relationships of the nodes can be arranged as follows: The ninth node, the topological sorting model, with the input being the function dependencies from the target refactoring plan. The ninth node is responsible for performing the dependency relationship parsing task based on these dependencies, generating a function mapping sequence list arranged in the call order, and sending this sequence list to the tenth node.
[0083] The tenth node, the mapping verification model, with the input being the function mapping sequence list output by the ninth node. The tenth node checks the function mapping sequence list to confirm whether there are abnormal mappings. If any problems are found, such as circular dependencies or missing dependencies, it will return information to the ninth node requesting re-parsing; if the verification passes, it will pass the correct function mapping sequence list to the eleventh node as its input.
[0084] The eleventh node, the task decomposition model, with the input being the verified function mapping sequence list from the tenth node. The eleventh node performs the task splitting operation based on this sequence list, generates at least two code refactoring tasks sorted according to the function dependencies, and sends the results to the twelfth node.
[0085] The twelfth node, the integrity detection model, with the input being 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 relationships. If problems are found, such as unassigned tasks, it will feedback to the eleventh node for correction; once it is confirmed that there are no errors, it will send these tasks to the thirteenth node and the fifteenth node.
[0086] The thirteenth node, the dependency parsing model, with the input being the code refactoring tasks output by the twelfth node. The thirteenth node performs the header file analysis task based on these tasks, determines the complete set of import declarations required for each target code segment, and sends these declaration sets to the fourteenth node.
[0087] The fourteenth node, the header file optimization model, takes as input the complete set of import declarations for each target code snippet provided by the thirteenth node. The task of the fourteenth node is to verify these sets of declarations, remove any redundant imports, and ensure that each set of declarations is both complete and free of excess content. If a problem is found, the thirteenth node will be notified to re - analyze; once it is confirmed that there are no errors, a header file statement dictionary will be generated.
[0088] The fifteenth node, the modular segmentation tool, takes as input the output of the twelfth node. The fifteenth node is responsible for dividing the initial code into a set of modular code snippets based on function boundaries, enabling each snippet to be refactored independently or step - by - step.
[0089] Step 106: Execute at least two code refactoring tasks in sequence to obtain each initial code snippet, and iteratively optimize each initial code snippet through static analysis and dynamic verification until candidate code snippets that conform to the function dependency relationship are generated.
[0090] It should be noted that candidate code snippets refer to the intermediate results or partially refactored code generated during the execution of the code refactoring tasks. These snippets need to conform to the function dependency relationship to ensure logical consistency; executing in sequence means completing each task in turn according to the sorting of the code refactoring tasks to avoid errors caused by unresolved dependencies.
[0091] Static analysis refers to discovering potential errors and dependency problems through syntax parsing and logical checks without executing the code. Dynamic verification refers to capturing the code execution results and comparing them with the expected output by actually running test cases to verify the functional correctness.
[0092] In actual implementation, the system executes each task one by one 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 at the same time ensures that the functions or modules it depends on have been refactored. If the current task involves multiple functions, the system will first process the functions upstream in the dependency chain and then gradually process the downstream functions. During the task execution, 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 method is to split the task into smaller subtasks and execute them step - by - step; another implementation method is to use an automated tool to assist in code modification to ensure the consistency of the refactoring.
[0093] Specifically, in an actual scenario, assume that the system needs to refactor a set of codes including 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 to generate candidate code snippets that meet the expectations; then executes the refactoring task for function B, using the refactored function C as a dependency to generate new candidate code snippets; finally executes the refactoring task for function A to generate the final candidate code snippets based on the refactored function B. Throughout the process, the system continuously verifies the correctness of the candidate code snippets to ensure that they satisfy the function dependency relationship.
[0094] In the embodiments of this specification, by executing the code refactoring tasks in sequence, it is possible to effectively avoid dependency conflicts, ensure the logical consistency of the generated candidate code snippets, and improve the reliability of the refactoring process. Generating 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 issues.
[0095] In an optional implementation manner of this embodiment, at least two code refactoring tasks are executed in sequence to obtain each initial code snippet, and each initial code snippet is iteratively optimized through static analysis and dynamic verification until candidate code snippets that conform to the function dependency relationship are generated, including: Construct a task through a prompt information construction model based on the execution information of at least two code refactoring tasks to obtain prompt information for prompting the code generation model to generate code snippets. Among them, 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 dependency libraries required for the target code to run, and the header file reference prompt words are used to prompt the code generation model to add the header files of the target code; Generate each initial code snippet that conforms to the function dependency relationship through the code generation model based on the prompt information; Perform static analysis and dynamic verification on each initial code snippet; In the case where the static analysis or the dynamic verification fails, input each initial code snippet into the iterative optimization model to obtain each optimized code snippet that passes the verification, and return the static analysis and dynamic verification of each initial code snippet; In the case where the static analysis and the dynamic verification pass, execute each optimized code snippet. In the case where each optimized code snippet is successfully executed, obtain each candidate code snippet.
[0096] It should be noted that executing the code refactoring tasks in sequence means processing each refactoring unit in turn according to the function dependency relationship to ensure that the pre-dependencies have been completed in the previous tasks; static analysis is used to verify the syntax structure, variable usage, and dependency integrity without running the code; dynamic verification is to further confirm whether the code behavior meets the expectations by executing test cases.
[0097] In actual implementation, the system sequentially executes at least two code refactoring tasks in the order of function dependencies to generate each initial code snippet. Subsequently, static analysis is performed on each initial code snippet to identify syntax errors, logical defects, or missing dependency declarations. If problems are found, the code snippet is corrected through an iterative optimization model, and static analysis is performed again until it passes the verification. After that, the code snippet that passes the static analysis is sent to the dynamic verification stage, where test cases are executed and the actual output results are captured and compared with the expected results. If the verification fails, it enters the iterative optimization process again for repair. This process is repeated until all code snippets pass both static analysis and dynamic verification, and finally, candidate code snippets that conform to the function dependencies are generated.
[0098] Specifically, in an actual scenario, a code refactoring task generates a code snippet that calls an external library function. Static analysis finds that it lacks the corresponding header file reference, and the system then triggers the optimization process to complete this reference. After optimization, static analysis is performed again without errors, and then it enters the dynamic verification stage to execute test cases to verify whether its functional performance meets the expectations. Only when all verification links pass will this code snippet be recognized as a qualified candidate code snippet.
[0099] In the embodiments of this specification, structural problems in the code can be detected early through static analysis, reducing ineffective execution and improving code quality.
[0100] In the embodiments of this specification, combined with dynamic verification, it can further ensure the correctness of the code in the actual running environment, enhancing the reliability and stability of the refactoring results.
[0101] In an optional implementation manner of this embodiment, after obtaining the prompt information for prompting the code generation model to generate code snippets by constructing a model based on the execution information of at least two code refactoring tasks through the prompt information, it further includes: In the case of detecting the prompt information output by the upstream node, use the downstream node corresponding to the upstream node to generate each initial code snippet that conforms to the function dependencies through the code generation model.
[0102] 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. The upstream node is responsible for outputting prompt information, while the downstream node generates initial code snippets based on this prompt information; the prompt information includes code dependency prompt words and header file reference prompt words, which are used to guide the code generation model to generate code that conforms to the function dependencies.
[0103] In actual implementation, after the upstream node (hint information construction model) detects and outputs hint information, the system uses the downstream node corresponding to the upstream node (code generation model) to generate an initial code snippet based on this hint information. The code dependency hint words in the hint information ensure that the generated code snippet can correctly import the dependency libraries required for the target code to run, while the header file reference hint words ensure that the generated code snippet contains the necessary header file declarations. This upstream and downstream collaboration mechanism can ensure that the hint information is accurately transmitted and applied to the code generation process, thereby improving the quality and standardization of the generated code.
[0104] Specifically, in an actual scenario, assume that the upstream node generates hint information, such as "import math library" as the code dependency hint word and "include <stdio.h>" as the header file reference hint word. After the downstream node receives this hint information, the code generation model generates an initial code snippet according to the hint. For example, for a refactoring task, the code generation model may generate a code snippet containing #include <stdio.h> and import math, while ensuring that this snippet conforms to the function dependency relationship. In this way, the hint information can effectively guide the code generation process and avoid omitting necessary dependencies or header files.
[0105] In the embodiments of this specification, through the collaborative work between the upstream and downstream nodes, it can be ensured that the hint information is accurately applied to the code generation process, improving the standardization and logical consistency of the generated code.
[0106] Furthermore, the upstream and downstream relationships of the nodes can be arranged as follows: The sixteenth node, the hint information construction model, takes at least two code refactoring tasks as input. 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 required, they are obtained through the seventeenth node; if dependency import hint words are required, they are extracted through the eighteenth node. The generated hint information is then sent to the nineteenth node as its input.
[0107] The seventeenth node, the header file dictionary query model, takes the information requesting header file reference hint words from the sixteenth node as input. 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.
[0108] The eighteenth node, the dependency code extraction model, takes the information requesting dependency import hint words from the sixteenth node as input. This node analyzes the target code to extract necessary dependency codes as dependency hint words and returns these hint words to the sixteenth node for constructing hint information.
[0109] The nineteenth node, the code generation model, takes the hint information output by the sixteenth node as input. This node generates initial code segments that conform to the functional dependencies based on the hint information and sends these initial code segments to the twentieth node (static analysis model) for further inspection.
[0110] The twentieth node, the static analysis model, takes the initial code segments from the nineteenth node as input. This node performs static analysis on these code segments to verify whether the syntax, logic, and dependencies are correct. If the static analysis fails, the code segments are sent to the twenty-second node for correction. If the static analysis passes, the code segments are sent to the twenty-first node for actual execution testing.
[0111] The twenty-first node, the code execution model, takes the code segments that have passed the static analysis output by the twentieth node as input. This node is responsible for executing these code segments. In the case of successful execution, candidate code segments are generated; if the execution fails, the code segments are fed back to the twenty-second node for further optimization.
[0112] The twenty-second node, the iterative optimization model, takes the code segments that have failed the static analysis from the twentieth node or the code segments that have failed the execution from the twenty-first node as input. This node performs multiple iterations of optimization on these code segments until the requirements are met. The optimized code segments are sent back to the twentieth node for static analysis until all validations are passed and finally become candidate code segments.
[0113] Step 108: Write test cases for each candidate code segment based on the code test plan, and test each candidate code segment based on each test case to obtain multiple target code segments that conform to the functional dependencies.
[0114] It should be noted that the test case is a specific test plan used to verify the functional correctness and logical consistency of the candidate code segment; the target code segment refers to the code part that conforms to the functional dependencies and meets the expected functions after being verified by the test. These segments will be used as the basis for the final refactored code.
[0115] In actual implementation, the system writes test cases for each candidate code snippet based on the code test plan. The design of test cases needs to cover the core functions, boundary conditions of the candidate code snippet, and its interaction logic with dependent functions. For unit tests, test cases can verify the functions of a single candidate code snippet; for integration tests, test cases need to simulate the call scenarios of the candidate code snippet and its dependent functions to ensure the correctness of the function dependencies. During the testing process, the system executes each test case, records the test results, and locates and fixes problems for candidate code snippets that fail the test. For complex test scenarios, one implementation method is to use automated testing tools to generate test data and execute tests; another implementation method is to combine manual analysis to design test cases for specific scenarios to improve test coverage.
[0116] 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 functions; 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 between A and B meets the requirements. During the testing process, if a candidate code snippet fails the test, the system will locate and fix the problem according to the test results until all candidate code snippets pass the test, forming multiple target code snippets that conform to the function dependencies.
[0117] In the embodiments of this specification, by writing test cases based on the code test plan and executing tests, the correctness of candidate code snippets can be comprehensively verified, and the functional integrity of the target code snippets can be ensured. Obtaining target code snippets that conform to the function dependencies can provide a high-quality code foundation for subsequent code integration and reduce the rework cost caused by logical errors.
[0118] In an optional implementation manner of this embodiment, test cases for each candidate code snippet are written based on the code test plan, and each candidate code snippet is tested based on each test case to obtain multiple target code snippets that conform to the function dependencies, including: Generating test cases for each candidate code snippet including expected output results by a test generation model based on the code test plan; Running the test case set by a test execution model and capturing the actual output results; Comparing the actual output results with the expected output results to determine the failed cases; Performing parameter adjustment and logic correction operations on the candidate code snippet corresponding to the failed case by a test repair model to obtain optimized code snippets; Updating each candidate code snippet based on the optimized code snippets to obtain multiple target code snippets that conform to the function dependencies.
[0119] It should be noted that test cases are specific test scenarios used to verify the functional correctness and logical consistency of candidate code snippets; the target code snippet refers to the code part that meets the function dependency relationship and satisfies the expected function after being verified by tests. The test generation model, test execution model, and test repair model together constitute a complete test and optimization process to ensure that candidate code snippets reach high-quality standards after multiple iterations of optimization.
[0120] In actual implementation, the system first generates test cases for each candidate code snippet based on the code test plan through the test generation model. These test cases contain clear input data and expected output results to verify whether the functions of the candidate code snippets meet the expectations. Subsequently, the test execution model runs the test case set and captures the actual output results of each candidate code snippet. The system identifies the failed test cases and their corresponding candidate code snippets by comparing the actual output results with the expected output results. For the failed test cases, the system uses the test repair model to perform parameter adjustment and logical correction operations on the candidate code snippets to eliminate errors and generate optimized code snippets. Finally, the system updates the candidate code snippet set based on the optimized code snippets to obtain multiple target code snippets that meet the function dependency relationship.
[0121] Specifically, in an actual scenario, assume that the system generates three candidate code snippets A, B, and C and designs corresponding test cases for them. After the test execution model runs these test cases, it is found that the actual output result of a certain test case for snippet B does not match the expectation. After the test repair model analyzes the reason for the failure, it performs logical correction on snippet B (such as adjusting conditional judgments or modifying variable assignments) to generate the optimized code snippet B'. The system adds the optimized B' back to the candidate code snippet set, and finally obtains a set of target code snippets that meet the function dependency relationship.
[0122] In the embodiments of this specification, generating test cases containing expected output results through the test generation model can provide clear verification criteria for subsequent tests, improving the pertinence and effectiveness of tests. Using the test execution model to run test cases and capture actual output results can comprehensively evaluate the functional correctness of candidate code snippets and promptly discover potential problems. By using the test repair model to perform parameter adjustment and logical correction on failed test cases, the quality of candidate code snippets can be effectively improved, reducing the risk of rework caused by logical errors. Updating the candidate code snippet set based on the optimized code snippets can ensure that the finally generated target code snippets not only meet the function dependency relationship but also satisfy the functional requirements, providing high-quality results for code refactoring.
[0123] In one embodiment of the present invention, through systematic code analysis and function dependency sorting, the initial code refactoring task is decomposed into ordered subtasks, ensuring that the generated candidate code snippets conform to the logical dependency relationships. At the same time, each snippet is verified based on the test plan, and finally multiple high-quality, functionally correct, and structurally clear target code snippets are obtained, effectively improving the efficiency and reliability of code refactoring.
[0124] The following combines the attached Figures 2 - 5 , taking the application of the code refactoring method provided by the present invention in a multi-agent system as an example, further illustrates the code refactoring method.
[0125] Figure 2 Figure 1 is a schematic diagram of data interaction of the first code refactoring system provided by an embodiment of the present invention, specifically including: Initial code: On the far left 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 the initial code based on the initial language. These files will undergo subsequent processing to generate a final set of target codes.
[0126] Code analysis agent: The code analysis agent is responsible for initially analyzing the initial code, extracting information such as code structure and function dependency relationships, and generating a target refactoring plan and a code test plan. The target refactoring plan is sent to the task scheduling agent, and the code test plan is sent to the code testing agent. The code analysis agent utilizes the code analysis ability of the large model to ensure the integrity and efficiency of the refactoring plan.
[0127] Task scheduling agent: The task scheduling agent splits the code refactoring task into an executable subtask sequence according to the target refactoring plan provided by the code analysis agent. It clarifies the specific input and output of each task and assigns a corresponding code generation agent to each task. The task scheduling agent ensures that the dependency relationships between tasks are correctly processed, thus ensuring the orderly progress of the code refactoring process.
[0128] The initial code is split according to the code refactoring task and correspondingly input into the code generation agents arranged by the code scheduling agent.
[0129] Code generation agent: The code generation agent is responsible for generating specific candidate code snippets according to the tasks assigned by the task scheduling agent. The code generation agent utilizes the code generation ability of the large model to ensure that the generated code conforms to the target language specification and is logically correct. The code generation agent is also responsible for automatic debugging, locating and fixing defects and problems in the code.
[0130] Code Testing Agent: The code testing agent is responsible for the quality assurance of the system, ensuring that the developed functions work as expected and identifying potential problems. This agent writes integration test cases based on the code testing scenarios sent by the code analysis agent and runs these test cases to check if the code meets the expectations. The code testing agent leverages the capabilities of large models to help generate test cases, automate the testing process, and automatically fix certain known problems during the testing process, thereby improving the testing efficiency and quality.
[0131] Target Code Snippets: At the end of the entire process, after all development and testing work is completed, the final output is multi-fragment target code. The target code is the comprehensive result of various stages of architecture design, development, and testing. With the optimization and support of AI agents, it meets high-quality and reliable standards and is ready for deployment.
[0132] Figure 3 It is a data interaction schematic diagram of the second code refactoring system provided by an embodiment of the present invention. The code analysis agent specifically includes: The initial code analysis agent parses elements such as 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 function and behavior of the code, and pays special attention to putting the code that is not within any function into a function named __others__ to ensure that subsequent steps can be based on accurate code structure information.
[0133] The target code structure agent designs the target code structure and target functions using a large model, determines the overall architecture of the refactored code, including the design of the directory structure, and simultaneously designs functions or variables that conform to the target language specification and are logically consistent. This agent also adjusts the code structure according to requirements to ensure its good maintainability and extensibility, thereby generating a detailed target code structure design plan.
[0134] The code testing plan writing agent designs a code testing plan for the functional requirements of the target code with the help of a large model and ensures that all functional paths are fully tested. It not only focuses on creating a comprehensive testing plan, including specific test inputs and expected outputs, but also aims to improve the test coverage to ensure the correctness of the code in various scenarios.
[0135] The function dependency analysis agent uses the chain of thought technique to identify and analyze the function dependencies in the target code and establish a detailed function dependency list. It deeply checks the correctness of the data flow and control flow, avoids circular dependencies or unreasonable dependency relationships, and finally forms a function dependency list with clear priorities.
[0136] The verification initial code analysis result agent validates and optimizes the output of the initial code analysis agent, ensuring that no key code logic or structure is overlooked and guaranteeing the accuracy of subsequent work. It uses the chain of thought technique to meticulously review the analysis results and provides timely feedback upon discovering issues, ensuring that every detail is precisely captured.
[0137] The verification target code structure design agent verifies whether the target code structure and target functions designed by the optimized target code structure agent are reasonable and efficient. By comparing the target code structure with the original code functionality, it ensures that the migrated code can fully implement the functionality of the original code. It also conducts a rigorous code review of the design scheme to ensure compliance with best practices and programming specifications, laying the foundation for high-quality code.
[0138] The verification code test plan agent validates the code test plan output by the optimized code test plan writing agent, ensuring that all key functions and boundary conditions are covered. It uses the chain of thought technique to verify the rationality of test data, ensuring the reliability of the test process, and provides optimization suggestions when deficiencies are found to improve the overall test efficiency and quality.
[0139] The verification function dependency relationship agent confirms that all dependencies in the function dependency relationship output by the function dependency relationship analysis agent can be correctly implemented by carefully examining the call relationship graph between functions, preventing the occurrence of unnecessary circular dependencies or undefined dependencies. It uses the chain of thought technique to optimize the dependency relationship, ensuring the smooth transfer of data and task execution and guaranteeing the coordinated operation within the code.
[0140] Finally, through the collaboration of each agent, a target refactoring plan containing dependency relationships is generated. Table 1 is an example: Table 1: Target refactoring plan containing the initial code and target code, and the dependency relationships between source functions and target functions
[0141] Figure 4 is the data interaction schematic diagram of the third code refactoring system provided by an embodiment of the present invention. The task scheduling agent specifically includes: The function dependency sorting agent, according to the target refactoring plan, generates a source-target function mapping sequence table sorted by dependency relationship by calling the large model to execute the SortTargetFunctions operation. The functions in the table are sorted by dependency and sent concurrently to the verification dependency sorting agent. Table 2 is an example: Table 2: Example of function mapping sequence table
[0142] The task generation and management agent divides tasks based on the source and target function mapping table sorted by the objective function output by the verification dependency sorting agent, obtaining at least two code refactoring tasks, ensuring that each code refactoring task has clear initial code, source function, and corresponding target code and target function, and verifying them to two code refactoring task agents. Table 3 is an example: Table 3: Task list of at least two code refactoring tasks
[0143] The header file import management agent writes import statements for each target code according to the source-target function mapping sequence table sorted by dependency relationship, and verifies the integrity and necessity of the import statements.
[0144] The code splitting agent splits the initial code into multiple code segments according to the division of the source function in the task list. The code not included in any function is written into one or more __others__ code segments according to the length, and the set of split code segments is verified.
[0145] The verification dependency sorting agent calls the large model through the chain of thought, verifies the dependencies of the sorting results in the source table line by line, examines the correctness of the dependency relationships between functions, and provides optimization suggestions to return to the function dependency sorting agent or, in the case of confirming that the verification passes, sends the source and target function mapping table sorted by the objective function to the task generation and management agent.
[0146] The agent for verifying at least two code refactoring tasks calls the large model through the chain of thought, verifies at least two code refactoring tasks line by line, ensures the accuracy of task division and sorting, and provides optimization suggestions to return to the task generation and management agent or, in the case of confirming that the verification passes, sends at least two code refactoring tasks to the header file import management agent.
[0147] The verification import statement agent calls the large model through the chain of thought to verify and optimize the import statements written by the header file import management agent, verifies its integrity and optimization space, and provides optimization suggestions to return to the header file import management agent, or generates an import statement dictionary in the case of passing the verification.
[0148] Figure 5 It is a data interaction schematic diagram of the fourth code refactoring system provided by an embodiment of the present invention. The code generation agent specifically includes: The context prompt word construction agent is responsible for constructing the context environment of code refactoring. Without relying on the output of other agents, it works directly from the initial code snippet, calls the large model, and selects a suitable model refactoring prompt word template according to the languages of the initial code and target code and the code dependency situation.
[0149] In the case where the reference header of the target code is required, the header file injection agent is triggered during context construction. If the context requires it, reference header statements will be prepared for the target code, extracting the necessary reference header statements from the reference header dictionary of each file prepared in advance by the task orchestration agent.
[0150] In the case where the target code needs to extract dependent code, the dependency import agent is also triggered during context construction. If the context requires it, the dependent code of the generated target code will be extracted for the target code, and all necessary dependencies will be correctly imported.
[0151] The code generation agent is triggered after the context prompt is generated, and the chain of thought calls the large model according to the context prompt to generate code.
[0152] The static analysis agent uses a static analysis tool to perform static analysis on the code after code generation or optimization. The analysis tool depends on the language. Taking Python as an example, the Pylint tool can be used to analyze the code quality and ensure that the code meets the preset coding standards, and give feedback when it does not meet the requirements.
[0153] The code execution agent is triggered after successful static analysis and is responsible for executing the code. It calls tools to run the code and verifies the correctness of the code. Necessary tools will be used to set up the running environment when executing the code. Taking Python as an example, the root directory where the code is generated will be configured as the Python code execution path so that the file can be found during execution. The execution result will be sent to the large model to determine whether there is an exception. If there is an exception, the code execution fails; otherwise, the code execution is successful and the target code is obtained.
[0154] The code optimization agent is triggered when the static analysis fails or the code execution fails. It will feed the feedback to the large model, allowing the large model to continuously optimize the generated code until it passes all verifications.
[0155] Through the above Figures 2 - 5 The code refactoring system shown solves large file tasks through code splitting and dependency analysis, ensuring that the target code not only conforms to the target language specification but also is logically correct and executable, and improving the efficiency and accuracy of code refactoring through the collaborative work of the multi-agent system, significantly reducing manual intervention, achieving efficient and automated cross-language code conversion, and greatly reducing time and labor costs.
[0156] Corresponding to the above method embodiments, the present invention also provides code refactoring system embodiments, Figure 6 showing a structural schematic diagram of a code refactoring system provided by an embodiment of the present invention. As Figure 6 shown, the system includes: The code analysis module 602 is configured to perform code analysis on the initial code to be refactored and generate a target refactoring solution. The target refactoring solution includes a code refactoring plan and a code testing plan. The code refactoring plan includes the function dependency relationship of the target code.
[0157] Optionally, the code analysis module 602 is further configured to analyze the function elements in the initial code through a code analysis model to obtain a list of function variables of the initial code; analyze the list of function variables of the initial code through a structure design model to generate a target code structure design in the target language; identify the function calls of the target code in the target code structure design through a first dependency analysis model to obtain the function dependency relationship of the target code; and generate a corresponding code testing plan based on the target code structure design through a test generation model.
[0158] Optionally, the code analysis module 602 is further configured to verify the list of function variables of the initial code through a second dependency analysis model; if there are missing function variables, return to analyze the function elements in the initial code through the code analysis model to obtain the list of function variables of the initial code until the function variable verification pass condition is met; verify the structure design of the target code through a structure evaluation model; if there are structural defects, return to analyze the list of function variables of the initial code through the structure design model to generate a target code structure design in the target language until the code structure verification pass condition is met; verify the function dependency relationship of the target code through a dependency verification model; if there are incorrect dependencies, return to identify the function calls of the target code in the target code structure design through the first dependency analysis model to obtain the function dependency relationship of the target code until the dependency relationship verification pass condition is met; verify the code testing plan through a test verification model; if there is untested logic, return to generate a corresponding code testing plan based on the target code structure design through the test generation model until the test verification pass condition is met.
[0159] Optionally, the code analysis module 602 is further configured to, when detecting that the upstream node outputs the list of function variables of the initial code, use the downstream node corresponding to the upstream node to verify the list of function variables of the initial code through the second dependency analysis model; if there are missing function variables, return to the upstream node until the function variable verification pass condition is met.
[0160] The task scheduling 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, where the at least two code refactoring tasks are sorted according to the function dependency relationship.
[0161] Optionally, the task orchestration module 604 is further configured to perform dependency resolution through a topological sorting model based on function dependencies to obtain a function mapping sequence list arranged in the calling order; perform a task splitting operation through a task decomposition model based on the function mapping sequence list to obtain at least two code refactoring tasks sorted according to function dependencies; perform a header file analysis task through a dependency resolution model based on the at least two code refactoring tasks to obtain a complete import statement set for each target code segment; and segment the initial code into a modular code segment set divided by function boundaries.
[0162] Optionally, the task orchestration module 604 is further configured to verify the function mapping sequence list through a mapping verification model; if there is an abnormal mapping, then return to perform a dependency resolution task through a topological sorting model based on function dependencies to obtain a function mapping sequence list arranged in the calling order until the function mapping verification pass condition is reached; verify the at least two code refactoring tasks through a completeness detection model; if there are uncovered code refactoring tasks, then return to perform a splitting task through a task decomposition model based on the function mapping sequence list to obtain at least two code refactoring tasks including the correspondence between the source function and the target function until the task coverage condition is reached; verify the complete import statement set through a header file optimization model; if there are redundant imports, then return to perform a header file analysis task through a dependency resolution model based on the at least two code refactoring tasks to obtain a complete import statement set for each target code segment until the import verification pass condition is reached.
[0163] The code generation module 606 is configured to sequentially execute at least two code refactoring tasks 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 conforms to the function dependencies is generated.
[0164] Optionally, the code generation module 606 is further configured to perform an information construction task based on the at least two code refactoring tasks through a prompt information construction model to obtain prompt information for prompting the code generation model to generate code segments, where the prompt information includes a code dependency prompt word and a header file reference prompt word. The code dependency prompt word is used to prompt the code generation model to import the dependency libraries required for the target code to run, and the header file reference prompt word is used to prompt the code generation model to add the header files of the target code; generate each initial code segment that conforms to the function dependencies through the code generation model based on the prompt information; perform static analysis on each initial code segment; in the case where the static analysis fails, then input each code segment into an iterative optimization model to obtain each optimized code segment that passes the verification; execute each optimized code segment, and in the case where each optimized code segment is successfully executed, obtain each candidate code segment.
[0165] 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 multiple target code snippets that conform to the function dependency relationship.
[0166] Optionally, the code testing module 608 is further configured to generate test cases for each candidate code snippet including expected output results based on the code testing plan through a test generation model; run the test case set through a test execution model and capture the actual output results; compare the actual output results with the expected output results to determine the failing cases; perform parameter adjustment and logic correction operations on the candidate code snippets corresponding to the failing cases through a test repair model to obtain optimized code snippets; update each candidate code snippet based on the optimized code snippets to obtain multiple target code snippets that conform to the function dependency relationship.
[0167] This embodiment of the present disclosure provides a code refactoring system. Through systematic code analysis and function dependency relationship sorting, the initial code refactoring task is decomposed into ordered subtasks to ensure that the generated candidate code snippets conform to the logical dependency relationship. At the same time, each snippet is verified based on the test plan, and finally multiple target code snippets with high quality, correct functionality, and clear structure are obtained, effectively improving the efficiency and reliability of code refactoring.
[0168] The above is a schematic solution of a code refactoring system according to this embodiment. It should be noted that the technical solution of this code refactoring system and the technical solution of the above code refactoring method belong to the same concept. For the details not described in detail in the technical solution of this code refactoring system, reference can be made to the description of the technical solution of the above code refactoring method.
[0169] Figure 7 The structural block diagram of a computing device 700 according to an embodiment of the present invention is shown. 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 to the memory 710 through a bus 730, and the large model 750 is used to execute related tasks of code refactoring.
[0170] 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 such networks include the Public Switched Telephone Network (PSTN), Local Area Network (LAN), Wide Area Network (WAN), Personal Area Network (PAN), or a combination of communication networks such as the Internet. The access device 740 may include one or more of any type of wired or wireless network interface (e.g., a network interface card (NIC)), 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) interface.
[0171] In one embodiment of the present invention, the above components of the computing device 700 and Figure 7 other components not shown therein may also be connected to each other, for example, via a bus. It should be understood that Figure 7 the block diagram of the computing device shown is for illustrative purposes only and is not a limitation on the scope of the present invention. Those skilled in the art may add or replace other components as needed.
[0172] 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 smartwatch, smart glasses, etc.) or other types of mobile devices, 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.
[0173] Among them, the processor 720 is used to execute the following computer program / instructions, and when the computer program / instructions are executed by the processor, the steps of the above code reconstruction method are implemented.
[0174] Each embodiment in the present invention is described in a progressive manner. For the same or similar parts among the embodiments, reference can be made to each other. Each embodiment focuses on the differences from other embodiments. In particular, for the embodiment of the computing device, since it is basically similar to the embodiment of the code refactoring method, the description is relatively simple. For the relevant parts, reference can be made to the partial description of the embodiment of the code refactoring method.
[0175] An embodiment of the present invention further provides a computer-readable storage medium, which stores computer programs / instructions. When the computer programs / instructions are executed by a processor, the steps of the above-mentioned code refactoring method are implemented.
[0176] Each embodiment in the present invention is described in a progressive manner. For the same or similar parts among the embodiments, reference can be made to each other. Each embodiment focuses on the differences from other embodiments. In particular, for the embodiment of the computer-readable storage medium, since it is basically similar to the embodiment of the code refactoring method, the description is relatively simple. For the relevant parts, reference can be made to the partial description of the embodiment of the code refactoring method.
[0177] An embodiment of the present invention further provides a computer program product, including computer programs / instructions. When the computer programs / instructions are executed by a processor, the steps of the above-mentioned code refactoring method are implemented.
[0178] The above is a schematic solution of a computer program product of this embodiment. It should be noted that the technical solution of this computer program product and the technical solution of the above-mentioned code refactoring method belong to the same concept. For the details not described in detail in the technical solution of the computer program product, reference can be made to the description of the technical solution of the above-mentioned code refactoring method.
[0179] The above specifically describes a particular embodiment of the present invention. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims may be executed in a different order than in the embodiments and still achieve the desired result. Additionally, the processes depicted in the figures do not necessarily require the particular order or sequential order shown to achieve the desired result. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0180] The computer instructions include computer program code, which may be in the form of source code, object code, executable files, or some intermediate forms, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording media, USB flash drives, external hard drives, magnetic disks, optical discs, computer memories, read-only memories (ROMs), random access memories (RAMs), electrical carrier signals, telecommunication signals, and software distribution media, etc. It should be noted that the content included in the computer-readable medium can be appropriately increased or decreased according to the requirements of patent practice. For example, in some regions, according to patent practice, the computer-readable medium does not include electrical carrier signals and telecommunication signals.
[0181] It should be noted that for the foregoing method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations. However, those skilled in the art should be aware that the embodiments of the present invention are not limited by the described action sequence, because according to the embodiments of the present invention, some steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily essential to the embodiments of the present invention.
[0182] In the above embodiments, the descriptions of the various embodiments have their own emphases. For the parts not detailed in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0183] The preferred embodiments of the present invention disclosed above are only used to help explain the present invention. The optional embodiments do not describe all the details in detail, nor do they limit the invention to the specific embodiments described. Obviously, many modifications and changes can be made according to the content of the embodiments of the present invention. The present invention selects and specifically describes these embodiments to better explain the principles and practical applications of the embodiments of the present invention, so that those skilled in the art can understand and utilize the present invention well. The present invention is only limited by the claims and their full scope and equivalents.
Claims
1. A code refactoring method, characterized in that, Including: Performing code analysis on the initial code to be refactored to generate a target refactoring plan, where the target refactoring plan includes a code refactoring plan and a code testing plan, and the code refactoring plan includes the function dependency relationship of the target code; Analyzing the function dependency relationship in the code refactoring plan to generate at least two code refactoring tasks corresponding to the initial code, where the at least two code refactoring tasks are sorted according to the function dependency relationship; 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 conforms to the function dependency relationship is generated; Writing test cases for each candidate code segment based on the code testing plan, and testing each candidate code segment based on each test case to obtain multiple target code segments that conform to the function dependency relationship.
2. The method according to claim 1, characterized in that, The code refactoring plan includes the target code structure design and the function dependency relationship; The performing code analysis on the initial code to be refactored to generate a target refactoring plan includes: Analyzing the function elements in the initial code through a code analysis model to obtain the function variable list of the initial code; Analyzing the function variable list of the initial code through a structure design model to generate a target code structure design in the target language; Identifying the function calls of the target code in the target code structure design through a first dependency analysis model to obtain the function dependency relationship of the target code; Based on the target code structure design, generating a corresponding code testing plan through a test generation model.
3. The method according to claim 2, wherein After the analyzing the function elements in the initial code through a code analysis model to obtain the function variable list of the initial code, it further includes: Verifying the function variable list of the initial code through a second dependency analysis model; if there are missing function variables, then return to analyze the function elements in the initial code through the code analysis model to obtain the function variable list of the initial code until the function variable verification passes condition is met; After the analyzing the function variable list of the initial code through a structure design model to generate a target code structure design in the target language, it further includes: Verifying the structure design of the target code through a structure evaluation model; if there are structural defects, then return to analyze the function variable list of the initial code through the structure design model to generate a target code structure design in the target language until the code structure verification passes condition is met; After the identifying the function calls of the target code in the target code structure design through a first dependency analysis model to obtain the function dependency relationship of the target code, it further includes: Verifying the function dependency relationship of the target code through a dependency verification model; if there are incorrect dependencies, then return to identify the function calls of the target code in the target code structure design through the first dependency analysis model to obtain the function dependency relationship of the target code until the dependency relationship verification passes condition is met; After designing based on the target code structure and generating a corresponding code test plan through a test generation model, the following steps are further included: Verify the code test plan through a test verification model; if there is un-covered test logic, return to design based on the target code structure and generate a corresponding code test plan through a test generation model until the test verification pass condition is met.
4. The method according to any one of claims 2-3, characterized in that After analyzing the function elements in the initial code through a code analysis model to obtain a function variable list of the initial code, the following steps are further included: When detecting that the upstream node outputs the function variable list of the initial code, use the downstream node corresponding to the upstream node to verify the function variable list of the initial code through a second dependency analysis model; If there are missing function variables, return to the upstream node until the function variable verification pass condition is met.
5. The method according to claim 1, characterized in that Analyzing the function dependency relationships in the code refactoring plan to generate at least two code refactoring tasks corresponding to the initial code includes: Performing dependency relationship parsing based on the function dependency relationships through a topological sorting model to obtain a function mapping sequence list arranged in the call order; Performing task splitting operations based on the function mapping sequence list through a task decomposition model to obtain at least two code refactoring tasks sorted according to the function dependency relationships; Performing a header file analysis task based on the at least two code refactoring tasks through a dependency parsing model to obtain a complete import declaration set for each target code segment; Segmenting the initial code into a modular code segment set divided by function boundaries.
6. The method according to claim 5, characterized in that, After performing a dependency relationship parsing task based on the function dependency relationships through a topological sorting model to obtain a function mapping sequence list arranged in the call order, the following steps are further included: Verifying the function mapping sequence list through a mapping verification model; if there are abnormal mappings, return to perform a dependency relationship parsing task based on the function dependency relationships through a topological sorting model to obtain a function mapping sequence list arranged in the call order until the function mapping verification pass condition is met; After performing a splitting task based on the function mapping sequence list through a task decomposition model to obtain at least two code refactoring tasks including the corresponding relationships between the initial functions and the target functions, the following steps are further included: Verifying the at least two code refactoring tasks through a completeness detection model; if there are un-covered code refactoring tasks, return to perform a splitting task based on the function mapping sequence list through a task decomposition model to obtain at least two code refactoring tasks including the corresponding relationships between the initial functions and the target functions until the task coverage condition is met; After performing a header file analysis task based on the at least two code refactoring tasks through a dependency parsing model to obtain a complete import declaration set for each target code segment, the following steps are further included: Verifying the complete import declaration set through a header file optimization model; if there are redundant imports, return to perform a header file analysis task based on the at least two code refactoring tasks through a dependency parsing model to obtain a complete import declaration set for each target code segment until the import verification pass condition is met.
7. The method according to claim 1, wherein Sequentially executing the at least two code refactoring tasks to obtain respective initial code segments, and iteratively optimizing the respective initial code segments through static analysis and dynamic verification until candidate code segments that conform to the function dependency relationship are generated, including: Constructing a task based on the execution information of 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, wherein the prompt information includes a code dependency prompt word and a header file reference prompt word, the code dependency prompt word is used to prompt the code generation model to import the dependency libraries required for running the target code, and the header file reference prompt word is used to prompt the code generation model to add the header files of the target code; Generating respective initial code segments that conform to the function dependency relationship through a code generation model based on the prompt information; Performing static analysis and dynamic verification on the respective initial code segments; In the case where the static analysis or the dynamic verification fails, inputting the respective initial code segments into an iterative optimization model to obtain respective optimized code segments that pass the verification, and returning the performing static analysis and dynamic verification on the respective initial code segments; In the case where the static analysis and the dynamic verification pass, executing the respective optimized code segments, and in the case where the respective optimized code segments are successfully executed, obtaining respective candidate code segments.
8. The method according to claim 1, characterized in that, Writing test cases for the respective candidate code segments based on the code test plan, and testing the respective candidate code segments based on the respective test cases to obtain multiple target code segments that conform to the function dependency relationship, including: Generating test cases for the respective candidate code segments including expected output results through a test generation model based on the code test plan; Running the test case set through a test execution model and capturing the actual output results; Comparing the actual output results with the expected output results to determine failed test cases; Performing parameter adjustment and logic correction operations on the candidate code segments corresponding to the failed test cases through a test repair model to obtain optimized code segments; Updating the respective candidate code segments based on the optimized code segments to obtain multiple target code segments that conform to the function dependency relationship.
9. A code refactoring system, characterized in that, Including: A code analysis module configured to perform code analysis on the initial code to be refactored to generate a target refactoring plan, wherein the target refactoring plan includes a code refactoring plan and a code test plan, and the code refactoring plan includes the function dependency relationship of the target code; A task scheduling module configured to analyze the function dependency relationship 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 relationship; A code generation module configured to sequentially execute the at least two code refactoring tasks to obtain respective initial code segments, and iteratively optimize the respective initial code segments through static analysis and dynamic verification until candidate code segments that conform to the function dependency relationship are generated; A code testing module, configured to write test cases for each candidate code snippet based on the code testing plan, test each candidate code snippet based on each test case, and obtain multiple target code snippets that conform to the function dependency relationship.
10. A computing device, characterized in that, 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. When the computer programs / instructions are executed by the processor, the steps of the code refactoring method according to any one of claims 1-8 are implemented.
11. A computer-readable storage medium, characterized in that, It stores computer programs / instructions. When the computer programs / instructions are executed by the processor, the steps of the code refactoring method according to any one of claims 1-8 are implemented.
12. A computer program product, characterized in that, Comprising computer programs / instructions. When the computer programs / instructions are executed by the processor, the steps of the code refactoring method according to any one of claims 1-8 are implemented.
Citation Information
Patent Citations
Binary code taint analysis system and method
CN116089299A
Software testing method and device, computer equipment and readable storage medium
CN118779234A
Code generation method, device and equipment based on multi-agent language system
CN119065650A
Automatic incremental compiling method and system for Java development
CN119396406A
Adaptive code annotation updating method and system, terminal and storage medium
CN119576363A
Cited By
Code optimization method, electronic equipment, storage medium and program product
CN120540966A
Code generation method and system based on large model
CN120763072A
Code generation method and device, computer equipment, storage medium and product
CN121209887A
Code generation method and device, computer device, storage medium and product
CN121209887B
