Code analysis method, code analysis device, electronic equipment and storage medium

By using artificial intelligence technology to perform detailed analysis of the source code and build a call relationship graph database, the problem of insufficient accuracy in code analysis is solved, and higher analysis accuracy is achieved.

CN115292180BActive Publication Date: 2026-03-24CHINA PING AN LIFE INSURANCE CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-08-09
Publication Date
2026-03-24

AI Technical Summary

Technical Problem

Existing technologies for code analysis are not very accurate, as they mainly rely on statistical analysis of code coverage, which leads to insufficient accuracy.

Method used

By employing artificial intelligence technology, the source code files are obtained, parsed, and processed to obtain target code files and syntax trees. Class method traversal and field parsing are performed to extract target variable information and build a call relationship graph database, thereby improving the accuracy of code analysis.

Benefits of technology

By analyzing and constructing a call relationship graph database in detail, the call relationships between source codes can be accurately reflected, improving the accuracy of analysis during code testing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115292180B_ABST
    Figure CN115292180B_ABST
Patent Text Reader

Abstract

The application provides a code analysis method, a code analysis device, an electronic device and a storage medium, and belongs to the technical field of artificial intelligence. The method comprises the following steps: obtaining a source code file of source code; performing analysis processing on the source code file to obtain a target code file comprising a target syntax tree; performing analysis processing on the target syntax tree to obtain first analysis data; performing class method iteration on the target code file to obtain a current class method; if the current class method is a target class method, performing field analysis processing on the first analysis data to obtain second analysis data; the second analysis data comprises target method information of the target class method; performing information extraction on the target method information to obtain target variable information; and performing call relationship analysis on the target variable information according to the first analysis data and the second analysis data to obtain a target call relationship. The application can improve the accuracy of code analysis.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of artificial intelligence, and particularly relates to a code analysis method, a code analysis device, an electronic device and a storage medium. BACKGROUND

[0002] At present, in the process of code testing, code analysis is often performed by counting the coverage of the code. This method often has the problem of low analysis accuracy. Therefore, how to improve the accuracy of code analysis has become a technical problem to be solved. SUMMARY

[0003] The main purpose of the embodiments of the present application is to provide a code analysis method, a code analysis device, an electronic device and a storage medium, which aims to improve the accuracy of code analysis.

[0004] To achieve the above purpose, a first aspect of the embodiments of the present application provides a code analysis method, which comprises the following steps:

[0005] Obtaining a source code file of source code;

[0006] Performing parsing processing on the source code file to obtain a target code file; wherein the target code file comprises a target syntax tree;

[0007] Performing parsing processing on the target syntax tree to obtain first parsing data;

[0008] Performing class method iteration on the target code file to obtain a current class method;

[0009] If the current class method is a target class method, performing field parsing processing on the first parsing data to obtain second parsing data; wherein the second parsing data comprises target method information of the target class method;

[0010] Performing information extraction on the target method information to obtain target variable information;

[0011] Performing call relationship analysis on the target variable information according to the first parsing data and the second parsing data to obtain target call relationship.

[0012] In some embodiments, the step of performing parsing processing on the source code file to obtain a target code file comprises:

[0013] Performing code analysis on the source code file by a preset code analysis tool to obtain preliminary code data;

[0014] Performing syntax analysis processing on the preliminary code data according to a preset syntax rule to obtain the target syntax tree.

[0015] In some embodiments, the first parsed data includes target class information, a first mapping relationship, and a method category. The step of parsing the target syntax tree to obtain the first parsed data includes:

[0016] Extract the target class name and class path from the target syntax tree, and obtain the mapping relationship between the target class name and the class path to obtain class path mapping information;

[0017] The input fields of the target code file are parsed and processed according to the classpath mapping information to obtain the target class information of the target code file.

[0018] Extract the first variable parameter from the target code file and parse the first variable parameter to obtain the first mapping relationship, wherein the first mapping relationship is used to characterize the mapping relationship between the first variable parameter and the first class name;

[0019] The annotation information of the target class method is extracted from the target syntax tree, and the annotation information is parsed to obtain the method category of the target class method;

[0020] The first parsed data is obtained based on the target class information, the first mapping relationship, and the method category.

[0021] In some embodiments, the second parsed data includes a second mapping relationship and a third mapping relationship. The step of performing field parsing processing on the first parsed data to obtain the second parsed data if the current class method is the target class method includes:

[0022] If the current class method is the target class method, then the first parsed data is parsed to obtain the second variable parameter and the third variable parameter of the target class method;

[0023] The second variable parameter is parsed to obtain a second mapping relationship, which is used to characterize the mapping relationship between the second variable parameter and the second class name;

[0024] The third variable parameter is parsed to obtain a third mapping relationship, which is used to characterize the mapping relationship between the third variable parameter and the third class name;

[0025] The second parsed data is obtained based on the second mapping relationship and the third mapping relationship.

[0026] In some embodiments, the first parsed data includes target class information and a first mapping relationship, and the second parsed data includes a second mapping relationship and a third mapping relationship. The step of parsing the target variable information based on the first parsed data and the second parsed data to obtain the target calling relationship includes:

[0027] The variable type for obtaining the target variable information;

[0028] The target class information, the first mapping relationship, the second mapping relationship, and the third mapping relationship are filtered according to a preset priority order and the target variable information to obtain the target mapping relationship;

[0029] The target invocation relationship is obtained based on the target mapping relationship and the target variable information.

[0030] In some embodiments, the step of obtaining the target invocation relationship based on the target mapping relationship and the target variable information includes:

[0031] Based on the target mapping relationship, at least one candidate class name is obtained;

[0032] The candidate class names are filtered according to preset filtering conditions to obtain the target class name;

