Code processing method and device and electronic equipment

By building a type inferencer and a dynamic non-intrusive method, we can accurately determine and delete the set of non-running methods, solve the problems of timeout and low efficiency in redundant code processing, and achieve code slimming and analysis accuracy.

CN118838580APending Publication Date: 2024-10-25BEIJING AUTONAVI YUNMAP TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410869660.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-28
Publication Date
2024-10-25

AI Technical Summary

Technical Problem

When the existing technology handles redundant code, it causes the Java virtual machine to stop and generate a large amount of boilerplate code after compilation, which affects the efficiency of analysis and slimming. Moreover, direct analysis of the source code is unreliable, resulting in missing information and incomplete analysis.

Method used

By building a type inferencer to analyze the formal parameter types of the methods in the source code file of the target object, determine the full method set, and obtain the running methods dynamically and non-intrusively, avoiding the generation of time stops and boilerplate code, thereby accurately determining the unrun methods Collection is deleted.

Benefits of technology

It achieves code slimming, reduces redundant code, improves analysis accuracy and efficiency, and avoids the generation of timeouts and boilerplate code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118838580A_ABST
    Figure CN118838580A_ABST
Patent Text Reader

Abstract

The invention discloses a code processing method which is used for obtaining a corresponding operation method set when a target object operates in order to realize code slimming. Meanwhile, the form parameter type of the method in the source code file of the target object is analyzed by utilizing a type inference device, and a first full-amount method set corresponding to the target object is determined based on the form parameter type. A non-running method set, namely a redundant code set, can be determined through a difference set of the two sets. And deleting full content or partial content in the non-running method set from the source code file so as to delete useless codes and realize code slimming. When the full-quantity method set is collected, the source code file is used as the basis, and compiled codes are not needed, so that sample codes generated by compiling are reduced, and the analysis quantity is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer processing technology, and in particular to a code processing method, device and electronic equipment. Background Art

[0002] As applications continue to iterate, a large amount of redundant code will be generated. This will bring a huge workload for developers to analyze the code, and will also cause efficiency loss in the compilation and deployment of the application. Based on this, in order to improve R&D efficiency and reduce the redundancy of the services provided by the application, it is necessary to deal with the redundant code in a timely manner. Summary of the invention

[0003] In view of this, the present application provides a code processing method, device and electronic device to delete redundant code, reduce the amount of code in the application, and achieve a code slimming effect.

[0004] To solve the above problems, the technical solutions provided by this application are as follows:

[0005] In a first aspect of the present application, a code processing method is provided, the method comprising:

[0006] Obtaining a set of running methods corresponding to the target object when it is running, wherein the target object is a target application or a target functional module in the target application;

[0007] Determine the parameter type of the method in the source code file corresponding to the target object by using a type inferencer, and determine a first full method set corresponding to the target object based on the parameter type, wherein the parameter type is used to determine the method included in the source code file; the type inferencer infers the parameter type of the method in the source code file based on a third-party library compiled from the source code file;

[0008] Determining a non-running method set based on the running method set and the first full method set;

[0009] The entire content or a portion of the content in the set of unexecuted methods is deleted from the source code file.

[0010] In a second aspect of the present application, a code processing device is provided, the device comprising:

[0011] An acquisition unit, used for acquiring a set of operation methods corresponding to the operation of a target object, wherein the target object is a target application or a target functional module in the target application;

[0012] a determining unit, configured to determine, by using a type inferencer, a parameter type of a method in a source code file corresponding to the target object, and determine a first full method set corresponding to the target object based on the parameter type, wherein the parameter type is used to determine the method included in the source code file; the type inferencer infers the parameter type of the method in the source code file based on a third-party library compiled from the source code file;

[0013] The determining unit is further configured to determine a non-running method set based on the running method set and the first full method set;

[0014] The deleting unit is used to delete all or part of the contents in the set of unexecuted methods from the source code file.

[0015] In a third aspect of the present application, an electronic device is provided, comprising: one or more processors; a memory on which one or more programs are stored;

[0016] When the one or more programs are executed by the one or more processors, the one or more processors implement the code processing method described in the first aspect.

[0017] In a fourth aspect of the present application, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the code processing method described in the first aspect is implemented.

[0018] In a fifth aspect of the present application, a computer program product is provided. When the computer program product is run on a computer, the computer is enabled to implement the code processing method described in the first aspect.

[0019] It can be seen that this application has the following beneficial effects:

[0020] The present application provides a code processing method for achieving code slimming and obtaining a corresponding set of running methods when a target object is running. At the same time, the parameter type of the method in the source code file of the target object is analyzed by using a type inferencer, and the first full method set corresponding to the target object is determined based on the parameter type. The non-running method set, that is, the redundant code set, can be determined by the difference between the two sets. The full content or part of the content in the non-running method set is deleted from the source code file to delete useless code and achieve code slimming. Since the source code file is used as the basis for collecting the full method set, there is no need to rely on the compiled code, which reduces the sample code generated by compilation and reduces the amount of analysis. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] Figure 1 A flow chart of a code processing method provided in an embodiment of the present application;

