Method, apparatus, and computer device for generating code service call relationships

By traversing the syntax parse tree in a distributed system to generate a code service call relationship table, the problem of low maintainability of the code service call relationship generation method in the existing technology is solved, and the service call relationship is determined before the code is run, which improves the maintenance of the code.

CN115729559BActive Publication Date: 2025-08-05INDUSTRIAL AND COMMERCIAL BANK OF CHINA
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211464974.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-11-22
Publication Date
2025-08-05
Estimated Expiration
2042-11-22

AI Technical Summary

Technical Problem

The existing code service call relationship generation method has a low maintenance problem in distributed systems. The reason is that the code needs to be run before it can be generated after obtaining the call relationship between services through the service registration center, which makes it difficult to maintain.

Method used

By traversing the syntax parsing tree of the source code to be detected, the relationship between the method declaration code module and the method call code module is obtained, and a code service call relationship table is generated based on these relationships, including determining the identity of the service provider and the service consumer.

Benefits of technology

Before the code is run, the service call relationship is generated by traversing the syntax tree, which improves the maintainability of the code and reduces the maintenance difficulty.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115729559B_ABST
    Figure CN115729559B_ABST
Patent Text Reader

Abstract

The present application relates to a method, apparatus, computer equipment, storage medium and computer program product for generating a code service call relationship. By traversing the syntax parse tree corresponding to the source code to be detected, the first relationship between the method declaration code module and at least one method call code module in its child node in the syntax parse tree, the second relationship between the method call module and its class and at least one method declaration code module in the corresponding parent class and child class, and the service party to which each code module belongs are obtained, and based on the first relationship and the second relationship, the call relationship table between the code modules in the source code to be detected is determined, and the corresponding service party identifier is added to the code module in the call relationship table to obtain the corresponding code service call relationship table. Compared with the traditional method of obtaining the call relationship between services through the service registration center, this solution obtains the service call relationship in the code by traversing the syntax tree before the code is run, thereby improving the maintainability of the code.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of cloud computing technology, and in particular to a method, apparatus, computer device, storage medium, and computer program product for generating a code service call relationship. Background Art

[0002] With the development of distributed technology, current computer program systems have gradually evolved from traditional monolithic architectures to distributed architectures composed of numerous microservices. Distributed architectures offer advantages such as on-demand scalability, flexible deployment, and high availability. However, the complex inter-service call relationships in distributed systems also increase the difficulty of architectural design, development, and debugging. Therefore, it is necessary to organize the inter-service call relationships in program code. Currently, determining inter-service call relationships is typically done by obtaining the call relationships between services registered and subscribed to the service registry. However, since services in the service registry cannot be generated until the code is run, code maintenance becomes difficult when call relationship issues are discovered.

[0003] Therefore, the current method for generating code service call relationships has the defect of low maintainability. Summary of the Invention

[0004] Based on this, it is necessary to provide a method, device, computer equipment, computer-readable storage medium and computer program product for generating a code service call relationship that can improve the maintainability of code services in response to the above technical problems.

[0005] In a first aspect, the present application provides a method for generating a code service call relationship, the method comprising:

[0006] Obtaining a source code to be detected, performing syntax parsing on the source code to be detected, and obtaining a corresponding syntax parse tree; the syntax parse tree includes a plurality of nodes corresponding to a plurality of code modules of the source code to be detected, and the relationship between the nodes is determined based on the corresponding relationship between the plurality of code modules;

[0007] Traversing the grammar parse tree, obtaining a first relationship between a method declaration code module and at least one method call code module in a corresponding child node of the grammar parse tree, a second relationship between a method call code module and its class and at least one method declaration code module in its corresponding parent class and child class, and a service provider to which each code module belongs; the service provider includes a service provider and a service consumer; and the code module includes a method declaration code module and a method call code module;

[0008] Based on the first relationship and the second relationship, a call relationship table between code modules in the source code to be detected is determined, and according to the service provider to which the code module belongs, the corresponding service provider identifier is added to the code module in the call relationship table to obtain a code service call relationship table corresponding to the source code to be detected.

[0009] In one embodiment, the step of obtaining the source code to be detected, parsing the source code to be detected, and obtaining a corresponding parse tree includes:

[0010] Detecting the declaration code module in the source code to be detected; the declaration code module includes a property declaration code module and a class declaration code module;

[0011] Detecting the attribute information code module in the attribute declaration code module, and detecting the class information code module, the method declaration code module, and the method call code module corresponding to the method declaration code module in the class declaration code module;

[0012] The project where the source code to be detected is located is taken as the root node, the attribute declaration code module, the attribute information code module and the class declaration code module are taken as parent nodes, the class information code module and the method declaration code module are taken as child nodes of the class declaration code module, and the method call code module is taken as the child node of the method declaration code module to obtain the syntax parse tree corresponding to the code to be detected.

[0013] In one embodiment, traversing the parse tree to obtain a first relationship between a method declaration code module in the parse tree and at least one method call code module in its corresponding child node, a second relationship between the method call code module and its class and at least one method declaration code module in its corresponding parent class and child class, and a service provider to which each code module belongs, includes:

[0014] Traversing the syntax parse tree, and determining a first relationship between the method declaration code module and at least one method call code module in its corresponding child node according to the method declaration code module and the method call code module in its corresponding child node in the syntax parse tree;

[0015] Determining, based on the method declaration code modules in the child nodes corresponding to each class declaration code module in the grammar parse tree, and the method declaration code modules in the parent class declaration code module and the child class declaration code module corresponding to each class declaration code module, a second relationship between the method calling code module and its class and at least one method declaration code module in the corresponding parent class and child class;

[0016] According to the class declaration code module and the property declaration code module in the syntax parse tree, the service party to which the method declaration code module and / or the method call code module in the class declaration code module belongs is determined.

[0017] In one embodiment, determining a first relationship between a method declaration code module and at least one method call code module in its corresponding child node based on the method declaration code module and the method call code module in its corresponding child node in the grammar parse tree includes:

[0018] Obtaining a method declaration code module in the grammar parse tree, detecting a subtree contained in the method declaration code module, and obtaining a method call code module in the subtree;

[0019] A first relationship between the method declaration code module and the method call code module in the subtree is established.

[0020] In one embodiment, determining the second relationship between the method calling code module and the class to which it belongs and at least one method declaration code module in the corresponding parent class and child class based on the method declaration code module in the child node corresponding to each class declaration code module in the grammar parse tree, and the method declaration code modules in the parent class declaration code module and the child class declaration code module corresponding to each class declaration code module, includes:

[0021] Obtaining a method declaration code module in a subtree of each class declaration code module in the grammar parse tree, and if it is detected that method implementation code exists in the method declaration code module in the subtree, establishing a second relationship between a method call code module corresponding to the method declaration code module in the subtree and the current class of the method declaration code module in the subtree;

[0022] Determine the parent-child relationship of each class declaration code module, and obtain the parent class declaration code module and the child class declaration code module corresponding to each class declaration code module in the syntax parse tree;

[0023] For each class declaration code module, if it is detected that a parent method declaration code module containing method implementation code exists in the parent class declaration code module corresponding to the class declaration code module, a parent class second relationship is established between the first target method calling code module corresponding to the class declaration code module and the parent method declaration code module; the method name of the first target method calling code module is the same as the method name of the parent method declaration code module;

[0024] If it is detected that a sub-method declaration code module containing method implementation code exists in the sub-class declaration code module corresponding to the class declaration code module, a sub-class second relationship is established between the second target method calling code module corresponding to the class declaration code module and the sub-method declaration code module; the method name of the second target method calling code module is the same as the method name of the sub-method declaration code module;

[0025] According to the second relationship of the current class, the second relationship of the parent class and the second relationship of the child class, the second relationship between the method calling code module and its class and at least one method declaration code module in the corresponding parent class and child class is obtained.

[0026] In one embodiment, determining the service provider to which the method declaration code module and / or method call code module in the class declaration code module belongs based on the class declaration code module and the property declaration code module in the grammar parse tree includes:

[0027] Obtaining a class information code module in a child node of a class declaration code module in the grammar parse tree;

[0028] If the class information corresponding to the class information code module is a service provider, determining that the method declaration code module corresponding to the class declaration code module belongs to the service provider;

[0029] and / or,

[0030] Obtaining an attribute information code module corresponding to an attribute declaration code module in the grammar parse tree;

[0031] If the attribute information corresponding to the attribute information code module includes a service consumer, and the calling subject of the method calling code module in the syntax parse tree is the attribute information, it is determined that the method calling code module belongs to the service consumer.

[0032] In one embodiment, determining a call relationship table between code modules in the source code to be detected based on the first relationship and the second relationship includes:

[0033] Determining a first association relationship between the method declaration code module and the method call code module based on the first relationship;

