Redundant code detection method, electronic device, storage medium and program product

By fully parsing the code base and analyzing its dynamic behavior characteristics, combined with the multimodal feature fusion of graph neural networks and temporal convolutional networks, the problems of high missed detection rate and high false positive rate in redundant code detection are solved, thereby improving detection accuracy and efficiency.

CN120508492BActive Publication Date: 2025-09-30INSPUR SUZHOU INTELLIGENT TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510999363.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-07-21
Publication Date
2025-09-30
Estimated Expiration
2045-07-21

AI Technical Summary

Technical Problem

Existing redundant code detection methods have problems such as high missed detection rate, coarse granularity and serious misjudgment, which leads to degraded system performance and reduced development efficiency.

Method used

By extending the semantic analysis rules, the target code base is fully parsed to generate an enhanced abstract syntax tree, build a code dependency graph, and use a hybrid model of graph neural network and temporal convolutional network to fuse multimodal features of dynamic behavior sequences and output the redundancy probability of each node.

Benefits of technology

The accuracy of redundant code detection is improved, memory usage is reduced, and higher detection accuracy and more comprehensive code redundancy identification are achieved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120508492B_ABST
    Figure CN120508492B_ABST
Patent Text Reader

Abstract

The present application discloses a redundant code detection method, electronic device, storage medium and program product, which relate to the field of software development technology, including performing full parsing of the target code base through extended semantic analysis rules, and generating an enhanced abstract syntax tree based on the full parsing results containing metadata information about the dynamic dependency relationship between the codes, and then constructing a code dependency graph based on the enhanced abstract syntax tree. A dynamic behavior sequence is generated based on the dynamic behavior features extracted during the code running process, and a hybrid model of a graph neural network and a time convolutional network is used to perform multimodal feature fusion on the code dependency graph and the dynamic behavior sequence to obtain the redundancy probability corresponding to each node in the code dependency graph, and then the redundant code is determined based on the redundancy probability, which solves the technical problems of too high missed detection rate, too coarse granularity and serious misjudgment, and achieves the technical effect of improving the detection accuracy of redundant code, improving the accuracy of redundant code detection, and reducing memory usage.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of software development technology, and in particular to a redundant code detection method, electronic equipment, storage medium, and program product. Background Art

[0002] Redundant code is common in long-term iterative software systems, primarily manifesting as uncalled methods, duplicated logic, and legacy interfaces. Redundant code can lead to degraded system performance, security risks, and reduced development efficiency.

[0003] There are currently two main methods for detecting redundant code. One is to target the project to be detected, use the project's startup method as input, build a function call relationship graph, detect the set of all reachable methods in the project, and detect the redundant code in the project. The other is to obtain all full classes of the project file and all loaded classes when the project file is running for the selected project file. Mark all the same classes between all full classes and all loaded classes, and the full classes that are not marked in all full classes are redundant classes. However, both methods have their own shortcomings and rely on static call graph analysis, resulting in a high missed detection rate. By comparing full classes with runtime loaded classes to identify redundancy, the granularity is too coarse and the misjudgment is serious. Classes that are preloaded but not actually called are mistakenly judged as valid. Summary of the Invention

[0004] The present application provides a redundant code detection method, electronic device, storage medium and program product to at least solve the problems in the related art of too high missed detection rate, too coarse granularity, serious misjudgment and high misjudgment rate.

[0005] This application provides a redundant code detection method, comprising:

[0006] Fully parse the target code base according to the pre-expanded semantic analysis rules to obtain a full parsing result that includes metadata information about the dynamic dependencies between codes;

[0007] Generate an enhanced abstract syntax tree according to the full parsing result, and construct a code dependency graph according to the enhanced abstract syntax tree;

[0008] Extracting dynamic behavior features during code execution in the target code library, and generating a dynamic behavior sequence based on the dynamic behavior features;

[0009] Using the pre-trained graph neural network and temporal convolutional network hybrid model, multimodal feature fusion is performed on the code dependency graph and the dynamic behavior sequence, and the redundancy probability corresponding to each node in the code dependency graph is output;

[0010] The redundant code is determined according to the redundant probability corresponding to each node.

[0011] The present application also provides an electronic device, comprising: a memory for storing a computer program; and a processor for implementing the steps of any of the above redundant code detection methods when executing the computer program.

[0012] The present application also provides a computer-readable storage medium, in which a computer program is stored. When the computer program is executed by a processor, the steps of any of the above redundant code detection methods are implemented.

[0013] The present application also provides a computer program product, including a computer program, which implements the steps of any of the above redundant code detection methods when executed by a processor.

[0014] Through this application, the target code base is fully parsed through extended semantic analysis rules, and an enhanced abstract syntax tree is generated based on the full parsing results containing metadata information about the dynamic dependencies between codes, and then a code dependency graph is constructed based on the enhanced abstract syntax tree. A dynamic behavior sequence is generated based on the dynamic behavior features extracted during code execution, and a hybrid model of a graph neural network and a time convolutional network is used to perform multimodal feature fusion on the code dependency graph and the dynamic behavior sequence to obtain the redundancy probability corresponding to each node in the code dependency graph, and then the redundant code is determined based on the redundancy probability. Through the fusion of dynamic and static dual-track data, the problem of high missed detection rate of traditional static analysis is avoided, and the collaborative modeling of graph structure and timing features is realized. Therefore, the technical problems of excessive missed detection rate, excessively coarse granularity and serious misjudgment, and high misjudgment rate can be solved, achieving the technical effect of greatly improving the detection accuracy of redundant code through redundant probability scoring, improving the accuracy of redundant code detection, and reducing memory usage. BRIEF DESCRIPTION OF THE DRAWINGS