[0022] Figure 2a A schematic diagram of a collection and operation method provided in an embodiment of the present application;

[0023] Figure 2b A schematic diagram of an installation collection module provided in an embodiment of the present application;

[0024] Figure 3a A schematic diagram of a method for collecting full data provided in an embodiment of the present application;

[0025] Figure 3b A logical diagram of a database update of a full-scale method provided in an embodiment of the present application;

[0026] Figure 3c A logical diagram of a deleted method database update provided in an embodiment of the present application;

[0027] Figure 4a A schematic diagram of determining a method that has not been run is provided in an embodiment of the present application;

[0028] Figure 4b A schematic diagram of marking a method that has not been run provided in an embodiment of the present application;

[0029] Figure 5 A code processing framework diagram provided for an embodiment of the present application;

[0030] Figure 6 A schematic diagram of the structure of a code processing device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0031] In order to make the above-mentioned objects, features and advantages of the present application more obvious and easy to understand, the embodiments of the present application are further described in detail below in conjunction with the accompanying drawings and specific implementation methods.

[0032] At present, the main methods for obtaining redundant code of an application are: collecting running methods through the debugger SA-JDI that comes with the Java language software development kit (Java Development Kit, JDK), and obtaining all methods through java-call-all-graph analysis of the compiled code, and then determining the non-running methods based on the full methods and the running methods. The code corresponding to the non-running method is the redundant code.

[0033] However, when using the JDK's built-in debugger to collect running methods, the Java Virtual Machine (JVM) will experience time-stopping, which will affect online services. At the same time, since the acquisition of the full method requires the use of compiled code, and compilation will generate a large amount of boilerplate code (generating a large number of methods not included in the source code), additional elimination and deduplication operations are required during full analysis, affecting the efficiency of analysis and weight loss. Time-stopping refers to the situation where a running application suddenly stops working or crashes.

[0034] In the process of implementing this application, the inventor found that the direct analysis of the source code is unreliable, which will cause information omissions, resulting in incomplete analysis, and thus affecting the accuracy of the obtained full method. On this basis, those skilled in the art will only obtain the full method by analyzing the compiled code, and will not use the source code to obtain the full method.

[0035] The inventor of the present application creatively uses the source code to obtain all methods. Specifically, the constructed type inferencer is used to analyze the parameter types of the methods in the source code file of the target object, and then all methods included in the source code file corresponding to the target are determined based on the analyzed parameter types, and then the methods that have not yet been run in the source code file are determined based on all methods and running methods, that is, the redundant code to be cleaned up. That is, the type inferencer constructed by the present application can determine the parameter types of all methods in the source code file, thereby avoiding missing references to methods defined by external classes and improving the accuracy of the analysis. In addition, the corresponding running methods when the target object is running can be obtained through a dynamic and non-intrusive method to avoid affecting the operation of the target object.

[0036] The technical terms involved in this application are explained below. Unless otherwise specified, the following technical terms shall be based on the definitions in this application.

[0037] 1. Java: Java is a widely used high-level, object-oriented programming language. Java is designed with the philosophy of "write once, run anywhere", thanks to the JVM, which allows Java programs to run on different operating systems and platforms without the need for separate compilation for each platform.

[0038] 2.JVM-Sandbox: It is a JVM non-intrusive runtime code instrumentation enhancement and online monitoring tool. It is mainly used to enhance the code functions without modifying the application code, such as monitoring method calls, exception capture, etc. It is often used in scenarios such as fault location, performance monitoring, and security auditing.

[0039] 3. JavaParser: is an open source Java source code parsing library that can parse Java code and generate an abstract syntax tree (AST). It can analyze and manipulate Java code without compiling the code, and is often used in scenarios such as static code analysis, code generation, and code migration.

[0040] 4.SonarQube: is an open source code quality management platform for continuous detection of code quality, including error detection, code smells, code complexity analysis, test coverage, duplicate code detection, etc. It supports multiple programming languages ​​and can be integrated into the continuous integration (CI) process to help developers find and fix code problems.

[0041] 5.Sonar-PMD-Rule: Sonar-PMD is a plug-in on the SonarQube platform that integrates PMD (a static source code analysis tool) and allows users to use PMD rules to check code in SonarQube. A rule is a condition for code analysis, and Sonar-PMD-Rule refers to the code analysis standard defined by PMD rules.

[0042] It should be noted that in this application, the following two principles are mainly used: one is to uniquely identify a method through package name, class name, method name and parameter type list, that is, package name + class name + method name + parameter type list is used as the method identifier of the method; the other is that since it is impossible to directly obtain the unrun method, the "unrun method" can be obtained by subtracting the "running method" from the "full method".

[0043] Based on the above principles, the technical solution provided by this application will be described below in conjunction with the accompanying drawings.

[0044] See also Figure 1 , which is a flow chart of a code processing method provided in an embodiment of the present application, such as Figure 1 As shown, the method includes:

[0045] S101: Obtain a set of running methods corresponding to the target object when it is running.

