Redundancy detection method, device, electronic device and readable storage medium
By dynamically comparing the loading classes and full classes of Java project files, and automatically marking and deleting redundant classes, the problem of low detection accuracy in the existing technology is solved, and efficient and accurate redundant class detection and processing is achieved.
Patent Information
- Application Number
- CN202110575121.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-05-25
- Publication Date
- 2024-11-19
- Estimated Expiration
- 2041-05-25
AI Technical Summary
When detecting redundant classes in Java projects, based on static code analysis, annotation classes, reflection classes, uncalled classes, etc. cannot be accurately detected, resulting in low detection accuracy and prone to misjudgment, and developers need to manually check it. and delete.
By obtaining the loading class and the full class of the source file when the project file is run, perform dynamic comparisons, marking the same class as redundant classes, and implementing automated redundant class detection and deletion.
Improve the accuracy and efficiency of redundant class detection, avoid missed detection and missed detection, realize undifferentiated scanning of all classes in project files, and can automatically process redundant classes, saving developers' time and cost.
Smart Images

Figure CN113220304B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and more specifically, to a redundancy detection method, device, electronic device, and computer-readable storage medium. Background Art
[0002] In the development process of a Java project, with the iteration of functions and the increase in the amount of code, some useless classes will inevitably appear in the project. These useless classes will increase the size of the project, affect the readability of the program, and cause inconvenience to the development and maintenance of subsequent projects.
[0003] The traditional method of detecting these classes is usually through manual troubleshooting, which requires developers to determine whether all classes are useful classes running in the project or useless classes, and delete the useless classes one by one. This method is time-consuming and labor-intensive for developers, and the troubleshooting process is relatively difficult. Summary of the invention
[0004] In view of this, the present disclosure provides a redundant class detection method and device.
[0005] One aspect of the present disclosure provides a method for detecting redundant classes, comprising: obtaining all full classes of a project file and all loaded classes when the project file is running; marking all identical classes between all full classes and all loaded classes, and unmarked full classes in all full classes are redundant classes.
[0006] According to an embodiment of the present disclosure, obtaining all full classes of a project file and all loaded classes when the project file is running includes: obtaining the project file at runtime and the path of the project file; obtaining the source file of the project file under the path; traversing the project file at runtime to obtain all loaded classes when the project file is running, and traversing the source file of the project file to obtain all full classes of the project file.
[0007] According to an embodiment of the present disclosure, traversing the project file at runtime to obtain all loaded classes of the project file at runtime includes: traversing the project file at runtime to obtain package names and class names of all loaded classes of the project file at runtime; traversing the source files of the project file to obtain all full classes of the project file includes: traversing the source files of the project file to obtain class paths and class names of all full classes of the project file.
[0008] According to an embodiment of the present disclosure, before marking all identical classes between all full classes and all loaded classes and unmarked full classes in all full classes as redundant classes, the method further includes: converting the class paths of all full classes into corresponding package names.
[0009] According to an embodiment of the present disclosure, marking all identical classes between all the full classes and all the loaded classes, and unmarked full classes in all the full classes as redundant classes, includes: iteratively matching all the full classes with all the loaded classes to obtain all identical classes between all the full classes and all the loaded classes; marking all the identical classes, and unmarked full classes in all the full classes as redundant classes.
[0010] According to an embodiment of the present disclosure, the marking of all identical classes between all the full classes and all the loaded classes, wherein the unmarked full classes in all the full classes are redundant classes, includes: matching the class names and package names of all the full classes with the class names and package names of all the loaded classes in sequence; when the class name and package name of the full class identical to the class name and package name of the loaded class are matched, marking the class name and package name of the full class, wherein the unmarked full classes in all the full classes are redundant classes.
[0011] According to an embodiment of the present disclosure, the method further includes: obtaining class paths and class names of all the redundant classes; obtaining all the redundant class files according to the class paths and types; and deleting all the redundant class files.
[0012] Another aspect of the present disclosure provides a redundant class detection device, including: a first acquisition module, used to acquire all full classes of a project file and all loaded classes when the project file is running; a marking module, used to mark all identical classes between all full classes and all loaded classes, and the unmarked full classes in all full classes are redundant classes.
[0013] According to an embodiment of the present disclosure, the first acquisition module includes: a first acquisition unit, used to acquire a project file at runtime and a path of the project file; a second acquisition unit, used to acquire a source file of the project file under the path; a traversal unit, used to traverse the project file at runtime to obtain all loaded classes of the project file at runtime, and traverse the source file of the project file to obtain all full classes of the project file.
[0014] According to an embodiment of the present disclosure, the traversal unit is specifically used to traverse the project file at runtime to obtain the package name and class name of all loaded classes of the project file at runtime; and to traverse the source file of the project file to obtain the class path and class name of all full classes of the project file.
[0015] According to an embodiment of the present disclosure, the device further includes: a conversion module, configured to convert the class paths of all full classes into corresponding package names.
[0016] According to an embodiment of the present disclosure, the marking module includes: a first matching unit, used to iteratively match all the full classes with all the loaded classes to obtain all the identical classes between all the full classes and all the loaded classes; a first marking unit, used to mark all the identical classes, and the unmarked full classes in all the full classes are redundant classes.
[0017] According to an embodiment of the present disclosure, the marking module also includes: a second matching unit, used to match the class names and package names of all the full classes with the class names and package names of all the loaded classes in sequence; a second marking unit, used to mark the class name and package name of the full class when the class name and package name of the full class that are identical to the class name and package name of the loaded class are matched, and the class name and package name of the full classes that are not marked in all the full classes are the class name and package name of redundant classes.
[0018] According to an embodiment of the present disclosure, the device also includes: a second acquisition module, used to obtain the class path and class name of all the redundant classes; a third acquisition module, used to obtain all the redundant class files according to the class path and type; and a deletion module, used to delete all the redundant class files.
[0019] Another aspect of the present disclosure provides an electronic device, comprising one or more processors; and a memory for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the method described in any one of the above claims.
[0020] Another aspect of the present disclosure provides a computer-readable storage medium storing computer-executable instructions, which are used to implement the above method when executed.
[0021] According to the embodiments of the present disclosure, because a technical means is adopted by comparing the loaded classes at the runtime of the project file with the full classes of the project source file, the technical problem that the detection mechanism in the prior art can only work based on static code analysis and cannot accurately detect redundant classes such as annotation classes, reflection classes, and uncalled classes is at least partially overcome. Therefore, all classes in the project file can be scanned indiscriminately to obtain complete redundant classes, thereby achieving a technical effect of wider detection range and higher accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] The above and other objects, features and advantages of the present disclosure will become more apparent through the following description of the embodiments of the present disclosure with reference to the accompanying drawings, in which:
[0023] Figure 1 An exemplary system architecture to which the redundant class detection method and apparatus of the present disclosure can be applied is schematically shown;
[0024] Figure 2 A flowchart of a method for detecting a redundancy class according to an embodiment of the present disclosure is schematically shown;
[0025] Figure 3A A flowchart of a method for obtaining a full class and loading a class according to an embodiment of the present disclosure is schematically shown;
[0026] Figure 3B A flowchart of a method for obtaining a redundancy class according to an embodiment of the present disclosure is schematically shown;
[0027] Figure 3C A flowchart of a method for obtaining a redundancy class according to another embodiment of the present disclosure is schematically shown;
[0028] Figure 3D A flowchart of a redundant class detection method according to another embodiment of the present disclosure is schematically shown;
[0029] Figure 4 A block diagram of a redundant class detection device according to an embodiment of the present disclosure is schematically shown;
[0030] Figure 5A A block diagram of a first acquisition module according to an embodiment of the present disclosure is schematically shown;
[0031] Figure 5B A block diagram of a marking module according to an embodiment of the present disclosure is schematically shown;
[0032] Figure 5C A block diagram of a marking module according to another embodiment of the present disclosure is schematically shown;
[0033] Figure 5D A block diagram schematically shows a redundant class detection device according to another embodiment of the present disclosure; and
[0034] Figure 6 A block diagram of an electronic device suitable for implementing a redundant detection device according to an embodiment of the present disclosure is schematically shown. DETAILED DESCRIPTION
[0035] Hereinafter, embodiments of the present disclosure will be described with reference to the accompanying drawings. However, it should be understood that these descriptions are exemplary only and are not intended to limit the scope of the present disclosure. In the following detailed description, for ease of explanation, many specific details are set forth to provide a comprehensive understanding of the embodiments of the present disclosure. However, it is apparent that one or more embodiments may also be implemented without these specific details. In addition, in the following description, descriptions of known structures and technologies are omitted to avoid unnecessary confusion of the concepts of the present disclosure.
[0036] The terms used herein are only for describing specific embodiments and are not intended to limit the present disclosure. The terms "comprise", "include", etc. used herein indicate the existence of the features, steps, operations and / or components, but do not exclude the existence or addition of one or more other features, steps, operations or components.
[0037] All terms (including technical and scientific terms) used herein have the meanings commonly understood by those skilled in the art, unless otherwise defined. It should be noted that the terms used herein should be interpreted as having a meaning consistent with the context of this specification, and should not be interpreted in an idealized or overly rigid manner.
[0038] In the case of using expressions such as "at least one of A, B, and C, etc.", it should generally be interpreted as the meaning of the expression generally understood by those skilled in the art (for example, "a system having at least one of A, B, and C" should include but is not limited to a system having A alone, B alone, C alone, A and B, A and C, B and C, and / or A, B, C, etc.). In the case of using expressions such as "at least one of A, B, or C, etc.", it should generally be interpreted as the meaning of the expression generally understood by those skilled in the art (for example, "a system having at least one of A, B, or C" should include but is not limited to a system having A alone, B alone, C alone, A and B, A and C, B and C, and / or A, B, C, etc.).
[0039] The embodiment of the present disclosure provides a redundant class detection method and device, the method comprising, for a selected project file, obtaining all full classes of the project file and all loaded classes when the project file is running. Marking all the same classes between all full classes and all loaded classes, and the full classes that are not marked in all full classes are redundant classes.
[0040] Figure 1 The exemplary system architecture 100 to which the redundant class detection method and apparatus according to the embodiment of the present disclosure can be applied is schematically shown. It should be noted that: Figure 1 What is shown is merely an example of a system architecture to which the embodiments of the present disclosure can be applied, in order to help those skilled in the art understand the technical content of the present disclosure, but it does not mean that the embodiments of the present disclosure cannot be used in other devices, systems, environments or scenarios.
[0041] like Figure 1As shown, the system architecture 100 according to this embodiment may include terminal devices 101, 102, 103, a network 104 and a server 105. The network 104 is used to provide a medium for communication links between the terminal devices 101, 102, 103 and the server 105. The network 104 may include various connection types, such as wired and / or wireless communication links, etc.
[0042] The user can use the terminal devices 101, 102, 103 to interact with the server 105 through the network 104 to receive or send messages, etc. Various communication client applications can be installed on the terminal devices 101, 102, 103, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients and / or social platform software, etc. (only as examples).
[0043] The terminal devices 101 , 102 , and 103 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers.
[0044] The server 105 may be a server that provides various services, such as a background management server (only an example) that provides support for websites browsed by users using the terminal devices 101, 102, and 103. The background management server may analyze and process the received data such as user requests, and feed back the processing results (such as web pages, information, or data obtained or generated according to user requests) to the terminal device.
[0045] It should be noted that the redundancy detection method provided in the embodiment of the present disclosure can generally be executed by the server 105. Accordingly, the redundancy detection device provided in the embodiment of the present disclosure can generally be set in the server 105. The redundancy detection method provided in the embodiment of the present disclosure can also be executed by a server or server cluster that is different from the server 105 and can communicate with the terminal devices 101, 102, 103 and / or the server 105. Correspondingly, the redundancy detection device provided in the embodiment of the present disclosure can also be set in a server or server cluster that is different from the server 105 and can communicate with the terminal devices 101, 102, 103 and / or the server 105. Alternatively, the redundancy detection device provided in the embodiment of the present disclosure can also be executed by the terminal devices 101, 102 or 103, or can also be executed by other terminal devices different from the terminal devices 101, 102 or 103. Correspondingly, the redundancy detection device provided in the embodiment of the present disclosure may also be arranged in the terminal device 101 , 102 or 103 , or in other terminal devices different from the terminal device 101 , 102 or 103 .
[0046] For example, the project file may be originally stored in any one of the terminal devices 101, 102, or 103 (for example, the terminal device 101, but not limited thereto), or stored in an external storage device and may be imported into the terminal device 101. Then, the terminal device 101 may locally execute the redundancy class detection method provided by the embodiment of the present disclosure, or send the project file to other terminal devices, servers, or server clusters, and the other terminal devices, servers, or server clusters that receive the project file may execute the redundancy class detection method provided by the embodiment of the present disclosure.
[0047] It should be understood that Figure 1 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0048] In the development process of any Java project, with the iteration of functions and the update of development technology, useless classes, namely redundant classes, are often generated in the project files. These redundant classes will not be loaded during the project operation, and will not have any impact on the project when deleted. In order to avoid the impact of redundant classes on project files, all classes in the project files will be detected to filter out all redundant classes.
[0049] Common open source code scanning tools include UCDetector and ProGuard. These tools are based on the detection mechanism of static code analysis to diagnose the classes of project files. Although some useless classes or codes can be detected, Java reflection, Spring injection and other contents contained in Java project files cannot be detected. At the same time, classes that are not called will also be marked as redundant classes. Therefore, using such tools for troubleshooting is not only less accurate, but also prone to misjudgment.
[0050] In addition, IDEs used for Java project development, such as IntelliJ IDEA, also include code detection functions. However, like the above-mentioned open source code scanning tools, they also use a detection mechanism based on static code analysis to scan and diagnose all classes. The results are not accurate and can only be used to assist in determining redundant code. They cannot actively delete redundant classes. Developers still need to manually delete redundant classes after a secondary investigation.
[0051] Figure 2 The flowchart of the redundant class detection method according to the embodiment of the present disclosure is schematically shown.
[0052] like Figure 2 As shown, the method includes operations S201-S202.
[0053] In operation S201, all full classes of the project file and all loaded classes when the project file is running are obtained.
[0054] The full class of the project file is the class included in the project source file, and the loaded class of the project file is the class actually loaded when the project file is running. During the running of the project file, not all full classes will be loaded. It is understandable that the Java project source file contains full classes that can implement multiple functions, but since not all functions need to be started during the running process, some full classes will not be loaded. After the full class corresponding to the required function is loaded, it is recorded as a loaded class.
[0055] It should be noted that during the actual running of a Java project file, the loaded classes are not only the classes in the source files of the project, but also the classes referenced during the running process. These referenced classes do not belong to the project file, but are classes in other project files.
[0056] In operation S202, all the same classes between all the full classes and all the loaded classes are marked, and the unmarked full classes in all the full classes are redundant classes.
[0057] In the disclosed embodiment, all the same classes between all full classes and all loaded classes can be marked as useful classes, that is, the classes loaded in the full classes during the operation of the project file, and the remaining classes in all full classes except the useful classes are redundant classes. The full classes are compared with the loaded classes, so that the detection process is based on the loaded classes when the project file is running, and it can be dynamically compared with the actual operation status of the project file to obtain accurate useful classes and useless classes, rather than just based on the semantic content and grammatical structure of the class itself.
[0058] Through the embodiments of the present disclosure, an automated redundant class detection method is provided. Compared with the traditional manual redundant class detection method, the detection efficiency can be greatly improved, time cost can be saved, and the detected redundant classes can be further processed, for example, batch automatic deletion, etc. Compared with the detection method based on the detection mechanism of static code analysis, the present disclosure performs scanning and detection on the running Java project, overcomes the shortcomings of static code analysis, and can perform indiscriminate scanning on all classes in the project to obtain all redundant classes and avoid missed detection and false detection. Since the embodiments of the present disclosure detect the entire Java project file, it can realize the detection of all sub-projects of the Java project file without running separately in the sub-projects in sequence, thereby improving the detection efficiency.
[0059] Reference below Figure 3A to Figure 3D , combined with specific embodiments Figure 2 The method shown is further explained.
[0060] Figure 3AThe flowchart of the method for obtaining all classes and loading classes according to an embodiment of the present disclosure is schematically shown.
[0061] like Figure 3A As shown, operation S201 obtains all full classes of the project file and all loaded classes when the project file is running, including operations S301 to S303:
[0062] In operation S301 , a runtime project file and a path of the project file are obtained.
[0063] In this operation, a running project file and a path of the running project file are obtained, where the path is a parent path of the project file.
[0064] In operation S302, a source file of a project file is obtained under a path.
[0065] In operation S303, the project file at runtime is traversed to obtain all loaded classes at runtime of the project file, and the source file of the project file is traversed to obtain all full classes of the project file.
[0066] In the disclosed embodiment, traversing the project file at runtime to obtain all loaded classes of the project file at runtime includes: traversing the project file at runtime to obtain the package name and class name of all loaded classes of the project file at runtime. Traversing the source file of the project file to obtain all full classes of the project file includes: traversing the source file of the project file to obtain the class path and class name of all full classes of the project file.
[0067] As an optional embodiment, the loading class of the Java project file is obtained through the Arthas open source diagnostic tool. The Java project file to be detected is scanned and analyzed by Arthas, and the loading class list of the project is printed in a log file.
[0068] For example, start the Arthas open source diagnostic tool and select the corresponding Java project process according to the process number of the Java project. Arthas automatically analyzes the JVM running status and class loading status of the process. Enable logging to print the list of loaded classes through log records. Execute the classloader command, set parameters to scan the entire Java process for loaded classes and output the loaded class list, which contains the package names and class names of all loaded classes.
[0069] The method for obtaining the full class of a Java project file may be to retrieve all Java source code files in a Java project under the same path, record all full class paths of the project files, and then generate a full class list of the project through character processing operations.
[0070] For example, create an empty text document under the Java project path to record the full class path of the project file. Traverse all files under the path of the project file, record the path names of all java type files in the text document created above, and generate a full class list of the project, which contains the class paths and class names of all full classes.
[0071] Through the disclosed embodiment, the class paths and class names of all full classes, as well as the package names and class names of all loaded classes, are obtained for subsequent comparison operations. That is, the full class list and the loaded class list only contain the class path names and class names of all full classes, as well as the package names and class names of all loaded classes, rather than class files. Therefore, the amount of computation in the later detection process can be reduced, and only the class path names and class names are analyzed and compared, thereby improving the detection efficiency.
[0072] Figure 3B The flowchart of the method for obtaining redundancy classes according to an embodiment of the present disclosure is schematically shown.
[0073] like Figure 3B As shown, operation S202 marks all the same classes between all full classes and all loaded classes, and all the full classes that are not marked in the full classes are redundant classes, including operations S304 to S305:
[0074] In operation S304, all full classes are iteratively matched with all loaded classes to obtain all identical classes between all full classes and all loaded classes.
[0075] In operation S305, all identical classes are marked, and all unmarked full classes in all full classes are redundant classes.
[0076] In the disclosed embodiment, all full classes are iteratively matched with all loaded classes to match the same classes between all full classes and all loaded classes, that is, the full classes and loaded classes under the same path are compared, and the intersection of the full classes and loaded classes can be marked as useful classes. Unmarked classes in the full classes are redundant classes, that is, the difference between the full classes and the useful classes is a redundant class.
[0077] Figure 3C The flowchart of the method for obtaining redundancy classes according to another embodiment of the present disclosure is schematically shown.
[0078] like Figure 3C As shown, operation S202 marks all the same classes between all full classes and all loaded classes, and all the full classes that are not marked in the full classes are redundant classes, including operations S306 to S307:
[0079] In operation S306, the class names and package names of all full classes are matched with the class names and package names of all loaded classes in sequence.
[0080] In operation S307, when the class name and package name of the full class that are the same as the class name and package name of the loaded class are matched, the class name and package name of the full class are marked, and the class names and package names of all full classes that are not marked are redundant class names and package names.
[0081] In the embodiment of the present disclosure, before marking all the same classes between all full classes and all loaded classes and all the unmarked full classes in all full classes as redundant classes, the method further includes:
[0082] Convert the class paths of all full classes into corresponding package names.
[0083] To ensure the consistency between the full class and the loaded class, character processing is performed on the class path and class name of the full class. That is, operations such as removing the path prefix and converting characters are performed on the class path, and the format of class path + class name is converted to package name + class name to make it consistent with the format of the loaded class list in the log.
[0084] Generally speaking, the number of full classes is much less than the number of loaded classes. Therefore, in order to further reduce the amount of calculation in the detection process, the class path of the full class in the full class list is converted into a corresponding package name, and then operations S306 and S307 are performed.
[0085] For example, obtain the list of loaded classes in the log file in the log file path of Arthas, and copy the list of loaded classes to the path of the project file. Create an empty text document in this path to record the redundant class list of the project. Match the class names and package names of all full classes in the same path with the class names and package names of all loaded classes in turn. When the class name and package name of the full class that are the same as the class name and package name of the loaded class are matched in the full class list, the class name and package name of the full class are marked as useful classes. Therefore, the class names and package names of the full classes that are not marked in the full class list are redundant classes, and the class names and package names of the redundant classes are recorded in the redundant class list.
[0086] Figure 3D The flowchart of a redundant class detection method according to another embodiment of the present disclosure is schematically shown.
[0087] like Figure 3D As shown, the method further includes operations S308 to S310.
[0088] In operation S308, the class paths and class names of all redundant classes are obtained.
[0089] In operation S309, all redundant class files are acquired according to the class path and the class name.
[0090] In operation S310, all redundant class files are deleted.
[0091] In the disclosed embodiment, after the matching is completed, the redundant class can be processed, for example, by deleting redundant class files in batches.
[0092] If you want to delete redundant classes in batches, you will output the redundant class list of the project and obtain the corresponding redundant class files. Since the package name and class name of the class are recorded in both the full class list and the loaded class list during the matching process, the package name and class name of the redundant class are also recorded in the generated redundant class list. Therefore, before deleting redundant classes, you also need to convert the package name and class name of the redundant class into a class path and class name. Then, based on the class path and class name of the redundant class, after obtaining all redundant class files, delete all redundant class files in batches.
[0093] It is also necessary to point out that when converting the package name into a class path, the class path may be a relative path, so the relative path needs to be converted into an absolute class path. Then, the redundant class files are obtained based on the absolute path and class name.
[0094] After the deletion operation is completed, all intermediate files generated need to be cleared, such as the full class list, the loaded class list, and the redundant class list, to reduce memory usage and reduce operating pressure. If the redundant class operation is not performed and only the redundant class is detected, after obtaining the redundant class list, the loaded class list and the full class list need to be cleared.
[0095] Figure 4 The block diagram of a redundant detection device according to an embodiment of the present disclosure is schematically shown.
[0096] like Figure 4 As shown, the redundant detection device 400 includes: a first acquisition module 410 and a marking module 420 .
[0097] The first acquisition module 410 is used to acquire all full classes of the project file and all loaded classes when the project file is running.
[0098] The marking module 420 is used to mark all the same classes between all the full classes and all the loaded classes, and the full classes that are not marked in all the full classes are redundant classes.
[0099] Through the embodiments of the present disclosure, an automated redundant class detection device is provided. Compared with the traditional tools for manually checking redundant classes, the checking efficiency can be greatly improved, the time cost can be saved, and the checked redundant classes can be further processed, for example, batch automatic deletion, etc. Compared with the detection method based on the detection mechanism of static code analysis, the present disclosure performs scanning and detection on the running Java project, which overcomes the shortcomings of static code analysis, and can perform indiscriminate scanning on all classes in the project to obtain all redundant classes, avoiding missed detection and false detection. Since the embodiments of the present disclosure detect the entire Java project file, it can realize the detection of all sub-projects of the Java project file, without running separately in the sub-projects in sequence, thereby improving the detection efficiency.
[0100] Reference below Figure 5A to Figure 5D ,right Figure 4 The method shown is further explained.
[0101] Figure 5A The block diagram of the first acquisition module according to the embodiment of the present disclosure is schematically shown.
[0102] like Figure 5A As shown, the first acquisition module 410 includes:
[0103] The first acquisition unit 4101 is used to acquire the project file and the path of the project file at runtime. The second acquisition unit 4102 is used to acquire the source file of the project file under the path. The traversal unit 4103 is used to traverse the project file at runtime to obtain all loaded classes of the project file at runtime, and traverse the source file of the project file to obtain all full classes of the project file.
[0104] The traversal unit 4103 is also specifically used to traverse the runtime project file to obtain the package name and class name of all loaded classes of the runtime project file, and to traverse the source file of the project file to obtain the class path and class name of all full classes of the project file.
[0105] Figure 5B The block diagram of the marking module according to an embodiment of the present disclosure is schematically shown.
[0106] like Figure 5B As shown, the marking module 420 includes:
[0107] The first matching unit 4201 is used to iteratively match all full classes with all loaded classes to obtain all identical classes between all full classes and all loaded classes. The first marking unit 4202 is used to mark all identical classes, and the unmarked full classes in all full classes are redundant classes.
[0108] Figure 5CA block diagram of a marking module according to another embodiment of the present disclosure is schematically shown.
[0109] like Figure 5C As shown, the marking module 420 also includes: a second matching unit 4203, which is used to match the class names and package names of all full classes with the class names and package names of all loaded classes in sequence. The second marking unit 4204 is used to mark the class name and package name of the full class when the class name and package name of the full class that are the same as the class name and package name of the loaded class are matched, and the class name and package name of the full class that are not marked in all full classes are the class name and package name of redundant classes.
[0110] Figure 5D A block diagram of a redundant detection device according to another embodiment of the present disclosure is schematically shown.
[0111] like Figure 5D As shown, the device 400 further includes: a second acquisition module 430, used to acquire the class path and class name of all redundant classes; a third acquisition module 440, used to acquire all redundant class files according to the class path and type; and a deletion module 450, used to delete all redundant class files.
[0112] According to the embodiments of the present invention, any one or more of the modules, submodules, units, and subunits or at least part of the functions of any one of them can be implemented in one module. According to the embodiments of the present invention, any one or more of the modules, submodules, units, and subunits can be split into multiple modules for implementation. According to the embodiments of the present invention, any one or more of the modules, submodules, units, and subunits can be at least partially implemented as hardware circuits, such as field programmable gate arrays (FPGAs), programmable logic arrays (PLAs), systems on chips, systems on substrates, systems on packages, application specific integrated circuits (ASICs), or can be implemented by hardware or firmware in any other reasonable way of integrating or packaging the circuit, or implemented in any one of the three implementation methods of software, hardware, and firmware or in any appropriate combination of any of them. Alternatively, according to the embodiments of the present invention, one or more of the modules, submodules, units, and subunits can be at least partially implemented as computer program modules, and when the computer program modules are run, the corresponding functions can be performed.
[0113] For example, any multiple of the first acquisition module 410 and the marking module 420 can be combined in one module / unit / subunit for implementation, or any one of the modules / units / subunits can be split into multiple modules / units / subunits. Alternatively, at least part of the functions of one or more of these modules / units / subunits can be combined with at least part of the functions of other modules / units / subunits and implemented in one module / unit / subunit. According to an embodiment of the present disclosure, at least one of the first acquisition module 410 and the marking module 420 can be at least partially implemented as a hardware circuit, such as a field programmable gate array (FPGA), a programmable logic array (PLA), a system on a chip, a system on a substrate, a system on a package, an application specific integrated circuit (ASIC), or can be implemented by hardware or firmware such as any other reasonable way of integrating or packaging the circuit, or implemented in any one of the three implementation methods of software, hardware and firmware or in any appropriate combination of any of them. Alternatively, at least one of the first acquisition module 410 and the marking module 420 can be at least partially implemented as a computer program module, and when the computer program module is run, the corresponding function can be executed.
[0114] It should be noted that the redundant detection device part in the embodiment of the present disclosure corresponds to the redundant detection method part in the embodiment of the present disclosure. The description of the redundant detection device part specifically refers to the redundant detection method part, which will not be repeated here.
[0115] Figure 6 A block diagram of an electronic device suitable for implementing the method described above according to an embodiment of the present disclosure is schematically shown. Figure 6 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0116] like Figure 6 As shown, the electronic device 500 according to an embodiment of the present disclosure includes a processor 501, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 502 or a program loaded from a storage part 508 to a random access memory (RAM) 503. The processor 501 may include, for example, a general-purpose microprocessor (e.g., a CPU), an instruction set processor and / or a related chipset and / or a special-purpose microprocessor (e.g., an application-specific integrated circuit (ASIC)), etc. The processor 501 may also include an onboard memory for caching purposes. The processor 501 may include a single processing unit or multiple processing units for performing different actions of the method flow according to an embodiment of the present disclosure.
[0117] In RAM 503, various programs and data required for the operation of system 500 are stored. Processor 501, ROM 502 and RAM 503 are connected to each other through bus 504. Processor 501 performs various operations of the method flow according to the embodiment of the present disclosure by executing the program in ROM 502 and / or RAM 503. It should be noted that the program can also be stored in one or more memories other than ROM 502 and RAM 503. Processor 501 can also perform various operations of the method flow according to the embodiment of the present disclosure by executing the program stored in the one or more memories.
[0118] According to an embodiment of the present disclosure, the system 500 may further include an input / output (I / O) interface 505, which is also connected to the bus 504. The system 500 may further include one or more of the following components connected to the I / O interface 505: an input section 506 including a keyboard, a mouse, etc.; an output section 507 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 508 including a hard disk, etc.; and a communication section 509 including a network interface card such as a LAN card, a modem, etc. The communication section 509 performs communication processing via a network such as the Internet. A drive 510 is also connected to the I / O interface 505 as needed. A removable medium 511, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 510 as needed, so that a computer program read therefrom is installed into the storage section 508 as needed.
[0119] According to an embodiment of the present disclosure, the method flow according to an embodiment of the present disclosure can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a computer-readable storage medium, and the computer program contains a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 509, and / or installed from the removable medium 511. When the computer program is executed by the processor 501, the above-mentioned functions defined in the system of the embodiment of the present disclosure are executed. According to an embodiment of the present disclosure, the system, equipment, device, module, unit, etc. described above can be implemented by a computer program module.
[0120] The present disclosure also provides a computer-readable storage medium, which may be included in the device / apparatus / system described in the above embodiments; or may exist independently without being assembled into the device / apparatus / system. The above computer-readable storage medium carries one or more programs, and when the above one or more programs are executed, the method according to the embodiment of the present disclosure is implemented.
[0121] According to an embodiment of the present disclosure, the computer-readable storage medium may be a non-volatile computer-readable storage medium. For example, it may include, but is not limited to: 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), a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that may be used by or in combination with an instruction execution system, apparatus, or device.
[0122] For example, according to an embodiment of the present disclosure, the computer-readable storage medium may include the ROM 502 and / or the RAM 503 described above and / or one or more memories other than the ROM 502 and the RAM 503 .
[0123] The flowcharts and block diagrams in the accompanying drawings illustrate the possible implementation architecture, functions and operations of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of a code, and the above-mentioned module, a program segment or a part of the code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two 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 box in the block diagram or flowchart, and the combination of boxes in the block diagram or flowchart, 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.
[0124] It will be appreciated by those skilled in the art that the features described in the various embodiments and / or claims of the present disclosure may be combined and / or combined in a variety of ways, even if such combinations and / or combinations are not explicitly described in the present disclosure. In particular, the features described in the various embodiments and / or claims of the present disclosure may be combined and / or combined in a variety of ways without departing from the spirit and teachings of the present disclosure. All of these combinations and / or combinations fall within the scope of the present disclosure.
[0125] The embodiments of the present disclosure are described above. However, these embodiments are only for illustrative purposes and are not intended to limit the scope of the present disclosure. Although the embodiments are described above separately, this does not mean that the measures in the various embodiments cannot be used in combination to advantage. The scope of the present disclosure is defined by the attached claims and their equivalents. Without departing from the scope of the present disclosure, those skilled in the art may make a variety of substitutions and modifications, which should all fall within the scope of the present disclosure.
Claims
1. A redundant class detection method, comprising: Obtain all full classes of the project file and all loaded classes when the project file is running, wherein the loaded classes include classes referenced during the running process; The obtaining of all full classes of the project file and all loaded classes of the project file when it is running includes: obtaining the path of the project file and the project file when it is running; obtaining the source file of the project file under the path; traversing the project file at runtime to obtain all loaded classes of the project file when it is running, and traversing the source file of the project file to obtain all full classes of the project file; traversing the project file at runtime to obtain all loaded classes of the project file when it is running includes: traversing the project file at runtime to obtain the package name and class name of all loaded classes of the project file when it is running; traversing the source file of the project file to obtain all full classes of the project file includes: traversing the source file of the project file to obtain the class path and class name of all full classes of the project file; Marking all identical classes between all the full classes and all the loaded classes, and unmarked full classes in all the full classes as redundant classes; marking all identical classes between all the full classes and all the loaded classes, and unmarked full classes in all the full classes as redundant classes, includes: iteratively matching all the full classes with all the loaded classes to obtain all identical classes between all the full classes and all the loaded classes; marking all the identical classes, and unmarked full classes in all the full classes as redundant classes.
2. The method according to claim 1, wherein: Before marking all the same classes between all the full classes and all the loaded classes, and before marking all the full classes that are not marked as redundant classes in all the full classes, the method further includes: Convert the class paths of all the full classes into corresponding package names.
3. The method according to claim 2, wherein: The marking of all the same classes between all the full classes and all the loaded classes, wherein the unmarked full classes in all the full classes are redundant classes, includes: Match the class names and package names of all the full classes with the class names and package names of all the loaded classes in sequence; When a full class class name and package name that are identical to the class name and package name of the loaded class are matched, the class name and package name of the full class are marked, and the class names and package names of the full classes that are not marked in all the full classes are the class names and package names of the redundant classes.
4. The method according to claim 1, further comprising: Get the class path and class name of all the redundant classes; According to the class path and type, obtain all the redundant class files; Delete all said redundant class files.
5. A redundant detection device, comprising: A first acquisition module is used to acquire all full classes of a project file and all loaded classes when the project file is running, wherein the loaded classes include classes referenced during the running process; The first acquisition module includes: a first acquisition unit, which is used to acquire a runtime project file and a path of the project file; a second acquisition unit, which is used to acquire a source file of the project file under the path; a traversal unit, which is used to traverse the runtime project file to obtain all loaded classes of the runtime project file, and traverse the source file of the project file to obtain all full classes of the project file; the traversal unit is specifically used to traverse the runtime project file to obtain the package name and class name of all loaded classes of the runtime project file; and traverse the source file of the project file to obtain the class path and class name of all full classes of the project file; A marking module, used to mark all the same classes between all the full classes and all the loaded classes, and the unmarked full classes in all the full classes are redundant classes; the marking module includes: A first matching unit, configured to iteratively match all the full classes with all the loaded classes to obtain all the same classes between all the full classes and all the loaded classes; The first marking unit is used to mark all the same classes, and the unmarked full classes in all the full classes are redundant classes.
6. The device according to claim 5, wherein: The device also includes: a conversion module, which is used to convert the class paths of all the full classes into corresponding package names.
7. The device according to claim 6, wherein: The marking module also includes: A second matching unit, used to match the class names and package names of all the full classes with the class names and package names of all the loaded classes in sequence; The second marking unit is used to mark the class name and package name of the full class when the class name and package name of the full class that are identical to the class name and package name of the loaded class are matched, and the class name and package name of the full class that are not marked in all the full classes are the class name and package name of the redundant class.
8. The device according to claim 5, wherein: The device also includes: A second acquisition module is used to acquire the class paths and class names of all the redundant classes; A third acquisition module, used for acquiring all the redundant class files according to the class path and type; The deletion module is used to delete all the redundant class files.
9. An electronic device, comprising: one or more processors; a memory for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 4.
10. A computer-readable storage medium having executable instructions stored thereon, which, when executed by a processor, enables the processor to implement the method according to any one of claims 1 to 4.