[0034] Determining a second association relationship between the method calling code module and the method declaring code module according to the second relationship;

[0035] If the method declaration code module corresponding to the method calling code module in the second association relationship exists in the first association relationship, establishing a third association relationship between the method declaration code module corresponding to the method calling code module in the first association relationship and the method declaration code module corresponding to the method calling code module in the second association relationship;

[0036] A calling relationship table between code modules is determined based on the first association relationship, the second association relationship, and the third association relationship.

[0037] In one embodiment, the step of adding a corresponding service provider identifier to the code module in the call relationship table according to the service provider to which the code module belongs, and obtaining a code service call relationship table corresponding to the source code to be detected includes:

[0038] For each method declaration code module in the call relationship table, if the method declaration code module belongs to a service provider and the method declaration code module does not have a parent association relationship in the call relationship table, add a service provider identifier to the method declaration code module;

[0039] For each method calling code module in the calling relationship table, if the method calling code module belongs to a service consumer and there is no child association relationship for the method calling code module in the calling relationship table, add a service consumer identifier to the method calling code module;

[0040] According to the call relationship table after adding the service provider identifier and the service consumer identifier, a code service call relationship table is obtained.

[0041] In one embodiment, after obtaining the code service call relationship table corresponding to the source code to be detected, the method further includes:

[0042] Receive code service call relationship query information, and obtain the project identifier, class identifier, and method identifier in the code service call relationship query information;

[0043] Querying the corresponding project according to the project identifier, querying the corresponding class name in the project according to the class identifier, and querying the corresponding method name in the class corresponding to the class name according to the method identifier;

[0044] A code service call relationship table containing the method name is obtained as a query result.

[0045] In a second aspect, the present application provides a device for generating a code service call relationship, the device comprising:

[0046] an acquisition module, configured to acquire a source code to be detected, perform syntax parsing on the source code to be detected, and obtain a corresponding syntax parse tree; the syntax parse tree includes a plurality of nodes corresponding to a plurality of code modules of the source code to be detected, and the relationship between the nodes is determined based on the corresponding relationship between the plurality of code modules;

[0047] a traversal module configured to traverse the grammar parse tree, obtain a first relationship between a method declaration code module and at least one method call code module in its corresponding child node, a second relationship between a method call code module and its class and at least one method declaration code module in its corresponding parent class and child class, and a service provider to which each code module belongs; the service provider includes a service provider and a service consumer; and the code module includes a method declaration code module and a method call code module;

[0048] A generation module is used to determine a call relationship table between code modules in the source code to be detected based on the first relationship and the second relationship, and add corresponding service provider identifiers to the code modules in the call relationship table based on the service providers to which the code modules belong, to obtain a code service call relationship table corresponding to the source code to be detected.

[0049] In a third aspect, the present application provides a computer device comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the above method when executing the computer program.

[0050] In a fourth aspect, the present application provides a computer-readable storage medium having a computer program stored thereon, which implements the steps of the above method when executed by a processor.

[0051] In a fifth aspect, the present application provides a computer program product, comprising a computer program, which implements the steps of the above method when executed by a processor.

[0052] The above-mentioned code service call relationship generation method, apparatus, computer equipment, storage medium and computer program product, by traversing the syntax parse tree corresponding to the source code to be detected, obtains the first relationship between the method declaration code module in the syntax parse tree and at least one method call code module in its child node, the second relationship between the method call module and its class and at least one method declaration code module in the corresponding parent class, and the service party to which each code module belongs, and determines the call relationship table between the code modules in the source code to be detected based on the first relationship and the second relationship, adds the corresponding service party identifier to the code module in the call relationship table, and obtains the corresponding code service call relationship table. Compared with the traditional method of obtaining the call relationship between services through the service registration center, this solution obtains the service call relationship in the code by traversing the syntax tree before the code is run, thereby improving the maintainability of the code. BRIEF DESCRIPTION OF THE DRAWINGS

[0053] Figure 1 An application environment diagram of a method for generating a code service call relationship in one embodiment;

[0054] Figure 2 A flowchart of a method for generating a code service call relationship in one embodiment is shown;

[0055] Figure 3 A flowchart of a syntax parsing tree acquisition step in one embodiment;

[0056] Figure 4 A flowchart illustrating steps for determining the relationship between method declaration and method call in one embodiment;

[0057] Figure 5 A flowchart illustrating steps for determining the relationship between method calls and method declarations in one embodiment;

[0058] Figure 6 A flowchart of steps for determining service provider status in one embodiment;

[0059] Figure 7 A flowchart of steps for determining a service provider in another embodiment;

[0060] Figure 8 A schematic diagram of a flow chart of steps for determining a code service call relationship table in one embodiment;

[0061] Figure 9 A schematic diagram of a flow chart of steps for querying a code service call relationship table in one embodiment;

[0062] Figure 10 A schematic flow chart of a code service call relationship table query step in another embodiment;

[0063] Figure 11 A flowchart of a method for generating a code service call relationship in another embodiment;

[0064] Figure 12 Schematic diagram of a flow chart of a method for generating a code service call relationship in another embodiment;

[0065] Figure 13 A structural block diagram of a device for generating a code service call relationship in one embodiment;

[0066] Figure 14 FIG. 1 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION

[0067] In order to make the purpose, technical solutions and advantages of this application more clear, the following further describes this application in detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain this application and are not intended to limit this application.

[0068] The code service call relationship generation method provided in the embodiment of the present application can be applied to Figure 1 In the application environment shown in the figure, the distributed system includes a source code unpacking and file distribution module, a source file distributed analysis module, a call relationship data storage module, a call relationship data retrieval module, and a call relationship query and visualization module. The distributed system can construct a syntax parse tree corresponding to the code to be detected through each module, and determine the service call relationship in the source code to be detected by traversing the syntax parse tree. In one embodiment, Figure 2 As shown, a code service call relationship generation method is provided, which is applied to Figure 1 The distributed system in the example is used to illustrate the following steps:

[0069] Step S202 , obtaining the source code to be detected, performing syntax parsing on the source code to be detected, and obtaining a corresponding syntax parse tree; the syntax parse tree includes multiple nodes corresponding to multiple code modules of the source code to be detected, and the relationship between the nodes is determined based on the corresponding relationship between the multiple code modules.

[0070] The source code to be tested can be the code for which service call relationships need to be determined. The source code to be tested can be one of the source code files in the project in which it resides. A service call relationship refers to the call relationship between methods belonging to the service provider and methods belonging to the service consumer in the source code. The service callee is the service provider, and the service caller is the service consumer. For example, in a movie system, a user initiates a ticket purchase request. When the request is fulfilled, the current user's information is queried. In this scenario, the method corresponding to the movie microservice belongs to the service consumer, and the method corresponding to the user microservice belongs to the service provider. An interface provided by the user microservice is consumed by the movie microservice. The aforementioned generation of service call relationships in the source code can be performed via a distributed system, which includes multiple modules, such as a source code unpacking and file distribution module, a source file distributed analysis module, a call relationship data storage module, a call relationship data retrieval module, and a call relationship query and visualization module. The source code unpacking and file distribution module is responsible for unpacking the source file package and distributing each unpacked file to the source file distributed analysis module for processing. The distributed source file analysis module is responsible for building a corresponding parse tree for the incoming source file and extracting service call relationship data from the parse tree, including tables representing the relationship between method declarations and method calls, between method calls and method declarations, and between service caller methods and service consumer methods. Because the parsing and analysis processes for each source file are independent of each other, they can be executed in parallel for every source file in every project in the system.

[0071] The distributed system can obtain the above-mentioned source code to be detected, such as a source code file in a project, and perform syntax analysis on the source code to be detected to obtain a syntax parse tree corresponding to the source code to be detected. Among them, the above-mentioned source code to be detected contains multiple code modules, each code module has a corresponding function, such as a class code module, a method code module, etc. The syntax parse tree includes multiple nodes corresponding to the multiple code modules of the above-mentioned source code to be detected, wherein each code module can be a node in the syntax parse tree, and the relationship between the nodes can be determined based on the corresponding relationship between the above-mentioned multiple code modules. That is, the syntax parse tree includes parent nodes and child nodes, and the distributed system can determine the parent-child relationship between each code module.

[0072] Wherein, the above-mentioned code modules include modules corresponding to the attribute information, class information and method information in the source code to be detected, and the distributed system can obtain the parent-child relationship of the above-mentioned modules by parsing the source code to be detected. For example, in one embodiment, the source code to be detected is obtained, the source code to be detected is parsed to obtain the corresponding syntax parsing tree, including: detecting the declaration code module in the source code to be detected; the declaration code module includes the attribute declaration code module and the class declaration code module; detecting the attribute information code module in the attribute declaration code module, and detecting the class information code module, the method declaration code module and the method call code module corresponding to the method declaration code module in the class declaration code module; taking the project where the source code to be detected is located as the root node, taking the attribute declaration code module, the attribute information code module and the class declaration code module as the parent node, taking the class information code module and the method declaration code module as the child nodes of the class declaration code module, and taking the method call code module as the child node of the method declaration code module, to obtain the syntax parsing tree corresponding to the code to be detected.