[0046] The target object refers to an application to be slimmed down (target application) or a certain functional module in the application (target functional module).

[0047] When acquiring the corresponding operation method set when the target object is running, the operation environment can be pre-configured to use the relevant data in the operation environment to run the target object. It should be noted that the operation parameters provided in different operation environments are different, but the collected operation method set is the same.

[0048] Among them, the operating environment refers to the environment corresponding to the running target object. Among them, the operating environment can include the daily environment (the environment for developing or testing applications), the production environment (the environment in which the application runs in real user scenarios after it is officially released, involving real user browsing and data), the grayscale environment (it is a partially productionized environment used to release the application's functions to a small number of precise users before the application is officially released, in order to optimize the experience and test the stability of the functions), etc. In actual applications, users can choose the operating environment according to their own needs so that they can collect the operating methods in this operating environment. Among them, the operating method refers to the method that has been run, which can also be called the run method.

[0049] In this embodiment, when it is necessary to slim down the target object, the target object can be registered in the system that implements the code slimming function by registration. Specifically, the target object's identifier and code address are input on the registration page of the code slimming system. After completing the registration of the above information, in response to the collection operation triggered by the user on the control page of the code slimming system, the methods that have been run when the target object is running can be collected in a dynamic and non-intrusive manner, thereby obtaining a set of running methods. That is, the running methods are collected without affecting the running process of the target object to avoid causing a time stop to it. Among them, the running method set can include the method identifier of the running method, and a method can be uniquely identified by the method identifier.

[0050] The collection of the operation method set by the dynamic non-intrusive method may include the following implementations:

[0051] One is that if the target object supports plug-ins, the set of running methods can be collected by loading the target plug-in, wherein the function of the target plug-in is to collect the running methods when the target object is running.

[0052] Another method is to collect the set of running methods by mounting a running agent. Specifically, the running agent can be mounted when the target object is running, and the collection method is executed through the running agent, and the collection method is used to record the methods run when the target object is running. After the mounting is completed, the collection method is called by the running agent to obtain the corresponding running method set when the target object is running. That is, this embodiment realizes the collection of running methods without business intrusion by dynamically mounting the running agent. Among them, the running agent can be understood as a proxy server for running the target object, thereby allowing the target object to be controlled and monitored.

[0053] For example, the collection method is injected into the JVM through JVM-SandBox, and the "package name and class name, method name, parameter type list" are collected before the method is executed and reported to the Kafka queue. Injection usually refers to the process of dynamically injecting code into the executing Java program or modifying its behavior when the Java virtual machine (JVM) is running. Kafka is a distributed message queue (Message Queue) based on the publish / subscribe model, which is mainly used for real-time data processing.

[0054] Specifically, the collection method is called based on the running agent to obtain the corresponding running method set when the target object is running through the collection method, including: calling the collection method based on the running agent to register a listener in the target object; if the target object runs a method, the registered listener records the method identifier corresponding to the method in the method queue. That is, the running method is monitored by registering a listener.

[0055] For example, Figure 2a The running method collection schematic diagram shown in the figure, wherein the collection method is implemented by the code slimming collection module, which is used to collect the running method, and the code slimming backend executes the code slimming function. When the target object is running on the JVM, that is, for the business JVM instance, the running agent javaagent is mounted, and the code slimming collection module in the SandBox-Module directory is called through the running agent javaagent to collect the running method through the code slimming collection module. Figure 2a The module in is the code slimming collection module.

[0056] Among them, the implementation process of the code slimming collection module includes: using the watch command to register the listener Listener in the JVM, and executing the Before method before the method of the target object is executed to achieve the purpose of recording the method, and write the method as the running method into the internal queue Queue. The reporting thread obtains the running method from the internal queue Queue and reports it to KafkaQueue. The code slimming backend reads the running method from KafkaQueue and records it. Among them, the Watch command is used to specify the object to be monitored, and the Before method is used to declare that the method to be executed is recorded before the monitored method is executed. In this way, the running method can be collected without disrupting the execution process of the target object.

[0057] Among them, the process of installing the code slimming collection module is as follows Figure 2bAs shown, it includes: 1. Modify setenv.sh, mount and run the agent javaagent in the corresponding directory; 2. Download the code slimming collection module to the SandBox-Module directory; 3. Restart the target object to make the code slimming collection module effective. Among them, setenv.sh is a script file for setting environment variables. By modifying this file, the agent javaagent can be mounted and run in the corresponding directory.

[0058] Another option is that if the target object supports the microservice architecture, the application corresponding to the target object can be split into multiple independent microservices, each of which focuses on a specific business function. Microservices can communicate with each other through the Application Programming Interface (API), and each microservice can be deployed independently without affecting the operation of other microservices. Based on this, the function of the collection and operation method can be deployed as a microservice.

[0059] S102: Determine the parameter type of the method in the source code file corresponding to the target object by using a type inferencer, and determine a first full method set corresponding to the target object based on the parameter type.

[0060] In this embodiment, the source code file can be relied upon to collect the full method corresponding to the target object. That is, when analyzing the full method, the source code file is analyzed, avoiding the extra work caused by analyzing the compiled code (compiled source code will generate a large number of methods not included in the source code), thereby improving the analysis efficiency.

[0061] Since the location of the method depends on the parameter type, but in some application scenarios, the same parameter is defined in different packages. In order to determine the specific source of the parameter in the method, type inference is required. To achieve type inference, a type inferencer is first constructed so that the type inferencer can be used to analyze the parameter type of the method in the source code file, and then, when the parameter type is obtained, all the methods included in the source code file are determined to obtain the first full method set. Among them, the type inferencer can determine the parameter type of each method in the source code file, and then obtain the method identifier of the method in the source code file according to the parameter type of the method. The method identifier includes the class name of the method, the method name, the package name of the package to which the method belongs, and the parameter type. Different methods have different method identifiers; then all methods included in the source code file corresponding to the target object are determined according to the method identifier, and the first full method set is obtained. Among them, the parameter type is used to determine the method included in the source code file, and the type inferencer infers the parameter type of the method in the source code file based on the third library compiled from the source code file.

[0062] The process of building a type inferencer includes: obtaining configuration information corresponding to the type inferencer, which includes a build command and a product directory; compiling the source code file corresponding to the target according to the build command to obtain a third-party library; and building a type inferencer according to the type inference capability of the preset code compiler, the type inference capability of the source code, and the type inference capability of the third-party library. In other words, the constructed type inferencer includes the native inference capability of the JDK, the inference capability of the source code, and the inference capability of the third-party library. Based on this, the type inferencer can accurately determine the parameter types of all methods in the source code file corresponding to the target object.

[0063] For example, Figure 3a The full-quantity method analysis flow chart shown includes:

[0064] Step 1: Read the configuration file.

[0065] The configuration file includes a build command and a product directory. The build command refers to a command line instruction or script used to compile, package, test and deploy a software project. In this embodiment, the build command is a compile command, which is used to compile the source code of the target application to obtain the compiled file. The product directory refers to the storage location of the output file generated during the build process. In this embodiment, it refers to the storage location of the compiled file.

[0066] Step 2: Execute the build command to compile the source code file and obtain the compiled file.

[0067] Step 3: Get the Jar package path according to the product directory.

[0068] That is, the compiled file Jar package path is obtained according to the product directory, so as to obtain the third-party library through the path.

[0069] Step 4: Get the Java source code directory, that is, get the source code file.

[0070] Step 5: Generate a type inferencer.

[0071] The type inferencer includes three aspects: JDK native inference capability (i.e. the so-called type inference capability of the code compiler), source code inference capability, and third-party library inference capability. JDK native inference refers to the introduction of local variable type inference capability in the Java programming language, and source code inference refers to the ability to automatically infer missing or completed code snippets based on existing source code or other context information. JDK's native inference capability and source code inference capability can be obtained by calling JavaParser.

[0072] Step 6: Traverse the source code based on the type inferencer to obtain the full method set.

[0073] After obtaining the full method set through the above method, all methods in the full method set obtained by this analysis are stored in a full method database, and the full method database can record the method identifiers of all methods corresponding to the target object.

[0074] S103: Determine a non-running method set based on the running method set and the first full method set.

[0075] After determining the first full method set and the running method set, the non-running method set is determined based on the running method set and the first full method set. That is, the method identifiers included in the running method set are removed from the first full method set according to the method identifiers, and the methods indicated by the remaining method identifiers form the non-running method set.

[0076] It should be noted that since the running method is the method determined after the source code is compiled, and the full method is the uncompiled code, there may be different properties corresponding to the running method and the full method. For example, there is no generic in the running method, but there is a generic in the full method. In order to ensure the accuracy of the determined non-running method, before determining the non-running method, it is necessary to unify the properties of the running method and the properties of the full method. Specifically, for each method corresponding to the first full method set, the generics of the method are removed to obtain the updated first full method set; for the anonymous inner class of the method in the updated first full method set, the anonymous inner class in the method is converted into an anonymous inner class that meets the preset rules to obtain the latest first full method set; the non-running method set is determined based on the latest first full method set and the running method set. Among them, the converted anonymous inner class and the anonymous inner class in the running method both meet the preset rules, and the preset rules are used to indicate the definition standard of the anonymous inner class.

[0077] Generally, the definition of the anonymous inner class in the full method parsed by the type inferencer does not follow the definition of the anonymous inner class of the Java virtual machine, while the anonymous inner class in the running method follows the definition of the anonymous inner class of the Java virtual machine. Based on this, the anonymous inner class in the full method needs to be converted into a unified definition. The full method refers to the method in the full method set.

[0078] Among them, generics are a language feature in programming languages ​​that enhances type safety and code reusability. By using generics, it is allowed not to specify specific data types when defining classes, interfaces, and methods, and to specify specific types when they are used. This not only avoids type conversion errors, but also improves the flexibility and reusability of the code. Anonymous inner classes are a way to define classes while declaring and creating objects. There is no displayed class name, and they can be defined and instantiated where they are used.

[0079] For example, the run method contains three elements: array, no generics, and an anonymous inner class of $1; the full method contains three elements: array, generics, and an anonymous inner class of Anonymous. The full method needs to remove the generics and convert Anonymous to $1.

[0080] It should be noted that the order of removing generics and converting anonymous inner classes in all methods is not subject to the above restrictions. You can convert anonymous inner classes first and then remove generics. Among them, you can remove generics and convert anonymous inner classes by regular expressions.

[0081] For example, run the method sample: org.example.User$1.test(List,Map);

[0082] Full method example: org.example.User.Anonymous-${a random Hash

[0083] Value}.test(List <List <user>>,Map <String,Map<String,String> >)

[0084] The two cannot be matched directly. Adjust the format of the "full method" in two steps:

[0085] 1. Anonymous inner class is converted to $1 (e.g. .Anonymous-${Hash} is changed to $1)

[0086] 2. Erasing generics (e.g. List <List <user>>,Map <String,Map<String,String> >Convert to List, Map)

[0087] The final full method can be changed to: org.example.User$1.test(List,Map).

[0088] When multiple anonymous inner classes appear in a full method, in order to ensure the accuracy of the conversion, the anonymous inner classes will be converted in the order in which they appear in the file to obtain the converted anonymous inner classes. For example, if three anonymous inner classes appear in the full method in sequence, they will be converted to $1, $2, and $3 in the order in which they appear. If $1 and $2 appear in the running method, then $3 exists in the unrunning method.

[0089] S104: Deleting all or part of the contents in the unexecuted method set from the source code file.

[0090] In this embodiment, after determining the set of unrun methods, all or part of the content in the set of unrun methods can be deleted immediately to achieve code slimming. Alternatively, the unrun methods can be displayed to the user so that the user can select the unrun methods to be deleted according to actual needs, thereby avoiding accidental deletion of unrun but actually useful code and ensuring the normal operation of the target object's service.

[0091] It can be seen that when collecting the full set of methods, the source code file is used as the basis, and there is no need to rely on the compiled code, which reduces the sample code generated by compilation and does not require additional elimination and deduplication operations, thereby improving analysis efficiency. When collecting running methods, a dynamic and non-intrusive approach can be used to avoid affecting the operation of the target object.

[0092] After the operation of S104 is performed here, the source code corresponding to the target object can be re-analyzed to obtain a second full method set. The data in the full method database is updated based on the comparison between the first full method set and the second full method set. Among them, the full method database is used to store the latest determined full methods. Specifically, the target method is determined by comparing the first full method set and the second full method set, and the target method includes the unrunning method deleted from the source code file, and / or the new method added after deleting the full content or part of the content in the unrunning method set from the source code file. The specific update process includes:

[0093] If the target method is in the first full method set and not in the second full method set, the target method is deleted from the full method database. In other words, if the target method exists in the full method database but does not exist in the second full method set obtained by the latest analysis, it indicates that the target method belongs to the method to be deleted, and it is deleted from the full method database.

[0094] If the target method is in the second full method set and not in the first full method set, the target method is added to the full method database. In other words, if the target method does not exist in the full method database, but exists in the second full method set obtained by the latest analysis, indicating that the target method is a method to be added, it will be added to the full method database.

[0095] Among them, the update logic of the full method database is as follows: Figure 3b The update diagram is shown. Among them, the current situation refers to the full method database, and the expectation refers to the full method set obtained through the latest analysis of the type inferencer. Among them, the method to be deleted refers to the method that exists in the "current situation" but not in the "expectation", and the method to be added refers to the method that exists in the "expectation" but not in the "current situation". For the method to be deleted, the deletion operation is performed on the full method database, and for the method to be added, the addition operation is performed on the full method database. For example, the first analysis obtains a full method data set including method A, method B and method C, then the full method database includes method A, method B and method C. The second analysis obtains a full method data set including method B, method C and method D, then method A is the method to be deleted, and method D is the method to be added.

[0096] At the same time, the deleted method database can also be updated. The deleted method database is used to record the methods that have been deleted. Specifically, in response to deleting a target method from the full method database and the target method does not exist in the deleted method database, the target method is added to the deleted method database. That is, if the target method deleted from the full method database does not exist in the deleted method database, it is necessary to record the target method in the deleted method database so that the deleted method database can be used to know that the target method has been deleted.

[0097] In response to the target method being added to the full method database and the target method existing in the deleted method database, the target method is deleted from the deleted method database. That is, if the target method newly added to the full method database exists in the deleted method database, the record for the target method needs to be deleted from the deleted method database.

[0098] For updates on the deleted method database, see Figure 3c The update logic shown. If the "method to be deleted" in the full method database does not exist in the deleted method database, then add the "method to be deleted" to the deleted method database; if it exists in the deleted method database, no operation is required. If the "method to be added" in the full method database exists in the deleted method database, then delete the "method to be added" from the deleted method database; if it does not exist in the deleted method database, no operation is required.

[0099] For example, if the first analysis updates the full method database including method A, method B, and method C, there is no record in the deleted method database. The second analysis obtains the full method set of method B, method C, and method D, then method A needs to be deleted from the full method database, which now includes method B, method C, and method D, and method A is added to the deleted method database. If the third analysis obtains the full method set of method A, method C, and method D, then method A is deleted from the deleted method database, and method B is added to the deleted method database.

[0100] Among them, the update logic for the unrunning method is as follows Figure 4a As shown. In this figure, "Current Status" refers to the database of unrun methods, and "Expectation" refers to the latest set of unrun methods. Before determining the set of unrun methods, first perform generic erasure on all methods (i.e. remove the angle brackets) and convert the anonymous inner class to $1, and then determine the unrun methods based on all methods and running methods. If the latest unrun method exists in the "Current Status" but not in the "Expectation", the unrun method belongs to the method to be deleted, that is, it is necessary to delete the method from the database of unrun methods. If the latest unrun method does not exist in the "Current Status" but exists in the "Expectation", the unrun method needs to be added to the database of unrun methods. The unrun method database is used to record unrun methods.

[0101] In some scenarios, in order to facilitate users to quickly know which methods are not run methods when browsing the source code, the non-run code can be marked in the source code to improve the viewing experience. Specifically, after determining the non-run method set, before deleting all or part of the content in the non-run method set from the source code file, for any non-run method in the non-run method set, remove the array and / or package name corresponding to the non-run method to obtain an updated non-run method set; convert the internal class name in the preset code location rule to the preset internal class name to obtain an updated code location rule; based on the updated code location rule and the updated non-run method set, mark the non-run code in the source code corresponding to the target object. Among them, the mark is used to identify the non-run code, and when the user browses the source code file, the mark can be used to quickly identify which code is not run. The preset internal class name is the internal class name defined in the non-run method.

[0102] Among them, the code location rules include the rules that the method needs to comply with when locating the method. Specifically, the code location rules indicate that the method has no arrays, no generics, no package names, the inner class name is "$", and the anonymous inner class must comply with the preset rules. The unrun method usually has arrays, no generics, a package name, an inner class name of ".", and an anonymous inner class that complies with the preset rules. Since the expression form of the unrun method is different from the expression form of the method that can be identified by the code location rules, the two will be unified to achieve accurate parsing. Specifically, for the unrun method, the package name corresponding to the method and the array contained will be removed; for the code location rules, the inner class name will be converted to the preset inner class name, which makes it easier to identify the unrun method so that the unrun method can be marked in the source code.

[0103] For example, the method that is not executed is org.example.User.Person.speak(org.example.Name[]), and the method that can be identified by the code location rule is org.example.User$Person.speak(Name). After conversion, the above two methods are unified into org.example.User.Person.speak(Name).

[0104] For the implementation of locating the unexecuted methods, see Figure 4b As shown. Among them, the unrunning methods include arrays, no generics, package names, inner class names ".", and anonymous inner class names $; the code location rule PMD can identify methods without arrays, no generics, no package names, inner class names "$", and anonymous inner class names $. Two steps will be performed for the unrunning methods: remove arrays and remove package names; the PMD rule will perform one step: replace the inner class name from "$" to ".".

[0105] To understand the overall implementation process of this application, see Figure 5 The code processing framework diagram shown in the figure. The schematic diagram includes 4 functional modules, namely 1. Collection end (running method collection); 2. Scanning end (full method analysis); 3. Core end (determination of unrunning methods), which reads the run methods from the Kafka queue through the Kafka consumer Consumer, obtains the full method from the scanning end, and calculates the unrunning methods through the adaptation layer; 4. Third party (SonarQube) (pull the unrunning methods and mark the unrunning methods in the source code). It should be noted that the specific implementation of each of the above functional modules can refer to the relevant description in the above method example.

[0106] Based on the above method, the present application provides a code processing device, which will be described below in conjunction with the accompanying drawings.

[0107] See also Figure 6 , which is a structural diagram of a code processing device provided in an embodiment of the present application, such as Figure 6 As shown, the device 600 may include: an acquiring unit 601 , a determining unit 602 , and a deleting unit 603 .

[0108] The acquisition unit 601 is used to acquire a set of operation methods corresponding to the operation of a target object, wherein the target object is a target application or a target function module in the target application;

[0109] A determining unit 602 is configured to determine the parameter type of the method in the source code file corresponding to the target object by using a type inferencer, and determine a first full method set corresponding to the target object based on the parameter type, wherein the parameter type is used to determine the method included in the source code file; the type inferencer infers the parameter type of the method in the source code file based on a third-party library compiled from the source code file;

[0110] The determining unit 602 is further configured to determine a non-running method set based on the running method set and the first full method set;

[0111] The deleting unit 603 is used to delete all or part of the contents in the unexecuted method set from the source code file.

[0112] In some embodiments, the determination unit 602 is specifically used to obtain the method identifier of the method in the source code file based on the formal parameter type, the method identifier includes the class name of the method, the method name, the package name of the package to which the method belongs, and the formal parameter type, and different methods correspond to different method identifiers; determine the first full method set corresponding to the target object according to the method identifier.

[0113] In some embodiments, the construction process of the type inferencer includes: obtaining configuration information corresponding to the construction of the type inferencer, the configuration information including a construction command and an artifact directory, the construction command is used to trigger the compilation of the source code file of the target object, and the artifact directory is used to store the compiled files; compiling the source code file of the target object according to the construction command to obtain a third-party library; and constructing the type inferencer according to the type inference capability of a preset code compiler, the type inference capability of the source code, and the type inference capability of the third-party library.

[0114] In some embodiments, the acquisition unit 601 is specifically used to dynamically mount an operation agent in response to the operation of the target object; the operation agent is used to execute a collection method, and the collection method is used to record the methods run when the target object is running; the collection method is called based on the operation agent to obtain the operation method set of the target object through the collection method.

[0115] In some embodiments, the determination unit 602 is specifically used to remove the generic type of each method corresponding to the first full method set to obtain an updated first full method set; for the anonymous inner class of the method existing in the updated first full method set, convert the anonymous inner class in the method into an anonymous inner class that meets the preset rules to obtain the latest first full method set; based on the latest first full method set and the running method set, determine the non-running method set. The converted anonymous inner class in the full method and the anonymous inner class in the running method both meet the preset rules, and the preset rules indicate the definition standard of the anonymous inner class.

[0116] In some implementations, after determining the unexecuted method set and before deleting all or part of the contents in the unexecuted method set from the source code file, the apparatus further includes: a processing unit;

[0117] The processing unit is used to remove the array and package name corresponding to any non-running method in the non-running method set, and obtain an updated non-running method set, wherein the non-running method has a package name and the non-running method contains an array; convert the internal class name in the preset code location rule into the preset internal class name, and obtain the updated code location rule; based on the updated code location rule and the updated non-running method set, mark the non-running code in the source code corresponding to the target object; the mark is used to indicate the non-running code. The code location rule includes the internal class name, and the preset internal class name is the internal class name defined in the non-running method.

[0118] In some embodiments, the device further comprises: an adding unit;

[0119] The determining unit 602 is further configured to determine, after deleting all or part of the contents in the non-executed method set from the source code file, a second full method set corresponding to the target object based on the source code file corresponding to the target object and the type inferencer; determine a target method, the target method including the non-executed method deleted from the source code file and / or a new method added after deleting all or part of the contents in the non-executed method set from the source code file;

[0120] The deleting unit 603 is further configured to delete the target method from a full method database if the target method is located in the first full method set and is not located in the second full method set; the full method database stores all methods in the source code file corresponding to the target object;

[0121] The adding unit is used to add the target method to the full method database if the target method is located in the second full method set and is not located in the first full method set.

[0122] In some embodiments, the adding unit is further configured to, in response to deleting the target method from the full method database and the target method not existing in the deleted method database, add the target method to the deleted method database; the deleted method database stores all deleted methods;

[0123] The deleting unit 603 is further configured to delete the target method from the deleted method database in response to adding the target method to the full method database and the target method existing in the deleted method database.

[0124] It should be noted that the specific implementation of each unit in this embodiment can refer to the relevant description in the above method embodiment. The division of units in the embodiment of the present application is schematic and is only a logical function division. There may be other division methods in actual implementation. The functional units in the embodiment of the present application can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. For example, in the above embodiment, the processing unit and the sending unit can be the same unit or different units. The above integrated units can be implemented in the form of hardware or in the form of software functional units.

[0125] Based on a code processing method provided in the above method embodiment, the present application also provides an electronic device, including: one or more processors; a memory, on which one or more programs are stored, when the one or more programs are executed by the one or more processors, the one or more processors implement the code processing method described in any of the above embodiments.

[0126] Based on a code processing method provided in the above method embodiment, an embodiment of the present application provides a computer-readable medium on which a computer program is stored, wherein when the program is executed by a processor, the code processing method as described in any of the above embodiments is implemented.

[0127] It should be noted that the computer-readable medium mentioned above in the present application may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present application, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present application, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. The computer readable signal medium may also be any computer readable medium other than a computer readable storage medium, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer readable medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (radio frequency), etc., or any suitable combination of the above.

[0128] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present application. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0129] The units involved in the embodiments described in this application may be implemented by software or hardware. The name of a unit / module does not limit the unit itself in some cases. For example, a voice data acquisition module may also be described as a "data acquisition module".

