It has been aware of PMD, and after understanding the principles of its work, you can approach the rule of the PMD for code check (Rules).
PMD comes with code inspection rules
In "Modify Your Codes with PMD (1)", I used PMD for plugins for JBuilder IDE. In the introduction of this plugin installation, I mentioned 4 * .jar in the "... / jbuilder / lib / ext" directory. The "PMD-1.5.jar" is guaranteed and declared by PMD code check rules.
Each inspection rule of PMD is existing in the form of a Java class, that is, PMD's check rules for a code specification are defined and described in Java languages. In "PMD-1.5.jar / RuleSets", these rules are used in metadata (Metadata) defined in "PMD-1.5.jar / rulesets" to read, understand, description, and examples of each rule.
The rule of the PMD-1.5.jar and its description XML document (note that it is in * .jar):
Increasing Rules Description XML is an understanding of each rule when helping developers use PMD to quickly modify or reconstruct their code. In JBuilder, click on the menu "Tools-> PMD-> Configure PMD" to enter the PMD configuration dialog box, you can configure the code check rules for PMD, register to add new custom checks. Rules or definitions, descriptions, and examples of a certain rule. The following figure is a description and example of the EMPTY TRY BLOCK in the PMD's Basic Rules. These descriptions and examples are all written in the corresponding XML document "Basic.xml" in "PMD-1.5.jar / Rulesets".
Through this graphical interface, you can make a comprehensive understanding of the rules from the PMD. Also from a side, I understand that high quality code and how far is my code. Of course, many rules don't necessarily be mandatory, or be sure to be your project group specification, you will take yourself.
2. Seek rules
All Rules Java categories are inherited in the "Net.SourceForge.Pmd.Abstractrules.class" abstraction class and take effect under the support of "Net.SourceForge.pmd. *". The rules of the rules are of course based on the abstract semantic tree (AST, ABSTRACT SYNTAX TREE) mentioned earlier. Still take a look in the actual environment.
In the installation directory of the PMD, find "ASTVIEWER.BAT". Note that it is not the directory where the PMD's JBuilder plugin is located, in my environment is "c: /pmd-1.8/etc/astViewer.bat". Run the command, you can call up the AST analyzer that can analyze a Java code to an abstract scribe tree (AST).
AST constitutes a tree in an unique code as a number of blocks (blocks, blocks). For a simple example: If one IF in our code is nested another IF, in AST, it is a branch of the IF block, and the other IF block is the leaves on it. Or take the "Empty Try Block" rule in the "Basic Rules" as an example to recognize the following AST.
Copy the "Empty Try Block" code of the "Empty Try Block" rule in the first window in "AST Viewer", and add a class appearance (ie: put it in a class, AST Viewer can be identified) , Then click the "Go" button, and the AST of this section will appear in the middle of one window. The PMD's inspection rules are based on the AST analysis of the code and is taken in effect in an event-based approach. It is a bit similar to the resolution of the SAX to the XML document, a PMD check rule (Rules) is an event handling class, when a particular code block (block, one node in AST) occurs, ie, the rule's judgment function . For example, in the picture shown above, we can judge whether there is other meaningful semantic nodes before "name: exception" in the "name: Exception" block. If not, it is illegal to "EMPTY TRY Block" rules.
Of course, such rules development requires a set of libraries, namely "net.sourceforge.pmd. *".
3. Summary
For the understanding of the PMD Rules (Rule), it is divided into understanding of its rules and understanding of their rule. The former is to help us improve our quality; the latter is to develop our own rules to improve our engineering quality. Next, let's write a personal PMD rule. Series 1: Modify Your Codes with PMD (1)