[0073] In this embodiment, the above-mentioned source code to be detected may include a declaration code module, a call code module, etc. The declaration code module represents a configuration type code module, including the configuration of attribute information, the configuration of classes, and the configuration of methods, etc. The call code module represents a usage type code module, including the use of attribute information, the use of class information, and the use of methods, etc. The distributed system can detect the declaration code modules in the above-mentioned source code to be detected, such as detecting the attribute declaration code module and the class declaration code module, etc. The attribute declaration code module may include an attribute information code module, and the distributed system can detect the attribute information code module in the attribute declaration code module, and detect the class information code module, the method declaration code module, and the method call module corresponding to the method declaration code module in the class declaration code module. The project where the source code to be detected is located is taken as the root node, the attribute declaration code module, the attribute information code module, and the class declaration code module are taken as the parent node, the class information code module and the method declaration code module are taken as the child nodes of the class declaration code module, and the method call code module is taken as the child node of the method declaration code module, to obtain the grammatical parsing tree corresponding to the source code to be detected.

[0074] The above grammar parsing tree may also include other types of nodes. Specifically, an example of the above source code to be detected may be as follows:

[0075] package org.example.module1;

[0076] import org.example.module2.FooService;

[0077] import org.example.module2.Reference;

[0078] @Service(version="1.0",group="demo-group")