[0033] The target invocation relationship is constructed based on the target class name and the target variable information.

[0034] In some embodiments, after the step of parsing the target variable information based on the first parsed data and the second parsed data to obtain the target call relationship, the method further includes:

[0035] Retrieve the preset graph database;

[0036] The target call relationship, the pre-acquired method node information, and the interface data are stored in the target storage location of the graph database to obtain target call data, wherein the target call data is used to represent the call relationship of the source code.

[0037] To achieve the above objectives, a second aspect of this application provides a code analysis apparatus, the apparatus comprising:

[0038] The source code file acquisition module is used to acquire the source code files.

[0039] The first parsing module is used to parse the source code file to obtain the target code file; wherein, the target code file includes a target syntax tree;

[0040] The second parsing module is used to parse the target syntax tree to obtain the first parsed data;

[0041] The traversal module is used to traverse the class methods of the target code file to obtain the current class method;

[0042] The third parsing module is used to perform field parsing processing on the first parsed data to obtain second parsed data if the current class method is the target class method; wherein, the second parsed data includes the target method information of the target class method;

[0043] The information extraction module is used to extract information from the target method information to obtain target variable information;

[0044] The relationship parsing module is used to parse the target variable information based on the first parsing data and the second parsing data to obtain the target call relationship.

[0045] To achieve the above objectives, a third aspect of this application provides an electronic device, which includes a memory, a processor, a program stored in the memory and executable on the processor, and a data bus for enabling communication between the processor and the memory. When the program is executed by the processor, it implements the method described in the first aspect above.

[0046] To achieve the above objectives, a fourth aspect of the present application provides a storage medium, which is a computer-readable storage medium for computer-readable storage, wherein the storage medium stores one or more programs that can be executed by one or more processors to implement the method described in the first aspect.

[0047] The code analysis method, device, electronic device, and storage medium proposed in this application obtain source code files from the source code; parse the source code files to obtain target code files; wherein the target code files include target syntax trees, which can conveniently obtain the code content of the source code files. Further, the target syntax trees are parsed to obtain first parsed data; class methods are traversed in the target code files to obtain the current class methods, which can conveniently obtain the characteristic content of each class object and class method in the target code files. If the current class method is a target class method, the first parsed data is parsed to obtain second parsed data; wherein the second parsed data includes target method information of the target class methods, which can accurately obtain the relevant data of each target class method. Further, information extraction is performed on the target method information to obtain target variable information, and then the call relationship of the target variable information is parsed based on the first and second parsed data to obtain target call relationships. These target call relationships clearly reflect the call relationships between the source code, improving the accuracy of code analysis during code testing. Attached Figure Description

[0048] Figure 1 This is a flowchart of the code analysis method provided in the embodiments of this application;

[0049] Figure 2 yes Figure 1 The flowchart of step S102 in the document;

[0050] Figure 3 yes Figure 1 The flowchart of step S103 in the process;

[0051] Figure 4 yes Figure 1 The flowchart of step S105 in the process;

[0052] Figure 5 yes Figure 1 The flowchart of step S107 in the process;

[0053] Figure 6 yes Figure 5 The flowchart of step S503 in the process;

[0054] Figure 7 This is another flowchart of the code analysis method provided in the embodiments of this application;

[0055] Figure 8 This is a schematic diagram of the structure of the code analysis device provided in the embodiments of this application;

[0056] Figure 9 This is a schematic diagram of the hardware structure of the electronic device provided in the embodiments of this application. Detailed Implementation

[0057] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.

[0058] It should be noted that although functional modules are divided in the device schematic diagram and a logical order is shown in the flowchart, in some cases, the steps shown or described may be performed in a different order than the module division in the device or the order in the flowchart. The terms "first," "second," etc., in the specification, claims, and the aforementioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence.

[0059] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. The terminology used herein is for the purpose of describing embodiments of this application only and is not intended to limit this application.

[0060] First, let's analyze some of the terms used in this application:

[0061] Artificial intelligence (AI) is a new branch of computer science that studies, develops, and applies theories, methods, technologies, and systems to simulate, extend, and expand human intelligence. It aims to understand the essence of intelligence and produce intelligent machines that can react in a way similar to human intelligence. Research in this field includes robotics, speech recognition, image recognition, natural language processing, and expert systems. AI can simulate the information processes of human consciousness and thought. Furthermore, AI utilizes digital computers or machines controlled by digital computers to simulate, extend, and expand human intelligence, perceiving the environment, acquiring knowledge, and using that knowledge to achieve optimal results.

[0062] Natural Language Processing (NLP): NLP uses computers to process, understand, and utilize human language (such as Chinese and English). NLP is a branch of artificial intelligence and an interdisciplinary field of computer science and linguistics, often referred to as computational linguistics. NLP includes syntactic analysis, semantic analysis, and discourse understanding. It is commonly used in machine translation, handwritten and printed character recognition, speech recognition and text-to-speech conversion, intent recognition, information extraction and filtering, text classification and clustering, sentiment analysis, and opinion mining. It involves data mining, machine learning, knowledge acquisition, knowledge engineering, artificial intelligence research, and linguistic research related to language computation.

[0063] Information Extraction (NER) is a text processing technique that extracts factual information such as entities, relationships, and events from natural language text and outputs it as structured data. Information extraction is a technique for extracting specific information from text data. Text data is composed of specific units, such as sentences, paragraphs, and chapters. Text information is composed of smaller, specific units, such as characters, words, phrases, sentences, paragraphs, or combinations of these units. Extracting noun phrases, names of people, and place names from text data is an example of text information extraction. Of course, text information extraction techniques can extract information of various types.

