Code clone detection method, system, device and medium based on visual image
By converting Java code into RGB visualization images and using a clone detection model with a Transformer subnetwork and a sparse attention module, the accuracy and efficiency issues of code clone detection in existing technologies are solved, achieving accurate detection of code details.
Patent Information
- Application Number
- CN202310042780.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-01-28
- Publication Date
- 2026-02-24
- Estimated Expiration
- 2043-01-28
AI Technical Summary
Existing code clone detection methods cannot accurately distinguish the subtle changes in the code, making it difficult to detect code clones of Type-1, Type-2 and Type-3 types. Furthermore, detection methods based on code syntax and semantic graphs are computationally intensive and prone to information loss.
By converting Java code into ASCII code, filling it with RGB primary color values to form a visual image, and using a clone detection model with Transformer subnetwork and sparse attention module, code clone detection is performed with accuracy down to each character. The representation distance is calculated by combining the Contrastive Loss function.
It achieves accurate detection of code clones, avoids confusion and information loss caused by surface shape judgment, and improves the accuracy and efficiency of detection.
Smart Images

Figure CN115964080B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the fields of code cloning and deep learning technology, and specifically relates to a code clone detection method, system, device and medium based on visual images. Background Technology
[0002] In recent years, code clone detection methods have mainly fallen into two categories: code shape-based detection and code syntax / semantic graph-based detection. Code shape-based detection converts the entire code segment into a grayscale image to distinguish between code and non-code parts (non-code parts have a grayscale value of 0, displayed as black on the grayscale image). The code clone is identified by comparing the shapes of the code parts displayed on the grayscale image. Code syntax / semantic graph-based detection, on the other hand, converts the code into a graph containing syntactic information, such as an Abstract Syntax Tree (AST), or a graph containing semantic information, such as a Control Flow Graph (CFG) or Program Dependence Graph (PDG). This extracts the syntactic and semantic information of the code, and then the similarity between these graphs is compared to identify code clones.
[0003] The drawbacks of code shape-based detection are that it relies solely on the surface of the code to make judgments by comparing the similarity of code shapes. Without delving into the details of the code, it cannot detect changes in a single word, character, or symbol. This makes it impossible to distinguish between Type-1 and Type-2 code clones. Furthermore, it struggles to detect Type-3 code clones, as the addition or removal of lines alters the shape, interfering with the method's judgment of code shape similarity and leading to detection errors.
[0004] The drawbacks of code syntax and semantic graph detection are as follows: While this method can delve into the syntax and semantics of code to detect similarity, it requires the use of subgraph isomorphisms to accurately match the graph, leading to an NP-hard problem and causing the computational cost to increase exponentially, resulting in significant time expenditure. Furthermore, the process involves intermediate graph transformations, which can easily lead to the loss of code information and result in missed or false detections. Summary of the Invention
[0005] The main objective of this invention is to overcome the shortcomings and deficiencies of the prior art and provide a code clone detection method, system, device and medium based on visual images. By detecting the code at the ASCII encoding level, it is accurate to every character of the code and delves into the details of the code, thereby more accurately identifying code clones.
[0006] To achieve the above objectives, the present invention adopts the following technical solution:
[0007] This invention provides a code clone detection method based on visualized images, comprising the following steps:
[0008] Obtain Java code data in .java file format; remove comments from the code in the .java file, and then call VoidVisitorAdapter in javaparser to separate the code functions in each .java file. Each separated code function is stored in a separate .java file to obtain the code function file.
[0009] The code characters in the code function file are converted into ASCII codes, and then the ASCII code values are filled with the RGB three primary color values to obtain RGB pixels. The pixels are combined into a visual color image to obtain a code visualization image.
[0010] The code visualization images are divided into cloned code visualization images and non-cloned code visualization images, and new cloned code visualization images are synthesized using the SMOTE algorithm.
[0011] The clone code visualization image and the non-clone code visualization image are input into a pre-established clone detection model for training to obtain a trained clone detection model;
[0012] The clone detection model comprises two Transformer sub-networks that share weights. Each Transformer sub-network includes a Transformer encoding module and a sparse attention module. The Transformer encoding module extracts features from the code visualization image, while the sparse attention module identifies discriminative pixel blocks in the code visualization image and uses the corresponding latent features as input to the next encoding module. The training process involves inputting either cloned or non-cloned code visualization image pairs into the two Transformer sub-networks. The Transformer sub-networks map the cloned or non-cloned code visualization image pairs to a high-dimensional feature space, outputting corresponding representations. The representation distance is then calculated using the Contrastive Loss function based on these representations.
[0013] The code visualization image to be detected is input into the trained clone detection model for similarity detection, and the detection result is obtained.
[0014] As a preferred technical solution, the removal of comments from the code in the .java file includes single-line comments, multi-line comments, and documentation comments.
[0015] As a preferred technical solution, the code characters in the code function file are converted into ASCII codes, and then the ASCII code values are filled with RGB primary color values to obtain RGB pixels. The pixels are then combined to form a visual color image, resulting in a code visualization image. Specifically:
[0016] The three primary colors are red (R), green (G), and blue (B);
[0017] The generated .java file size is between 0 and 30kB. Following the conversion rule of converting 3 characters to 1 pixel, the resulting pixel count is between 0 and 10240.
[0018] Meanwhile, the red (R), green (G), and blue (B) are sorted in various ways, and the ASCII codes are filled with the color values of the three primary colors in different orders each time, resulting in multiple different visual color images.
[0019] Furthermore, since the methods and functions in the source code are different, the size of the code visualization image converted from each .java file containing the code functions is also different. Therefore, code visualization images larger than the preset size are cropped to the preset size; code visualization images smaller than the preset size are padded with 0 to the preset size, which to a certain extent ensures that the code visualization image is close to the center of the padded image.
[0020] As a preferred technical solution, the synthesis of a new cloned code visualization image using the SMOTE algorithm means that if the number of cloned code visualization images is much smaller than the number of non-cloned code visualization images, the SMOTE algorithm is used to randomly select a cloned code visualization image y from the nearest neighbors of each sample x in the cloned code visualization images, and then x and y are synthesized into a new cloned code visualization image, thereby reducing the risk of overfitting; then the cloned code visualization image is labeled as 1, and the non-cloned code visualization image is labeled as 0.
[0021] As a preferred technical solution, the Transformer encoding module specifically includes several Transformer encoding modules with identical structures in the Transformer sub-network; wherein, the Transformer encoding module includes a multi-head self-attention module and a multilayer perceptron module;
[0022] The multi-head self-attention module includes several self-attention modules. Different self-attention modules learn relevant features in mutually independent feature subspaces. Finally, the outputs of the multi-head self-attention modules are concatenated and then linearly transformed to obtain the output of the multi-head self-attention module. This output is residually connected with the input matrix, i.e., matrix addition, and finally standardized by layers as the input of the next multilayer perceptron module.
[0023] The multilayer perceptron module includes two fully connected layers. The activation function of the first layer is ReLU, and the second layer does not use an activation function. The multilayer perceptron module deepens the fitting of complex processes and enhances the ability of the clone detection model.
[0024] As a preferred technical solution, the sparse attention module specifically: if the Transformer subnetwork contains L Transformer encoding modules, the sparse attention module uses the weights learned by the first L-1 Transformer encoding modules to filter the latent features input to the last Transformer encoding module; due to the abstract nature of high-level features, attention maps are difficult to represent the feature information of the corresponding input visual image patches; therefore, using the attention map information learned by all previous Transformer encoding modules, combined with the compression activation module, the weight of each attention map is autonomously learned, that is, the sparse attention module first fuses the previously obtained attention map information into a two-dimensional matrix through average pooling, and then uses two fully connected layers to model the correlation between attention map information to obtain the weight value of each attention map; finally, the weight values are normalized and weighted summed with the attention maps to obtain the final attention weights.
[0025] As a preferred technical solution, the Transformer sub-network maps cloned code visualization image pairs or non-cloned code visualization image pairs to a high-dimensional feature space, outputs corresponding representations, and calculates the representation distance based on the representations using the Contrastive Loss function; specifically:
[0026] First, the images of either cloned or non-cloned code visualization image pairs are segmented into N image patches of equal size. Then, these image patches are linearly mapped into serialized embedding vectors, and learnable classification vectors and positional encoding information are added. Second, the embedding vectors are combined into a matrix and input into multiple Transformer encoding modules for feature extraction. Before the last Transformer encoding module, the corresponding hidden features are input into a sparse attention module to find the identifiable pixel patches for either cloned or non-cloned code visualization image pairs. Finally, the classification features output by the Transformer encoding modules are processed by fully connected layers to obtain the category information for either cloned or non-cloned code visualization image pairs.
[0027] Secondly, using the Contrastive Loss function, the calculation is performed based on the fact that the visual image representation distance of cloned code is small and the visual image representation distance of non-cloned code is large. The calculation process is as follows:
[0028] First, select a pair of samples (X) a X b The Euclidean distance of this sample is:
[0029]
[0030] Among them, X a X represents the representation of the visualized image sample a. b This represents the visualization of image sample b;
[0031] The Contrastive Loss function is then expressed as:
[0032]
[0033] Where Y represents the label, Y=0 represents a non-clone pair or Y=1 represents a clone pair, d represents the Euclidean distance, and m represents the distance threshold of the samples; when (X a X b When the distance of ) is less than m, the Contrasive Loss will become 0, making X a With X b Similarity, rather than identicality, ensures the generalization ability of the algorithm to a certain extent;
[0034] Finally, the trained clone detection model outputs a sample representation distance based on the generated features. If the representation distance value is less than 0.5, the source code of the two input visualization images is a pair of clone codes.
[0035] In another aspect, the present invention provides a code clone detection system based on visualized images, applied to the aforementioned code clone detection method based on visualized images, including a dataset creation module, a code visualization module, a data preprocessing module, a clone detection model construction module, and a clone detection module;
[0036] The dataset creation module is used to obtain Java code data in .java file format; remove comments from the code in the .java file, and then call VoidVisitorAdapter in javaparser to separate the code functions in each .java file. Each separated code function is stored in a separate .java file to obtain the code function file.
[0037] The code visualization module is used to convert the code characters in the code function file into ASCII codes, then fill the ASCII code values with RGB primary color values to obtain RGB pixels, and combine the pixels into a visualized color image to obtain a code visualization image.
[0038] The data preprocessing module is used to divide the visualized code image into cloned code visualized images and non-cloned code visualized images, and synthesize a new cloned code visualized image using the SMOTE algorithm;
[0039] The clone detection model construction module is used to input the clone code visualization image and the non-clone code visualization image into a pre-established clone detection model for training, so as to obtain a trained clone detection model.
[0040] The clone detection model comprises two Transformer sub-networks that share weights. Each Transformer sub-network includes a Transformer encoding module and a sparse attention module. The Transformer encoding module extracts features from the code visualization image, while the sparse attention module identifies discriminative pixel blocks in the code visualization image and uses the corresponding latent features as input to the next encoding module. The training process involves inputting either cloned or non-cloned code visualization image pairs into the two Transformer sub-networks. The Transformer sub-networks map the cloned or non-cloned code visualization image pairs to a high-dimensional feature space, outputting corresponding representations. The representation distance is then calculated using the Contrastive Loss function based on these representations.
[0041] The clone detection module is used to input the code visualization image to be detected into the trained clone detection model for similarity detection and obtain the detection result.
[0042] In another aspect, the present invention provides an electronic device, characterized in that the electronic device comprises:
[0043] At least one processor; and,
[0044] A memory communicatively connected to the at least one processor; wherein,
[0045] The memory stores computer program instructions that can be executed by the at least one processor, which enables the at least one processor to perform the code clone detection method based on the visual image.
[0046] In another aspect, the present invention provides a computer-readable storage medium storing a program that, when executed by a processor, implements the code clone detection method based on visual images.
[0047] Compared with the prior art, the present invention has the following advantages and beneficial effects:
[0048] 1. This invention can perform clone detection based on the ASCII encoding level of the code, accurate to every character of the code, and delve into the details of the code. It avoids confusion of code clone types caused by simply judging the surface of the code, or detection errors caused by simply using the shape of the code as the detection basis, thus more accurately judging code clones.
[0049] 2. By using the ASCII encoding of the code to fill the pixels with the three primary color values, the resulting visual image can retain the source code information relatively completely without causing the loss of code information.
[0050] 3. Convert the code into an image, and then judge the similarity of the code by comparing the similarity of the images. The process is simple and easy to understand. Attached Figure Description
[0051] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0052] Figure 1 This is a flowchart of a code clone detection method based on visualized images according to an embodiment of the present invention;
[0053] Figure 2 This is a schematic diagram illustrating the code visualization of an embodiment of the present invention;
[0054] Figure 3 This is a schematic diagram illustrating the construction of the clone detection model according to an embodiment of the present invention;
[0055] Figure 4 This is a block diagram of the code clone detection system based on visualized images according to an embodiment of the present invention.
[0056] Figure 5 This is a structural diagram of an electronic device according to an embodiment of the present invention. Detailed Implementation
[0057] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are merely some embodiments of the present application, and not all embodiments. All other embodiments obtained by those skilled in the art based on the embodiments of the present application without creative effort are within the scope of protection of the present application.
[0058] In this application, the reference to "embodiment" means that a specific feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of this phrase in various places throughout the specification does not necessarily refer to the same embodiment, nor is it a mutually exclusive, independent, or alternative embodiment. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described in this application can be combined with other embodiments.
[0059] Terminology Explanation: Code Cloning Type
[0060] Type-1 (Complete Clone): Two code snippets are completely identical (except for comments and whitespace).
[0061] Type-2 (Renamed Clones): The two code snippets are identical except for changes to the names of variables, types, literals, and functions.
[0062] Type-3 (Create, Read, Update, Clone): Two code snippets are similar, with some statements added, deleted, or modified, as well as changes to the code layout.
[0063] Type-4 (self-implementing clone): Two code snippets achieve the same functionality, but in different ways.
[0064] Please see Figure 1 One embodiment of this application provides a code clone detection method based on visualized images, comprising the following steps:
[0065] S1. Obtain Java code data in .java file format; remove comments from the code in the .java file, and then call VoidVisitorAdapter in javaparser to separate the code functions in each .java file. Each separated code function is stored in a separate .java file to obtain the code function file.
[0066] Furthermore, a large amount of Java code data in .java file format was obtained through BigCloneBench and GitHub. Comments, including single-line comments, multi-line comments, and documentation comments, were removed from the code in the .java files. Then, the VoidVisitorAdapter in the javaparser was called to separate the code functions in each .java file. Each separated code function was stored in a separate .java file to obtain code function files. The same operation was performed on the data to be tested.
[0067] S2. Convert the code characters in the code function file into ASCII codes, then fill the ASCII code values with the RGB three primary color values to obtain RGB pixels. Combine the pixels into a visual color image to obtain a code visualization image.
[0068] Furthermore, the three primary colors are red (R), green (G), and blue (B); each character can be converted to ASCII code, and the value of the ASCII code can be used as the color value of the RGB three primary colors; for example, the string "public" is converted to ASCII code p(112), u(117), b(98), l(108), i(105), c(99), resulting in two primary color values [112,117,98] and [108,105,99], thus obtaining a yellow-gray pixel and a brown-gray pixel; Figure 2 As shown.
[0069] After the source code of the selected dataset is separated by the function in step S1, the size of the generated .java file is generally between 0 and 30kB. According to the conversion rule: 3 characters are converted into 1 pixel. Therefore, the number of pixels obtained after conversion is between 0 and 10240 (30*1024 / 3).
[0070] Meanwhile, by sorting the letters red R, green G, and blue B in six different ways: RBG, RGB, BGR, BRG, GRB, and GBR, and filling the ASCII codes with the color values of the three primary colors in a different order each time, six different visual color images can be obtained.
[0071] Furthermore, since the methods and functions in the source code are different, the size of the code visualization image converted from each .java file containing the code functions is also inconsistent. To solve this problem, code visualization images larger than 105*105 are cropped to 105*105 size; code visualization images smaller than 105*105 are padded with 0 to 105*105 size, while ensuring that the code visualization image is close to the center of the padded image to a certain extent.
[0072] S3. Divide the code visualization image into cloned code visualization image and non-cloned code visualization image, and synthesize a new cloned code visualization image using the SMOTE algorithm.
[0073] Furthermore, the synthesis of new cloned code visualization images using the SMOTE algorithm refers to the following: if the number of cloned code visualization images is much smaller than the number of non-cloned code visualization images, the SMOTE algorithm is used to randomly select a cloned code visualization image y from the nearest neighbors of each sample x in the cloned code visualization images, and then x and y are synthesized into a new cloned code visualization image. This oversampling method for synthesizing new samples can reduce the risk of overfitting. Then, the cloned code visualization images are labeled as 1 and the non-cloned code visualization images are labeled as 0. Using this as the dataset, it is divided into a 70% training set and a 30% validation set. The training dataset is mainly used to train the model, and the validation set is used to evaluate the results of the trained model.
[0074] S4. Input the cloned code visualization image and the non-cloned code visualization image into a pre-established clone detection model for training to obtain a trained clone detection model. The clone detection model includes two Transformer sub-networks, which share weights. Each Transformer sub-network includes a Transformer encoding module and a sparse attention module. The Transformer encoding module is used for feature extraction from the code visualization image, and the sparse attention module is used to find discriminative pixel blocks in the code visualization image and use their corresponding latent features as input to the next encoding module. The training process is as follows: input the cloned code visualization image pair or the non-cloned code visualization image pair into the two Transformer sub-networks. The Transformer sub-networks map the cloned code visualization image pair or the non-cloned code visualization image pair to a high-dimensional feature space and output the corresponding representation. The representation distance is calculated using the Contrastive Loss function based on the representation.
[0075] Furthermore, the Transformer encoding module specifically includes several Transformer encoding modules with identical structures in the Transformer sub-network; wherein, the Transformer encoding module includes a multi-head self-attention module and a multilayer perceptron module;
[0076] The multi-head self-attention module includes several self-attention modules. Different self-attention modules learn relevant features in mutually independent feature subspaces. Finally, the outputs of the multi-head self-attention modules are concatenated and then linearly transformed to obtain the output of the multi-head self-attention module. This output is residually connected with the input matrix, i.e., matrix addition, and finally standardized by layers as the input of the next multilayer perceptron module.
[0077] The multilayer perceptron module includes two fully connected layers. The activation function of the first layer is ReLU, while the second layer does not use an activation function. This multilayer perceptron module deepens the fitting of complex processes and enhances the ability of the clone detection model.
[0078] Specifically, the sparse attention module is proposed to fully utilize weight information for localization of identifiable regions. If the Transformer subnetwork contains L Transformer encoding modules, the sparse attention module uses the weights learned by the first L-1 Transformer encoding modules to filter the latent features input to the final Transformer encoding layer. Due to the abstract nature of high-level features, attention maps are difficult to represent the feature information of the corresponding input visual image patches. Therefore, the attention map information learned by all previous Transformer encoding modules is used in conjunction with the compression activation module to autonomously learn the weights of each attention map. The sparse attention module first fuses the previously obtained attention map information into a two-dimensional matrix through average pooling, then uses two fully connected layers to model the correlation between attention map information, and finally obtains the weight value of each attention map. Finally, the weight values are normalized and weighted summed with the attention maps to obtain the final attention weights.
[0079] Specifically, such as Figure 3 As shown, the Transformer sub-network maps cloned code visualization image pairs or non-cloned code visualization image pairs to a high-dimensional feature space, outputs corresponding representations, and calculates the representation distance using the Contrastive Loss function based on these representations; specifically:
[0080] First, the images of either cloned or non-cloned code visualization pairs are segmented into N image patches of equal size. Then, these image patches are linearly mapped into serialized embedding vectors, and learnable classification vectors and positional encoding information are added. Second, the embedding vectors are combined into a matrix and input into multiple Transformer encoding modules for feature extraction. Before the last Transformer encoding module, the corresponding hidden features are input into a sparse attention module to find identifiable pixel patches in either the cloned or non-cloned code visualization pairs. Finally, the classification features output by the Transformer encoding modules are processed by fully connected layers to obtain the category information for either the cloned or non-cloned code visualization pairs.
[0081] Secondly, using the Contrastive Loss function, the calculation is performed based on the fact that the visual image representation distance of cloned code is small and the visual image representation distance of non-cloned code is large. The calculation process is as follows:
[0082] First, select a pair of samples (X) a X b The Euclidean distance of this sample is:
[0083]
[0084] Among them, X a X represents the representation of the visualized image sample a. b This represents the visualization of image sample b.
[0085] The Contrastive Loss function is then expressed as:
[0086]
[0087] Where Y represents the label, Y=0 represents a non-clone pair or Y=1 represents a clone pair, d represents the Euclidean distance, and m represents the distance threshold of the samples; when (X a X b When the distance of ) is less than m, the Contrasive Loss will become 0, making X a With X b Similarity, rather than identicality, ensures the generalization ability of the algorithm to a certain extent;
[0088] Finally, the trained clone detection model outputs a sample representation distance based on generated features. If the representation distance value is less than 0.5, the source code of the two input visualization images is a pair of clone codes.
[0089] S5. Input the visualized image of the code to be detected into the trained clone detection model to perform similarity detection and obtain the detection results.
[0090] Furthermore, the code visualization image to be detected is input into the trained clone detection model for similarity detection, and the detection result is used to determine the code similarity based on image similarity.
[0091] It should be noted that, for the sake of simplicity, the aforementioned method embodiments are all described as a series of actions. However, those skilled in the art should understand that the present invention is not limited to the described order of actions, because according to the present invention, some steps can be performed in other orders or simultaneously.
[0092] Based on the same idea as the code clone detection method based on visual images in the above embodiments, the present invention also provides a code clone detection system based on visual images, which can be used to execute the above-described code clone detection method based on visual images. For ease of explanation, the structural diagram of the embodiment of the code clone detection system based on visual images only shows the parts related to the embodiments of the present invention. Those skilled in the art will understand that the illustrated structure does not constitute a limitation on the device, and may include more or fewer components than shown, or combine certain components, or have different component arrangements.
[0093] Please see Figure 4 In another embodiment of this application, a code clone detection system 100 based on visualized images is provided. The system includes a dataset creation module 101, a code visualization module 102, a data preprocessing module 103, a clone detection model construction module 104, and a clone detection module 105.
[0094] The dataset creation module 101 is used to obtain Java code data in .java file format; remove comments from the code in the .java file, and then call VoidVisitorAdapter in javaparser to separate the code functions in each .java file. Each separated code function is stored in a separate .java file to obtain the code function file.
[0095] The code visualization module 102 is used to convert the code characters in the code function file into ASCII codes, then fill the ASCII code values with RGB three primary color values to obtain RGB pixels, and combine the pixels into a visualized color image to obtain a code visualization image.
[0096] The data preprocessing module 103 is used to divide the code visualization image into cloned code visualization image and non-cloned code visualization image, and synthesize a new cloned code visualization image using the SMOTE algorithm.
[0097] The clone detection model construction module 104 is used to input the clone code visualization image and the non-clone code visualization image into a pre-established clone detection model for training, so as to obtain a trained clone detection model.
[0098] The clone detection model comprises two Transformer sub-networks that share weights. Each Transformer sub-network includes a Transformer encoding module and a sparse attention module. The Transformer encoding module extracts features from the code visualization image, while the sparse attention module identifies discriminative pixel blocks in the code visualization image and uses the corresponding latent features as input to the next encoding module. The training process involves inputting either cloned or non-cloned code visualization image pairs into the two Transformer sub-networks. The Transformer sub-networks map the cloned or non-cloned code visualization image pairs to a high-dimensional feature space, outputting corresponding representations. The representation distance is then calculated using the Contrastive Loss function based on these representations.
[0099] The clone detection module 105 is used to input the code visualization image to be detected into the trained clone detection model for similarity detection and obtain the detection result.
[0100] It should be noted that the code clone detection system based on visual images of the present invention corresponds one-to-one with the code clone detection method based on visual images of the present invention. The technical features and beneficial effects described in the embodiments of the code clone detection method based on visual images described above are applicable to the embodiments of the code clone detection system based on visual images. For details, please refer to the description in the embodiments of the method of the present invention, which will not be repeated here.
[0101] Furthermore, in the above embodiments of the code clone detection system based on visual images, the logical division of each program module is merely an example. In actual applications, the above functions can be assigned to different program modules as needed, for example, for the sake of corresponding hardware configuration requirements or the convenience of software implementation. That is, the internal structure of the code clone detection system based on visual images can be divided into different program modules to complete all or part of the functions described above.
[0102] Please see Figure 5In one embodiment, an electronic device is provided for implementing a code clone detection method based on a visual image. The electronic device 200 may include a first processor 201, a first memory 202 and a bus, and may also include a computer program stored in the first memory 202 and executable on the first processor 201, such as a code clone detection program 203 based on a visual image.
[0103] The first memory 202 includes at least one type of readable storage medium, including flash memory, portable hard drive, multimedia card, card-type memory (e.g., SD or DX memory), magnetic memory, magnetic disk, optical disk, etc. In some embodiments, the first memory 202 can be an internal storage unit of the electronic device 200, such as the portable hard drive of the electronic device 200. In other embodiments, the first memory 202 can also be an external storage device of the electronic device 200, such as a plug-in portable hard drive, smart media card (SMC), secure digital card (SD), flash card, etc., equipped on the electronic device 200. Furthermore, the first memory 202 can include both internal and external storage units of the electronic device 200. The first memory 202 can be used not only to store application software and various types of data installed on the electronic device 200, such as the code of the code cloning detection program 203 for visual images, but also to temporarily store data that has been output or will be output.
[0104] In some embodiments, the first processor 201 may be composed of integrated circuits, such as a single packaged integrated circuit or multiple integrated circuits with the same or different functions, including combinations of one or more central processing units (CPUs), microprocessors, digital processing chips, graphics processors, and various control chips. The first processor 201 is the control unit of the electronic device, connecting various components of the entire electronic device through various interfaces and lines. It executes programs or modules stored in the first memory 202 and calls data stored in the first memory 202 to perform various functions of the electronic device 200 and process data.
[0105] Figure 5 Only electronic devices with components are shown; it will be understood by those skilled in the art that... Figure 5 The structure shown does not constitute a limitation on the electronic device 200, and may include fewer or more components than shown, or combine certain components, or have different component arrangements.
[0106] The code clone detection program 203 for the visualized image stored in the first memory 202 of the electronic device 200 is a combination of multiple instructions, which, when run in the first processor 201, can achieve the following:
[0107] Obtain Java code data in .java file format; remove comments from the code in the .java file, and then call VoidVisitorAdapter in javaparser to separate the code functions in each .java file. Each separated code function is stored in a separate .java file to obtain the code function file.
[0108] The code characters in the code function file are converted into ASCII codes, and then the ASCII code values are filled with the RGB three primary color values to obtain RGB pixels. The pixels are combined into a visual color image to obtain a code visualization image.
[0109] The code visualization images are divided into cloned code visualization images and non-cloned code visualization images, and new cloned code visualization images are synthesized using the SMOTE algorithm.
[0110] The clone code visualization image and the non-clone code visualization image are input into a pre-established clone detection model for training to obtain a trained clone detection model;
[0111] The clone detection model comprises two Transformer sub-networks that share weights. Each Transformer sub-network includes a Transformer encoding module and a sparse attention module. The Transformer encoding module extracts features from the code visualization image, while the sparse attention module identifies discriminative pixel blocks in the code visualization image and uses the corresponding latent features as input to the next encoding module. The training process involves inputting either cloned or non-cloned code visualization image pairs into the two Transformer sub-networks. The Transformer sub-networks map the cloned or non-cloned code visualization image pairs to a high-dimensional feature space, outputting corresponding representations. The representation distance is then calculated using the Contrastive Loss function based on these representations.
[0112] The code visualization image to be detected is input into the trained clone detection model for similarity detection, and the detection result is obtained.
[0113] Furthermore, if the modules / units integrated in the electronic device 200 are implemented as software functional units and sold or used as independent products, they can be stored in a non-volatile computer-readable storage medium. The computer-readable medium may include: any entity or device capable of carrying the computer program code, a recording medium, a USB flash drive, a portable hard drive, a magnetic disk, an optical disk, a computer memory, or a read-only memory (ROM).
[0114] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, storage, databases, or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), RAMbus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and RAMbus dynamic RAM (RDRAM), etc.
[0115] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0116] The above embodiments are preferred embodiments of the present invention, but the embodiments of the present invention are not limited to the above embodiments. Any changes, modifications, substitutions, combinations, or simplifications made without departing from the spirit and principle of the present invention shall be considered equivalent substitutions and shall be included within the protection scope of the present invention.
Claims
1. A code clone detection method based on visualized images, characterized in that, Includes the following steps: Retrieve Java code data, in .java file format; Remove the comments from the code in the .java files, then call the VoidVisitorAdapter in javaparser to separate the code functions in each .java file. Each separated code function is stored in a separate .java file, resulting in a code function file. The code characters in the code function file are converted into ASCII codes, and then the ASCII code values are filled with the RGB three primary color values to obtain RGB pixels. The pixels are combined into a visual color image to obtain a code visualization image. The code visualization images are divided into cloned code visualization images and non-cloned code visualization images, and new cloned code visualization images are synthesized using the SMOTE algorithm. The clone code visualization image and the non-clone code visualization image are input into a pre-established clone detection model for training to obtain a trained clone detection model; The clone detection model comprises two Transformer sub-networks that share weights. Each Transformer sub-network includes a Transformer encoding module and a sparse attention module. The Transformer encoding module extracts features from the code visualization image, while the sparse attention module identifies discriminative pixel blocks in the code visualization image and uses the corresponding latent features as input to the next encoding module. The training process involves inputting either cloned or non-cloned code visualization image pairs into the two Transformer sub-networks. The Transformer sub-networks map the cloned or non-cloned code visualization image pairs to a high-dimensional feature space, outputting corresponding representations. The representation distance is then calculated using the Contrastive Loss function based on these representations. The code visualization image to be detected is input into the trained clone detection model for similarity detection, and the detection result is obtained.
2. The code clone detection method based on visual images according to claim 1, characterized in that, The removal of comments from the code in the .java file includes single-line comments, multi-line comments, and documentation comments.
3. The code clone detection method based on visual images according to claim 1, characterized in that, The process involves converting the code characters in the code function file into ASCII codes, then filling the ASCII code values with RGB primary color values to obtain RGB pixels. These pixels are then combined to form a visual color image, resulting in a code visualization image. Specifically: The three primary colors are red (R), green (G), and blue (B); The generated .java file size is between 0 and 30kB. Following the conversion rule of converting 3 characters to 1 pixel, the resulting pixel count is between 0 and 10240. Meanwhile, the red (R), green (G), and blue (B) are sorted in various ways, and the ASCII codes are filled with the color values of the three primary colors in different orders each time, resulting in multiple different visual color images. Furthermore, since the methods and functions in the source code are different, the size of the code visualization image converted from each .java file containing the code functions is also different. Therefore, code visualization images larger than the preset size are cropped to the preset size. Code visualization images smaller than the preset size will be padded with 0s to the preset size.
4. The code clone detection method based on visualized images according to claim 1, characterized in that, The process of synthesizing a new cloned code visualization image using the SMOTE algorithm refers to the following: if the number of cloned code visualization images is much smaller than the number of non-cloned code visualization images, the SMOTE algorithm is used to randomly select a cloned code visualization image y from the nearest neighbors of each sample x in the cloned code visualization images, and then x and y are synthesized into a new cloned code visualization image, thereby reducing the risk of overfitting; then the cloned code visualization image is labeled as 1, and the non-cloned code visualization image is labeled as 0.
5. The code clone detection method based on visualized images according to claim 1, characterized in that, Specifically, the Transformer encoding module includes several Transformer encoding modules with identical structures in the Transformer sub-network; wherein, the Transformer encoding module includes a multi-head self-attention module and a multilayer perceptron module; The multi-head self-attention module includes several self-attention modules. Different self-attention modules learn relevant features in mutually independent feature subspaces. Finally, the outputs of the multi-head self-attention modules are concatenated and then linearly transformed to obtain the output of the multi-head self-attention module. This output is residually connected with the input matrix, i.e., matrix addition, and finally standardized by layers as the input of the next multilayer perceptron module. The multilayer perceptron module includes two fully connected layers. The activation function of the first layer is ReLU, and the second layer does not use an activation function. The multilayer perceptron module enhances the fitting of complex processes.
6. The code clone detection method based on visualized images according to claim 1, characterized in that, Specifically, the sparse attention module works as follows: If the Transformer subnetwork contains L Transformer encoding modules, the sparse attention module uses the weights learned by the first L-1 Transformer encoding modules to filter the latent features input to the last Transformer encoding module. Due to the abstract nature of high-level features, attention maps are difficult to represent the feature information of the corresponding input visual image patches. Therefore, using the attention map information learned by all previous Transformer encoding modules, combined with the compression activation module, the module autonomously learns the weights of each attention map. That is, the sparse attention module first fuses the previously obtained attention map information into a two-dimensional matrix through average pooling, and then uses two fully connected layers to model the correlation between attention map information to obtain the weight value of each attention map. Finally, the weight values are normalized and then weighted and summed with the attention maps to obtain the final attention weights.
7. The code clone detection method based on visual images according to claim 1, characterized in that, The Transformer subnetwork maps either cloned code visualization image pairs or non-cloned code visualization image pairs to a high-dimensional feature space, outputs corresponding representations, and calculates the representation distance using the Contrastive Loss function based on these representations; specifically: First, the images of either cloned code visualization image pairs or non-cloned code visualization image pairs are segmented into N image patches of the same size. Then, the image patches are linearly mapped into serialized embedding vectors, and learnable classification vectors and positional encoding information are added. Secondly, the embedding vectors are combined into a matrix and input into multiple Transformer encoding modules for feature extraction. Before the last Transformer encoding module, the corresponding hidden features are input into a sparse attention module to find the identifiable pixel blocks of cloned code visualization image pairs or non-cloned code visualization image pairs. Finally, the classification features output by the Transformer encoding module are processed by a fully connected layer to obtain the category information of cloned code visualization image pairs or non-cloned code visualization image pairs. Secondly, using the Contrastive Loss function, the calculation is performed based on the fact that the visual image representation distance of cloned code is small and the visual image representation distance of non-cloned code is large. The calculation process is as follows: First, select a pair of samples ( , The Euclidean distance of this sample is: in, This represents the representation of the visualized image sample a. This represents the visualization of image sample b; The Contrastive Loss function is then expressed as: Where Y represents the label, Y=0 represents a non-clone pair or Y=1 represents a clone pair. Let m represent the Euclidean distance, and m represent the distance threshold of the samples; when ( , When the distance between ) and m is less than m, the Contrasive Loss will become 0, making and Similar, but not identical; Finally, the trained clone detection model outputs a sample representation distance based on the generated features. If the representation distance value is less than 0.5, the source code of the two input visualization images is a pair of clone codes.
8. A code clone detection system based on visualized images, characterized in that, The code clone detection method based on visualized images, applied to any one of claims 1-7, includes a dataset creation module, a code visualization module, a data preprocessing module, a clone detection model construction module, and a clone detection module; The dataset creation module is used to acquire Java code data in .java file format; Remove the comments from the code in the .java files, then call the VoidVisitorAdapter in javaparser to separate the code functions in each .java file. Each separated code function is stored in a separate .java file, resulting in a code function file. The code visualization module is used to convert the code characters in the code function file into ASCII codes, then fill the ASCII code values with RGB primary color values to obtain RGB pixels, and combine the pixels into a visualized color image to obtain a code visualization image. The data preprocessing module is used to divide the code visualization image into cloned code visualization images and non-cloned code visualization images, and synthesize a new cloned code visualization image using the SMOTE algorithm; The clone detection model construction module is used to input the clone code visualization image and the non-clone code visualization image into a pre-established clone detection model for training, so as to obtain a trained clone detection model. The clone detection model comprises two Transformer sub-networks that share weights. Each Transformer sub-network includes a Transformer encoding module and a sparse attention module. The Transformer encoding module extracts features from the code visualization image, while the sparse attention module identifies discriminative pixel blocks in the code visualization image and uses the corresponding latent features as input to the next encoding module. The training process involves inputting either cloned or non-cloned code visualization image pairs into the two Transformer sub-networks. The Transformer sub-networks map the cloned or non-cloned code visualization image pairs to a high-dimensional feature space, outputting corresponding representations. The representation distance is then calculated using the Contrastive Loss function based on these representations. The clone detection module is used to input the code visualization image to be detected into the trained clone detection model for similarity detection and obtain the detection result.
9. An electronic device, characterized in that, The electronic device includes: At least one processor; and, A memory communicatively connected to the at least one processor; wherein, The memory stores computer program instructions that can be executed by the at least one processor to enable the at least one processor to perform the code clone detection method based on visual images as described in any one of claims 1-7.
10. A computer-readable storage medium storing a program, characterized in that, When the program is executed by the processor, it implements the code clone detection method based on visual images as described in any one of claims 1-7.