[0079] public class BarServiceImpl implements Barservice{

[0080] @Reference(version=1.0",group="demo-group")

[0081] private FooService foo;

[0082] @Override

[0083] public void bar(){

[0084] foo.fooO;}}

[0085] Taking the above source code to be tested as an example, the grammatical parse tree can be obtained as follows Figure 3 As shown, Figure 3 The following is a flow chart of the steps for obtaining a grammatical parsing tree in one embodiment. The grammatical parsing tree can be a data structure that represents the grammatical structure of the source code in a tree form. The process of obtaining a grammatical parsing tree in a distributed system can be a static code scanning process, which refers to a method of analyzing software by checking the software source code rather than by running the software. Figure 3 As can be seen, the project containing the above parse tree can include multiple source files containing source code. Each source file can be a compilation unit. The parse tree contains parent nodes such as package declarations, import declarations, property declarations, and class declarations. Property declarations include child nodes such as annotations, modifiers, and variable declarations. Class declarations include child nodes such as annotations, class names, and method declarations. Method declarations include child nodes such as method names and block declarations. Block declarations include child nodes such as method call expressions. Each of these nodes can exist in the form of code modules.

[0086] Step S204, traverse the syntax parse tree, obtain the first relationship between the method declaration code module in the syntax parse tree and at least one method call code module in its corresponding child node, the second relationship between the method call code module and its class and at least one method declaration code module in the corresponding parent class and child class, and the service party to which each code module belongs; the service party includes the service provider and the service consumer; the code module includes the method declaration code module and the method call code module.

[0087] Among them, after the distributed system obtains the above-mentioned syntax parse tree, it can traverse the syntax parse tree to obtain the service call relationship therein, and the service call relationship refers to the relationship of mutual calling between the service provider and the service consumer in the distributed system. Among them, the service call relationship includes not only the calling relationship between services, but also the calling relationship at the method level between services and services. The above-mentioned syntax parse tree contains nodes corresponding to the method declaration code module and nodes corresponding to the method call code module, wherein there is a corresponding relationship between the method declaration code module and the method call code module. The distributed system can obtain the first relationship between the method declaration code module and at least one method call code module in the corresponding child node in the syntax parse tree, and the distributed system can also obtain the second relationship between the method call code module and the class to which it belongs and at least one method declaration code module in the parent class and / or child class corresponding to the class. That is, when determining the first relationship between the method declaration code module and the method call code module, it is obtained based on searching for the method call code module to which the current method declaration code module belongs in the child nodes of the node where the current method call code module is located; and when determining the second relationship between the method call code module and the method declaration code module, it is obtained based on whether there are method declaration code modules with methods of the same name in the parent node and child nodes of the node where the current method call code module is located, and the parent class and child class of the class where the method declaration code module is located, and the method implementation codes of these methods of the same name need to exist in the method declaration code modules. In addition, the distributed system can also obtain the service party to which the above-mentioned code modules belong, for example, whether the code module belongs to the service provider or the service consumer. The code module of the service party that needs to be determined can be the above-mentioned method declaration code module and method call code module.

[0088] Specifically, in one embodiment, a syntax parse tree is traversed to obtain a first relationship between a method declaration code module in the syntax parse tree and at least one method call code module in its corresponding child node, a second relationship between the method call code module and its class and at least one method declaration code module in its corresponding parent class and child class, and a service party to which each code module belongs, including: traversing the syntax parse tree, and determining a first relationship between the method declaration code module and at least one method call code module in its corresponding child node based on the method declaration code module in the syntax parse tree and the method call code module in its corresponding child node; determining a second relationship between the method call code module and its class and at least one method declaration code module in its corresponding parent class and child class based on the method call code module and the method declaration code module in the child node corresponding to each class declaration code module in the syntax parse tree, and the method declaration code module in the parent class declaration code module and the child class declaration code module corresponding to each class declaration code module; and determining a service party to which the method declaration code module and / or the method call code module in the class declaration code module belongs based on the class declaration code module and the attribute declaration code module in the syntax parse tree.

[0089] In this embodiment, the distributed system can respectively determine the above-mentioned first relationship, second relationship and service party information through three traversal methods. Among them, after the distributed system traverses the syntax parse tree, it can obtain the method declaration code module in the syntax parse tree and the method call code module in its corresponding child node, so that the distributed system can determine the first relationship between the method declaration code module and at least one method call code module in its corresponding child node based on the above-mentioned method declaration code module in the syntax parse tree and the method call code module in its corresponding child node. Among them, the first relationship between the method declaration code module and the method call code module can be a relationship obtained by traversing the method call code modules in the child nodes of the node where the method declaration code module is located, and the distributed system can also generate a corresponding inter-method call relationship table based on the above-mentioned first relationship.

[0090] The distributed system can also obtain each class declaration code module in the above-mentioned grammatical parse tree, obtain the method call code module and method declaration code module in the child node corresponding to each class declaration code module, obtain the parent class declaration code module in the parent node corresponding to each class declaration code module, and obtain the child class declaration code module in the child node corresponding to each class declaration code module, detect the method declaration code module in the parent class declaration code module, and detect the method declaration module in the child class declaration code module. Thus, the distributed system can determine the second relationship between the method call code module and its class and at least one method declaration code module in the corresponding parent class and child class based on the method declaration code modules in the child node corresponding to each class declaration code module and the method declaration code modules in the parent class declaration code module and child class declaration code module corresponding to each class declaration code module. That is, the second relationship between the method call and the method declaration can be a traversal method that searches from the node where the method declaration code in a class is located upward to search for the method declaration code module in the parent node and downward to search for the method declaration code module in the child node. This is not limited to the current class and can also search for nodes corresponding to method declaration code modules with the same name in the parent class of the current class and search for nodes corresponding to method declaration code modules with the same name in the child class of the current class. After determining the second relationship between the method call and the method declaration, the distributed system may also generate a method call relationship table corresponding to the second relationship.

[0091] The above-mentioned syntax parse tree also includes a class declaration code module and a property declaration code module. The distributed system can determine the service party to which the method declaration code module in the class declaration code module belongs based on the class declaration code module and the property declaration code module in the syntax parse tree, and can also determine the service party to which the method call code module in the class declaration code module belongs. Specifically, the distributed system can determine whether the method declaration code module in the class declaration code module belongs to the service provider or the service consumer based on the annotation information in the above-mentioned class declaration code module and the property declaration code module, and can also determine whether the method call code module in the class declaration code module belongs to the service provider or the service consumer. After the distributed system determines the service party to which the above-mentioned method declaration code module and the method call code module belong, it can generate a service party relationship table containing the method declaration code module, the method call code module and the service party to which the above-mentioned code modules belong.

[0092] Step S206: Determine a call relationship table between code modules in the source code to be detected based on the first relationship and the second relationship, and add corresponding service provider identifiers to the code modules in the call relationship table based on the service providers to which the code modules belong, to obtain a code service call relationship table corresponding to the source code to be detected.

[0093] Among them, the above-mentioned first relationship can be the relationship between the method declaration code module and the method call code module, and the second relationship can be the relationship between the method call code module and the method declaration code module, wherein the first relationship is a method of taking the method declaration code module as the starting point to find the method call code module in the child node, and the second relationship can be a method of taking the method call code module as the starting point to find the method declaration code module with the same name in the current class and the parent class of the current class. Based on the above-mentioned first and second relationships, the distributed system can determine the call relationship table between each code module in the source code to be detected. Specifically, it can be a call relationship table between the method declaration code module and the method call code module. The call relationship table can include the method declaration code module as the starting node, and several child nodes associated with the starting node. Each child node can be a method declaration code module or a method call code module.

[0094] After the distributed system determines the above-mentioned call relationship table, it can also add corresponding service party identifiers to the code modules in the call relationship table according to the service parties to which the above-mentioned various code modules belong, and obtain a code service call relationship table corresponding to the source code to be detected. Among them, the above-mentioned service party identifiers include the identifier of the service provider and the identifier of the service consumer. If the distributed system detects that the method declaration code module in the code service call relationship table belongs to the service provider or detects that the method call code module belongs to the service consumer, the distributed system can add the corresponding service party identifier to the corresponding code module. Among them, the distributed system can add the corresponding service party identifier only when it detects that the method declaration code module belongs to the starting node or the method call module belongs to the last child node.

[0095] In the above-mentioned code service call relationship generation method, by traversing the syntax parse tree corresponding to the source code to be tested, the first relationship between the method declaration code module in the syntax parse tree and at least one method call code module in its child node, the second relationship between the method call module and its class and at least one method declaration code module in the corresponding parent class, and the service party to which each code module belongs are obtained. Based on the first relationship and the second relationship, the call relationship table between the code modules in the source code to be tested is determined, and the corresponding service party identifier is added to the code module in the call relationship table to obtain the corresponding code service call relationship table. Compared with the traditional method of obtaining the call relationship between services through the service registration center, this solution obtains the service call relationship in the code by traversing the syntax tree before the code is run, thereby improving the maintainability of the code.

[0096] In one embodiment, based on a method declaration code module in a grammar parse tree and a method call code module in its corresponding child node, determining a first relationship between a method declaration code module and at least one method call code module in its corresponding child node includes: obtaining a method declaration code module in the grammar parse tree, detecting a subtree contained in the method declaration code module, and obtaining a method call code module in the subtree; and establishing a first relationship between the method declaration code module and the method call code module in the subtree.

[0097] In this embodiment, the distributed system can obtain the first relationship between the method declaration code module and the method call code module by traversing the syntax parse tree. The above-mentioned syntax parse tree contains a variety of tree structures. The distributed system can obtain the method declaration code module in the syntax parse tree. The method declaration code module can appear in the node of the subtree of the class declaration code module. The method declaration code module also has a corresponding subtree node. The distributed system can detect the node in the subtree contained in the method declaration code module and obtain the method call code module in the subtree. There can be one or more method call code modules in the subtree. The distributed system can use all the method call code modules in the subtree as the method call code modules corresponding to the method declaration code module. Thus, the distributed system can establish the first relationship between the above-mentioned method declaration code module and the method call code module in the subtree.

[0098] Specifically, Figure 4 As an example, Figure 4 The following is a flow chart of the steps for determining the relationship between a method declaration and a method call in one embodiment. When the distributed system determines the first relationship between the method declaration code module and the method call code module, it can obtain the method declaration A by traversing the grammar parse tree. Further, by searching all subtrees of the method declaration A, all method call expressions in the subtrees are found, such as Figure 4 In addition, the distributed system can also determine whether the method call expression is a system internal method call. If the method in the method call expression B is determined to be a system internal method call, a call relationship record from method declaration A to method call B is generated. Figure 4 Each node in the structure can be a code module. The above method declaration A is a method declaration code module, and the above method call expression B is a method call code module. An intra-system method call refers to a type of method call statement characterized by the method in the statement being declared and implemented within the IT system. Since service call relationships often do not exist between enterprise IT systems and those outside the system, distinguishing between intra-system method calls and extra-system method calls and analyzing only intra-system method call code statements can help improve the processing efficiency of the analysis system.

[0099] Through this embodiment, the distributed system can determine the first relationship by traversing the method declaration code module and the method call code module in the syntax parsing tree, so that the distributed system can determine the service call relationship in the source code to be detected based on the first relationship, thereby improving the maintainability of the source code to be detected.

[0100] In one embodiment, based on the method calling code module and the method declaration code module in the child node corresponding to each class declaration code module in the grammar parse tree, and the method declaration code module in the parent class declaration code module and the child class declaration code module corresponding to each class declaration code module, the second relationship between the method calling code module and the class to which it belongs and at least one method declaration code module in the corresponding parent class and child class is determined, including: obtaining the method declaration code module in the subtree of each class declaration code module in the grammar parse tree, if it is detected that there is method implementation code in the method declaration code module in the subtree, then establishing the current class second relationship between the method calling code module corresponding to the method declaration code module in the subtree and the method declaration code module in the subtree; determining the parent-child relationship of each class declaration code module, and obtaining the parent class declaration code module and the child class declaration code module corresponding to each class declaration code module in the grammar parse tree; for each class declaration code module, if it is detected that there is method implementation code in the method declaration code module in the subtree If it is detected that there is a parent method declaration code module containing method implementation code in the parent class declaration code module corresponding to the class declaration code module, a parent class second relationship is established between the first target method calling code module corresponding to the class declaration code module and the parent method declaration code module; the method name of the first target method calling code module is the same as the method name of the parent method declaration code module; if it is detected that there is a child method declaration code module containing method implementation code in the child class declaration code module corresponding to the class declaration code module, a child class second relationship is established between the second target method calling code module corresponding to the class declaration code module and the child method declaration code module; the method name of the second target method calling code module is the same as the method name of the child method declaration code module; based on the current class second relationship, the parent class second relationship and the child class second relationship, a second relationship is obtained between the method calling code module and the class to which it belongs and at least one method declaration code module in the corresponding parent class and child class.

[0101] In this embodiment, the distributed system can determine the second relationship between the method call code module and the method declaration code module by traversing the syntax parse tree. The second relationship can be a relationship obtained by traversing from the method declaration code module of the current class to the upper node and the lower node in the syntax parse tree. For example, the distributed system can obtain the method declaration code module in the subtree of each class declaration code module in the syntax parse tree. If it is detected that there is method implementation code in the method declaration code module in the subtree, the current class second relationship between the method call code module corresponding to the method declaration code module in the subtree and the method declaration code module in the subtree is established. Among them, the current class second relationship represents the second relationship between the method call code module and the method declaration code module in the class corresponding to the above-mentioned class declaration code module. The distributed system can also determine the parent-child relationship of each class declaration code module mentioned above, and obtain the parent class declaration code module and the child declaration code module corresponding to each class declaration code module in the syntax parse tree. Wherein, the above-mentioned class declaration code module may include multiple, and there may be a parent-child class relationship between each class declaration code module. For each class declaration code module, if the distributed system detects that there is a parent method declaration code module containing method implementation code in the parent class declaration code module corresponding to the class declaration code module, then the distributed system may establish a parent class second relationship between the first target method call code module corresponding to the class declaration code module and the parent method declaration code module. Wherein, the method name of the first target call code module is the same as the method name of the parent method declaration code module, that is, the above-mentioned parent class second relationship represents the second relationship between the method call code module and the method declaration code module corresponding to the method declaration code modules of different classes, and the method call code module is in the above-mentioned child class.

[0102] The distributed system may also detect whether there is a sub-method declaration code module containing method implementation code in the sub-class declaration code module corresponding to the class declaration code module. If so, the distributed system may establish a sub-class second relationship between the second target method call code module corresponding to the class declaration code module and the sub-method declaration code module. The method name of the second target method call code module is the same as the method name of the sub-method declaration code module, that is, the sub-class second relationship indicates that there are second relationships between the method call code module and the method declaration code module corresponding to the method declaration code modules of different classes, and the method call code module is in the parent class.

[0103] After obtaining the above-mentioned second relationship of the current class, the second relationship of the parent class, and the second relationship of the child class, the distributed system can obtain the second relationship between the method calling code module and the class to which it belongs and at least one method declaration code module in the corresponding parent class and child class based on the second relationship of the current class, the second relationship of the parent class, and the second relationship of the child class. That is, a method calling code module can correspond to one or more method declaration code modules, and the distributed system can also establish a corresponding calling relationship table based on the above-mentioned second relationship. Specifically, as shown in the following example: Figure 5 Take the parse tree shown as an example, Figure 5 The following is a flow chart of steps for determining the relationship between method call and method declaration in one embodiment. Figure 5 The tree structure determines the second relationship between the method call code module and the method declaration code module, including the current class second relationship, parent class second relationship, and child class second relationship. The distributed system can obtain the relationship between the class and its corresponding parent class from the syntax parse tree and record it in the class and parent class relationship table. Figure 5 As shown, the distributed system can find the class declaration B from the grammatical parse tree. Since the child node of class declaration B contains class or interface type A, the distributed system can obtain type A as the parent class or interface of type B, and record the relationship in the class and its parent class relationship table. Similarly, type B can be obtained as the parent class or interface of type C, and type C is the child class of type B. The distributed system can establish a relationship table between method calls and method declarations. Among them, there are usually three relationships between a method call and a method declaration: the method call subject calls the method declared in this class, which is the current class second relationship; the method call subject calls the method declared in its parent class, which is the parent class second relationship; the method call subject calls the method declared in the child class, which is the child class second relationship. For the above three situations, as shown in the following example, Figure 5 As shown, for the second relationship of the current class, for example, for the class declaration A, the distributed system can find all its method declarations, such as Aa(). If the distributed system detects that the method is only a method interface declaration and does not contain a method implementation, it means that the method is implemented by its subclass and is not recorded in the relationship table between method calls and method declarations. If the method declaration contains a specific method implementation, the distributed system can record it in the relationship table between method calls and method declarations, that is, obtain the mapping relationship from method call Aa to method declaration Aa as the second relationship of the current class. In addition, the distributed system can also find out whether there is a parent class based on the class declaration in the record, and whether there is a method with the same name in the parent class. If there is a method with the same name, it means that the method call also corresponds to the method declaration in the parent class. The distributed system can record the second relationship between the above method call and the method declaration in the parent class in the method call and method declaration relationship table. As shown Figure 5As shown, for the second relationship of the parent class, for example, for the class declaration B, the distributed system can obtain the mapping relationship from method call Bb to method declaration Bb according to the method in the previous step. In addition, since class B inherits class A, although method a is not declared in class B, the distributed system can call method Ba through the inheritance relationship. Therefore, the distributed system can obtain an implicit mapping relationship, that is, the mapping relationship from method call Ba to method declaration Aa, as the above-mentioned second relationship of the parent class. In addition, the distributed system can also find out whether there is a subclass based on the class declaration in the record, and whether there is a method with the same name in the subclass. If so, it means that when the method in the current class is called, the method in the subclass may be called. Therefore, the distributed system can establish a second relationship between the method call in the current class and the method declaration in its subclass. As shown in Figure 5 As shown, for type declaration C, the distributed system can first obtain an implicit call relationship based on the inheritance relationship, namely, method call Ca to method declaration Aa. Secondly, because method b is declared in class declaration C, the distributed system can establish a mapping relationship from method call Cb to method declaration Cb. Furthermore, because method Cb overrides its parent class method Bb, when calling Bb, its method implementation may be Cb. Therefore, the distributed system can establish a mapping relationship from method call Bb to Cb, as the second subclass relationship mentioned above.

[0104] Through this embodiment, the distributed system can determine the second relationship in the same class or across classes based on methods with the same name by traversing the relationship between classes and parent classes and child classes in the syntax parse tree, as well as the method declaration code module and method call code module in each class. The distributed system can then determine the service call relationship in the source code to be tested based on the second relationship, thereby improving the maintainability of the source code to be tested.

[0105] In one embodiment, based on the class declaration code module and the attribute declaration code module in the syntax parse tree, determining the service party to which the method declaration code module and / or the method call code module in the class declaration code module belongs includes: obtaining the class information code module in the child node of the class declaration code module in the syntax parse tree; if the class information corresponding to the class information code module is the service provider, determining that the method declaration code module corresponding to the class declaration code module belongs to the service provider; and / or, obtaining the attribute information code module corresponding to the attribute declaration code module in the syntax parse tree; if the attribute information corresponding to the attribute information code module includes the service consumer, and the calling subject of the method call code module in the syntax parse tree is the attribute information, determining that the method call code module belongs to the service consumer.

[0106] In this embodiment, the service parties to which the above-mentioned code modules belong include service providers and service consumers. The distributed system can determine the service parties to which the method declaration code modules and method call code modules belong by detecting the class declaration code modules and the attribute declaration code modules. For example, the distributed system can obtain the class information code module in the child node of the class declaration code module in the above-mentioned syntax parse tree. If the distributed system detects that the class information corresponding to the class information code module is the service provider, the distributed system determines that the method declaration code module corresponding to the class declaration code module belongs to the service provider. After the distributed system determines the service party to which the code module belongs, it can also generate a corresponding table, which corresponds to the attributes of the service party. For the service provider, the generated table is a service provider method table. Specifically, the distributed system can find the class declaration node from the syntax parse tree. If the child node of the node carries a service provider declaration annotation, the distributed system can create a service provider method record for each method in the class and record it in the service provider method table. Figure 6 Take the parse tree shown as an example, Figure 6 The following is a flow chart of the steps for determining the service provider in one embodiment. The distributed system can detect the class declaration "BarServiceImpl" and its corresponding package declaration "org.example.module1" in the parse tree; the distributed system can determine whether the child node of the class has a service provider declaration annotation, such as Figure 6 The child node shown has a service provider declaration annotation "@Service(version="1.0", group="demo-group")", so the distributed system can record the method declaration "bar()" in the class declaration into the service provider method table.

[0107] Among them, in addition to the service provider, the distributed system can also detect whether the method call code module in the above-mentioned syntax parse tree belongs to the service consumer. The distributed system can obtain the attribute information code module corresponding to the attribute declaration code module in the syntax parse tree, and detect the attribute information in the attribute information code module. If the distributed system detects that the attribute information corresponding to the attribute information code module contains the service consumer, and the calling subject of the method call code module in the above-mentioned syntax parse tree is the above-mentioned attribute information, then the distributed system can determine that the above-mentioned method call code module belongs to the service consumer. Specifically, when detecting the service consumer, the distributed system can obtain all attribute declarations from the syntax parse tree. If the distributed system detects that the attribute declaration has a service consumer declaration annotation, then the distributed system can identify the field as a service consumer attribute declaration. If the distributed system detects that the method call subject in the method call expression in the syntax parse tree is the attribute declaration, then the method call is recorded in the service consumer method table. Figure 7Take the parse tree shown as an example, Figure 7 This is a flowchart of the service determination step in another embodiment. Since the attribute declaration "FooService foo" carries the service consumer declaration annotation "@Reference(version="1.0", group="demo-group")", the distributed system can identify the attribute declaration as a service consumer attribute. Figure 7 If the method call expression "foo.foo()" exists in the syntax parse tree shown, the distributed system can record the method call in the service consumer method table.

[0108] Furthermore, in some embodiments, service provider and service consumer methods may be declared not through annotation types but through structured configuration files, such as XML or YAML. In this case, the distributed system can parse the service provider and service consumer class names using common parsing methods within the field. The system can then determine whether the associated method declarations and method call expressions are service provider and service consumer methods using the aforementioned methods, and record these in the corresponding tables. Furthermore, a distributed system may contain more than one source code to be tested. The distributed system can perform the aforementioned first relationship, second relationship, and service determination steps on all source code in the project, thereby obtaining the service call relationship corresponding to each source code.

[0109] Through this embodiment, the distributed system can determine the service parties of the method declaration code module and the method call code module based on attribute information and class information, so that the distributed system can determine the service call relationship based on the service parties of each code module, thereby improving the maintainability of the code.

[0110] In one embodiment, based on the first relationship and the second relationship, a calling relationship table between code modules in the source code to be detected is determined, including: based on the first relationship, determining a first association relationship between a method declaration code module and a method calling code module; based on the second relationship, determining a second association relationship between a method calling code module and a method declaration code module; if the method declaration code module corresponding to the method calling code module in the second association relationship exists in the first association relationship, establishing a third association relationship between the method declaration code module corresponding to the method calling code module in the first association relationship and the method declaration code module corresponding to the method calling code module in the second association relationship; based on the first association relationship, the second association relationship and the third association relationship, determining the calling relationship table between the code modules.

[0111] In this embodiment, after the distributed system determines the first relationship between the above-mentioned method declaration code module and the method call code module and the second relationship between the method call code module and the method declaration code module, it can determine the corresponding call relationship table, which can be expressed as a call relationship table between methods. For example, the distributed system can determine the first association relationship between the method declaration code module and the method call code module based on the above-mentioned first relationship, and determine the second association relationship between the method call code module and the method declaration code module based on the second relationship. The distributed system can detect whether the method declaration code module corresponding to the method call code module in the second association relationship exists in the first association relationship. If so, the distributed system can establish a third association relationship between the method declaration code module corresponding to the method call code module in the first association relationship and the method declaration code module corresponding to the method call code module in the second association relationship. Thus, the distributed system can determine the call relationship table between code modules based on the above-mentioned first association relationship, the second association relationship and the third association relationship.

[0112] Among them, the project where the source code to be detected is located may include multiple source codes, and the distributed system can obtain the first relationship, the second relationship and the corresponding service party information in each source code respectively, and determine the call relationship table based on the first relationship and the second relationship of all source codes, and determine the code service call relationship table based on the service party information of all source codes. Specifically, the above-mentioned first relationship, second relationship and service party information all have corresponding tables. The distributed system can perform aggregate analysis based on the method declaration and method call relationship table corresponding to the first relationship, the method call and method declaration relationship table corresponding to the second relationship, the service caller method table, and the service consumer method table to obtain the call relationship from the service provider method to the service consumer method in the above-mentioned project. Figure 8 For example, Figure 8 The figure is a flow chart of steps for determining the code service call relationship table in one embodiment. The distributed system can establish a call relationship graph between methods based on the method declaration and method call relationship table, and the method call and method declaration relationship table.

[0113] The distributed system can also add a service provider identifier to each code module in the call relationship table. For example, in one embodiment, according to the service provider to which the code module belongs, the corresponding service provider identifier is added to the code module in the call relationship table to obtain a code service call relationship table corresponding to the source code to be detected, including: for each method declaration code module in the call relationship table, if the method declaration code module belongs to the service provider and the method declaration code module does not have a parent association relationship in the call relationship table, the service provider identifier is added to the method declaration code module; for each method call code module in the call relationship table, if the method call code module belongs to the service consumer and the method call code module does not have a child association relationship in the call relationship table, the service consumer identifier is added to the method call code module; according to the call relationship table after adding the service provider identifier and the service consumer identifier, the code service call relationship table is obtained.

[0114] In this embodiment, there can be multiple method declaration code modules in the above-mentioned call relationship table, but not every method declaration code module needs to add a service provider identifier. For each method declaration code module in the call relationship table, the distributed system can detect whether the method declaration code module belongs to the service provider, and detect that there is no parent association relationship for the method declaration code module in the call relationship table, that is, there is no parent node, then the distributed system can add a service provider identifier to the method declaration code module. There can also be multiple method call code modules in the above-mentioned call relationship table. For each method call code module in the call relationship table, the distributed system can detect whether the method call code module belongs to the service consumer, and detect that there is no child association relationship for the method call code module in the call relationship table, that is, there is no child node, then the distributed system can add a service consumer identifier to the method call code module.

[0115] Specifically, the above code modules and their corresponding service provider information can form corresponding tables. Including the service provider method table and the service consumer method table, the distributed system can mark the service provider method and the service consumer method in the method call relationship diagram according to the service provider method table and the service consumer method table. Figure 8As shown, the distributed system can obtain that method declaration A contains method call B and method call C based on the method declaration and method call relationship table corresponding to the first relationship. For method call B, the distributed system can obtain that it has two associated method declarations, namely method declaration D and method declaration E, based on the method call and method declaration relationship table corresponding to the second relationship. For method declaration D, the distributed system can obtain that it contains method call E based on the method declaration and method call relationship table. The distributed system can obtain the relationship graph of all method calls in the project by analogy with the above method. Finally, the distributed system can mark the methods based on the service provider method table and the service consumer method table. Among them, the above project file can be more than one project file. The distributed system can traverse each project in the distributed system and determine the above code service call relationship table for each project, thereby obtaining the method-level call relationship between all services in the distributed system.

[0116] Through the above embodiment, the distributed system can determine the code service call relationship table of the source code to be detected based on the first relationship, the second relationship and the service provider information corresponding to each code module, thereby improving the maintainability of the code.

[0117] In one embodiment, after obtaining the code service call relationship table corresponding to the source code to be detected, it also includes: receiving code service call relationship query information, obtaining the project identifier, class identifier and method identifier in the code service call relationship query information; querying the corresponding project according to the project identifier, querying the corresponding class name in the project according to the class identifier, and querying the corresponding method name in the class corresponding to the class name according to the method identifier; obtaining the code service call relationship table containing the method name as the query result.

[0118] In this embodiment, after the distributed system obtains the above-mentioned code service call relationship table, it can also query the code service call relationship table based on the corresponding identifier. When querying, the distributed system can receive the code service call relationship query information, obtain the project identifier, class identifier and method identifier in the code service call relationship query information, and the distributed system can query the corresponding project based on the project identifier, and query the corresponding class name in the project based on the class identifier, and query the corresponding method name in the class corresponding to the class name based on the above-mentioned method identifier, so that the distributed system can obtain the code service call relationship table containing the method name as the query result. Specifically, the above-mentioned query can be a multi-level query, that is, querying the method of the code service call relationship table based on multiple identifiers. As Figure 9 As shown, Figure 9The figure is a flowchart illustrating the steps for querying the code service call relationship table in one embodiment. Based on the service provider method table and service consumer method table generated above, the distributed system can obtain the unique identifier of the method. The unique identifier includes five fields: "service version," "service group," "package name," "class name," and "method name." Thus, the distributed system can establish a multi-level hash index for the service provider and a multi-level hash index for the service consumer based on the unique identifiers of the service provider method and the service consumer method, and the distributed system can also establish corresponding reverse indexes. Based on the unique identifier of the service provider method, the distributed system can establish a corresponding hash table in the order of "service group," "service version," "package name," "class name," and "method name." For the service group hash table, the key of each record is the hash value of the service group name, and the value is the address of the service version hash table. Similarly, the keys and values of the other hash tables can be obtained. When performing an index query, the distributed system can find the corresponding service version hash table from the service group hash table based on the service group name hash value, and then find the package name hash table from the service version hash table based on the service version hash value. This process continues until the corresponding record in the method declaration and method call relationship table is found using the hash value of the service provider method name. Using this query index, the distributed system can quickly retrieve the service call relationship within O(1) time complexity based on the unique identifier of the service provider method. Similarly, the distributed system can also use the above method to establish a multi-level hash index for the service consumer based on the unique identifier of the service consumer method.

[0119] In addition, the distributed system can also query the call relationship table through the reverse index method. Specifically, Figure 10 As shown, Figure 10 This is a flowchart illustrating the steps for querying the code service call relationship table in another embodiment. A distributed system can establish a multi-level hash index based on "package name," "class name," "method name," "group name," and "service version." When searching by fully qualified class name, the forward indexing method described above requires a time complexity of O(n), while the reverse indexing method can improve query performance to O(1).

[0120] Through the above embodiments, the distributed system can obtain the methods in the call relationship table through forward query and reverse query based on the multi-level hash index, thereby improving the query efficiency of the method code module.

[0121] In one embodiment, Figure 11 As shown, Figure 11The figure is a flow chart of the code service call relationship generation method in another embodiment. In this embodiment, the distributed system can unpack the source file and analyze each source file through multiple nodes. Specifically, after the source file package is processed by the unpacking and source file distribution module, the source file is distributed to each node in the source file distributed analysis module. Each node processes a source file independently and there is no dependency relationship. After each source file analysis node is processed, the generated service call relationship data is uploaded to the call relationship data storage module respectively. Among them, the call relationship data storage module is responsible for storing service call relationship data such as the declaration and method call relationship table, the method call and method declaration relationship table, the service caller method table, and the service consumer method table. The above-mentioned call relationship data retrieval module is responsible for indexing the service call relationship data according to the aforementioned index generation method. The above-mentioned call relationship query and visualization module is responsible for querying the service call relationship according to the index and performing visualization.

[0122] Among them, for the syntax analysis of each source file, the process can be as follows Figure 12 As shown, Figure 12 This is a flow chart of a method for generating a code service call relationship in another embodiment. The distributed system can perform syntax analysis on each source file in the project to obtain a corresponding syntax parse tree, wherein the source file can be the source code to be detected. The distributed system can analyze the syntax parse tree to obtain a method declaration and method call relationship table, a method call and method declaration relationship table, a service provider method table, and a service consumer method table in the syntax parse tree. The distributed system can traverse all source code files in the above project, obtain the tables corresponding to the above relationships for each file, and determine the information of each service party. Thus, the distributed system can aggregate and analyze all the obtained method declaration and method call relationship tables, method call and method declaration relationship tables, service caller method tables, and service consumer method tables to obtain the call relationship from the service provider method to the service consumer method in the project, i.e., the above code service call relationship table. The distributed system can also traverse all projects in the above distributed system and obtain the code service call relationship table corresponding to each project, thereby obtaining the method-level call relationship between all services in the distributed system.

[0123] Through the above embodiment, the distributed system obtains the service call relationship in the code by traversing the syntax tree before the code is run, thereby improving the maintainability of the code.

[0124] It should be understood that, although the various steps in the flowcharts involved in the various embodiments described above are displayed in sequence according to the instructions of the arrows, these steps are not necessarily executed in sequence in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and these steps can be executed in other orders. Moreover, at least a portion of the steps in the flowcharts involved in the various embodiments described above can include multiple steps or multiple stages, and these steps or stages are not necessarily executed and completed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a portion of steps or stages in other steps.

[0125] Based on the same inventive concept, the embodiments of the present application also provide a device for generating a code service call relationship for implementing the aforementioned method for generating a code service call relationship. The solution provided by this device is similar to the solution described in the aforementioned method. Therefore, the specific limitations of one or more embodiments of the device for generating a code service call relationship provided below can be found in the above-mentioned limitations of the method for generating a code service call relationship, and will not be repeated here.

[0126] In one embodiment, Figure 13 As shown, a device for generating a code service call relationship is provided, comprising: an acquisition module 500, a traversal module 502 and a generation module 504, wherein:

[0127] The acquisition module 500 is used to obtain the source code to be detected, perform syntax analysis on the source code to be detected, and obtain a corresponding syntax parse tree; the syntax parse tree includes multiple nodes corresponding to multiple code modules of the source code to be detected, and the relationship between the nodes is determined based on the corresponding relationship between the multiple code modules.

[0128] The traversal module 502 is used to traverse the syntax parse tree, obtain the first relationship between the method declaration code module in the syntax parse tree and at least one method call code module in its corresponding child node, the second relationship between the method call code module and its class and at least one method declaration code module in the corresponding parent class and child class, and the service party to which each code module belongs; the service party includes a service provider and a service consumer; the code module includes a method declaration code module and a method call code module.

[0129] The generation module 504 is used to determine the call relationship table between the code modules in the source code to be detected based on the first relationship and the second relationship, and add the corresponding service provider identifier to the code module in the call relationship table according to the service provider to which the code module belongs, so as to obtain the code service call relationship table corresponding to the source code to be detected.

[0130] In one embodiment, the above-mentioned acquisition module 500 is specifically used to detect the declaration code module in the source code to be detected; the declaration code module includes the attribute declaration code module and the class declaration code module; detect the attribute information code module in the attribute declaration code module, and detect the class information code module, the method declaration code module and the method call code module corresponding to the method declaration code module in the class declaration code module; take the project where the source code to be detected is located as the root node, the attribute declaration code module, the attribute information code module and the class declaration code module as the parent node, the class information code module and the method declaration code module as the child nodes of the class declaration code module, and the method call code module as the child node of the method declaration code module, to obtain the syntax parse tree corresponding to the code to be detected.

[0131] In one embodiment, the traversal module 502 is specifically used to traverse the syntax parse tree, and determine a first relationship between the method declaration code module and at least one method call code module in its corresponding child node based on the method declaration code module in the syntax parse tree and the method call code module in its corresponding child node; determine a second relationship between the method call code module and its class and at least one method declaration code module in the corresponding parent class and child class based on the method declaration code module in the child node corresponding to each class declaration code module in the syntax parse tree, and the method declaration code module in the parent class declaration code module and the child class declaration code module corresponding to each class declaration code module; determine the service party to which the method declaration code module and / or method call code module in the class declaration code module belongs based on the class declaration code module and the attribute declaration code module in the syntax parse tree.

[0132] In one embodiment, the traversal module 502 is specifically used to obtain the method declaration code module in the syntax parse tree, detect the subtree contained in the method declaration code module, obtain the method call code module in the subtree; and establish a first relationship between the method declaration code module and the method call code module in the subtree.

[0133] In one embodiment, the traversal module 502 is specifically used to obtain the method declaration code module in the subtree of each class declaration code module in the grammar parse tree. If it is detected that there is method implementation code in the method declaration code module in the subtree, a second relationship between the method call code module corresponding to the method declaration code module in the subtree and the current class of the method declaration code module in the subtree is established; the parent-child relationship of each class declaration code module is determined, and the parent class declaration code module and the child class declaration code module corresponding to each class declaration code module in the grammar parse tree are obtained; for each class declaration code module, if it is detected that there is a parent method declaration code module containing method implementation code in the parent class declaration code module corresponding to the class declaration code module, a first relationship corresponding to the class declaration code module is established. The target method calling code module has a parent class second relationship with the parent method declaration code module; the method name of the first target method calling code module is the same as the method name of the parent method declaration code module; if it is detected that there is a sub-method declaration code module containing method implementation code in the sub-class declaration code module corresponding to the class declaration code module, then a sub-class second relationship is established between the second target method calling code module corresponding to the class declaration code module and the sub-method declaration code module; the method name of the second target method calling code module is the same as the method name of the sub-method declaration code module; based on the current class second relationship, the parent class second relationship and the sub-class second relationship, the second relationship of the method calling code module and its class and at least one method declaration code module in the corresponding parent class and sub-class is obtained.

[0134] In one embodiment, the traversal module 502 is specifically used to obtain the class information code module in the child node of the class declaration code module in the syntax parsing tree; if the class information corresponding to the class information code module is a service provider, it is determined that the method declaration code module corresponding to the class declaration code module belongs to the service provider.

[0135] In one embodiment, the above-mentioned traversal module 502 is specifically used to obtain the attribute information code module corresponding to the attribute declaration code module in the syntax parsing tree; if the attribute information corresponding to the attribute information code module includes the service consumer, and the calling subject of the method calling code module in the syntax parsing tree is the attribute information, it is determined that the method calling code module belongs to the service consumer.

[0136] In one embodiment, the above-mentioned generation module 504 is specifically used to determine a first association relationship between a method declaration code module and a method calling code module based on a first relationship; determine a second association relationship between a method calling code module and a method declaration code module based on a second relationship; if a method declaration code module corresponding to the method calling code module in the second association relationship exists in the first association relationship, establish a third association relationship between the method declaration code module corresponding to the method calling code module in the first association relationship and the method declaration code module corresponding to the method calling code module in the second association relationship; determine a calling relationship table between code modules based on the first association relationship, the second association relationship, and the third association relationship.

[0137] In one embodiment, the above-mentioned generation module 504 is specifically used to, for each method declaration code module in the call relationship table, add a service provider identifier to the method declaration code module if the method declaration code module belongs to the service provider and the method declaration code module does not have a parent association relationship in the call relationship table; for each method call code module in the call relationship table, add a service consumer identifier to the method call code module if the method call code module belongs to the service consumer and the method call code module does not have a child association relationship in the call relationship table; and obtain a code service call relationship table based on the call relationship table after adding the service provider identifier and the service consumer identifier.

[0138] In one embodiment, the above-mentioned device also includes: a query module, which is used to receive code service call relationship query information, obtain the project identifier, class identifier and method identifier in the code service call relationship query information; query the corresponding project according to the project identifier, query the corresponding class name in the project according to the class identifier, and query the corresponding method name in the class corresponding to the class name according to the method identifier; obtain the code service call relationship table containing the method name as the query result.

[0139] Each module in the above-mentioned code service call relationship generation device can be implemented in whole or in part through software, hardware, or a combination thereof. Each of the above-mentioned modules can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory of the computer device in the form of software, so that the processor can call and execute the corresponding operations of each of the above modules.

[0140] In one embodiment, a computer device is provided. The computer device may be a distributed system, and its internal structure diagram may be as follows: Figure 14As shown. The computer device includes a processor, a memory, a communication interface, a display screen and an input device connected via a system bus. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The communication interface of the computer device is used to communicate with an external distributed system in a wired or wireless manner, and the wireless manner can be achieved through WIFI, a mobile cellular network, NFC (near field communication) or other technologies. When the computer program is executed by the processor, a code service call relationship generation method is implemented. The display screen of the computer device can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device can be a touch layer covering the display screen, or a button, trackball or touchpad provided on the computer device housing, or an external keyboard, touchpad or mouse.

[0141] Those skilled in the art will understand that Figure 14 The structure shown in the figure is only a block diagram of a part of the structure related to the scheme of the present application, and does not constitute a limitation on the computer device to which the scheme of the present application is applied. The specific computer device may include more or fewer components than shown in the figure, or combine certain components, or have a different arrangement of components. In one embodiment, a computer device is provided, including a memory and a processor, a computer program is stored in the memory, and the processor implements the above-mentioned code service call relationship generation method when executing the computer program. In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored, and the computer program implements the above-mentioned code service call relationship generation method when executed by the processor. In one embodiment, a computer program product is provided, including a computer program, and the computer program implements the above-mentioned code service call relationship generation method when executed by the processor.

[0142] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties.

[0143] Those skilled in the art will appreciate that all or part of the processes in the above-mentioned embodiment methods can be implemented by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, database or other media used in the embodiments provided in this application may include at least one of non-volatile and volatile memory. Non-volatile memory may include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory may include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in the various embodiments provided herein may include at least one of a relational database and a non-relational database. Non-relational databases may include, but are not limited to, distributed databases based on blockchains. The processor involved in the various embodiments provided herein may be, but are not limited to, a general-purpose processor, a central processing unit, a graphics processing unit, a digital signal processor, a programmable logic unit, a data processing logic unit based on quantum computing, and the like.

[0144] The technical features of the above embodiments can be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0145] The above-described embodiments merely represent several implementation methods of the present application. While the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present application. It should be noted that a person of ordinary skill in the art may make various modifications and improvements without departing from the spirit of the present application, and these modifications and improvements fall within the scope of protection of the present application. Therefore, the scope of protection of the present application shall be determined by the appended claims.