[0064] Syntax Tree (AST): Also known as an abstract syntax tree, it is a tree-like representation of the abstract syntactic structure of source code, specifically referring to the source code of a programming language. Each node in the tree represents a structure in the source code. The syntax is described as abstract because it does not represent every detail of the actual syntax.

[0065] Source code, also known as source program, refers to an uncompiled text file written according to a specific programming language specification. It is a series of human-readable computer language instructions. In modern programming languages, source code can appear in the form of books or tapes, but the most common format is a text file. This typical format is designed to be compiled into a computer program. The ultimate goal of computer source code is to translate human-readable text into binary instructions that a computer can execute. This process is called compilation and is accomplished by a compiler.

[0066] Class: A class is a collection of entities that share certain common characteristics. It is an abstract concept. For example, "car" is a class.

[0067] Class variables: In Java, class variables (also called static variables) are variables within a class that are independent of methods, and are modified with the `static` keyword. (`static` means "global" or "static," and is used to modify member variables and member methods, or static code blocks. Static code blocks are independent of class members; they are executed when the JVM loads the class, with each block executed only once, in sequence.)

[0068] Method: A method can be described as a function that an object has, such as a person being able to work, a car being able to drive, and so on.

[0069] Object: An object is an instantiation of a class, an abstract description of a thing. Take a person as an example. A person is a specific object, an instantiation of the class "human". Each instantiated object encapsulates attributes and methods. For example, the attributes of a certain object may include: name, age, gender, height, etc., and the methods may include: you can walk, you can talk, etc.

[0070] Interface: An interface is a declaration of a series of methods, or a collection of functionalities. This reflects Java's encapsulation principle. For example, a class may be defined with private attributes that are inaccessible from the outside, while the outside can access the class through public methods. We say that a class's public methods are its external interface. It's simply encapsulating these attributes and methods, allowing us to access functionality through the interface.

[0071] Call relationships: These are the connections between code segments. They are often used to model the calling or communication dependencies between code segments (i.e., one piece of code calling or using the service of another piece of code).

[0072] Currently, code analysis in code testing often relies on statistical code coverage. However, this method often suffers from low accuracy. Therefore, improving the accuracy of code analysis has become a pressing technical issue.

[0073] Based on this, embodiments of this application provide a code analysis method, a code analysis device, an electronic device, and a storage medium, aiming to improve the accuracy of code analysis.

[0074] The code analysis method, code analysis device, electronic device, and storage medium provided in this application are specifically described through the following embodiments. First, the code analysis method in this application is described.

[0075] The embodiments of this application can acquire and process relevant data based on artificial intelligence technology. Artificial intelligence (AI) refers to the theories, methods, technologies, and application systems that use digital computers or machines controlled by digital computers to simulate, extend, and expand human intelligence, perceive the environment, acquire knowledge, and use that knowledge to obtain optimal results.

[0076] Foundational technologies for artificial intelligence generally include sensors, dedicated AI chips, cloud computing, distributed storage, big data processing, operating / interactive systems, and mechatronics. AI software technologies mainly encompass computer vision, robotics, biometrics, speech processing, natural language processing, and machine learning / deep learning.

[0077] The code analysis method provided in this application relates to the field of artificial intelligence technology. The code analysis method provided in this application can be applied to a terminal, a server, or software running on either a terminal or a server. In some embodiments, the terminal can be a smartphone, tablet, laptop, desktop computer, etc.; the server can be configured as an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms; the software can be an application implementing the code analysis method, but is not limited to the above forms.

[0078] This application can be used in a wide variety of general-purpose or special-purpose computer system environments or configurations. Examples include: personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronics, network PCs, minicomputers, mainframe computers, and distributed computing environments including any of the above systems or devices. This application can be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc., that perform specific tasks or implement specific abstract data types. This application can also be practiced in distributed computing environments where tasks are performed by remote processing devices connected via a communication network. In distributed computing environments, program modules can reside in local and remote computer storage media, including storage devices.

[0079] Figure 1 This is an optional flowchart of the code analysis method provided in the embodiments of this application. Figure 1The method may include, but is not limited to, steps S101 to S107.

[0080] Step S101: Obtain the source code file of the source code;

[0081] Step S102: Parse the source code file to obtain the target code file; wherein, the target code file includes the target syntax tree;

[0082] Step S103: Parse the target syntax tree to obtain the first parsed data;

[0083] Step S104: Traverse the class methods of the target code file to obtain the current class methods;

[0084] Step S105: If the current class method is the target class method, then the first parsed data is parsed to obtain the second parsed data; wherein, the second parsed data includes the target method information of the target class method;

[0085] Step S106: Extract information from the target method information to obtain target variable information;

[0086] Step S107: Based on the first and second parsing data, the target variable information is parsed to obtain the target call relationship.

[0087] Steps S101 to S107 of this embodiment involve obtaining the source code file; parsing the source code file to obtain the target code file; wherein the target code file includes a target syntax tree, which facilitates obtaining the code content of the source code file. Further, parsing the target syntax tree yields first parsed data; traversing class methods in the target code file yields the current class method, facilitating the acquisition of the characteristic content of each class object and class method in the target code file. If the current class method is the target class method, then field parsing is performed on the first parsed data to obtain second parsed data; wherein the second parsed data includes target method information of the target class method, which allows for more accurate acquisition of relevant data for each target class method. Further, information extraction is performed on the target method information to obtain target variable information, and then the target variable information is parsed based on the first and second parsed data to obtain the target call relationship. The target call relationship clearly reflects the call relationships existing between the source code, improving the accuracy of code analysis during code testing.

[0088] In step S101 of some embodiments, the source code file can be obtained by writing a web crawler, setting up a data source, and then selectively crawling data. Alternatively, the source code file can be obtained through other methods, such as extracting data from a pre-defined text library; this is not limited to these methods. The source code file includes a text file designed according to actual business requirements. This text file is uncompiled and conforms to the writing standards of a programming language.

