A code comment generation method and device
By compiling source code into bytecode and generating control flow graphs, and then using an annotation translation model to train and generate annotations, the problem of high learning costs between projects in different languages is solved, and efficient cross-language annotation generation is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SUN YAT SEN UNIV
- Filing Date
- 2021-12-30
- Publication Date
- 2026-05-08
AI Technical Summary
When dealing with projects written in different languages, existing technologies for generating code comments require training models specifically for each language, resulting in high learning costs that are difficult to reduce effectively.
By compiling the source code into bytecode and generating a control flow graph, the model is trained using an annotation translation model. The training data from the second project is used to generate annotations for the first project. The annotation translation model includes a positional encoding layer, an encoder, a decoder, a fully connected layer, and a GRU encoding layer. The model parameters are optimized using a loss function.
This reduces the learning cost of the annotation translation model, making annotation generation between different language projects more efficient and reducing the complexity and cost of model training.
Smart Images

Figure CN114296792B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of code translation, and in particular to a method and apparatus for generating code annotations. Background Technology
[0002] With the development of social science and technology, various computer coding languages have emerged. Code comments are one of the important means to help programmers understand code. They usually use natural language to record the description of the code, express the programmer's intention and the implementation details of the code, and are of great significance in program understanding and software maintenance.
[0003] Traditional code comment generation methods primarily utilize information retrieval techniques to select appropriate terms for deep summarization from the original code snippets, or code cloning techniques to retrieve similar code snippets from a codebase and generate new comments based on the comments of these similar snippets. These methods rely excessively on semantic information such as identifiers, and their performance deteriorates significantly when faced with projects written in different languages. This necessitates analyzing source code for different languages and training different models specifically, resulting in high learning costs. Therefore, reducing these learning costs is a key concern. Summary of the Invention
[0004] In view of this, this application provides a code annotation generation method and apparatus to reduce learning costs.
[0005] To achieve the above objectives, the following solution is proposed:
[0006] A code comment generation method, comprising:
[0007] Obtain the source code of the first project;
[0008] Compile the source code into bytecode;
[0009] A control flow diagram is generated based on the bytecode;
[0010] The bytecode and the control flow diagram are input into the annotation translation model to obtain the annotations of the source code output by the annotation translation model.
[0011] The annotation translation model is trained using the bytecode of the second project and its control flow graph as training samples, and the annotations of the source code of the second project as training labels.
[0012] Optionally, the annotation translation model includes a positional encoding layer, an encoder, a decoder, a fully connected layer, and a GRU encoding layer;
[0013] The training process of the annotation translation model includes:
[0014] The acquired sample bytecode and sample control flowchart are positionally encoded using a positional encoding layer to obtain a language sequence.
[0015] The encoded features are obtained by processing the sample bytecode using an encoder.
[0016] The decoder generates a decoding result corresponding to the encoded features based on the language sequence.
[0017] The sample control flowchart is processed through a GRU encoding layer to obtain a feature vector;
[0018] The annotation is generated using a fully connected layer based on the decoding result and the feature vector.
[0019] The translation loss value is obtained by calculating the difference between the generated annotation and the annotation label corresponding to the sample bytecode and sample control flow diagram using a loss function;
[0020] The parameters of the annotation translation model are updated based on the translation loss value.
[0021] Optionally, generating the annotation based on the decoding result and the feature vector includes:
[0022] Based on a pre-defined vocabulary, the following formula is used to predict and generate each word in the annotation:
[0023] P({y i |y1,..,y i-1},x c ,x v )=softmax(W*Concat(Transformer{y1,..,y i-1},x c ),GRU(x v ))+b)
[0024]
[0025] Where P represents the prediction that the next word is y. i The probability, y i Represents the i-th word in the generated decoding result, {y1,...,y i-1} represents the decoding result, x c Represents bytecode, x v This represents the control flow graph, where W represents the weight matrix of the fully connected layer, b represents the bias matrix of the fully connected layer, and GRU(x) represents the control flow graph. v ) represents the feature vector of the control flow graph, softmax is used as the activation function, and P i' indicates that the label smoothing parameter P has been added, and K represents the total number of words in the preset vocabulary. These are preset parameters.
[0026] Optionally, the loss function includes:
[0027]
[0028] Where L represents the loss function.
[0029] Optionally, generating a control flow diagram based on the bytecode includes:
[0030] The bytecode is decompiled into a three-address code;
[0031] The three-address code is divided into multiple basic blocks and connected according to the bytecode writing order to generate a control flow chart.
[0032] Optionally, prior to the training process of the annotation translation model, the following may also be included:
[0033] The sample bytecode and the annotation are preprocessed to obtain new sample bytecode and new annotation;
[0034] The new sample bytecode and control flow graph are used as training samples, and the new annotations are used as training labels.
[0035] Optionally, the sample bytecode and the annotation are preprocessed to obtain new sample bytecode and new annotation, including:
[0036] Obtain the local variable table, which is a table stored locally that records variables with user-defined annotation habits;
[0037] The bytecode code area of the sample bytecode is de-duplicated to obtain the de-duplicated bytecode code area;
[0038] The cleaned bytecode Code area is combined with the local variable table to obtain a new sample bytecode;
[0039] The annotation is divided into multiple words using a preset delimiter rule;
[0040] The form of the word is converted into a preset form to obtain a word in the preset form, and then a new annotation is obtained.
[0041] Optionally, after converting the form of the word to a preset form, obtaining a word in the preset form, and forming a new annotation, the method further includes:
[0042] The new sample bytecode and new comments are deduplicated to obtain the deduplicated new sample bytecode and new comments.
[0043] A code annotation generation device, comprising:
[0044] The source code acquisition unit is used to acquire the source code of the first project.
[0045] A bytecode compilation unit is used to compile the source code into bytecode;
[0046] A control flowchart generation unit is used to generate a control flowchart based on the bytecode.
[0047] The model output unit is used to input the bytecode and the control flow diagram into the annotation translation model to obtain the annotations of the source code output by the annotation translation model;
[0048] The annotation translation model is trained using the bytecode of the second project and its control flow graph as training samples, and the annotations of the source code of the second project as training labels.
[0049] Optionally, the control flowchart generation unit includes:
[0050] The first control flowchart generation subunit is used to decompile the bytecode into a three-address code;
[0051] The second control flowchart generation subunit is used to divide the three address codes into multiple basic blocks and connect them according to the bytecode writing order to generate a control flowchart.
[0052] This application obtains the source code of the first project, compiles it into bytecode, generates a control flow diagram from the bytecode, and inputs the bytecode and control flow diagram into an annotation translation model to obtain annotations for the source code output by the annotation translation model. The proposed solution converts the source code of the second project into bytecode and a control flow diagram, and trains the annotation translation model using the bytecode and control flow diagram of the second project to obtain annotations for the source code of the second project output by the annotation translation model. Compared to existing technologies, because the bytecode of the second and first projects' source code is similar, the training data from the second project can be used to train the annotation translation model. The resulting annotation translation model can then be used by the first project with similar bytecode, reducing the learning cost of the annotation translation model. Attached Figure Description
[0053] Figure 1 A flowchart of a code annotation generation method provided in this application embodiment;
[0054] Figure 2 A schematic diagram of a code annotation generation device provided in an embodiment of this application;
[0055] Figure 3This is a hardware structure block diagram of a code annotation generation device provided in an embodiment of this application. Detailed Implementation
[0056] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0057] Figure 1 A flowchart of a code comment generation method provided in this application embodiment is included, which may include the following steps:
[0058] Step S100: Obtain the source code of the first project.
[0059] Specifically, the source code of the first project can be the source code of a language project that can run on a virtual machine, such as Kotlin, Groovy, Scala, Java, etc.
[0060] Step S110: Compile the source code into bytecode.
[0061] Specifically, bytecode can be a type of code that a virtual machine can read and execute. For example, JVM bytecode is a hexadecimal file with a fixed structure and rules, containing information such as the Code section (assembly instructions), local variable table, exception table, code line offset mapping table, and constant pool. Source code can be compiled into bytecode.
[0062] Step S120: Generate a control flow chart based on the bytecode.
[0063] Specifically, a control flow diagram can be an abstract representation of a process or program. It is an abstract data structure used in the compiler and maintained internally by the compiler. It represents all the paths that a program will traverse during execution. The control flow diagram represents the possible flow of execution of all basic blocks in a program process in the form of a graph. It can also reflect the real-time execution process of a process. Control flow diagrams can be generated from bytecode.
[0064] Step S130: Input the bytecode and the control flow diagram into the annotation translation model to obtain the annotation of the source code output by the annotation translation model.
[0065] Specifically, the annotation translation model is trained using the bytecode and control flow diagram of the second project as training samples, and the comments of the second project's source code as training labels. The source code of the second project does not need to be in the same language as the source code of the first project; the bytecode compiled from both projects only needs to be similar. For example, if the bytecode of a Kotlin project and a Java project are similar, the first project can be a Kotlin project, and the second project can be a Java project. The annotation translation model is trained using the training data from the Java project, and then the trained model is used to translate the Kotlin project. Similarly, if the bytecode of a Groovy project and a Scala project are similar, the first project can also be a Groovy project, and the second project can also be a Scala project. The bytecode and control flow diagram can be input into the annotation translation model to obtain the annotations of the source code output by the model.
[0066] In this embodiment, the source code of the first project is obtained, compiled into bytecode, and a control flow diagram is generated based on the bytecode. The bytecode and control flow diagram are then input into an annotation translation model to obtain annotations for the source code output by the annotation translation model. This solution converts the source code of the second project into bytecode and a control flow diagram, and trains the annotation translation model using the bytecode and control flow diagram of the second project to obtain annotations for the source code of the second project output by the annotation translation model. Compared to existing technologies, since the bytecode of the source code of the second project and the first project is similar, the training data of the second project can be used to train the annotation translation model. The resulting annotation translation model can then be used by the first project with similar bytecode, reducing the learning cost of the annotation translation model.
[0067] In some embodiments of this application, the annotation translation model may include a positional encoding layer, an encoder, a decoder, a fully connected layer, and a GRU encoding layer. The training process of the annotation translation model is described below, which may include:
[0068] Step S200: The obtained sample bytecode and sample control flowchart are positionally encoded through a positional encoding layer to obtain a language sequence.
[0069] Specifically, the acquired sample bytecode and sample control flow chart can be positionally encoded using a positional encoding layer to obtain a language sequence for use in subsequent steps.
[0070] Step S210: The sample bytecode is processed by an encoder to obtain encoded features.
[0071] Specifically, to speed up the encoding process, the encoder can be a two-layer structure, with each layer containing two sub-layers. These sub-layers can be used to extract the encoding features of the sample bytecode, providing effective semantic information for the decoding process. The encoder can process the sample bytecode to obtain the encoding features.
[0072] Step S220: Generate a decoding result corresponding to the encoded features based on the language sequence using a decoder.
[0073] Specifically, to speed up decoding, the decoder can be a two-layer structure, with each layer containing two sub-layers. The decoder can output the next result in the language sequence based on the encoded features output by the encoder and the result of the previous prediction.
[0074] Step S230: The sample control flowchart is processed through the GRU encoding layer to obtain the feature vector.
[0075] Specifically, feature vectors can be obtained by iteratively processing the sample control flow graph through the GRU encoding layer.
[0076] Step S240: Generate the annotation based on the decoding result and the feature vector through a fully connected layer.
[0077] Specifically, annotations can be generated and output by integrating the decoding results based on the feature vectors through a fully connected layer.
[0078] Furthermore, the process of generating the annotation in step S240, based on the decoding result and the feature vector through a fully connected layer, is described below. This process may include:
[0079] Based on a pre-defined vocabulary, the following formula is used to predict and generate each word in the annotation:
[0080] P({y i |y1,..,y i-1},x c ,x v )=softmax(W*Concat(Transformer{y1,..,y i-1},x c ),GRU(x v ))+b)
[0081]
[0082] Where P represents the prediction that the next word is y. i The probability, y i Represents the i-th word in the generated decoding result, {y1,...,y i-1} represents the decoding result, xc Represents bytecode, x v This represents the control flow graph, where W represents the weight matrix of the fully connected layer, b represents the bias matrix of the fully connected layer, and GRU(x) represents the control flow graph. v ) represents the feature vector of the control flow graph, softmax is used as the activation function, and P i ' indicates that the label smoothing parameter P has been added, and K represents the total number of words in the preset vocabulary. These are preset parameters.
[0083] Step S250: Calculate the difference between the generated annotation and the annotation label corresponding to the sample bytecode and sample control flow diagram using a loss function to obtain the translation loss value.
[0084] Specifically, the difference between the sample bytecode and the annotation labels corresponding to the sample control flow diagram can be calculated. This involves calculating the difference between the generated annotation and the annotation labels corresponding to the sample bytecode and sample control flow diagram, and then substituting the obtained difference into the loss function to calculate the translation loss value of the annotation labels corresponding to the sample bytecode and sample control flow diagram.
[0085] Furthermore, the loss function can be as follows:
[0086]
[0087] Where L represents the loss function.
[0088] Step S260: Update the parameters of the annotation translation model based on the translation loss value.
[0089] Specifically, since a larger translation loss value indicates a larger difference between the generated annotation and the annotation label corresponding to the sample bytecode and sample control flow diagram, it also means that the prediction result obtained by the model differs more from the training label. The parameters of the annotation translation model can be updated based on the translation loss value.
[0090] In some embodiments of this application, the process of step S120, generating a control flow chart based on the bytecode, is described, and this process may include:
[0091] Step S121: Decompile the bytecode into a three-address code.
[0092] Specifically, static analysis tools can be used to decompile bytecode into three-address code, such as the soot tool.
[0093] Step S122: Divide the three-address code into multiple basic blocks and connect them according to the bytecode writing order to generate a control flowchart.
[0094] Specifically, the three-address code can be divided into multiple basic blocks according to the goto statement, and the multiple basic blocks can be connected with arrows to form a control flow diagram. In order to facilitate machine recognition, the control flow diagram can be described in a machine-readable form using the DOT language.
[0095] In some embodiments of this application, before training the annotation translation model, the scheme may further include a preprocessing procedure for the training data, which may include:
[0096] Step S300: Preprocess the sample bytecode and the annotation to obtain new sample bytecode and new annotation.
[0097] Specifically, in order to enable the model to process data better and make the translated annotations more accurate, the sample bytecode and annotations can be preprocessed to obtain new sample bytecode and new annotations.
[0098] Step S310: Use the new sample bytecode and control flow diagram as training samples, and the new annotations as training labels.
[0099] Furthermore, the following describes step S300, the process of preprocessing the sample bytecode and the annotation to obtain new sample bytecode and new annotation. This process may include:
[0100] Step S301: Obtain the local variable table.
[0101] Specifically, considering the personal style of user annotations, the local variable table is a table stored locally that records variables with user annotation habits. It can include the variable name, variable type, and the slot where the variable is located. This information is often closely related to the annotation and contains very important natural semantic information, so the local variable table can be obtained.
[0102] Step S302: Perform noise removal processing on the bytecode code area of the sample bytecode to obtain the noise-removed bytecode code area.
[0103] Specifically, the Code area mainly contains two parts: opcode information and API call path information. For the opcode information, the sequence number and other useless symbols before the opcode can be removed, and then... <line>As a line separator. For API call information, identifiers can be split into semantically meaningful words based on camelCase naming rules. The processed bytecode code area is shown below:
[0104] <beg>final groovyx gpars extra166y custom concurrent hash map nodefind node java lang object int groovyx gpars extra166y custom concurrent hashmap segment <line>code <line>aload seg lgroovyx gpars extra166y customconcurrent hash map segment <line>ifnull 93.. <line>return <end>
[0105] Step S303: Combine the cleaned bytecode Code area with the local variable table to obtain new sample bytecode.
[0106] Specifically, the bytecode Code section contains aload, aload_ <n>and astore, astore_ <n>Instructions (where aload n is equivalent to aload_) <n>astore n is equivalent to astore_ <n>The 'n' in these instructions corresponds one-to-one with the slots in the local variable table. Therefore, by replacing the 'n' in these instructions with the variable name and variable type of the corresponding variable, the bytecode code area can be combined with the local variable table to obtain new sample bytecode.
[0107] Step S304: The annotation is divided into multiple words according to a preset separation rule.
[0108] Specifically, the preset delimiter rule can be a camelCase naming rule, which can be used to split comments into multiple words.
[0109] Step S305: Convert the form of the word into a preset form to obtain a word in the preset form, and combine them to obtain a new annotation.
[0110] Specifically, the default form can be lowercase. To make it easier for users to read, the word form can be converted to lowercase to obtain the lowercase word, as shown in the code below:
[0111] <beg>returns node for key <seg>or null if none <end>
[0112] In some embodiments of this application, considering the potential problem of duplicate annotations, after step S305, converting the form of the word into a preset form to obtain a word of the preset form and forming a new annotation, the obtained new annotation can be deduplicated to obtain a new sample bytecode and a new annotation after deduplication.
[0113] The code annotation generation apparatus provided in the embodiments of this application is described below. The code annotation generation apparatus described below can be referred to in correspondence with the code annotation generation method described above.
[0114] like Figure 2 As shown, a schematic diagram of a code annotation generation device is disclosed, which may include:
[0115] Source code acquisition unit 11 is used to acquire the source code of the first project;
[0116] Bytecode compilation unit 12 is used to compile the source code into bytecode;
[0117] Control flowchart generation unit 13 is used to generate a control flowchart based on the bytecode;
[0118] The annotation translation unit 14 is used to input the bytecode and the control flow diagram into the annotation translation model to obtain the annotations of the source code output by the annotation translation model;
[0119] The annotation translation model is trained using the bytecode of the second project and its control flow graph as training samples, and the annotations of the source code of the second project as training labels.
[0120] Optionally, this code comment generation device may also include:
[0121] The model training module is used to train the annotation translation model, wherein the annotation translation model includes: a positional encoding layer, an encoder, a decoder, a fully connected layer, and a GRU encoding layer;
[0122] The training process of the annotation translation model may include:
[0123] The acquired sample bytecode and sample control flowchart are positionally encoded using a positional encoding layer to obtain a language sequence.
[0124] The encoded features are obtained by processing the sample bytecode using an encoder.
[0125] The decoder generates a decoding result corresponding to the encoded features based on the language sequence.
[0126] The sample control flowchart is processed through a GRU encoding layer to obtain a feature vector;
[0127] The annotation is generated using a fully connected layer based on the decoding result and the feature vector.
[0128] The translation loss value is obtained by calculating the difference between the generated annotation and the annotation label corresponding to the sample bytecode and sample control flow diagram using a loss function;
[0129] The parameters of the annotation translation model are updated based on the translation loss value.
[0130] Optionally, generating the annotation based on the decoding result and the feature vector may include:
[0131] Based on a pre-defined vocabulary, the following formula is used to predict and generate each word in the annotation:
[0132] P({y i |y1,..,y i-1 },x c ,x v )=softmax(W*Concat(Transformer{y1,..,y i-1 },x c ),GRU(x v ))+b)
[0133]
[0134] Where P represents the prediction that the next word is y. i The probability, y i Represents the i-th word in the generated decoding result, {y1,...,y i-1 } represents the decoding result, x c Represents bytecode, x v This represents the control flow graph, where W represents the weight matrix of the fully connected layer, b represents the bias matrix of the fully connected layer, and GRU(x) represents the control flow graph. v ) represents the feature vector of the control flow graph, softmax is used as the activation function, and P i ' indicates that the label smoothing parameter P has been added, and K represents the total number of words in the preset vocabulary. These are preset parameters.
[0135] Optionally, the loss function may include:
[0136]
[0137] Where L represents the loss function.
[0138] Optionally, the control flowchart generation unit may include:
[0139] The first control flowchart generation subunit is used to decompile the bytecode into a three-address code;
[0140] The second control flowchart generation subunit is used to divide the three address codes into multiple basic blocks and connect them according to the bytecode writing order to generate a control flowchart.
[0141] Optionally, this code comment generation device may also include:
[0142] The sample preprocessing unit is used to preprocess the sample bytecode and the annotation before executing the model training module to obtain new sample bytecode and new annotation. The new sample bytecode and control flow graph are used as training samples, and the new annotation is used as training labels.
[0143] Optionally, the sample preprocessing unit may include:
[0144] The first sample preprocessing subunit is used to obtain a local variable table, which is a table stored locally that records variables with user annotation habits;
[0145] The second sample preprocessing subunit is used to perform de-duplication on the bytecode code area of the sample bytecode to obtain the de-duplicated bytecode code area;
[0146] The third sample preprocessing subunit is used to combine the cleaned bytecode Code area with the local variable table to obtain new sample bytecode;
[0147] The fourth sample preprocessing subunit is used to segment the annotation into multiple words according to a preset splitting rule;
[0148] The fifth sample preprocessing subunit is used to convert the form of the word into a preset form, obtain words in the preset form, and compose new annotations.
[0149] Optionally, the sample preprocessing unit may also include:
[0150] The sixth sample preprocessing subunit is used to deduplicate the new sample bytecode and new comments after the fifth sample preprocessing subunit is executed, so as to obtain the deduplicated new sample bytecode and new comments.
[0151] The code annotation generation apparatus provided in this application embodiment can be applied to a code annotation generation device. The code annotation generation device can be a terminal. Figure 3 A hardware block diagram of the code comment generation device is shown, for reference. Figure 3 The hardware structure of the code comment generation device may include: at least one processor 1, at least one communication interface 2, at least one memory 3, and at least one communication bus 4;
[0152] In this embodiment of the application, the number of processor 1, communication interface 2, memory 3, and communication bus 4 is at least one, and processor 1, communication interface 2, and memory 3 communicate with each other through communication bus 4;
[0153] Processor 1 may be a central processing unit (CPU), an application-specific integrated circuit (ASIC), or one or more integrated circuits configured to implement embodiments of the present invention.
[0154] Memory 3 may include high-speed RAM, and may also include non-volatile memory, such as at least one disk storage device;
[0155] The memory stores a program, which the processor can call. The program is used for:
[0156] Obtain the source code of the first project;
[0157] Compile the source code into bytecode;
[0158] A control flow diagram is generated based on the bytecode;
[0159] The bytecode and the control flow diagram are input into the annotation translation model to obtain the annotations of the source code output by the annotation translation model.
[0160] The annotation translation model is trained using the bytecode of the second project and its control flow graph as training samples, and the annotations of the source code of the second project as training labels.
[0161] Optionally, the refined and extended functions of the program can be found in the description above.
[0162] This application embodiment also provides a storage medium that can store a program suitable for execution by a processor, the program being used for:
[0163] Obtain the source code of the first project;
[0164] Compile the source code into bytecode;
[0165] A control flow diagram is generated based on the bytecode;
[0166] The bytecode and the control flow diagram are input into the annotation translation model to obtain the annotations of the source code output by the annotation translation model.
[0167] The annotation translation model is trained using the bytecode of the second project and its control flow graph as training samples, and the annotations of the source code of the second project as training labels.
[0168] Optionally, the refined and extended functions of the program can be found in the description above.
[0169] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0170] The various embodiments in this specification are described in a progressive manner. Each embodiment focuses on the differences from other embodiments. The various embodiments can be combined with each other, and the same or similar parts can be referred to each other.
[0171] The above description of the disclosed embodiments enables those skilled in the art to make or use this application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of this application. Therefore, this application is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.< / end> < / seg> < / beg> < / n> < / n> < / n> < / n> < / end> < / line> < / line> < / line> < / line> < / beg> < / line>
Claims
1. A method for generating code comments, characterized in that, include: Obtain the source code of the first project; The source code for the first project is the source code for a language project that can run on a virtual machine; Compile the source code into bytecode; Bytecode is a type of code that a virtual machine can read and execute. It includes the Code section, local variable table, exception table, line offset mapping table, and constant pool. A control flow diagram is generated based on the bytecode; the control flow diagram represents the possible flow of execution of all basic blocks within a program process in the form of a graph; The bytecode and the control flow diagram are input into the annotation translation model to obtain the annotations of the source code output by the annotation translation model. The annotation translation model is trained using the bytecode of the second project and the control flow diagram of the bytecode of the second project as training samples, and the annotations of the source code of the second project as training labels. Prior to the training process of the annotation translation model, the following is also included: The sample bytecode and the annotation are preprocessed to obtain new sample bytecode and new annotation. The new sample bytecode and control flow graph are used as training samples, and the new annotations are used as training labels. The sample bytecode and the annotation are preprocessed to obtain new sample bytecode and new annotations, including: Obtain the local variable table, which is a table stored locally that records variables with user-defined annotation habits; The code area of the sample bytecode is de-duplicated to obtain the de-duplicated code area. The cleaned bytecode Code area is combined with the local variable table to obtain a new sample bytecode; The annotation is split into multiple words using a preset delimiter rule; The form of the word is converted into a preset form to obtain a word in the preset form, and then a new annotation is obtained.
2. The method according to claim 1, characterized in that, The annotation translation model includes a positional encoding layer, an encoder, a decoder, a fully connected layer, and a GRU encoding layer; The training process of the annotation translation model includes: The acquired sample bytecode and sample control flow diagram are positionally encoded through a positional encoding layer to obtain a language sequence. The bytecode of the sample is processed by an encoder to obtain encoded features; The decoder generates a decoding result corresponding to the encoded features based on the language sequence. The sample control flowchart is processed through a GRU encoding layer to obtain a feature vector; The annotation is generated using a fully connected layer based on the decoding result and the feature vector. The translation loss value is obtained by calculating the difference between the generated annotation and the annotation label corresponding to the sample bytecode and sample control flow diagram using a loss function; The parameters of the annotation translation model are updated based on the translation loss value.
3. The method according to claim 2, characterized in that, The step of generating the annotation based on the decoding result and the feature vector includes: Based on a pre-defined vocabulary, the following formula is used to predict and generate each word in the annotation: Where P represents the prediction that the next word is The probability, This represents the i-th word in the generated decoding result. This indicates the decoding result. Represents bytecode, Let represent the control flow graph, W represent the weight matrix of the fully connected layer, and b represent the bias matrix of the fully connected layer. The feature vectors representing the control flow graph are represented by softmax as the activation function. P represents the addition of label smoothing parameters, and K represents the total number of words in the preset vocabulary. These are preset parameters.
4. The method according to claim 3, characterized in that, The loss function includes: Where L represents the loss function.
5. The method according to claim 1, characterized in that, The step of generating a control flow diagram based on the bytecode includes: The bytecode is decompiled into a three-address code; The three-address code is divided into multiple basic blocks and connected according to the bytecode writing order to generate a control flow chart.
6. The method according to claim 1, characterized in that, After converting the form of the word to a preset form, obtaining words in the preset form, and forming new annotations, the process also includes: The new sample bytecode and new comments are deduplicated to obtain the deduplicated new sample bytecode and new comments.
7. A code annotation generation device, characterized in that, include: The source code acquisition unit is used to acquire the source code of the first project. The source code for the first project is the source code for a language project that can run on a virtual machine; The bytecode compilation unit is used to compile the source code into bytecode; bytecode is a type of code that a virtual machine can read and execute, which includes the Code section, local variable table, exception table, line offset mapping table, and constant pool. A control flow diagram generation unit is used to generate a control flow diagram based on the bytecode; the control flow diagram represents the possible flow of execution of all basic blocks within a program process in the form of a graph; The model output unit is used to input the bytecode and the control flow diagram into the annotation translation model to obtain the annotations of the source code output by the annotation translation model; The annotation translation model is trained using the bytecode of the second project and the control flow diagram of the bytecode of the second project as training samples, and the annotations of the source code of the second project as training labels. Prior to the training process of the annotation translation model, the following is also included: The sample bytecode and the annotation are preprocessed to obtain new sample bytecode and new annotation. The new sample bytecode and control flow graph are used as training samples, and the new annotations are used as training labels. The sample bytecode and the annotation are preprocessed to obtain new sample bytecode and new annotations, including: Obtain the local variable table, which is a table stored locally that records variables with user-defined annotation habits; The code area of the sample bytecode is de-duplicated to obtain the de-duplicated code area. The cleaned bytecode Code area is combined with the local variable table to obtain a new sample bytecode; The annotation is split into multiple words using a preset delimiter rule; The form of the word is converted into a preset form to obtain a word in the preset form, and then a new annotation is obtained.
8. The apparatus according to claim 7, characterized in that, The control flowchart generation unit includes: The first control flowchart generation subunit is used to decompile the bytecode into a three-address code; The second control flowchart generation subunit is used to divide the three address codes into multiple basic blocks and connect them according to the bytecode writing order to generate a control flowchart.
Citation Information
Patent Citations
Method for automatically generating Java code annotations based on Graph2Seq of deep reinforcement learning
CN110018820A
Java function annotation automatic generation method based on program analysis
CN112433754A