Claims

1. A method for generating a code service call relationship, characterized in that: The method comprises: Obtaining a source code to be detected, performing syntax parsing on the source code to be detected, and obtaining a corresponding syntax parse tree; the syntax parse tree includes a plurality of nodes corresponding to a plurality of code modules of the source code to be detected, and the relationship between the nodes is determined based on the corresponding relationship between the plurality of code modules; Traversing the syntax parse tree, and determining a first relationship between the method declaration code module and at least one method call code module in its corresponding child node according to the method declaration code module and the method call code module in its corresponding child node in the syntax parse tree; Determining, based on the method declaration code modules in the child nodes corresponding to each class declaration code module in the grammar parse tree, and the method declaration code modules in the parent class declaration code module and the child class declaration code module corresponding to each class declaration code module, a second relationship between the method calling code module and its class and at least one method declaration code module in the corresponding parent class and child class; Determine, based on the class declaration code module and the property declaration code module in the grammar parse tree, the service party to which the method declaration code module and / or the method call code module in the class declaration code module belongs; the service party includes a service provider and a service consumer; the code module includes a method declaration code module and a method call code module; Based on the first relationship and the second relationship, a call relationship table between code modules in the source code to be detected is determined, and according to the service provider to which the code module belongs, the corresponding service provider identifier is added to the code module in the call relationship table to obtain a code service call relationship table corresponding to the source code to be detected.

