Disclosure of Invention
The application provides a useless class detection method, which comprises the following steps:
creating an initialized useless class set and a useful class file set;
performing iterative matching on the class names in the garbage collection in an initialized useful class file collection so as to remove the class names of the useful classes in the garbage collection;
and when the class name of the useful class in the useless class set is removed, outputting the useless class set as a useless class detection result.
Optionally, the creating the initialized garbage class set and the initialized useful class file set includes:
identifying class files in the target directory;
matching the identified class files with a preset useful class sample to detect useless classes and useful classes in the class files;
creating an initialized useful class set based on the detected class name of the useful class;
creating the initialized useful class file set based on the detected class files of the useful class;
creating the initialized garbage collection based on the detected class name of the garbage.
Optionally, the preset useful class sample includes one or more of a preset useful class white list, a useful class directory list, a useful class name feature list and a useful class file feature list;
before identifying the class file in the target directory, the method further comprises:
and clearing the invalid codes in the target directory based on a preset code clearing tool.
Optionally, the iteratively matching the class names in the garbage collection in the initialized useful class file collection to remove the class names of the useful classes in the garbage collection includes:
traversing the garbage collection;
matching the class names in the useless class set with the class files in the useful class file set in sequence;
and when the class names in the useless class set are matched in the class files in the useful class file set, taking the class names as the class names of the useful classes to be moved into the useful class set, and taking the class files corresponding to the class names as the class files of the useful classes to be moved into the useful class file set.
Optionally, the method further includes:
when the class name is taken as the class name of the useful class and moved into the useful class set, and the class file corresponding to the class name is taken as the class file of the useful class and moved into the useful class file set, setting a preset useful class mark as a first mark; the first mark is used for identifying the class name of the useful class which is newly added into the useful class set;
when the class names in the useless class set are not matched in all class files in the useful class file set, setting a preset useful class mark as a second mark; the second mark is used for identifying the class name of the useful class which is not newly added in the useful class set.
Optionally, the method further includes:
reading the preset useful class mark;
judging whether the read useful mark is the first mark or not;
and when the useful class mark is judged to be the first mark, re-traversing the useless class set, and sequentially matching the class names in the useless class set with the class files in the useful class file set until the matching is finished when the useful class mark is judged to be the second mark.
The present application also proposes a garbage detection apparatus, comprising:
the creating module is used for creating an initialized useless class set and an initialized useful class file set;
the matching module is used for carrying out iterative matching on the class names in the useless class set in the initialized useful class file set so as to remove the class names of the useful classes in the useless class set;
and the output module is used for outputting the useless class set as a useless class detection result after the class name of the useful class in the useless class set is removed.
Optionally, the creating module is specifically configured to:
identifying class files in the target directory;
matching the identified class files with a preset useful class sample to detect useless classes and useful classes in the class files;
creating an initialized useful class set based on the detected class name of the useful class;
creating the initialized useful class file set based on the detected class files of the useful class;
creating the initialized garbage collection based on the detected class name of the garbage.
Optionally, the preset useful class sample includes one or more of a preset useful class white list, a useful class directory list, a useful class name feature list and a useful class file feature list;
the creation module is further to:
before identifying the class files in the target directory, clearing invalid codes in the target directory based on a preset code clearing tool.
Optionally, the matching module is specifically configured to:
traversing the garbage collection;
matching the class names in the useless class set with the class files in the useful class file set in sequence;
and when the class names in the useless class set are matched in the class files in the useful class file set, taking the class names as the class names of the useful classes to be moved into the useful class set, and taking the class files corresponding to the class names as the class files of the useful classes to be moved into the useful class file set.
Optionally, the matching module is further configured to:
when the class name is taken as the class name of the useful class and moved into the useful class set, and the class file corresponding to the class name is taken as the class file of the useful class and moved into the useful class file set, setting a preset useful class mark as a first mark; the first mark is used for identifying the class name of the useful class which is newly added into the useful class set;
when the class names in the useless class set are not matched in all class files in the useful class file set, setting a preset useful class mark as a second mark; the second mark is used for identifying the class name of the useful class which is not newly added in the useful class set.
Optionally, the matching module is further configured to:
reading the preset useful class mark;
judging whether the read useful mark is the first mark or not;
and when the useful class mark is judged to be the first mark, re-traversing the useless class set, and sequentially matching the class names in the useless class set with the class files in the useful class file set until the matching is finished when the useful class mark is judged to be the second mark.
According to the method, the initialized useless class set and the useful class file set are created, the class names in the useless class set are subjected to iterative matching in the initialized useful class file set to remove the class names of the useful classes in the useless class set, and after the class names of the useful classes in the useless class set are removed, the useless class set is output as a useless class detection result.
Detailed Description
In the related art, when code cleaning is performed on an android wireless terminal, the code cleaning is usually performed based on a code cleaning tool such as a PMD or a UCDetector.
However, PMD tools are typically only capable of scanning useless proprietary methods, proprietary properties, and useless code and variable definitions within code sections, and are not able to identify their usefulness for other types of code sections, nor are PMD tools capable of class-level scanning.
Although the UCDetector tool can scan useless classes in a standard android project by configuring a useful class white list, the UCDetector does not consider the reference relationship between the classes when scanning the useless classes, so that for the android project providing the API (the android project providing the API has the class which refers to the API), the UCDetector tool cannot accurately define a useful class set, and cannot scan all the useless classes in the project.
Therefore, the existing code cleaning tool cannot meet the code cleaning requirement in the actual working scene.
In view of the above, the present application provides a method for detecting useless classes, which creates an initialized useless class set and a useful class file set, iteratively matches class names in the useless class set in the initialized useful class file set to remove class names of useful classes in the useless class set, and outputs the useless class set as a result of detecting the useless classes after removing the class names of the useful classes in the useless class set.
The present application is described below with reference to specific embodiments and specific application scenarios.
Referring to fig. 1, fig. 1 is a flowchart illustrating a garbage detection method applied to a client according to an embodiment of the present application, where the method includes the following steps:
step 101, creating an initialized useless class set and a useful class file set;
102, performing iterative matching on the class names in the useless class set in an initialized useful class file set to remove the class names of the useful classes in the useless class set;
and 103, when the class name of the useful class in the useless class set is removed, outputting the useless class set as a useless class detection result.
The client can comprise client software for providing useless detection service for software developers; for example, taking the development of the android project as an example, the client software may be a garbage detection tool for providing garbage detection service to the android developer, and the developer may perform garbage detection on the created android project by using the garbage detection tool.
In this embodiment, the hardware environment for carrying the client software is not particularly limited; for example, it may be a PC host, a mobile terminal, or a server.
The following description will be given taking the garbage detection tool, which provides garbage detection service to android developers, by the client software.
In this embodiment, when a developer performs useless class detection on a created android project, the client software may recursively scan all directories under the android project as target directories to identify all class files of the android project.
For the android project, because the android project uses java language as implementation language, class files of the android project are usually system files with the extension name of java, and when identifying the class files, client software can identify the system files with the extension name of java in all directories of the android project.
In addition, before identifying all class files under the android project, the client software can clear invalid codes in the target directory based on a preset code cleaning tool, so that the useless classes can be prevented from being referred to invalid codes such as useless member variables or useless code segments of the useful classes.
In this embodiment, after all the class files of the android project are identified, the client software may match all the identified class files with a preset useful class sample to detect useful classes and useless classes in all the class files of the android project.
Wherein the preset useful class sample may include one or more of a useful class white list, a useful class directory list, a useful class name feature list and a useful class file feature list customized by a developer.
For example, in implementation, a developer may pre-define four variables, a whiteList variable, a usedDirList variable, a usedNamePrefList variable, and a usedContentPrefList variable.
The whiteList variable is a white list of useful classes, and represents a set of classes that do not need to be processed, and the classes in the list are all useful classes. The used dirlist variable is a list of useful class directories, the directories in the list are all useful class directories, and the classes in the useful class directories are all useful classes. The user-added NamePrefList variable is a useful class name feature list, the list comprises class name feature characters of a plurality of useful classes, and if the characters in the list appear in class names, the class is a useful class. The usedContentPrefList variable is a useful class file feature list, which includes feature characters of class files of several useful classes, and if the characters in the list appear in the class file (i.e. the content of the class), the class is a useful class.
Based on the above, when the client software detects the useful classes and the useless classes in all the identified class files, the client software can respectively match all the identified class files in the above four lists, if the matching is successful, the class files are indicated to be the useful classes, and if the matching is unsuccessful, the class files are indicated to be the useless classes. In this way, the client software can detect useful classes and useless classes in all identified class files.
After all the useful classes and useless classes in the identified class files are detected, the client software can create an initialized useful class set based on the class names of all the detected useful classes; the class file can be read based on the file paths of all the useful classes, and an initialized useful class file set (namely a content set of the useful classes) is created based on the read class file; an initialized garbage collection may also be created based on the class names of all the garbage detected.
For the created initialized useless class set, the client software can also record the file paths of all the useless classes in the set, so that in the subsequent iterative matching, when the useless classes in the set become useful classes, the class files of the useless classes are conveniently loaded into the useful class file set based on the file paths.
In this embodiment, since there may be a class name referenced by a class file of a useful class in an initialized garbage collection, and since classes are usually used according to class names in the android project, in the android project of the code specification, once a class name in a certain garbage collection can be matched with a class file in a useful class file collection, a class file corresponding to the class name is always used, and at this time, the class name is referenced by a class file in the useful class file collection, and thus the class file corresponding to the class name is a useful class.
From this perspective, after the client software creates the initialized useful class file set, the useful class file set and the useless class set, the class names of the useful classes in the useless class set can be detected by sequentially matching the class names in the useless class set with the class files in the useful class file set.
Referring to fig. 2, fig. 2 is a flowchart illustrating that the client software performs iterative matching on class names in the useless class set in the useful class file set according to this embodiment.
Before starting to carry out iterative matching on the class names in the useless class set in the useful class file set, the client software can preset a useful class mark for the useful class set.
The useful class mark may include a first mark and a second mark, the first mark is used for identifying the class name of the useful class which is newly added into the useful class set, and the second mark is used for identifying the class name of the useful class which is not newly added into the useful class set.
For example, the client software may preset a flag bit as the flag of the useful class for the set of useful classes, and define two flag values, namely a first flag and a second flag, for the flag bit.
Referring to fig. 2, when iteratively matching a class name in the garbage collection file, the client software may first set a default flag to the useful class flag, where the default flag may be a first flag or a second flag.
For example, the first flag may be a flag T preset for the set of useful classes, the second flag may be a flag F preset for the set of useful classes, and the client software may set the useful class flag to the flag F and also to the flag T when setting the useful class flag to the default flag (the default flag shown in fig. 2 is the flag F).
Referring to fig. 2, after the client software sets the useful class flag as a default flag, the client software may traverse the useless class set, and sequentially match class names in the useless class set with class files in the useful class file set to detect the class names of the useful classes in the useless class set.
On the other hand, if a certain class name in the useless class set is matched in class files in the useful class file set, at this time, the class name is referred to by the useful class file, the class file corresponding to the class name is a useful class, the client software can move the class name as the class name of the useful class into the useful class set, and meanwhile, the class file corresponding to the class name can be moved into the useful class file set as the class file of the useful class based on a pre-recorded file path of the class name.
When the client software moves the class name into the useful class set as the class name of the useful class and moves the class file corresponding to the class name into the useful class file set as the class file of the useful class, the client software may set the useful class flag to be a first flag (the first flag shown in fig. 2 is a flag T) to identify the class name of the useful class newly added into the useful class set.
When the client software sets the useful class mark as a first mark, if the default mark set for the useful class mark by the client software is a second mark, the client software can change the useful class mark from the second mark to the first mark at the moment, and then the client software can read the next class name from the useless class set to be continuously matched with the class files in the useful class file set, and so on until the class names in the useless class set are completely matched. If the default mark set by the client software for the useful class mark is the first mark, the client can directly read the next class name from the useless class set without changing the client at the moment and continuously match with the class files in the useful class file set, and so on until the class names in the useless class set are matched.
On the other hand, if a class name in the useless class set is not matched with a class file in the useful class file set, the class name is not referred by the class file of the useful class at this time, the class file corresponding to the class name is a useless class, the client software can read the next class name from the useless class set and continue to match with the class file in the useful class file set, and so on until the class names in the useless class set are all matched.
Of course, if none of the class files in the useful class file set matches the class name in the useless class set, at this time, none of the class names in the useless class set is referenced by the class file of the useful class, and all of the class files corresponding to the class name in the useless class set are useless classes, in this case, the client may set the useful class flag to be a second flag (the second flag shown in fig. 2 is flag F) to identify the class name of the useful class set to which no useful class is newly added.
When the client software sets the useful class flag to be the second flag, if the useful class flag is set to be the first flag by the client as a default, the client may change the useful class flag from the first flag to the second flag at this time, and if the useful class flag is set to be the second flag by the client as a default, the client may not change at this time, and this round of iteration ends at this time.
The above describes a round of iterative process of the client software in iteratively matching the class names in the garbage collection in the useful class file collection in conjunction with fig. 2.
In this embodiment, after a round of iteration is finished, if a class name of a useful class is newly added to a useful class set, at this time, a class name used by a class file of the newly added useful class, that is, a class name referred by the class file of the newly added useful class, may still exist in the useless class set.
Thus, in this case, if the class name of the useful class is newly added to the useful class set, the client software may continue to start the next iteration until it stops when no more new class names of the useful class are added to the useless class set.
Referring to fig. 2, after a round of iteration is finished, the client software may read the useful class mark, and determine whether the read useful mark is the first mark.
If the client software judges that the useful class mark is the first mark, the class name of the useful class still possibly exists in the useless class set at the moment, the client software can perform the next iteration according to the iteration process, set the useful class mark as a default mark, traverse the useless class set again, and match the class name in the useless class set with the class file in the useful class file set in sequence, and the specific process is not repeated.
In this way, the client software can continuously move the class names of the useful classes in the garbage collection out of the garbage collection and move the useful classes into the useful class collection through repeated iteration.
Referring to fig. 2 again, when the client software finally determines that the read useful mark is the second mark after repeated iteration, all the class names of the useful classes in the garbage class set are moved into the useful class set, at this time, the iterative matching is finished, and the remaining class names in the garbage class set are the final garbage class detection result.
In this case, the client software may output the current garbage set to the developer as a garbage detection result, and the developer may remove the garbage in the current android project based on the garbage detection result output by the client software.
Therefore, the reference relation between the class name of the useless class and the class file of the useful class can be fully considered, and the problem that all the useless classes in the android project cannot be scanned due to the fact that the UCDetector tool cannot define the useful class set inaccurately because the reference relation between the classes is not considered can be avoided. Therefore, useless files can be detected more efficiently.
In addition, it should be noted that, in the flow shown in fig. 2, when the client starts to perform iterative matching on the class name in the useless class set in the useful class file set, in another implementation manner, a default flag may not be set for the useful class flag, but a subsequent matching step is directly performed, the useless class set is traversed, the class name in the useless class set is sequentially matched with the class files in the useful class file set, until one round of iteration is finished, a first flag or a second flag is set for the flag bit according to an iteration result, and then the client software may determine whether to start a next round of iteration by judging whether the useful class flag is the first flag.
For example, the client software may still preset a flag bit as the useful class flag for the useful class set, and define two flag values, namely a first flag and a second flag, for the flag bit, and only retain the flag bit when the iterative matching starts, and the client software does not set a specific flag value for the flag, but directly performs subsequent matching steps until a round of iteration is completed, and then sets the first flag or the second flag for the flag bit according to the iteration result, which is not described again.
In the above embodiment, the initialized useless class set and the useful class file set are created, the class names in the useless class set are iteratively matched in the initialized useful class file set to remove the class names of the useful classes in the useless class set, and after the class names of the useful classes in the useless class set are removed, the useless class set is output as the useless class detection result.
Corresponding to the method embodiment, the application also provides an embodiment of the device.
Referring to fig. 3, the present application provides a garbage detection apparatus 30 applied to a client; referring to fig. 4, the hardware architecture related to the client that carries the idle detection device 30 generally includes a CPU, a memory, a non-volatile memory, a network interface, an internal bus, and the like; in software implementation, the garbage detection apparatus 30 may be generally understood as a computer program loaded in a memory, and a logic apparatus formed by combining software and hardware after being executed by a CPU, where the apparatus 30 includes:
a creating module 301, configured to create an initialized garbage class set and a useful class file set;
a matching module 302, configured to perform iterative matching on the class names in the garbage collection in an initialized useful class file collection to remove the class names of the useful classes in the garbage collection;
an output module 303, configured to output the garbage collection as a garbage detection result after the class name of the useful class in the garbage collection is removed.
In this embodiment, the creating module 301 is specifically configured to:
identifying class files in the target directory;
matching the identified class files with a preset useful class sample to detect useless classes and useful classes in the class files;
creating an initialized useful class set based on the detected class name of the useful class;
creating the initialized useful class file set based on the detected class files of the useful class;
creating the initialized garbage collection based on the detected class name of the garbage.
In this embodiment, the preset useful class sample includes one or more of a preset useful class white list, a useful class directory list, a useful class name feature list and a useful class file feature list;
the creating module 301 is further configured to:
before identifying the class files in the target directory, clearing invalid codes in the target directory based on a preset code clearing tool.
In this embodiment, the matching module 302 is specifically configured to:
traversing the garbage collection;
matching the class names in the useless class set with the class files in the useful class file set in sequence;
and when the class names in the useless class set are matched in the class files in the useful class file set, taking the class names as the class names of the useful classes to be moved into the useful class set, and taking the class files corresponding to the class names as the class files of the useful classes to be moved into the useful class file set.
In this embodiment, the matching module 302 is further configured to:
when the class name is taken as the class name of the useful class and moved into the useful class set, and the class file corresponding to the class name is taken as the class file of the useful class and moved into the useful class file set, setting a preset useful class mark as a first mark; the first mark is used for identifying the class name of the useful class which is newly added into the useful class set;
when the class names in the useless class set are not matched in all class files in the useful class file set, setting a preset useful class mark as a second mark; the second mark is used for identifying the class name of the useful class which is not newly added in the useful class set.
In this embodiment, the matching module 302 is further configured to:
reading the preset useful class mark;
judging whether the read useful mark is the first mark or not;
and when the useful class mark is judged to be the first mark, re-traversing the useless class set, and sequentially matching the class names in the useless class set with the class files in the useful class file set until the matching is finished when the useful class mark is judged to be the second mark.
Other embodiments of the present application will be apparent to those skilled in the art from consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the invention following, in general, the principles of the application and including such departures from the present disclosure as come within known or customary practice within the art to which the invention pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the application being indicated by the following claims.
It will be understood that the present application is not limited to the precise arrangements described above and shown in the drawings and that various modifications and changes may be made without departing from the scope thereof. The scope of the application is limited only by the appended claims.
The above description is only exemplary of the present application and should not be taken as limiting the present application, as any modification, equivalent replacement, or improvement made within the spirit and principle of the present application should be included in the scope of protection of the present application.