[0089] Please see Figure 2 In some embodiments, step S102 may include, but is not limited to, steps S201 to S202:

[0090] Step S201: The source code file is parsed using a preset code parsing tool to obtain preliminary code data;

[0091] Step S202: Perform syntax parsing on the preliminary code data according to the preset syntax rules to obtain the target syntax tree.

[0092] In step S201 of some embodiments, the preset code parsing tool can be the static code parsing tool javaParser. javaParser parses the source code file to obtain the compilation unit of the source code file, and then javaParser instantiates the compilation unit to obtain all node information of each class in the compilation unit, thus obtaining preliminary code data.

[0093] In step S202 of some embodiments, the preset syntax rules can be common Java syntax. The initial code data is analyzed using Java syntax to obtain a target code file. The target code file includes an abstract target syntax tree, which includes field information corresponding to the source code. Examples include import fields, class variable definition fields, etc. A variable is the most basic storage unit in a program, consisting of a variable type, variable name, and stored value. A variable is a storage area in memory where data of the same data type can continuously change. A class variable is a static variable that can be shared by all objects of the same class.

[0094] Please see Figure 3 In some embodiments, the first parsed data includes target class information, a first mapping relationship, and a method category. Step S103 may include, but is not limited to, steps S301 to S305:

[0095] Step S301: Extract the target class name and class path from the target syntax tree, and obtain the mapping relationship between the target class name and the class path to get the class path mapping information;

[0096] Step S302: Parse the input fields of the target code file according to the classpath mapping information to obtain the target class information of the target code file;

[0097] Step S303: Extract the first variable parameter from the target code file and parse the first variable parameter to obtain the first mapping relationship, wherein the first mapping relationship is used to characterize the mapping relationship between the first variable parameter and the first class name;

[0098] Step S304: Extract the annotation information of the target class method from the target syntax tree, and parse the annotation information to obtain the method category of the target class method;

[0099] Step S305: Obtain the first parsed data based on the target class information, the first mapping relationship, and the method category.

[0100] In step S301 of some embodiments, the target class name and class path are extracted from the target syntax tree by a preset script program, and the mapping relationship between the target class name and class path is obtained to obtain class path mapping information. The encapsulation data of the target class method can be easily obtained by parsing the target code file through the class path mapping information.

[0101] In step S302 of some embodiments, since the input field of each class in the target code file contains information about other Java classes referenced, the input field (i.e., the import field) can be parsed according to the obtained classpath mapping information to parse the input field into data in map form, thereby obtaining all Java class information of the target code file, wherein the Java class information includes the target class information.

[0102] In step S303 of some embodiments, a first variable parameter is extracted from the target code file by a preset script program, etc. The first variable parameter is a class variable of the target class. The class variable is a static variable. The class variable mainly refers to a class-level variable. For example, in a certain target code file, demoservice is a class variable. By extracting the corresponding fields in the class variable, the correspondence between the class variable and the class name is obtained. This correspondence is used as the first mapping relationship. The first mapping relationship is used to represent the mapping relationship between the first variable parameter (i.e., the class variable) and the first class name. The specific data structure of the first mapping relationship is in the form of a map.

[0103] In step S304 of some embodiments, annotation information of the target class method is extracted from the target syntax tree using a preset script program. This annotation information includes a first annotation of the target class and a second annotation of the target class method. The first annotation is parsed to extract the interface data of the target class, thereby determining the interface type of the target class based on the interface data. If the target class is an interface class, the interface path of the target class is extracted, where the interface path is a URL path. Further, the second annotation is parsed to extract the interface mapping data of the target class method. If the target class method has interface mapping data, an interface node is created based on the interface mapping data. This interface node includes the interface service name and interface URL information, etc., and can be represented by an API. For example, if the annotation information of a target class method in a target code file includes the first annotation "@RestController" and the second annotation "@GetMapping", then based on the first annotation "@RestController", the target class containing the target class method is determined to be an interface class, and the extracted URL path is the value of the annotation marked "". Furthermore, based on the content of the second annotation "@GetMapping", it is determined that the target class method has an interface mapping.

[0104] In step S305 of some embodiments, the target class information, the first mapping relationship, and the method category are integrated to obtain the first parsed data.

[0105] Through the above steps S301 to S305, the Java classes of the target code file can be parsed and processed relatively easily, including parsing the input fields, class variables and annotation information of each target class, thereby obtaining the correspondence between the class variables and class names of the target class, the correspondence between the target class information and class names, and the encapsulated data of the target class methods in the target class, etc., which improves the comprehensiveness of code analysis.

[0106] In step S104 of some embodiments, the target code file is traversed by class method, and the class methods of each Java class in the target code file are parsed to obtain the current class method. The class method is a static method, and in actual application scenarios, the class method can be called by class name or class method name.

[0107] Please see Figure 4 In some embodiments, the second parsed data includes a second mapping relationship and a third mapping relationship, and step S105 may include, but is not limited to, steps S401 to S404:

[0108] Step S401: If the current class method is the target class method, then the first parsed data is parsed to obtain the second variable parameter and the third variable parameter of the target class method;

[0109] Step S402: The second variable parameter is parsed to obtain the second mapping relationship, which is used to characterize the mapping relationship between the second variable parameter and the second class name;

[0110] Step S403: The third variable parameter is parsed to obtain the third mapping relationship. The third mapping relationship is used to characterize the mapping relationship between the third variable parameter and the third class name.

[0111] Step S404: Obtain the second parsed data based on the second mapping relationship and the third mapping relationship.