2. The method according to claim 1, characterized in that The step of obtaining the source code to be detected and performing syntax parsing on the source code to be detected to obtain a corresponding syntax parse tree includes: Detecting the declaration code module in the source code to be detected; the declaration code module includes an attribute declaration code module and a class declaration code module; Detecting the attribute information code module in the attribute declaration code module, and detecting the class information code module, the method declaration code module, and the method call code module corresponding to the method declaration code module in the class declaration code module; The project where the source code to be detected is located is taken as the root node, the attribute declaration code module, the attribute information code module and the class declaration code module are taken as parent nodes, the class information code module and the method declaration code module are taken as child nodes of the class declaration code module, and the method call code module is taken as the child node of the method declaration code module to obtain the syntax parse tree corresponding to the source code to be detected.

3. The method according to claim 2, characterized in that The determining, based on the method declaration code module in the grammar parse tree and the method call code module in the corresponding child node, a first relationship between the method declaration code module and at least one method call code module in the corresponding child node, includes: Obtaining a method declaration code module in the grammar parse tree, detecting a subtree contained in the method declaration code module, and obtaining a method call code module in the subtree; A first relationship between the method declaration code module and the method call code module in the subtree is established.

4. The method according to claim 2, characterized in that The determining, based on the method declaration code modules in the child nodes corresponding to each class declaration code module in the grammar parse tree, and the method declaration code modules in the parent class declaration code module and the child class declaration code module corresponding to each class declaration code module, of a second relationship between the method calling code module and its class and at least one method declaration code module in the corresponding parent class and child class, includes: Obtaining a method declaration code module in a subtree of each class declaration code module in the grammar parse tree, and if it is detected that method implementation code exists in the method declaration code module in the subtree, establishing a second relationship between a method call code module corresponding to the method declaration code module in the subtree and the current class of the method declaration code module in the subtree; Determine the parent-child relationship of each class declaration code module, and obtain the parent class declaration code module and the child class declaration code module corresponding to each class declaration code module in the syntax parse tree; For each class declaration code module, if it is detected that a parent method declaration code module containing method implementation code exists in the parent class declaration code module corresponding to the class declaration code module, a parent class second relationship is established between the first target method calling code module corresponding to the class declaration code module and the parent method declaration code module; the method name of the first target method calling code module is the same as the method name of the parent method declaration code module; If it is detected that a sub-method declaration code module containing method implementation code exists in the sub-class declaration code module corresponding to the class declaration code module, a sub-class second relationship is established between the second target method calling code module corresponding to the class declaration code module and the sub-method declaration code module; the method name of the second target method calling code module is the same as the method name of the sub-method declaration code module; According to the second relationship of the current class, the second relationship of the parent class and the second relationship of the child class, the second relationship between the method calling code module and its class and at least one method declaration code module in the corresponding parent class and child class is obtained.