[0130] It should be noted that the various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments, and the same or similar parts between the various embodiments can be referred to each other. For the system or device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part description.

[0131] It should be understood that in the present application, "at least one (item)" means one or more, and "plurality" means two or more. "And / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" can mean: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following items" or similar expressions refers to any combination of these items, including any combination of single items or plural items. For example, at least one of a, b or c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or plural.

[0132] It should also be noted that the terms "comprises", "includes" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the presence of other identical elements in the process, method, article or device including the element.

[0133] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to the embodiments shown herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.< / user> < / user>

Claims

1. A code processing method, characterized in that: The method comprises: Obtaining a set of running methods corresponding to the target object when it is running, wherein the target object is a target application or a target functional module in the target application; Determine the parameter type of the method in the source code file corresponding to the target object by using a type inferencer, and determine a first full method set corresponding to the target object based on the parameter type, wherein the parameter type is used to determine the method included in the source code file, and the type inferencer infers the parameter type of the method in the source code file based on a third-party library compiled from the source code file; Determining a non-running method set based on the running method set and the first full method set; The entire content or a portion of the content in the set of unexecuted methods is deleted from the source code file.

2. The method according to claim 1, characterized in that The determining, based on the parameter type, a first full set of methods corresponding to the target object includes: Obtaining a method identifier of a method in the source code file based on the parameter type, wherein the method identifier includes a class name, a method name, a package name of a package to which the method belongs, and the parameter type, and different methods correspond to different method identifiers; A first full set of methods corresponding to the target object is determined according to the method identifier.