[0112] In step S401 of some embodiments, if the current class method is the target class method, then the specific code information of the target class method needs to be parsed. That is, the first parsed data is parsed by a script program to obtain the second parsed data. The second parsed data includes the target method information of the target class method. The target method information includes the second variable parameter, the third variable parameter, and the method node information of the target class method. The second variable parameter can be the input parameter variable of the target class method, and the third variable parameter can be the method local variable defined internally by the target class method. The method node information includes the method name, method attributes, etc. The method name can be a string containing letters, numbers, etc., or other forms without restriction; the method attributes can include the category to which the target class method belongs, etc.

[0113] In step S402 of some embodiments, the second variable parameter is parsed and processed by a preset script program, etc. The second variable parameter can be the input parameter variable of the target class method. By extracting the corresponding fields in the input parameter variable, the correspondence between the input parameter variable and the class name is obtained. This correspondence is used as the second mapping relationship. The second mapping relationship is used to represent the mapping relationship between the second variable parameter (i.e., the input parameter variable) and the second class name. The specific data structure of the second mapping relationship is in map form.

[0114] In step S403 of some embodiments, the third variable parameter is parsed and processed by a preset script program, etc. The third variable parameter is a method local variable defined internally by the target class method. By extracting the corresponding fields in the method local variable, the correspondence between the method local variable and the class name is obtained. This correspondence is used as the third mapping relationship. The third mapping relationship is used to represent the mapping relationship between the third variable parameter (i.e., the method local variable) and the third class name. The specific data structure of the third mapping relationship is in map form.

[0115] In step S404 of some embodiments, the second mapping relationship and the third mapping relationship are integrated to obtain the second parsed data.

[0116] Through the above steps S401 to S404, the relevant data of each target class method can be obtained more accurately, thereby improving the accuracy of code analysis by analyzing the code based on the target class methods and the data content of the target class.

[0117] In step S106 of some embodiments, target method information of the target class method is extracted by a preset script level to obtain target variable information and method node information. The target variable information includes the variable type and variable value of the target class method, and the method node information includes the method name of the target class method.

[0118] Please see Figure 5 In some embodiments, the first parsed data includes target class information and a first mapping relationship, the second parsed data includes a second mapping relationship and a third mapping relationship, and step S107 may include, but is not limited to, steps S501 to S503:

[0119] Step S501: Obtain the variable type of the target variable information;

[0120] Step S502: Filter the target class information, first mapping relationship, second mapping relationship, and third mapping relationship according to the preset priority order and target variable information to obtain the target mapping relationship;

[0121] Step S503: Obtain the target invocation relationship based on the target mapping relationship and target variable information.

[0122] In step S501 of some embodiments, the variable type of the target variable information is obtained by a written script program. The variable type includes member variables and local variables. Member variables include class variables, target class information, etc., that is, member variables generally refer to class-level variables. Local variables include formal parameter variables (i.e., method input parameter variables, etc.), method local variables (i.e., variables defined inside the method), and code block local variables (i.e., variables defined within the code block).

[0123] In step S502 of some embodiments, the preset priority order can be set according to actual business needs. For example, in a specific application scenario, the priority order is: first, determine the target class name based on the method local variables; then, determine the target class name based on the formal parameter variables; then, determine the target class name based on the class variables; and finally, determine the target class name based on the target class information. That is, when searching for the target class name corresponding to the target variable information, the target class information, the first mapping relationship, the second mapping relationship, and the third mapping relationship are filtered according to the priority order and the variable type of the target variable information. Data information corresponding to the variable type of the target variable information is selected to obtain the target mapping relationship. For example, if the variable type of the target variable information includes the second variable parameter (i.e., the input parameter variable) and the third variable parameter (i.e., the method local variable), then the second mapping relationship and the third mapping relationship are used as the target mapping relationship.

[0124] In step S503 of some embodiments, when obtaining the target call relationship based on the target mapping relationship and target variable information, firstly, candidate class names are determined based on the target mapping relationship, and then the candidate class names are filtered according to preset filtering conditions to obtain the target class name. Finally, the call method node information is obtained based on the target class name and target variable information. The call method node information includes the call method name and the target class name. Finally, based on the call method node information obtained from the target class name and target variable information, the target call relationship between the target class method and the call method is constructed. That is, the node association between the call method node and the target class method is constructed based on the method name of the target class method and the method name of the call method to obtain the target call relationship.

[0125] Please see Figure 6 In some embodiments, step S503 includes, but is not limited to, steps S601 to S603:

[0126] Step S601: Obtain at least one candidate class name based on the target mapping relationship;

[0127] Step S602: Filter the candidate class names according to the preset filtering conditions to obtain the target class name;

[0128] Step S603: Construct the target invocation relationship based on the target class name and target variable information.

[0129] In step S601 of some embodiments, class name information contained in the target mapping relationship is extracted according to the target mapping relationship to obtain at least one candidate class name. For example, when the target mapping relationship includes a second mapping relationship and a third mapping relationship, the candidate class name will include a second class name and a third class name.

[0130] In step S602 of some embodiments, the preset filtering conditions can be set according to actual conditions. For example, the preset filtering conditions mainly filter Java built-in classes and imported third-party library classes, retaining Java classes defined under the current business requirements. Specifically, the preset filtering conditions can be based on the Java class path, that is, adding a fixed string, such as om.abc.lcloud, to the prefix of the path of the Java class in the current business requirements. Then, Java classes whose candidate class names contain this prefix are retained, and Java classes that do not contain this prefix are filtered out, thereby obtaining the target class and target class name that meet the requirements.

[0131] In step S603 of some embodiments, the calling method node information is obtained according to the target class name and target variable information. The calling method node information includes the calling method name and the target class name. Finally, based on the calling method node information obtained from the target class name and target variable information, the target calling relationship between the target class method and the calling method is constructed. That is, the node association between the calling method node and the target class method is constructed based on the method name of the target class method and the method name of the calling method, and the target calling relationship is obtained.