5. The method according to claim 2, characterized in that The determining, based on the class declaration code module and the property declaration code module in the grammar parse tree, the service party to which the method declaration code module and / or the method call code module in the class declaration code module belongs includes: Obtaining a class information code module in a child node of a class declaration code module in the grammar parse tree; If the class information corresponding to the class information code module is a service provider, determining that the method declaration code module corresponding to the class declaration code module belongs to the service provider; and / or, Obtaining an attribute information code module corresponding to an attribute declaration code module in the grammar parse tree; If the attribute information corresponding to the attribute information code module includes a service consumer, and the calling subject of the method calling code module in the syntax parse tree is the attribute information, it is determined that the method calling code module belongs to the service consumer.

6. The method according to claim 1, characterized in that Determining a call relationship table between code modules in the source code to be detected based on the first relationship and the second relationship includes: Determining a first association relationship between the method declaration code module and the method call code module based on the first relationship; Determining a second association relationship between the method calling code module and the method declaring code module according to the second relationship; If the method declaration code module corresponding to the method calling code module in the second association relationship exists in the first association relationship, establishing a third association relationship between the method declaration code module corresponding to the method calling code module in the first association relationship and the method declaration code module corresponding to the method calling code module in the second association relationship; A calling relationship table between code modules is determined based on the first association relationship, the second association relationship, and the third association relationship.