[0015] In order to more clearly illustrate the embodiments of the present application, the following is a brief introduction to the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.

[0016] Figure 1 A flowchart of a redundant code detection method according to an embodiment of the present invention;

[0017] Figure 2 A flowchart of another redundant code detection method provided in an embodiment of the present application;

[0018] Figure 3 This is a structural block diagram of a redundant code detection device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0019] The following will be combined with the accompanying drawings in the embodiments of this application to clearly and completely describe the technical solutions in the embodiments of this application. Obviously, the embodiments described are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.

[0020] It should be noted that, in the description of this application, the terms "comprises," "includes," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or device. The terms "first," "second," etc., in this application are used to distinguish similar objects, and are not used to describe a particular order or sequence.

[0021] In order to enable those skilled in the art to better understand the present application, the present application is further described in detail below with reference to the accompanying drawings and specific implementation methods.

[0022] An embodiment of the present application provides a redundant code detection method, and the method is described in detail in conjunction with the execution process of the redundant code detection method.

[0023] See also Figure 1 , Figure 1 This is a flowchart of an implementation method of a redundant code detection method provided in an embodiment of the present application. The method may include the following steps.

[0024] S101: Fully parse the target code base according to the pre-expanded semantic analysis rules to obtain a full parsing result including metadata information of dynamic dependency relationships between codes.

[0025] The semantic analysis rules are expanded in advance, and the target code base is fully parsed according to the pre-expanded semantic analysis rules to obtain a full parsing result including metadata information of dynamic dependency relationships between codes.

[0026] S102: Generate an enhanced abstract syntax tree based on the full parsing result, and construct a code dependency graph based on the enhanced abstract syntax tree.

[0027] After obtaining the full parsing result containing metadata information about the dynamic dependencies between codes, an enhanced Abstract Syntax Tree (AST) is generated based on the full parsing result, and a code dependency graph is constructed based on the enhanced AST.

[0028] S103: extracting dynamic behavior features of the code in the target code library during execution, and generating a dynamic behavior sequence according to the dynamic behavior features.

[0029] The dynamic behavior features of the code in the target code library during execution are extracted, and a dynamic behavior sequence is generated based on the dynamic behavior features.

[0030] S104: Use the pre-trained graph neural network and time convolution network hybrid model to perform multimodal feature fusion on the code dependency graph and dynamic behavior sequence, and output the redundancy probability corresponding to each node in the code dependency graph.

[0031] Pre-train a hybrid model of graph neural network and temporal convolutional network, use the pre-trained hybrid model of graph neural network and temporal convolutional network to perform multimodal feature fusion on code dependency graph and dynamic behavior sequence, and output the redundancy probability corresponding to each node in the code dependency graph.

[0032] S105: Determine a redundant code according to the redundancy probability corresponding to each node.

[0033] After determining the redundancy probability corresponding to each node in the code dependency graph, the redundant code is determined based on the redundancy probability corresponding to each node. The higher the redundancy probability, the more likely it is a redundant code.

[0034] Through this application, the target code base is fully parsed through extended semantic analysis rules, and an enhanced abstract syntax tree is generated based on the full parsing results containing metadata information about the dynamic dependencies between codes, and then a code dependency graph is constructed based on the enhanced abstract syntax tree. A dynamic behavior sequence is generated based on the dynamic behavior features extracted during code execution, and a hybrid model of a graph neural network and a time convolutional network is used to perform multimodal feature fusion on the code dependency graph and the dynamic behavior sequence to obtain the redundancy probability corresponding to each node in the code dependency graph, and then the redundant code is determined based on the redundancy probability. Through the fusion of dynamic and static dual-track data, the problem of high missed detection rate of traditional static analysis is avoided, and the collaborative modeling of graph structure and timing features is realized. Therefore, the technical problems of excessive missed detection rate, excessively coarse granularity and serious misjudgment, and high misjudgment rate can be solved, so as to achieve the technical effect of greatly improving the detection accuracy of redundant code through redundant probability scoring, improving the accuracy of redundant code detection, and reducing memory usage.

[0035] See also Figure 2 , Figure 2 This is a flowchart of another redundant code detection method provided in an embodiment of the present application. The method may include the following steps.

[0036] S201: The target code base is fully parsed by the parser to obtain a basic abstract syntax tree.

[0037] The target code base is fully parsed through a parser, such as a Java parser tool, to obtain a basic abstract syntax tree.

[0038] S202: Perform reflection call identification on the basic abstract syntax tree to obtain the dynamically loaded class name and method signature.

[0039] After parsing the base abstract syntax tree, reflective calls are performed on the tree to identify the dynamically loaded class names and method signatures. For example, custom syntax tree traversal logic can be used to detect reflection patterns such as loading a specified class using Class.forName() and calling a specified method using Method.invoke(), recording the dynamically loaded class names and method signatures.

[0040] In a specific implementation of the present application, step S202 may include the following steps:

[0041] Step 1: Traverse the basic abstract syntax tree according to the preset syntax tree traversal logic to detect the reflection call pattern;

[0042] Step 2: Determine the class name and method signature corresponding to the reflection call mode;

[0043] Step 3: Determine the class name and method signature corresponding to the reflection call mode as the class name and method signature to be dynamically loaded.

