An Automatic Repair Method for Method Call Defects in Object-Oriented Programs
The integration of deep learning and genetic algorithms enhances the repair of method invocation defects in object-oriented programs by training a neural machine translation model and refining candidate patches, improving defect repair rates.
Patent Information
- Application Number
- CN202311097295.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-29
- Publication Date
- 2025-07-15
- Estimated Expiration
- 2043-08-29
AI Technical Summary
The existing software automatic defect repair technology lacks targeting, resulting in a low defect repair rate, especially in object-oriented programs, which are not effective in defect repair.
Combining deep learning and genetic algorithms, the defect repair model is called through training methods, candidate patches are generated and verified, and the repair process is optimized by using CNN and abstract syntax tree, and the fitness calculation and genetic algorithm are combined to optimize candidate patch generation.
Improve the repair rate of method call defects in object-oriented programs, achieving more efficient defect repair effects.
Smart Images

Figure BDA0004419544930000051 
Figure HDA0004419544940000011
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of software debugging in software engineering, and particularly relates to a method for automatically repairing method call defects in object-oriented programs. Background Art
[0002] In the process of software development and software maintenance, when a defect is found, software debugging is required to locate and repair the defect in order to improve the quality of the software. However, repairing software defects requires a large amount of human cost. With the gradual increase in the scale and complexity of modern software, traditional manual debugging methods are facing challenges. In order to reduce the debugging pressure of developers and reduce the cost of software defect repair, researchers have proposed software defect automatic repair methods, which have received extensive attention and application.
[0003] Software defect automatic repair technologies include repair technologies based on search, repair templates, semantic driving, and deep learning. Most of the existing software defect automatic repair technologies discuss general solutions and lack specific analysis of different defect types, resulting in weak pertinence of defect repair, low defect repair rate, and poor repair effect. Summary of the Invention
[0004] In order to improve the defect repair effect, the present invention proposes a novel method for automatically repairing method call defects based on the combination of deep learning and genetic algorithms for method call defects with a relatively high occurrence probability in object-oriented programs. The specific repair process includes the following three steps:
[0005] Step 1, training a method call defect repair model based on deep learning;
[0006] The said Step 1, training a method call defect repair model based on deep learning, includes the following steps:
[0007] Step 1-1, collecting method call defect data and performing preprocessing;
[0008] Preparing a data set for training the model. Moreover, the deep learning technology used in the present invention belongs to the category of neural machine translation in natural language processing. A typical neural machine translation method takes token vectors as input, and tokens are obtained through tokenization. Therefore, the collected data needs to be preprocessed into a token sequence before being input into the model.
[0009] Step 1-2, selecting a neural machine translation model based on CNN;
[0010] The defect automatic repair method based on the neural machine translation model can automatically learn the complex relationships between input and output sequences that are difficult to capture manually; in this invention, a convolutional neural network is used as the main component of the encoder and decoder because it can better capture the dependency relationships of defect repair than RNN and LSTM.
[0011] Step 1-3, the training and evaluation method calls the defect repair model;
[0012] Train and evaluate the method call defect repair model on the method call defect dataset, determine the final model parameters, and obtain the method call defect repair model.
[0013] Step 2, use the defect repair model generated in Step 1 to generate candidate patches and verify them;
[0014] The said Step 2, using the defect repair model generated in Step 1 to generate candidate patches and verify them, includes the following steps:
[0015] Step 2-1, preprocess the defective line code and defect context of the program to be repaired;
[0016] Preprocess the defective line code and defect context of the program to be repaired and convert them into a token sequence.
[0017] Step 2-2, load the defect repair model and generate candidate patches;
[0018] Step 2-3, verify the candidate patches. If the candidate patches are valid, output the patches and the defect repair is successful. If the candidate patches are invalid, execute Step 3;
[0019] Step 3, generate candidate patches based on the genetic algorithm;
[0020] The said Step 3, generating candidate patches based on the genetic algorithm, includes the following steps:
[0021] Step 3-1, replace the defective statement in the program to be repaired with the invalid candidate patch in Step 2 to form an initial population;
[0022] Using the candidate patches generated by the defect repair model to form the initial population is more targeted and improves the quality of the population.
[0023] Step 3-2, calculate the fitness of each variant in the population;
[0024] Run the test cases corresponding to the program to be repaired on the variants. The fitness is the ratio of the number of passed test cases to the total number of test cases. By calculating the fitness of the variants, it is judged whether there are valid patches, and in the case where the fitness is not 1, the fitness is the criterion for selecting variants in the population subsequently.
[0025] Step 3-3, if there is a variant with a fitness of 1, output the candidate patch corresponding to the variant, and the defect is successfully repaired; otherwise, if the maximum number of iterations or the search time is reached, the defect repair fails. If the maximum number of iterations and the search time are not reached, remove the non-compilable variants from the population and continue with the following steps;
[0026] Step 3-4, convert all variants in the population into abstract syntax trees to facilitate subsequent crossover and mutation operations;
[0027] Converting the variants into abstract syntax trees facilitates subsequent crossover and mutation operations by accessing the nodes of the abstract syntax tree of the program.
[0028] Step 3-5, select half of the parent variants in the population according to the fitness value. The selected parent variants perform single-point crossover in pairs to generate child variants, which are added to the population;
[0029] The diversity of the population is enriched by generating child variants through crossover of parent variants.
[0030] Step 3-6, perform mutation operations on all variants in the population to form candidate patches;
[0031] Step 3-7, replace the defective statements in the program to be repaired with the candidate patches to form the next generation population, and return to Step 3-2.
[0032] The present invention relates to the problem of automatic repair of method call defects in object-oriented programs. First, a method call defect repair model is trained based on deep learning; then, candidate patches are generated based on the method call defect repair model, and the effectiveness of the patches is verified. If there is an effective patch, the patch is output and the defect is successfully repaired. Otherwise, the candidate patches form an initial population, and then a genetic algorithm-based method is used to generate more candidate patches, and the fitness is calculated to determine whether the patch is effective. The present invention combines deep learning and genetic algorithms, can repair more method call defects, and has a better defect repair rate. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] Figure 1 It is a flowchart of the implementation of the method of the present invention. DETAILED DESCRIPTION OF THE INVENTION
[0034] In order to more clearly show the purpose and technical solution of the present invention, the specific implementation process and drawings will be used below to describe the specific implementation manner and parameters of the present invention in more detail.
[0035] An automatic repair method for method call defects in object-oriented programs, as Figure 1 shown, includes the following steps:
[0036] Step 1, train a method call defect repair model based on deep learning;
[0037] In step 1, a defect repair model is called based on a deep learning training method, as shown in S1 in Figure 1 and includes the following steps:
[0038] Step 1-1: Collect method call defect data and preprocess it;
[0039] Screen for defects and corresponding repairs regarding method calls in the code version control system, organize them, and find the code of the defect line, defect context, and repair line. The format of each piece of data obtained after organization is: defect line code <ctx>Defect context code\tFix line code, where " <ctx>" indicates that the subsequent content is defect context information, and "\t" is a tab character.
[0040] Convert the collected dataset code into a sequence of tokens using a word-grained tokenization method similar to splitting by spaces. First, separate operators and variables. Second, separate code lines by spaces. Then, use underscores and camel-case letters as delimiters to tokenize variable names. Since the model needs to correctly regenerate the source code from the token list when generating candidate patches, a new token is used when splitting the source code. <camel>To mark the position where the camel case code splitting occurs. After tokenizing the collected dataset, a vocabulary is constructed from the formed tokens. Subsequently, the token sequences in the defective line code, defective context code, and repaired line code are mapped to the corresponding indices in the vocabulary and input into the model for training.
[0041] Step 1-2, select a neural machine translation model based on CNN;
[0042] The neural machine translation architecture in the present invention consists of two encoders, a decoder, and an attention mechanism module. One encoder is used for the input of defective line code to extract the relationship between the defective line code and the repaired code, and the other is used for the input of defective context code to help the model learn the relationships between potential repaired codes and variables, etc. from the defective context. A convolutional neural network is used as the main component in the encoder and decoder to build the model.
[0043] Step 1-3, the training and evaluation method calls the defect repair model;
[0044] The dataset is divided into a training set and a validation set in a ratio of 9:1, and the cross-entropy loss function is used to calculate the loss. Twenty models are trained on the training set with different hyperparameters for 10 epochs, using a batch size of 12, setting the learning rate to 0.0001 during training. After training, the hyperparameter sets are sorted according to their perplexity on the validation set, and the hyperparameter combination with the lowest corresponding perplexity is used to continue training for 200 epochs with the same batch size to obtain an instance of the method call defect repair model, and the trained model is saved.
[0045] Step 2, use the defect repair model generated in Step 1 to generate candidate patches and verify them;
[0046] In the said Step 2, using the defect repair model generated in Step 1 to generate candidate patches and verify them, as Figure 1 shown in S2, includes the following steps:
[0047] Step 2-1, preprocess the defective line code and defective context of the program to be repaired;
[0048] The overall input of the present invention is a defective program, defective line code, defective context code, and a test suite for verifying candidate patches. Before using the repair model to generate candidate patches, the defective line code and defective context in the input are preprocessed in the manner of Step 1-1.
[0049] Step 2-2, load the defect repair model and generate candidate patches;
[0050] Step 2-3, verify the candidate patches. If the candidate patch is valid, output the patch and the defect repair is successful. If the candidate patch is invalid, execute Step 3;
[0051] The verification of a candidate patch refers to replacing the defective statement in the program to be repaired with the candidate patch and running the test cases corresponding to the program to be repaired to evaluate whether the generated patch meets the requirements.
[0052] Step 3: Generate candidate patches based on the genetic algorithm;
[0053] In the said Step 3, generating candidate patches based on the genetic algorithm, as shown in S3 in Figure 1 includes the following steps:
[0054] Step 3-1: Replace the defective statement in the program to be repaired with the invalid candidate patch in Step 2 to form an initial population;
[0055] Step 3-2: Calculate the fitness of each variant in the population;
[0056] Run the test cases corresponding to the program to be repaired on the variant. The fitness calculation method for each variant is shown in Formula 1:
[0057]
[0058] Step 3-3: If there is a variant with a fitness of 1, output the candidate patch corresponding to the variant, and the defect repair is successful; otherwise, if the maximum number of iterations or the search time is reached, the defect repair fails. If the maximum number of iterations and the search time are not reached, remove the non-compilable variants in the population and continue to execute the following steps;
[0059] Step 3-4: Convert all variants in the population into abstract syntax trees to facilitate subsequent crossover and mutation operations;
[0060] Use the Javalang open-source package to convert the variant into an abstract syntax tree.
[0061] Step 3-5: Select half of the parent variants in the population according to the fitness size. The selected parent variants perform pairwise single-point crossover to generate child variants and add them to the population;
[0062] Sort the variants in the population according to the fitness, select half of the variants with large fitness, and then use the single-point crossover method to generate child variants. Single-point crossover is to select a crossover point at the same position on the abstract syntax tree nodes of two parent variants and exchange the two crossover points to generate child variants. For the repair of method call defective statements, when crossing over, select the crossover point on the node of the method call defective statement.
[0063] Step 3-6: Perform mutation operations on all variants in the population to form candidate patches;
[0064] Modify the statement node at the node of the defective statement to generate a new patch variant. According to the usage frequencies of the three mutation operators in actual defect repair, mutate the method call defect node in the order of replacement, insertion, and deletion during the mutation process, so as to optimize the mutation process to quickly find the correct patch. Among them, the replacement and insertion mutation operations need to search for repair components within the program scope, that is, reuse existing code elements to generate repair patches. The present invention limits the search for repair components in the same file as the defect. When using a node N S as a repair component, use this node N S and the similarity of the pedigree context of the method call defect node N T to calculate the correctness of this repair component. When searching for repair components, preferentially use node components with high correctness to generate patches to reduce the search time.
[0065] Step 3-7, replace the defective statement in the program to be repaired with the candidate patch to form the next generation population, and return to Step 3-2.< / camel> < / ctx> < / ctx>
Claims
1. An automatic repair method for method call defects in an object-oriented program, characterized in that, Including the following steps: Step 1, call the defect repair model based on the deep learning training method. Step 1 includes the following steps: Step 1-1, collect method call defect data and perform preprocessing; Step 1-2, select a neural machine translation model based on CNN; Step 1-3, train and evaluate the method call defect repair model; Step 2, use the defect repair model generated in Step 1 to generate and verify candidate patches. Step 2 includes the following steps: Step 2-1, preprocess the defect line code and defect context of the program to be repaired; Step 2-2, load the defect repair model and generate candidate patches; Step 2-3, verify the candidate patches. If the candidate patches are valid, output the patches and the defect repair is successful. If the candidate patches are invalid, execute Step 3; Step 3, generate candidate patches based on the genetic algorithm. Step 3 includes the following steps: Step 3-1, replace the defect statement in the program to be repaired with the invalid candidate patch in Step 2 to form an initial population; Step 3-2, calculate the fitness of each variant in the population; Step 3-3, if there is a variant with a fitness of 1, output the candidate patch corresponding to the variant and the defect repair is successful; Otherwise, if the maximum number of iterations or the search time is reached, the defect repair fails. If the maximum number of iterations and the search time are not reached, remove the non-compilable variants in the population and continue to execute the following steps; Step 3-4, convert all variants in the population into abstract syntax trees to facilitate subsequent crossover and mutation operations; Step 3-5, select half of the parent variants in the population according to the fitness size. The selected parent variants perform pairwise single-point crossover to generate child variants and add them to the population; Step 3-6, perform mutation operations on all variants in the population to form candidate patches; Step 3-7, replace the defect statement in the program to be repaired with the candidate patch to form the next generation population, and return to Step 3-2.
2. The method according to claim 1, wherein In Step 3-6, performing mutation operations on all variants to form candidate patches includes: when performing mutation operations, according to the frequency of using mutation operators in actual defect repair, consider the priority of mutation operators, and select mutation operators in the order of replacement, insertion, and deletion; when performing replacement or insertion mutations, the search space of the repair components is limited to the file where the defect is located, and repair components with high similarity to the defect statement context are preferentially selected to form candidate patches.