7. The method according to claim 1, characterized in that According to the service provider to which the code module belongs, the corresponding service provider identifier is added to the code module in the call relationship table to obtain the code service call relationship table corresponding to the source code to be detected, including: For each method declaration code module in the call relationship table, if the method declaration code module belongs to a service provider and the method declaration code module does not have a parent association relationship in the call relationship table, add a service provider identifier to the method declaration code module; For each method calling code module in the calling relationship table, if the method calling code module belongs to a service consumer and there is no child association relationship for the method calling code module in the calling relationship table, add a service consumer identifier to the method calling code module; According to the call relationship table after adding the service provider identifier and the service consumer identifier, a code service call relationship table is obtained.

8. The method according to claim 1, characterized in that After obtaining the code service call relationship table corresponding to the source code to be detected, the method further includes: Receive code service call relationship query information, and obtain the project identifier, class identifier, and method identifier in the code service call relationship query information; Querying the corresponding project according to the project identifier, querying the corresponding class name in the project according to the class identifier, and querying the corresponding method name in the class corresponding to the class name according to the method identifier; A code service call relationship table containing the method name is obtained as a query result.

9. A device for generating a code service call relationship, used to implement a method for generating a code service call relationship according to claim 1, characterized in that: The device comprises: an acquisition module, configured to acquire a source code to be detected, perform syntax parsing on the source code to be detected, and obtain a corresponding syntax parse tree; the syntax parse tree includes a plurality of nodes corresponding to a plurality of code modules of the source code to be detected, and the relationship between the nodes is determined based on the corresponding relationship between the plurality of code modules; a traversal module configured to traverse the grammar parse tree, obtain a first relationship between a method declaration code module and at least one method call code module in its corresponding child node, a second relationship between a method call code module and its class and at least one method declaration code module in its corresponding parent class and child class, and a service provider to which each code module belongs; the service provider includes a service provider and a service consumer; and the code module includes a method declaration code module and a method call code module; A generation module is used to determine a call relationship table between code modules in the source code to be detected based on the first relationship and the second relationship, and add corresponding service provider identifiers to the code modules in the call relationship table based on the service providers to which the code modules belong, to obtain a code service call relationship table corresponding to the source code to be detected.

10. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 8 are implemented.

11. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 8 are implemented.

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

Citation Information

Patent Citations

  • Code structure view analyzing and previewing method

    CN114527989A

  • System and method for source code translation using stream expressions

    US20200285454A1