[0132] Please see Figure 7 After step S107 in some embodiments, the code analysis method may also include, but is not limited to, steps S701 to S702:

[0133] Step S701: Obtain the preset graph database;

[0134] Step S702: Store the target call relationship, the pre-acquired method node information, and the interface data in the target storage location of the graph database to obtain the target call data, wherein the target call data is used to represent the call relationship of the source code.

[0135] In step S701 of some embodiments, the preset graph database may be a neo4j graph database, etc., which can be pre-built by a script or other means.

[0136] In step S702 of some embodiments, the target call relationship, method node information, and interface data are input into the graph database. The graph database is queried using a preset script MERGE. If interface data matching the requirements exists in the graph database, no new interface is created. The target call relationship and method node information are imported into the target storage location of the graph database through the interface in the graph database. This target storage location can be determined based on the file extension of the source code file, etc. If interface data matching the requirements does not exist in the graph database, a new interface is created based on the preset interface data. The target call relationship and method node information are imported into the target storage location of the graph database through the new interface. The target call data can be used to represent the call relationship between source code. The target call data includes the call relationship between the target class method and the class method of the same class in the source code, the method node information of the target class method, the calling method node information, etc. These call relationships between code can provide effective support for code analysis during code testing and improve the accuracy of code analysis.

[0137] The code analysis method of this application embodiment obtains the source code file of the source code; parses the source code file to obtain the target code file; wherein the target code file includes a target syntax tree, which can conveniently obtain the code content of the source code file. Further, the target syntax tree is parsed to obtain first parsed data; class methods are traversed in the target code file to obtain the current class method, which can conveniently obtain the characteristic content of each class object and class method in the target code file. If the current class method is the target class method, the first parsed data is parsed to obtain second parsed data; wherein the second parsed data includes the target method information of the target class method, which can accurately obtain the relevant data of each target class method. Further, the target method information is extracted to obtain target variable information, and then the target variable information is parsed according to the first and second parsed data to obtain the target call relationship. The target call relationship can clearly reflect the call relationship between the source code, which can improve the accuracy of code analysis during code testing.

[0138] Please see Figure 8 This application also provides a code analysis apparatus that can implement the above-described code analysis method. The apparatus includes:

[0139] The source code file acquisition module 801 is used to acquire the source code files of the source code.

[0140] The first parsing module 802 is used to parse the source code file to obtain the target code file; wherein, the target code file includes the target syntax tree;

[0141] The second parsing module 803 is used to parse the target syntax tree to obtain the first parsed data;

[0142] The traversal module 804 is used to traverse class methods in the target code file and obtain the current class methods.

[0143] The third parsing module 805 is used to perform field parsing processing on the first parsing data to obtain the second parsing data if the current class method is the target class method; wherein, the second parsing data includes the target method information of the target class method;

[0144] The information extraction module 806 is used to extract information from the target method information to obtain the target variable information;

[0145] The relation parsing module 807 is used to parse the target variable information based on the first parsing data and the second parsing data to obtain the target call relationship.

[0146] In some embodiments, the first parsing module 802 includes:

[0147] The code parsing unit is used to parse the source code file using a preset code parsing tool to obtain preliminary code data;

[0148] The syntax parsing unit is used to perform syntax parsing on the initial code data according to preset syntax rules to obtain the target syntax tree.

[0149] In some embodiments, the first parsed data includes target class information, a first mapping relationship, and a method category, and the second parsing module 803 includes:

[0150] The relation extraction unit is used to extract the target class name and class path from the target syntax tree, and obtain the mapping relationship between the target class name and class path to get the class path mapping information;

[0151] The first field parsing unit is used to parse the input fields of the target code file according to the classpath mapping information to obtain the target class information of the target code file;

[0152] The first parameter parsing unit is used to extract the first variable parameter from the target code file and parse the first variable parameter to obtain the first mapping relationship, wherein the first mapping relationship is used to characterize the mapping relationship between the first variable parameter and the first class name;

[0153] The information parsing unit is used to extract annotation information of target class methods from the target syntax tree, and to parse and process the annotation information to obtain the method category of the target class method;

[0154] The first integration unit is used to obtain the first parsed data based on the target class information, the first mapping relationship, and the method category.

[0155] In some embodiments, the second parsed data includes a second mapping relationship and a third mapping relationship, and the third parsing module 805 includes:

[0156] The second field parsing unit is used to perform field parsing processing on the first parsed data if the current class method is the target class method, to obtain the second variable parameter and the third variable parameter of the target class method;

[0157] The second parameter parsing unit is used to parse the second variable parameter to obtain the second mapping relationship. The second mapping relationship is used to characterize the mapping relationship between the second variable parameter and the second class name.

[0158] The third parameter parsing unit is used to parse the third variable parameter to obtain the third mapping relationship. The third mapping relationship is used to characterize the mapping relationship between the third variable parameter and the third class name.

[0159] The second integration unit is used to obtain the second parsed data based on the second mapping relationship and the third mapping relationship.

[0160] In some embodiments, the first parsed data includes target class information and a first mapping relationship, the second parsed data includes a second mapping relationship and a third mapping relationship, and the relationship parsing module 807 includes:

[0161] The type acquisition unit is used to obtain the variable type information of the target variable.

[0162] The filtering unit is used to filter the target class information, the first mapping relationship, the second mapping relationship, and the third mapping relationship according to the preset priority order and target variable information to obtain the target mapping relationship;

[0163] The relationship building unit is used to obtain the target invocation relationship based on the target mapping relationship and target variable information.

[0164] In some embodiments, the relationship building unit includes:

