The content of the invention
The application proposes a kind of useless class detection method, and the method includes:
Create the useless class set and useful class file set of initialization;
Class name in the useless class set is iterated matching in the useful class file set of initialization,
To remove the class name of the useful class in the useless class set;
After the class name of the useful class in the useless class set is removed, it is by the useless class set cooperation
Useless class testing result output.
Optionally, the useless class set and useful class file set for creating initialization includes:
Class file in identification target directory;
The class file that will identify that is matched to detect the class file with default useful class sample
In useless class and useful class;
The useful class set of the class name creation initialization based on the described useful class for detecting;
Class file based on the described useful class for detecting creates the useful class file set of the initialization;
The useless class set initialized described in class name creation based on the described useless class for detecting.
Optionally, the default useful class sample includes the list of default useful class white list, useful class
One or more in directory listing, useful class class name feature list and useful class file feature list;
Before class file in the identification target directory, methods described also includes:
Invalid code in the target directory is removed based on default code cleaning tool.
Optionally, the class name by the useless class set is in the useful class file set of initialization
Matching is iterated, is included with the class name for removing the useful class in the useless class set:
Travel through the useless class set;
Class name in the useless class set is carried out with the class file in the useful class file set successively
Matching;
Class name in the useless class set is matched in the class file in the useful class file set
When, move into the useful class set using the class name as the class name of useful class, and by the class name pair
The class file answered moves into the useful class file set as the class file of useful class.
Optionally, methods described also includes:
When moving into the useful class set using the class name as the class name of useful class, and by the class name
After corresponding class file moves into the useful class file set as the class file of useful class, have default
Marked with class and be set as the first mark;Described first marks and newly add in the useful class set for identifying
The class name of useful class;
When the useless class set is not matched in all class files in the useful class file set
In class name when, default useful class mark is set as the second mark;Described second marks for identifying
The class name of useful class is not added in the useful class set newly.
Optionally, methods described also includes:
Read the default useful class mark;
Judge to read described useful marks whether to be the described first mark;
When the useful class is judged labeled as the described first mark, the useless class set is traveled through again,
Class name in the useless class set is matched with the class file in the useful class file set successively,
Matching terminates when judging the useful class labeled as the described second mark.
The application also proposes a kind of useless class detection means, and the device includes:
Creation module, useless class set and useful class file set for creating initialization;
Matching module, for by the class name in the useless class set initialization useful class file set
In be iterated matching, to remove the class name of the useful class in the useless class set;
Output module, for after the class name of the useful class in the useless class set is removed, by described in
Useless class set cooperation is exported for useless class testing result.
Optionally, the creation module specifically for:
Class file in identification target directory;
The class file that will identify that is matched to detect the class file with default useful class sample
In useless class and useful class;
The useful class set of the class name creation initialization based on the described useful class for detecting;
Class file based on the described useful class for detecting creates the useful class file set of the initialization;
The useless class set initialized described in class name creation based on the described useless class for detecting.
Optionally, the default useful class sample includes the list of default useful class white list, useful class
One or more in directory listing, useful class class name feature list and useful class file feature list;
The creation module is further used for:
Before the class file in recognizing target directory, the mesh is removed based on default code cleaning tool
Invalid code in heading record.
Optionally, the matching module specifically for:
Travel through the useless class set;
Class name in the useless class set is carried out with the class file in the useful class file set successively
Matching;
Class name in the useless class set is matched in the class file in the useful class file set
When, move into the useful class set using the class name as the class name of useful class, and by the class name pair
The class file answered moves into the useful class file set as the class file of useful class.
Optionally, the matching module is further used for:
When moving into the useful class set using the class name as the class name of useful class, and by the class name
After corresponding class file moves into the useful class file set as the class file of useful class, have default
Marked with class and be set as the first mark;Described first marks and newly add in the useful class set for identifying
The class name of useful class;
When the useless class set is not matched in all class files in the useful class file set
In class name when, default useful class mark is set as the second mark;Described second marks for identifying
The class name of useful class is not added in the useful class set newly.
Optionally, the matching module is further used for:
Read the default useful class mark;
Judge to read described useful marks whether to be the described first mark;
When the useful class is judged labeled as the described first mark, the useless class set is traveled through again,
Class name in the useless class set is matched with the class file in the useful class file set successively,
Matching terminates when judging the useful class labeled as the described second mark.
In the application, by creating the useless class set and useful class file set of initialization, will be described
Class name in useless class set is iterated matching in the useful class file set of initialization, to remove
The class name of the useful class in useless class set is stated, when the class name of the useful class in the useless class set is moved
After removing, by the useless class set cooperation for useless class testing result is exported, due to carrying out nothing in the application
During with the detection of class, can take into full account that the reference between the class name of useless class and the class file of useful class is closed
System, such that it is able to more efficiently detect useless class file, for cleaning system redundancy code provides foundation.
Specific embodiment
In the related art, when entering line code cleaning, PMD is being normally based on to android wireless sides
Or the code cleaning tool such as UCDetector is completed.
However, PMD instruments are typically only capable to useless private method, the privately owned attribute of scanning, and code segment
Interior dead code and variable-definition, for other types of its serviceability of code segment None- identified, and
PMD instruments do not possess the scan capability of class rank yet.
Although UCDetector instruments can use class white list by being configured with, standard android works are scanned
Useless class in journey, but UCDetector is when useless class scanning is carried out, between consideration class
Adduction relationship, therefore (can exist in the android engineerings that API is provided for providing the android engineerings of API
Quote the class of the API), UCDetector instruments will be unable to accurate definition and go out useful class set, thus cannot
Scan all useless class in the project.
It can be seen that, existing code cleaning tool cannot meet the code cleaning demand in real work scene.
In view of this, the application proposes a kind of useless class detection method, by the useless class for creating initialization
Set and useful class file set, the class name in the useless class set is literary in the useful class of initialization
Matching is iterated in part set, to remove the class name of the useful class in the useless class set, when described
After the class name of the useful class in useless class set is removed, by the useless class set cooperation for useless class is detected
Result is exported, due to that, when the detection of useless class is carried out, can take into full account the class of useless class in the application
Adduction relationship between name and the class file of useful class, it is achieved thereby that can more efficiently detect useless
Class file, for cleaning system redundancy code provides foundation.
The application is described below by specific embodiment and with reference to specific application scenarios.
Fig. 1 is refer to, Fig. 1 is a kind of useless class detection method that the embodiment of the application one is provided, application
In client, methods described performs following steps:
Step 101, creates the useless class set and useful class file set of initialization;
Step 102, the class name in the useless class set is entered in the useful class file set of initialization
Row iteration is matched, to remove the class name of the useful class in the useless class set;
Step 103, after the class name of the useful class in the useless class set is removed, will be described useless
Class set cooperation is exported for useless class testing result.
Above-mentioned client can include that the client of the software-oriented developer useless class detection service of offer is soft
Part;For example, by taking the exploitation of android engineerings as an example, the client software can be for towards android
Developer provides the useless class detection instrument of useless class detection service, and developer can be useless by this
Class detection instrument carries out useless class detection to the android engineerings for having created.
Wherein, the hardware environment of above-mentioned client software is carried, is not particularly limited in the present embodiment;
For example, it may be PC main frames, mobile terminal or server.
Below with above-mentioned client software to provide useless class detection service towards android developer
Illustrated as a example by useless class detection instrument.
In the present embodiment, when developer carries out useless class detection in the android engineerings to having created
When, all catalogues under the android engineerings can be carried out recurrence by client software as target directory
Scan to recognize all class files of the android engineerings.
Wherein, for android engineerings, because android engineerings use java language as realization
Language, therefore the class file of the android engineerings is usually the system file for extending entitled java, client
End software, can be to extending entitled java systems when class file is recognized in all catalogues of android engineerings
System file is identified.
In addition, client software is before all class files under recognizing the android engineerings, can be with
Invalid code in target directory is removed based on default code cleaning tool, such that it is able to avoid useless class
It is referred in the invalid code such as the useless member variable of useful class or dead code section.
In the present embodiment, after when all class files of the android engineerings are identified, client is soft
All class files that part will can have been identified are matched to detect this with default useful class sample
Useful class and useless class in all class files under android engineerings.
Wherein, the default useful class sample can include by the customized useful class white list of developer
In list, useful class directory listing, useful class class name feature list and useful class file feature list one
Individual or multiple.
For example, when realizing, developer can pre-define out whiteList variables, usedDirList
Variable, four kinds of variables of usedNamePrefList variables and usedContentPrefList variables.
Wherein, whiteList variables, as useful class white list list, what expression need not be processed
The set of class, the class in the list is useful class.UsedDirList variables, as useful class catalogue row
Table, the catalogue under the list is useful class catalogue, and the class in useful class catalogue is useful class.
UsedNamePrefList variables, as useful class class name feature list, the list includes some useful
The class name characteristic character of class, if the character in the list occurs in class name, such is useful class.
UsedContentPrefList variables, as useful class file feature list, list includes some having
With the characteristic character of the class file of class, if the character in the list is in class file (i.e. the content of class)
Occur, then such is useful class.
Based on this, useful class of the client software in all class files for having identified of detection and useless
During class, all class files that can be will identify that are matched respectively more than in four kinds of lists, if
With success, then show the class file for useful class, if matching is unsuccessful, show that the class file is useless
Class.In this way, client software can detect the useful class in all class files for identifying
With useless class.
After useful class and useless class in detecting all class files for identifying, client software can be with
The useful class set of the class name creation initialization based on all useful class for detecting;Can be had based on all
Class file is read with the file path of class, and the useful class text of initialization is created based on the class file for reading
Part set (properties collection of i.e. useful class);The class name wound of all useless class for being also based on detecting
Build the useless class set of initialization.
Wherein, the useless class set of the initialization for being completed for establishment, client software can also record this
The file path of all useless classes in set, so that in follow-up Iterative matching, the nothing in the set
After turning into useful class with class, being easy to be loaded into the class file of the useless class based on this document path above-mentioned is had
With in class file set.
In the present embodiment, in due to the useless class set of initialization, may exist by the class of useful class
The class name that file is quoted, and class is typically used according to class name in android engineerings, therefore in code
In the android engineerings of specification, once the class name in certain useless class set is in useful class file set
Class file in can be matched, then class file corresponding with the class name is necessarily used to, now should
Class name is cited in the class file in useful class file set, thus class file corresponding with the class name is have
Use class.
From this angle, when client software is created that the useful class file set of initialization, useful
After class file set and useless class set, can by by the class name in useless class set successively with it is useful
Class file in class file set is matched to detect the class name of the useful class in useless class set.
Fig. 2 is referred to, a kind of client softwares of the Fig. 2 shown in the present embodiment is by useless class set
Class name is iterated the flow chart of matching in useful class file set.
Client software is starting to be iterated the class name in useless class set in useful class file set
Can be default one useful class mark of useful class set before matching.
Wherein, useful class mark can include the first mark and the second mark, and this first is marked for marking
Know the class name that useful class is newly added in the useful class set, second mark can be used for mark, and this is useful
Do not add the class name of useful class in class set newly.
For example, client software can be default one mark marked as above-mentioned useful class of useful class set
Note position, and for the marker bit defines two kinds of mark values of the first mark and the second mark.
Refer to Fig. 2, client software by the class name in useless class set in useful class file set
When being iterated matching, the useful class mark can be set to default label first, wherein the acquiescence mark
Note can be that the first mark can also be the second mark.
For example, first mark can be the default mark T of useful class set, second mark
It is the default flag F of useful class set that can be, client software is set by the useful class mark
During for default label, the useful class mark can be set to flag F it can also be provided that mark T (figures
The default label shown in 2 is flag F).
Continuing with referring to Fig. 2, after the useful class mark is set to default label by client software, visitor
Family end software can travel through useless class set, by the class name in useless class set successively with useful class file collection
Class file in conjunction is matched to detect the class name of the useful class in useless class set.
On the one hand, if in matching the useless class set in class file in useful class file set
During a certain class name, now the class name is quoted by useful class file, and the corresponding class file of the class name is useful
Class, client software can move into useful class set using such as the class name of useful class, while may be used also
With the file path based on the pre-recorded class name, using the corresponding class file of the class name as useful class
Class file moves into useful class file set.
When such is moved into useful class set by client software, and by such
After the corresponding class file of name moves into useful class file set as the class file of useful class, now client is soft
Above-mentioned useful class mark can be set to the first mark (shown in Fig. 2 first is labeled as mark T) by part,
The class name of useful class is newly added in the useful class set to identify.
Wherein, client software is by above-mentioned useful class mark when the first mark is set to, if client
Software is that the default label that the useful class mark is set is the second mark, and now client can be useful by this
Class mark is changed to the first mark by the second mark, and then client software can be read in useless class set
Next class name is taken to proceed to match with the class file in useful class file set, by that analogy, until
Class name in useless class set matches completion.If client software is the silent of the useful class mark setting
Recognize labeled as the first mark, now client can not be altered, under directly being read in useless class set
One class name proceeds to match with the class file in useful class file set, by that analogy, until useless
Class name in class set matches completion.
On the other hand, if not matching the useless class set in class file in useful class file set
In a certain class name, now the class name not by useful class class file quote, the corresponding class file of the class name
It is useless class, client software can read next class name with useful class file collection in useless class set
Class file in conjunction proceeds matching, by that analogy, until the class name in useless class set has been matched
Into.
Certainly, if in not matching the useless class set in class file in useful class file set
Class name, now the class name in the useless class set is not by the class file reference of useful class, the useless class set
The corresponding class file of class name in conjunction is useless class, and in this case, client can have above-mentioned
Marked with class and be set to the second mark (shown in Fig. 2 second is labeled as flag F), it is useful to identify this
Do not add the class name of useful class in class set newly.
Wherein, client software is by above-mentioned useful class mark when the second mark is set to, if this is useful
Class mark is the first mark by client default setting, and now client can be by the useful class mark by the
One mark is changed to the second mark, if the useful class mark is the second mark by client default setting,
Now client can not be altered, and now epicycle iteration terminates.
Above in association with Fig. 2 describe client software by the class name in useless class set in useful class file
When matching is iterated in set one process for taking turns iteration.
In the present embodiment, after a wheel iteration terminates, if with newly adding useful class in class set
Class name after, now be may possibly still be present by the class file institute of the useful class of the new addition in useless class set
Use the class name for arriving, the i.e. class name cited in the class file of the useful class of the new addition.
Therefore, in this case, if with the class name that useful class is newly added in class set, now
Client software can continue start next round iteration, until in useless class set there is no new useful class
Class name add when stop.
Continuing with referring to Fig. 2, after a wheel iteration terminates, client software can read above-mentioned useful class
Mark, and judge to read described useful mark whether to be described first to mark.
If client software judges the useful class labeled as the first mark, now in useless class set
Still the class name of useful class is there may be, client software can carry out next round according to above iterative process
Iteration, useful class mark is set to default label, and useless class set is traveled through again, by the nothing
Matched with the class file in useful class file set successively with the class name in class set, detailed process is not
Repeat again.
It can be seen that, in this way, client software, can constantly by useless class by iteration repeatedly
The class name of the useful class in set removes useless class set, and is moved into useful class set.
Continuing with referring to Fig. 2, after client software is by iterating, finally judge what is read
When useful mark is for mark, the class name of the useful class in useless class set is all moved into useful class
Set, now Iterative matching terminates, and remaining class name is final useless class detection in useless class set
As a result.
Now, client software can by current useless class set cooperation be useless class testing result to exploitation
Personnel export, and developer can be based on the useless class testing result of client software output, remove and work as
Useless class in preceding android engineerings.
It can be seen that, in this way, can take into full account the class name of useless class and useful class class file it
Between adduction relationship, such as UCDetector instruments can be avoided due to not considering the adduction relationship between class,
And useful class set cannot be gone out without accurate definition, thus being whether there is in android engineerings cannot be scanned
With the problem of class.So as to more efficiently detect useless class file.
In addition, what deserves to be explained is, Fig. 2 is shown in flow, and client starts in useless class set
When class name is iterated matching in useful class file set, can not also be in another implementation
The useful class mark sets default label, but directly performs follow-up matching step, travels through described useless
Class set, the class name in useless class set is matched with the class file in useful class file set successively,
After a wheel iteration terminates, come to set the first mark or second for the marker bit further according to iteration result
Mark, then client software can be by judging that the useful class marks whether to determine to be for the first mark
The no iteration for starting next round.
For example, client software still can preset one as above-mentioned useful category for the useful class set
The marker bit of note, and for the marker bit defines two kinds of mark values of the first mark and the second mark, and repeatedly
Only retain the marker bit during generation matching beginning, client software is not that should specifically be marked labeled as setting
Value, but follow-up matching step is directly performed, after a wheel iteration terminates, further according to iteration knot
Fruit to set the first mark or the second mark for the marker bit, repeats no more.
In the embodiment above, by creating the useless class set and useful class file set of initialization,
Class name in the useless class set is iterated matching in the useful class file set of initialization, with
The class name of the useful class in the useless class set is removed, when the class of the useful class in the useless class set
Name be removed after, will the useless class set cooperation be useless class testing result output, due in the application
When carrying out the detection of useless class, can take into full account between the class name of useless class and the class file of useful class
Adduction relationship, is cleaning system redundancy generation it is achieved thereby that useless class file can be detected more efficiently
Code provides foundation.
It is corresponding with above method embodiment, present invention also provides the embodiment of device.
Fig. 3 is referred to, the application proposes a kind of useless class detection means 30, is applied to client;Wherein,
Refer to Fig. 4, as the hardware structure carried involved by the client of the useless class detection means 30 in,
Generally include CPU, internal memory, nonvolatile memory, network interface and internal bus etc.;With software
As a example by realization, the useless class detection means 30 is it is generally understood that the computer journey being carried in internal memory
Sequence, the logic device that the software and hardware formed after being run by CPU is combined, described device 30 includes:
Creation module 301, useless class set and useful class file set for creating initialization;
Matching module 302, for by the class name in the useless class set initialization useful class file
Matching is iterated in set, to remove the class name of the useful class in the useless class set;
Output module 303, for after the class name of the useful class in the useless class set is removed, inciting somebody to action
The useless class set cooperation is exported for useless class testing result.
In the present embodiment, the creation module 301 specifically for:
Class file in identification target directory;
The class file that will identify that is matched to detect the class file with default useful class sample
In useless class and useful class;
The useful class set of the class name creation initialization based on the described useful class for detecting;
Class file based on the described useful class for detecting creates the useful class file set of the initialization;
The useless class set initialized described in class name creation based on the described useless class for detecting.
In the present embodiment, the default useful class sample include the list of default useful class white list,
One in useful class directory listing, useful class class name feature list and useful class file feature list or
It is multiple;
The creation module 301 is further used for:
Before the class file in recognizing target directory, the mesh is removed based on default code cleaning tool
Invalid code in heading record.
In the present embodiment, the matching module 302 specifically for:
Travel through the useless class set;
Class name in the useless class set is carried out with the class file in the useful class file set successively
Matching;
Class name in the useless class set is matched in the class file in the useful class file set
When, move into the useful class set using the class name as the class name of useful class, and by the class name pair
The class file answered moves into the useful class file set as the class file of useful class.
In the present embodiment, the matching module 302 is further used for:
When moving into the useful class set using the class name as the class name of useful class, and by the class name
After corresponding class file moves into the useful class file set as the class file of useful class, have default
Marked with class and be set as the first mark;Described first marks and newly add in the useful class set for identifying
The class name of useful class;
When the useless class set is not matched in all class files in the useful class file set
In class name when, default useful class mark is set as the second mark;Described second marks for identifying
The class name of useful class is not added in the useful class set newly.
In the present embodiment, the matching module 302 is further used for:
Read the default useful class mark;
Judge to read described useful marks whether to be the described first mark;
When the useful class is judged labeled as the described first mark, the useless class set is traveled through again,
Class name in the useless class set is matched with the class file in the useful class file set successively,
Matching terminates when judging the useful class labeled as the described second mark.
Those skilled in the art will readily occur to this after considering specification and putting into practice invention disclosed herein
Other embodiments of application.The application is intended to any modification, purposes or the adaptability of the application
Change, these modifications, purposes or adaptations follow the general principle of the application and including this Shen
Please undocumented common knowledge or conventional techniques in the art.Description and embodiments only by
It is considered as exemplary, the true scope of the application and spirit are pointed out by following claim.
It should be appreciated that the application be not limited to be described above and be shown in the drawings it is accurate
Structure, and can without departing from the scope carry out various modifications and changes.Scope of the present application is only by institute
Attached claim is limited.
The preferred embodiment of the application is the foregoing is only, is not used to limit the application, it is all at this
Within the spirit and principle of application, any modification, equivalent substitution and improvements done etc. should be included in
Within the scope of the application protection.