[0044] For the convenience of description, the above three steps can be combined for explanation.

[0045] Syntax tree traversal logic is pre-set for detecting reflective call patterns. The base abstract syntax tree is traversed according to the preset syntax tree traversal logic to detect the reflective call pattern, determine the class name and method signature corresponding to the reflective call pattern, and then determine the class name and method signature corresponding to the reflective call pattern as the class name and method signature being dynamically loaded. By detecting the reflective call pattern according to the preset syntax tree traversal logic and determining the class name and method signature corresponding to the reflective call pattern as the class name and method signature being dynamically loaded, the efficiency of detecting dynamically loaded class names and method signatures is improved.

[0046] S203: Perform dynamic proxy association on the basic abstract syntax tree to obtain a mapping relationship between the proxy class and the original class.

[0047] After parsing the base abstract syntax tree, a dynamic proxy association is performed on the base abstract syntax tree to obtain a mapping relationship between the proxy class and the original class. For example, the mapping relationship between the proxy class and the original class is established by analyzing the interface parameters of the Proxy.newProxyInstance() method call generated by the dynamic proxy instance.

[0048] In a specific implementation of the present application, step S203 may include the following steps:

[0049] The basic abstract syntax tree is analyzed to obtain the interface parameters of the dynamic proxy interface method call and the mapping relationship between the proxy class and the original class.

[0050] After parsing the base abstract syntax tree, the interface parameters of the dynamic proxy interface method calls are analyzed to determine the mapping relationship between the proxy class and the original class. This direct analysis of the interface parameters of the dynamic proxy interface method calls improves the efficiency of determining the mapping relationship between the proxy class and the original class.

[0051] S204: Annotate the basic abstract syntax tree and trace its dependencies to obtain the implicit dependency chain between the codes.

[0052] After parsing the base abstract syntax tree, annotation dependency tracking is performed on the base abstract syntax tree to obtain implicit dependency chains between codes. For example, annotations such as @Autowired and @Resource are parsed to build implicit dependency chains across modules.

[0053] S205: Generate dynamic dependency metadata information between codes based on the dynamically loaded class name and method signature, the mapping relationship between the proxy class and the original class, and the implicit dependency chain between codes.

[0054] After obtaining the dynamically loaded class name and method signature, the mapping relationship between the proxy class and the original class, and the implicit dependency chain between the codes, dynamic dependency metadata information between the codes is generated based on the dynamically loaded class name and method signature, the mapping relationship between the proxy class and the original class, and the implicit dependency chain between the codes.

[0055] S206: Enhance the basic abstract syntax tree using the dynamic dependency metadata information to obtain an enhanced abstract syntax tree.

[0056] After generating metadata about the dynamic dependencies between code, the base abstract syntax tree is enhanced using this metadata to produce an enhanced abstract syntax tree. By extending semantic analysis rules to generate metadata about the dynamic dependencies between code, dynamic scenarios are covered, and the base abstract syntax tree is enhanced based on this metadata.

[0057] S207: Obtain the node type corresponding to each node in the enhanced abstract syntax tree and the relationship type between the nodes.

[0058] After obtaining the enhanced abstract syntax tree, obtain the node type corresponding to each node in the enhanced abstract syntax tree and the relationship type between the nodes. Node types can include class, method, field, dynamic dependency, etc. The relationship types between nodes can include (1) call (CALLS): method A explicitly calls method B; (2) inheritance (EXTENDS): class A inherits class B; (3) dynamic reference (DYNAMIC_REF): implicit call relationship generated by reflection or proxy.

[0059] S208: Obtain node attributes corresponding to each node in the enhanced abstract syntax tree.

[0060] After obtaining the enhanced abstract syntax tree, node attributes corresponding to each node in the enhanced abstract syntax tree are obtained. The node attributes may include code complexity, call frequency, etc. of the node.

[0061] S209: Construct a code dependency graph according to the node types corresponding to the nodes, the relationship types between the nodes, and the node attributes corresponding to the nodes.

[0062] After obtaining the node types and relationship types corresponding to each node in the enhanced abstract syntax tree, as well as the node attributes corresponding to each node, a code dependency graph is constructed based on the node types, relationship types, and node attributes corresponding to each node. By converting AST nodes into entities in a graph database (such as Neo4j), a multi-level dependency network is constructed. By constructing a code dependency graph based on the node types, relationship types, and node attributes corresponding to each node, the resulting code dependency graph is enriched with more node information and more comprehensive code features.

[0063] In a specific implementation of the present application, step S208 may include the following steps:

[0064] Step 1: Obtain the code complexity corresponding to each node in the enhanced abstract syntax tree;

[0065] Step 2: Obtain the call frequency corresponding to each node in the enhanced abstract syntax tree;

[0066] Accordingly, step S209 may include the following steps:

[0067] A code dependency graph is constructed based on the node type corresponding to each node, the relationship type between nodes, and the code complexity and call frequency corresponding to each node.

[0068] For the convenience of description, the above steps can be combined for explanation.

[0069] Obtaining the code complexity and call frequency corresponding to each node in the enhanced abstract syntax tree can determine the call frequency based on the number of times the method has been called in historical log statistics. A code dependency graph is constructed based on the node type corresponding to each node, the type of relationship between nodes, the code complexity corresponding to each node, and the call frequency. By constructing a code dependency graph based on the node type corresponding to each node, the type of relationship between nodes, the code complexity corresponding to each node, and the call frequency, the richness of the node information contained in the constructed code dependency graph and the comprehensiveness of the code features are further improved.