[0165] The class name retrieval sub-unit is used to obtain at least one candidate class name based on the target mapping relationship;

[0166] The filtering subunit is used to filter candidate class names according to preset filtering conditions to obtain the target class name;

[0167] The relationship building subunit is used to build target invocation relationships based on the target class name and target variable information.

[0168] In some embodiments, the code analysis apparatus further includes a data integration module, specifically comprising:

[0169] The database acquisition unit is used to acquire a preset graph database;

[0170] The storage unit is used to store the target call relationship, the pre-acquired method node information and interface data into the target storage location of the graph database to obtain the target call data, which is used to represent the call relationship of the source code.

[0171] The specific implementation of this code analysis device is basically the same as the specific implementation of the code analysis method described above, and will not be repeated here.

[0172] This application also provides an electronic device, which includes: a memory, a processor, a program stored in the memory and executable on the processor, and a data bus for communication between the processor and the memory. When the program is executed by the processor, it implements the code analysis method described above. This electronic device can be any smart terminal, including tablet computers, in-vehicle computers, etc.

[0173] Please see Figure 9 , Figure 9 The hardware structure of an electronic device according to another embodiment is illustrated. The electronic device includes:

[0174] The processor 901 can be implemented using a general-purpose CPU (Central Processing Unit), microprocessor, application-specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of this application.

[0175] The memory 902 can be implemented as a read-only memory (ROM), a static storage device, a dynamic storage device, or a random access memory (RAM). The memory 902 can store the operating system and other applications. When the technical solutions provided in the embodiments of this specification are implemented through software or firmware, the relevant program code is stored in the memory 902 and is called and executed by the processor 901 using the code analysis method of the embodiments of this application.

[0176] The input / output interface 903 is used to implement information input and output;

[0177] The communication interface 904 is used to enable communication and interaction between this device and other devices. Communication can be achieved through wired means (such as USB, Ethernet cable, etc.) or wireless means (such as mobile network, WIFI, Bluetooth, etc.).

[0178] Bus 905 transmits information between various components of the device (e.g., processor 901, memory 902, input / output interface 903, and communication interface 904);

[0179] The processor 901, memory 902, input / output interface 903, and communication interface 904 are connected to each other within the device via bus 905.

[0180] This application also provides a storage medium, which is a computer-readable storage medium for computer-readable storage. The storage medium stores one or more programs, which can be executed by one or more processors to implement the above-described code analysis method.

[0181] Memory, as a non-transitory computer-readable storage medium, can be used to store non-transitory software programs and non-transitory computer-executable programs. Furthermore, memory may include high-speed random access memory, and may also include non-transitory memory, such as at least one disk storage device, flash memory device, or other non-transitory solid-state storage device. In some embodiments, memory may optionally include memory remotely located relative to the processor, and these remote memories can be connected to the processor via a network. Examples of such networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof.

[0182] The code analysis method, device, electronic device, and storage medium provided in this application obtain source code files from the source code; parse the source code files to obtain target code files; wherein the target code files include target syntax trees, which can conveniently obtain the code content of the source code files. Further, the target syntax trees are parsed to obtain first parsed data; wherein the first parsed data includes the encapsulation data of target class methods; and class method traversal is performed on the target code files to obtain the current class methods, which can conveniently obtain the characteristic content of each class object and class method in the target code files. If the current class method is the target class method, the first parsed data is parsed to obtain second parsed data; wherein the second parsed data includes target method information of the target class methods, which can accurately obtain the relevant data of each target class method. Further, information extraction is performed on the target method information to obtain target variable information and method node information, and then the target variable information is parsed according to the first and second parsed data to obtain target call relationships. The target call relationships can clearly reflect the call relationships between the source code, which can improve the accuracy of code analysis during code testing.

[0183] The embodiments described in this application are for the purpose of more clearly illustrating the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions provided by the embodiments of this application. As those skilled in the art will know, with the evolution of technology and the emergence of new application scenarios, the technical solutions provided by the embodiments of this application are also applicable to similar technical problems.

[0184] It will be understood by those skilled in the art that Figures 1-7 The technical solutions shown do not constitute a limitation on the embodiments of this application, and may include more or fewer steps than shown, or combine certain steps, or different steps.

[0185] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.

[0186] Those skilled in the art will understand that all or some of the steps in the methods disclosed above, as well as the functional modules / units in the systems and devices, can be implemented as software, firmware, hardware, or suitable combinations thereof.

[0187] The terms “first,” “second,” “third,” “fourth,” etc. (if present) in the specification and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms “comprising” and “having,” and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.

[0188] It should be understood that in this application, "at least one (item)" means one or more, and "more than" means two or more. "And / or" is used to describe the relationship between related objects, indicating that three relationships can exist. For example, "A and / or B" can represent three cases: only A exists, only B exists, and both A and B exist simultaneously, where A and B can be singular or plural. The character " / " generally indicates that the preceding and following related objects are in an "or" relationship. "At least one (item) of the following" or similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one (item) of a, b, or c can represent: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, and c can be single or multiple.

[0189] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of the units described above is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.

[0190] The units described above as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0191] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.

[0192] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes multiple instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing programs, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0193] The preferred embodiments of the present application have been described above with reference to the accompanying drawings, but this does not limit the scope of the claims of the present application. Any modifications, equivalent substitutions, and improvements made by those skilled in the art without departing from the scope and substance of the embodiments of the present application shall be within the scope of the claims of the present application.

Claims