3. The method according to claim 1 or 2, characterized in that: The construction process of the type inferencer includes: Obtain configuration information corresponding to building the type inferencer, the configuration information including a build command and an artifact directory, the build command is used to trigger compilation of the source code file of the target object, and the artifact directory is used to store the compiled files; Compile the source code file of the target object according to the build command to obtain a third-party library; The type inferencer is constructed according to the type inference capability of the preset code compiler, the type inference capability of the source code, and the type inference capability of the third-party library.

4. The method according to claim 1, characterized in that The method of obtaining the corresponding running method set of the target object at runtime includes: In response to the target object running, mounting a running agent; the running agent is used to execute a collection method, and the collection method is used to record the methods that have been run when the target object is running; The collection method is called based on the running agent to obtain the running method set of the target object through the collection method.

5. The method according to claim 1, characterized in that Determining a non-running method set based on the first full method set and the running method set includes: For each method corresponding to the first full method set, remove the generic type of the method to obtain an updated first full method set; For the anonymous inner class of the method in the updated first full method set, convert the anonymous inner class in the method into an anonymous inner class that complies with a preset rule, and obtain the latest first full method set, wherein the converted anonymous inner class and the anonymous inner class in the running method both satisfy the preset rule, and the preset rule indicates a definition standard of the anonymous inner class, and the generic type and the anonymous inner class are attributes of the method; Based on the latest first full method set and the running method set, a non-running method set is determined.