[0070] Calculate cyclomatic complexity, that is, code complexity, the formula is:

[0071] ;

[0072] Among them, E is the number of control flow edges, N is the number of inlet nodes, and P is the number of outlet nodes.

[0073] In a specific implementation of the present application, obtaining the code complexity corresponding to each node in the enhanced abstract syntax tree may include the following steps:

[0074] Step 1: Obtain the number of control flow edges, import nodes, and export nodes corresponding to each node in the enhanced abstract syntax tree;

[0075] Step 2: Calculate the code complexity corresponding to each node based on the number of control flow edges, import nodes, and export nodes corresponding to each node.

[0076] For the convenience of description, the above two steps can be combined for explanation.

[0077] The number of control flow edges, the number of import nodes, and the number of export nodes corresponding to each node in the enhanced abstract syntax tree are obtained, and the code complexity corresponding to each node is calculated according to the number of control flow edges, the number of import nodes, and the number of export nodes corresponding to each node. By calculating the code complexity corresponding to each node according to the number of control flow edges, the number of import nodes, and the number of export nodes corresponding to each node, the calculation efficiency of the code complexity corresponding to each node is improved.

[0078] S210: Extracting the class loading mode and resource access sequence during code execution in the target code library.

[0079] During the code execution in the target code library, a class loading mode and a resource access sequence during the code execution in the target code library are extracted.

[0080] Key behavioral features can be extracted from runtime data, including the following: class loading patterns, which count the number of class loads and their distribution over time in development, test, and production environments; resource access sequences, which record file read and write operations by time window (e.g., 5 minutes) to generate time series; and abnormal call detection, which flags code segments that have not been called for a long time but suddenly become active (possibly hidden dependencies).

[0081] S211: Generate a dynamic behavior sequence according to the class loading mode and resource access sequence.

[0082] After extracting the class loading pattern and resource access sequence, a dynamic behavior sequence is generated based on the class loading pattern and resource access sequence. By generating the dynamic behavior sequence based on the extracted class loading pattern and resource access sequence, the generation efficiency of the dynamic behavior sequence is improved.

[0083] Deploy a low-intrusive monitoring agent in the target environment to capture actual code execution behavior. For Linux environments, use the Extended Berkeley Packet Filter (eBPF) technology to monitor system calls such as open() and read() at the kernel level, filtering out access paths to class files (.class) and resource files (such as configuration files). This process is then linked to specific Java services through process identities (IDs) to prevent cross-process interference. For Windows environments, use Event Tracing for Windows (ETW) technology to subscribe to process or image load events, recording all loaded classes and their source JAR (Java Archive) packages. Based on the monitoring results, a global resource access heat map can also be generated.

[0084] S212: Utilize the graph neural network in the hybrid model of graph neural network and temporal convolutional network to update the node features of each node according to the node data and edge data of the code dependency graph, and output the graph structure embedding corresponding to each node.

[0085] After constructing the code dependency graph, the graph neural network in the hybrid model of graph neural network and temporal convolutional network is used to update the node features of each node according to the node data and edge data of the code dependency graph, and output the graph structure embedding corresponding to each node.

[0086] The hybrid model of graph neural network and temporal convolutional network includes a graph neural network (GNN) branch. The input is the node and edge data of the dependent graph using a three-layer graph attention network (GATv2). The node feature update formula is:

[0087] ;

[0088] in, is the embedding vector of node i at layer l+1 (i.e., the updated feature representation); is the embedding vector of node j in layer l; An activation function (such as ReLU, Sigmoid, etc.) is used to introduce nonlinearity; is the set of neighbor nodes of node i (i.e., nodes directly connected to node i); is the attention weight between node i and node j, indicating the importance of node j to node i; is the learnable weight matrix of the lth layer, which is used to linearly transform the embedding of neighbor nodes.

[0089] S213: Use the temporal convolutional network in the hybrid model of graph neural network and temporal convolutional network to extract temporal features of dynamic behavior sequences and obtain temporal embedding.

[0090] After generating the dynamic behavior sequence, the temporal convolutional network in the hybrid model of graph neural network and temporal convolutional network is used to extract the temporal features of the dynamic behavior sequence and obtain the temporal embedding.

[0091] The hybrid model of graph neural network and temporal convolutional network includes a temporal convolutional network (TCN) branch. TCN is used to extract temporal features of dynamic behavior sequences and a causal convolution kernel is designed to capture long-distance dependencies. Define the convolution kernel weight matrix and the output sequence The calculation formula is:

[0092] ;

[0093] in, is the input sequence, and K is the convolution kernel length.

[0094] S214: Use the feature fusion layer in the hybrid model of the graph neural network and the temporal convolutional network to perform weighted fusion on the graph structure embedding and the temporal embedding corresponding to each node to obtain the fused feature embedding corresponding to each node.

[0095] After obtaining the graph structure embedding corresponding to each node and the temporal embedding, the feature fusion layer in the hybrid model of graph neural network and temporal convolutional network is used to perform weighted fusion on the graph structure embedding and temporal embedding corresponding to each node to obtain the fused feature embedding corresponding to each node.

[0096] The hybrid model of graph neural network and temporal convolutional network includes a feature fusion layer, which uses a gated cross-attention mechanism to perform weighted fusion of the graph structure embedding output by GNN and the temporal embedding output by TCN:

[0097] ;