1. A code analysis method, characterized in that, The method includes: Obtain the source code file; The source code file is parsed to obtain the target code file; wherein, the target code file includes a target syntax tree; The target syntax tree is parsed to obtain first parsed data, which includes target class information, first mapping relationship, and method category; The target code file is traversed to obtain the current class method; If the current class method is the target class method, then the first parsed data is parsed to obtain the second parsed data; wherein, the second parsed data includes a second mapping relationship, a third mapping relationship, and the target method information of the target class method; Information is extracted from the target method information to obtain target variable information; Based on the first and second parsed data, the target variable information is parsed to obtain the target call relationship; The parsing process of the target syntax tree to obtain the first parsed data includes: The target class name and classpath are extracted from the target syntax tree, and the mapping relationship between the target class name and the classpath is obtained to obtain classpath mapping information. The input fields of the target code file are parsed according to the classpath mapping information to obtain the target class information of the target code file. The first variable parameter of the target code file is extracted and parsed to obtain a first mapping relationship, wherein the first mapping relationship is used to characterize the mapping relationship between the first variable parameter and the first class name. The annotation information of the target class method is extracted from the target syntax tree and parsed to obtain the method category of the target class method. The first parsed data is obtained based on the target class information, the first mapping relationship, and the method category. If the current class method is the target class method, then the first parsed data is parsed to obtain the second parsed data, including: If the current class method is the target class method, then the first parsed data is parsed to obtain the second variable parameter and the third variable parameter of the target class method; the second variable parameter is parsed to obtain the second mapping relationship, which is used to represent the mapping relationship between the second variable parameter and the second class name; the third variable parameter is parsed to obtain the third mapping relationship, which is used to represent the mapping relationship between the third variable parameter and the third class name; the second parsed data is obtained based on the second mapping relationship and the third mapping relationship.

2. The code analysis method according to claim 1, characterized in that, The step of parsing the source code file to obtain the target code file includes: The source code file is parsed using a pre-set code parsing tool to obtain preliminary code data; The initial code data is parsed according to preset syntax rules to obtain the target syntax tree.

3. The code analysis method according to claim 1, characterized in that, The first parsed data includes target class information and a first mapping relationship; the second parsed data includes a second mapping relationship and a third mapping relationship; the step of parsing the target variable information based on the first parsed data and the second parsed data to obtain the target calling relationship includes: The variable type for obtaining the target variable information; The target class information, the first mapping relationship, the second mapping relationship, and the third mapping relationship are filtered according to a preset priority order and the target variable information to obtain the target mapping relationship; The target invocation relationship is obtained based on the target mapping relationship and the target variable information.

4. The code analysis method according to claim 3, characterized in that, The step of obtaining the target invocation relationship based on the target mapping relationship and the target variable information includes: Based on the target mapping relationship, at least one candidate class name is obtained; The candidate class names are filtered according to preset filtering conditions to obtain the target class name; The target invocation relationship is constructed based on the target class name and the target variable information.

5. The code analysis method according to any one of claims 1 to 4, characterized in that, After the step of parsing the target variable information based on the first parsed data and the second parsed data to obtain the target call relationship, the method further includes: Retrieve the preset graph database; The target call relationship, the pre-acquired method node information, and the interface data are stored in the target storage location of the graph database to obtain target call data, wherein the target call data is used to represent the call relationship of the source code.

6. A code analysis device, characterized in that, The device includes: The source code file acquisition module is used to acquire the source code files. The first parsing module is used to parse the source code file to obtain the target code file; wherein, the target code file includes a target syntax tree; The second parsing module is used to parse the target syntax tree to obtain first parsed data, which includes target class information, first mapping relationship, and method category. The traversal module is used to traverse the class methods of the target code file to obtain the current class method; The third parsing module is used to perform field parsing processing on the first parsed data to obtain second parsed data if the current class method is the target class method; wherein, the second parsed data includes a second mapping relationship, a third mapping relationship, and target method information of the target class method; The information extraction module is used to extract information from the target method information to obtain target variable information; The relationship parsing module is used to parse the target variable information based on the first parsing data and the second parsing data to obtain the target call relationship; The parsing process of the target syntax tree to obtain the first parsed data includes: The target class name and classpath are extracted from the target syntax tree, and the mapping relationship between the target class name and the classpath is obtained to obtain classpath mapping information. The input fields of the target code file are parsed according to the classpath mapping information to obtain the target class information of the target code file. The first variable parameter of the target code file is extracted and parsed to obtain a first mapping relationship, wherein the first mapping relationship is used to characterize the mapping relationship between the first variable parameter and the first class name. The annotation information of the target class method is extracted from the target syntax tree and parsed to obtain the method category of the target class method. The first parsed data is obtained based on the target class information, the first mapping relationship, and the method category. If the current class method is the target class method, then the first parsed data is parsed to obtain the second parsed data, including: If the current class method is the target class method, then the first parsed data is parsed to obtain the second variable parameter and the third variable parameter of the target class method; the second variable parameter is parsed to obtain the second mapping relationship, which is used to represent the mapping relationship between the second variable parameter and the second class name; the third variable parameter is parsed to obtain the third mapping relationship, which is used to represent the mapping relationship between the third variable parameter and the third class name; the second parsed data is obtained based on the second mapping relationship and the third mapping relationship.

7. An electronic device, characterized in that, The electronic device includes a memory, a processor, a program stored in the memory and executable on the processor, and a data bus for establishing communication between the processor and the memory. When the program is executed by the processor, it implements the steps of the code analysis method as described in any one of claims 1 to 5.

8. A storage medium, said storage medium being a computer-readable storage medium for computer-readable storage, characterized in that, The storage medium stores one or more programs, which can be executed by one or more processors to implement the steps of the code analysis method according to any one of claims 1 to 5.

Citation Information

Patent Citations

  • Code testing method and device, storage medium and electronic device

    CN109446078A

  • Code visualization analysis method and device based on abstract syntax

    CN112306479A