6. The method according to claim 1, characterized in that After determining the unexecuted method set, and before deleting all or part of the contents in the unexecuted method set from the source code file, the method further includes: For any unexecuted method in the unexecuted method set, remove the array and / or package name corresponding to the unexecuted method to obtain an updated unexecuted method set, wherein the unexecuted method has a package name and / or the unexecuted method contains an array; Converting the internal class name in the preset code location rule into a preset internal class name to obtain an updated code location rule, wherein the code location rule includes an internal class name, and the preset internal class name is an internal class name defined in the non-running method; Based on the updated code location rule and the updated set of unexecuted methods, the unexecuted code is marked in the source code corresponding to the target object; the mark is used to identify the unexecuted code.

7. The method according to claim 1, characterized in that After deleting all or part of the contents in the set of unexecuted methods from the source code file, the method further includes: Determine, by using the type inferencer, a formal parameter type of a method in a source code file corresponding to the target object, and determine, based on the formal parameter type, a second full method set corresponding to the target object; Determine a target method, wherein the target method includes an unexecuted method deleted from the source code file and / or a new method added after deleting all or part of the contents in the unexecuted method set from the source code file; If the target method is in the first full method set and not in the second full method set, deleting the target method from a full method database, wherein the full method database stores all methods in the source code file corresponding to the target object; If the target method is located in the second full method set and is not located in the first full method set, the target method is added to the full method database.

8. The method according to claim 7, characterized in that The method further comprises: In response to deleting the target method from the full method database and the target method not existing in the deleted method database, adding the target method to the deleted method database; In response to adding the target method to the full method database and the target method existing in the deleted method database, deleting the target method from the deleted method database.

9. A code processing device, characterized in that: The device comprises: An acquisition unit, used for acquiring a set of operation methods corresponding to the operation of a target object, wherein the target object is a target application or a target functional module in the target application; a determining unit, configured to determine, by using a type inferencer, a parameter type of a method in a source code file corresponding to the target object, and determine a first full method set corresponding to the target object based on the parameter type, wherein the parameter type is used to determine a method included in the source code file, and the type inferencer infers the parameter type of the method in the source code file based on a third-party library compiled from the source code file; A determining unit, configured to determine a non-running method set based on the running method set and the first full method set; The deleting unit is used to delete all or part of the contents in the set of unexecuted methods from the source code file.

10. An electronic device, characterized in that: include: one or more processors; a memory having one or more programs stored therein, When the one or more programs are executed by the one or more processors, the one or more processors implement the code processing method as described in any one of claims 1 to 8.