[0098] in, is a learnable fusion coefficient, which is optimized by the cross entropy loss function; is the graph-level embedding output by GNN, which is the final embedding of all nodes Aggregate; is the global temporal embedding of TCN output, which is composed of all time steps Aggregate; is the feature embedding after fusion.

[0099] S215: Use the fully connected network in the hybrid model of the graph neural network and the temporal convolutional network to calculate the redundancy probability score of each fused feature embedding to obtain the redundancy probability corresponding to each node.

[0100] After obtaining the fused feature embeddings corresponding to each node, the fully connected network in the hybrid model of the graph neural network and temporal convolutional network is used to calculate the redundancy probability score for each fused feature embedding, thereby obtaining the redundancy probability corresponding to each node. By extracting the graph structure embedding and temporal embedding of each node through each layer of the hybrid model of the graph neural network and temporal convolutional network, and performing a weighted fusion to obtain the fused feature embedding, the redundancy probability score corresponding to each node is accurately calculated, improving the efficiency of the calculation of the redundancy probability corresponding to each node.

[0101] The redundancy probability can be obtained according to the redundancy probability formula:

[0102] ;

[0103] in, and are network parameters, is the redundancy probability.

[0104] S216: Obtain a preset redundancy probability threshold.

[0105] A redundancy probability threshold is preset to obtain the preset redundancy probability threshold.

[0106] S217: Compare the redundancy probability corresponding to each node with the redundancy probability threshold to obtain the repair priority corresponding to each node.

[0107] After calculating the redundancy probability corresponding to each node and obtaining the preset redundancy probability threshold, the redundancy probability corresponding to each node is compared with the redundancy probability threshold to obtain the repair priority corresponding to each node.

[0108] It should be noted that the redundancy probability threshold can be set and adjusted according to actual conditions, and the embodiment of the present application does not limit this. For example, the upper limit of the redundancy probability threshold can be set to 0.9, and the lower limit of the redundancy probability threshold can be set to 0.7.

[0109] In a specific implementation of the present application, step S217 may include the following steps:

[0110] Step 1: Determine the repair priority of the node whose redundancy probability is greater than the upper limit of the redundancy probability threshold as a high priority;

[0111] Step 2: Determine the repair priority of the node whose redundancy probability is between the upper limit and the lower limit of the redundancy probability threshold as medium priority;

[0112] Step 3: The repair priority of the node whose redundancy probability is lower than the lower limit of the redundancy probability threshold is determined as a low priority.

[0113] For the convenience of description, the above three steps can be combined for explanation.

[0114] After calculating the redundancy probability corresponding to each node and obtaining the preset redundancy probability threshold, the repair priority of nodes with a redundancy probability greater than the upper limit of the redundancy probability threshold is determined to be high priority. The repair priority of nodes with a redundancy probability between the upper and lower limits of the redundancy probability threshold is determined to be medium priority. The repair priority of nodes with a redundancy probability below the lower limit of the redundancy probability threshold is determined to be low priority. By determining the repair priority based on the upper and lower limits of the redundancy probability threshold, the efficiency of repair priority determination is further improved.

[0115] S218: Determine the redundant code according to the repair priorities corresponding to the nodes.

[0116] After obtaining the repair priorities corresponding to each node, the redundant code is determined according to the repair priorities corresponding to each node. The redundant code is determined by setting a redundant probability threshold, which improves the convenience and efficiency of redundant code identification.

[0117] You can also set up manual confirmation and false positive feedback. Developers can confirm the recommended results and return false positive samples (actual non-redundant code) to the training set to update model parameters. This false positive return mechanism forms a self-optimizing closed loop, significantly reducing the cost of manual intervention.

[0118] In a specific implementation of the present application, step S218 may include the following steps:

[0119] Step 1: Determine the code corresponding to the node with a high repair priority as redundant code;

[0120] Step 2: Mark the code corresponding to the node with medium priority as code to be verified;

[0121] Step 3: Keep the code corresponding to the node with low repair priority.

[0122] For the convenience of description, the above three steps can be combined for explanation.

[0123] After determining the repair priority for each node, the code corresponding to nodes with a high repair priority is identified as redundant, the code corresponding to nodes with a medium repair priority is marked as pending, and the code corresponding to nodes with a low repair priority is retained. By processing the code corresponding to each node according to the repair priority, the accuracy of the code processing for each node is greatly improved.

[0124] Java redundant code detection technology significantly improves system maintainability and performance by accurately identifying unused, duplicated, or inefficient code. This technology directly optimizes compilation speed, reduces memory usage, and improves deployment efficiency. It also mitigates system anomalies, security vulnerabilities, and compatibility issues caused by legacy code, allowing teams to focus on core feature development. First, it uses enhanced abstract syntax tree parsing and semantic rule expansion to construct a code dependency graph, accurately capturing static features such as reflection calls. Simultaneously, it leverages eBPF or ETW to low-costly trace runtime system calls and extract dynamic behavior data such as class loading and resource access. These two features are fed into a hybrid GNN+TCN model for multimodal feature fusion, outputting a redundancy probability score and generating graded remediation recommendations. This technology overcomes the limitations of traditional static or dynamic analysis, achieving more comprehensive code redundancy identification through dual-track data fusion. Developers confirm and then safely delete them. False positive samples are then fed back into the model to drive model iteration, forming a closed loop of "analysis-decision-verification-optimization." Ultimately, this achieves high detection rates and low performance loss in complex scenarios, balancing detection accuracy with system stability and significantly reducing labor costs.

[0125] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus the necessary general hardware platform, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method.

[0126] An embodiment of the present application also provides a redundant code detection device.

[0127] See also Figure 3 , Figure 3 This is a structural block diagram of a redundant code detection device provided in an embodiment of the present application. The device may include:

[0128] A full parsing module 31 is used to perform a full parsing of the target code base according to the pre-expanded semantic analysis rules to obtain a full parsing result including metadata information about dynamic dependencies between codes;

[0129] A code dependency graph construction module 32 is configured to generate an enhanced abstract syntax tree based on the full parsing result and to construct a code dependency graph based on the enhanced abstract syntax tree;

[0130] A dynamic behavior sequence generation module 33 is used to extract dynamic behavior features during code execution in the target code library and generate a dynamic behavior sequence based on the dynamic behavior features;

[0131] The redundancy probability output module 34 is used to use the pre-trained graph neural network and time convolution network hybrid model to perform multimodal feature fusion on the code dependency graph and dynamic behavior sequence, and output the redundancy probability corresponding to each node in the code dependency graph;

[0132] The redundant code determination module 35 is configured to determine the redundant code according to the redundant probability corresponding to each node.

[0133] Through this application, the target code base is fully parsed through extended semantic analysis rules, and an enhanced abstract syntax tree is generated based on the full parsing results containing metadata information about the dynamic dependencies between codes, and then a code dependency graph is constructed based on the enhanced abstract syntax tree. A dynamic behavior sequence is generated based on the dynamic behavior features extracted during code execution, and a hybrid model of a graph neural network and a time convolutional network is used to perform multimodal feature fusion on the code dependency graph and the dynamic behavior sequence to obtain the redundancy probability corresponding to each node in the code dependency graph, and then the redundant code is determined based on the redundancy probability. Through the fusion of dynamic and static dual-track data, the problem of high missed detection rate of traditional static analysis is avoided, and the collaborative modeling of graph structure and timing features is realized. Therefore, the technical problems of excessive missed detection rate, excessively coarse granularity and serious misjudgment, and high misjudgment rate can be solved, so as to achieve the technical effect of greatly improving the detection accuracy of redundant code through redundant probability scoring, improving the accuracy of redundant code detection, and reducing memory usage.

[0134] In a specific embodiment of the present application, the full-data parsing module 31 may include:

[0135] The basic abstract syntax tree acquisition submodule is used to fully parse the target code base through the parser to obtain the basic abstract syntax tree;

[0136] The reflection call identification submodule is used to perform reflection call identification on the basic abstract syntax tree to obtain the class name and method signature of the dynamically loaded class.

[0137] The mapping relationship acquisition submodule is used to dynamically proxy the basic abstract syntax tree to obtain the mapping relationship between the proxy class and the original class;

[0138] Obtain submodules through implicit dependency chains, which are used to annotate dependency tracking on the basic abstract syntax tree and obtain implicit dependency chains between codes;

[0139] The metadata information generation submodule is used to generate metadata information about the dynamic dependency relationship between codes based on the dynamically loaded class names and method signatures, the mapping relationship between the proxy class and the original class, and the implicit dependency chain between codes;

[0140] The code dependency graph construction module 32 is specifically a module that uses dynamic dependency metadata information to enhance the basic abstract syntax tree to obtain an enhanced abstract syntax tree.

[0141] In a specific embodiment of the present application, the reflection call identification submodule may include:

[0142] A reflection call mode obtaining unit is used to traverse the basic abstract syntax tree according to a preset syntax tree traversal logic to detect and obtain a reflection call mode;

[0143] A class name and method signature determination unit, used to determine the class name and method signature corresponding to the reflection call mode;

[0144] The class name and method signature determining unit is used to determine the class name and method signature corresponding to the reflection call mode as the class name and method signature to be dynamically loaded.

[0145] In a specific embodiment of the present application, the mapping relationship obtaining submodule is specifically a module that analyzes the interface parameters of the dynamic proxy interface method call on the basic abstract syntax tree to obtain the mapping relationship between the proxy class and the original class.

[0146] In a specific embodiment of the present application, the code dependency graph construction module 32 may include:

[0147] The relationship type acquisition submodule is used to obtain the node type corresponding to each node in the enhanced abstract syntax tree and the relationship type between the nodes;

[0148] The node attribute acquisition submodule is used to obtain the node attributes corresponding to each node in the enhanced abstract syntax tree;

[0149] The code dependency graph construction submodule is used to construct a code dependency graph based on the node type corresponding to each node, the relationship type between nodes, and the node attributes corresponding to each node.

[0150] In a specific embodiment of the present application, the node attribute acquisition submodule may include:

[0151] A code complexity obtaining unit, used to obtain the code complexity corresponding to each node in the enhanced abstract syntax tree;

[0152] A call frequency acquisition unit, used to acquire the call frequency corresponding to each node in the enhanced abstract syntax tree;

[0153] The code dependency graph construction submodule is specifically a module that constructs a code dependency graph based on the node type corresponding to each node, the relationship type between nodes, and the code complexity and call frequency corresponding to each node.

[0154] In a specific embodiment of the present application, the code complexity acquisition unit may include:

[0155] The edge number and node number acquisition subunit is used to obtain the control flow edge number, import node number, and export node number corresponding to each node in the enhanced abstract syntax tree;

[0156] The code complexity calculation subunit is used to calculate the code complexity corresponding to each node according to the number of control flow edges, the number of import nodes, and the number of export nodes corresponding to each node.

[0157] In a specific embodiment of the present application, the dynamic behavior sequence generation module 33 may include:

[0158] The class loading mode and resource access sequence extraction submodule is used to extract the class loading mode and resource access sequence during the code execution process in the target code library;

[0159] The dynamic behavior sequence generation submodule is used to generate dynamic behavior sequences based on class loading patterns and resource access sequences.

[0160] In a specific embodiment of the present application, the redundancy probability output module 34 may include:

[0161] The graph structure embedding output submodule is used to update the node features of each node based on the node data and edge data of the code dependency graph using the graph neural network in the hybrid model of the graph neural network and the temporal convolutional network, and output the graph structure embedding corresponding to each node;

[0162] The temporal embedding acquisition submodule is used to extract temporal features of dynamic behavior sequences using the temporal convolutional network in the hybrid model of graph neural network and temporal convolutional network to obtain temporal embedding;

[0163] The fused feature embedding acquisition submodule is used to perform weighted fusion of the graph structure embedding and temporal embedding corresponding to each node using the feature fusion layer in the hybrid model of graph neural network and temporal convolutional network to obtain the fused feature embedding corresponding to each node;

[0164] The redundancy probability acquisition submodule is used to calculate the redundancy probability score of each fused feature embedding using the fully connected network in the hybrid model of graph neural network and temporal convolutional network, and obtain the redundancy probability corresponding to each node.

[0165] In a specific embodiment of the present application, the redundant code determination module 35 may include:

[0166] A redundancy probability threshold acquisition submodule is used to obtain a preset redundancy probability threshold;

[0167] The repair priority acquisition submodule is used to compare the redundancy probability corresponding to each node with the redundancy probability threshold to obtain the repair priority corresponding to each node;

[0168] The redundant code determination submodule is used to determine the redundant code according to the repair priorities corresponding to the nodes.

[0169] In a specific embodiment of the present application, the repair priority obtaining submodule may include:

[0170] a high priority determination unit, configured to determine the repair priority of a node whose redundancy probability is greater than an upper limit value of a redundancy probability threshold as a high priority;

[0171] a medium priority determination unit, configured to determine the repair priority of a node whose redundancy probability is between an upper limit value and a lower limit value of a redundancy probability threshold as a medium priority;

[0172] The low priority determination unit is configured to determine the repair priority of a node whose redundancy probability is lower than a lower limit of a redundancy probability threshold as a low priority.

[0173] In a specific implementation of the present application, the redundant code determination submodule may include:

[0174] A redundant code determining unit, configured to determine a code corresponding to a node having a high repair priority as a redundant code;

[0175] A code-to-be-verified determining unit, configured to mark the code corresponding to the node with a medium repair priority as a code to be verified;

[0176] The code reservation unit is used to reserve the codes corresponding to the nodes with low repair priorities.

[0177] For the description of the features in the embodiment corresponding to the redundant code detection device, reference can be made to the relevant description of the embodiment corresponding to the redundant code detection method, which will not be repeated here.

[0178] An embodiment of the present application further provides an electronic device, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to execute the steps in any of the above redundant code detection method embodiments.

[0179] An embodiment of the present application further provides a computer-readable storage medium, in which a computer program is stored. The computer program is configured to execute the steps of any of the above redundant code detection method embodiments when running.

[0180] In an exemplary embodiment, the computer-readable storage medium may include, but is not limited to, various media that can store computer programs, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk, or an optical disk.

[0181] An embodiment of the present application further provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the steps in any of the above redundant code detection method embodiments are implemented.

[0182] An embodiment of the present application further provides another computer program product, including a non-volatile computer-readable storage medium, wherein the non-volatile computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of any of the above redundant code detection method embodiments are implemented.

[0183] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the above description has generally described the components and steps of each example according to their functions. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0184] The above describes in detail a redundant code detection method, electronic device, storage medium, and program product provided by the present application. Specific examples are used herein to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only intended to help understand the method and core concept of the present application. It should be noted that, for those skilled in the art, without departing from the principles of the present application, several improvements and modifications may be made to the present application, and these improvements and modifications also fall within the scope of protection of the present application.

Claims

1. A redundant code detection method, characterized in that: include: Fully parse the target code base according to the pre-expanded semantic analysis rules to obtain a full parsing result that includes metadata information about the dynamic dependencies between codes; Generate an enhanced abstract syntax tree according to the full parsing result, and construct a code dependency graph according to the enhanced abstract syntax tree; Extracting dynamic behavior features during code execution in the target code library, and generating a dynamic behavior sequence based on the dynamic behavior features; Using the pre-trained graph neural network and temporal convolutional network hybrid model, multimodal feature fusion is performed on the code dependency graph and the dynamic behavior sequence, and the redundancy probability corresponding to each node in the code dependency graph is output; Determine the redundant code according to the redundant probability corresponding to each node; The target code base is fully parsed according to the pre-expanded semantic analysis rules to obtain a full parsing result containing metadata information about the dynamic dependencies between codes, including: The target code base is fully parsed by a parser to obtain a basic abstract syntax tree; Performing reflection call identification on the basic abstract syntax tree to obtain the class name and method signature to be dynamically loaded; Performing dynamic proxy association on the basic abstract syntax tree to obtain a mapping relationship between the proxy class and the original class; Perform annotation dependency tracing on the basic abstract syntax tree to obtain an implicit dependency chain between codes; Generate dynamic dependency metadata between codes based on the dynamically loaded class names and method signatures, the mapping relationship between proxy classes and original classes, and the implicit dependency chains between codes; Accordingly, an enhanced abstract syntax tree is generated according to the full parsing result, including: Enhancing the basic abstract syntax tree using the dynamic dependency metadata information to obtain the enhanced abstract syntax tree; The pre-trained graph neural network and temporal convolutional network hybrid model is used to perform multimodal feature fusion on the code dependency graph and the dynamic behavior sequence, and the redundancy probability corresponding to each node in the code dependency graph is output, including: Utilize the graph neural network in the hybrid model of the graph neural network and the temporal convolutional network to update the node features of each node according to the node data and edge data of the code dependency graph, and output the graph structure embedding corresponding to each node; Utilizing the temporal convolutional network in the hybrid model of the graph neural network and the temporal convolutional network to extract temporal features of the dynamic behavior sequence to obtain a temporal embedding; The feature fusion layer in the hybrid model of the graph neural network and the temporal convolutional network is used to perform weighted fusion on the graph structure embedding and the temporal embedding corresponding to each node to obtain the fused feature embedding corresponding to each node; The fully connected network in the hybrid model of the graph neural network and the temporal convolutional network is used to calculate the redundancy probability score of each fused feature embedding to obtain the redundancy probability corresponding to each node.

2. The redundant code detection method according to claim 1, wherein: Performing reflection call identification on the basic abstract syntax tree to obtain the dynamically loaded class name and method signature, including: Traversing the basic abstract syntax tree according to a preset syntax tree traversal logic to detect and obtain a reflection call pattern; Determine the class name and method signature corresponding to the reflection call mode; The class name and method signature corresponding to the reflection call mode are determined as the class name and method signature to be dynamically loaded.

3. The redundant code detection method according to claim 1, wherein: Dynamic proxy association is performed on the basic abstract syntax tree to obtain a mapping relationship between the proxy class and the original class, including: The basic abstract syntax tree is analyzed to obtain interface parameters of the dynamic proxy interface method call to obtain a mapping relationship between the proxy class and the original class.

4. The redundant code detection method according to claim 1, wherein: Constructing a code dependency graph according to the enhanced abstract syntax tree includes: Obtaining the node type corresponding to each node in the enhanced abstract syntax tree and the relationship type between the nodes; Obtaining node attributes corresponding to each node in the enhanced abstract syntax tree; The code dependency graph is constructed according to the node types corresponding to the nodes, the relationship types between the nodes, and the node attributes corresponding to the nodes.

5. The redundant code detection method according to claim 4, wherein: Obtaining node attributes corresponding to each node in the enhanced abstract syntax tree, including: Obtaining the code complexity corresponding to each node in the enhanced abstract syntax tree; Obtaining the call frequency corresponding to each node in the enhanced abstract syntax tree; Accordingly, the code dependency graph is constructed according to the node types corresponding to the nodes, the relationship types between the nodes, and the node attributes corresponding to the nodes, including: The code dependency graph is constructed according to the node type corresponding to each node, the relationship type between the nodes, and the code complexity and calling frequency corresponding to each node.

6. The redundant code detection method according to claim 5, wherein: Obtaining the code complexity corresponding to each node in the enhanced abstract syntax tree includes: Obtain the number of control flow edges, the number of import nodes, and the number of export nodes corresponding to each node in the enhanced abstract syntax tree; The code complexity corresponding to each node is calculated based on the number of control flow edges, import nodes, and export nodes corresponding to each node.

7. The redundant code detection method according to claim 1, wherein: Extracting dynamic behavior features during code execution in the target code library and generating a dynamic behavior sequence based on the dynamic behavior features, including: Extracting the class loading mode and resource access sequence during code execution in the target code library; The dynamic behavior sequence is generated according to the class loading mode and the resource access sequence.

8. The redundant code detection method according to claim 1, wherein: The redundant code is determined according to the redundant probability corresponding to each node, including: Obtaining a preset redundancy probability threshold; Compare the redundancy probability corresponding to each node with the redundancy probability threshold to obtain the repair priority corresponding to each node; The redundant code is determined according to the repair priority corresponding to each node.

9. The redundant code detection method according to claim 8, wherein: Compare the redundancy probability corresponding to each node with the redundancy probability threshold to obtain the repair priority corresponding to each node, including: Determine the repair priority of the node whose redundancy probability is greater than the upper limit value of the redundancy probability threshold as a high priority; Determine the repair priority of the node whose redundancy probability is between the upper limit value and the lower limit value of the redundancy probability threshold as a medium priority; The repair priority of the node whose redundancy probability is lower than the lower limit value of the redundancy probability threshold is determined as a low priority.

10. The redundant code detection method according to claim 9, wherein: Redundant codes are determined based on the repair priorities corresponding to the nodes, including: Determining the code corresponding to the node with a high repair priority as a redundant code; Mark the code corresponding to the node with the medium priority as a code to be verified; The codes corresponding to the nodes with low repair priorities are retained.

11. An electronic device, characterized in that: include: memory for storing computer programs; A processor, configured to implement the steps of the redundant code detection method according to any one of claims 1 to 10 when executing the computer program.

12. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, wherein when the computer program is executed by a processor, the steps of the redundant code detection method according to any one of claims 1 to 10 are implemented.

13. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the redundant code detection method according to any one of claims 1 to 10 are implemented.