Code Static resolution PMD

Source: Internet
Author: User

Before formally entering the test, it is helpful to do some static code analysis and code review to improve the quality and the system, the above is the data proof

PMD It is a Java source code parser based on a static rule set that identifies potential problems such as:
– Possible bug--empty try/catch/finally/switch blocks.
– Useless codes (Dead code): Useless local variables, method arguments, and private methods.
– An empty If/while statement.
– Overly complex expressions-unnecessary if statements, which can be used while loops but with a for loop.
– Optimized code: Wasteful performance of the use of String/stringbuffer.

Download PMD, configure classpath content, and more


    • Java JRE 1.6 or higher
    • Winzip or the free 7-zip

Unzip the package on the C drive

CMD and then enter the following:

-DIR Specifies the directory,-f specifies the format of the resulting results-R uses what rules to detect

Rulesets/java/*.xml specific address in Pmd-bin-5.4.2\bin

The rules are as follows:

PMD contains 16 rule sets that cover a wide range of Java FAQs, some of which are more controversial than others:

  • Basic (Rulesets/basic.xml)-a basic collection of rules that most developers might disagree with: catch blocks should not be empty, rewritten whenever they are rewritten, and equals() hashCode() so on.
  • naming (Rulesets/naming.xml)-Testing of Standard Java command specifications: Variable names should not be too short; method names should not be too long; class names should start with lowercase letters, method and field names should start with lowercase letters, and so on.
  • Unused code (rulesets/unusedcode.xml)-finds private and local variables that have never been used, statements that are not executed, private methods that have never been called, and so on.
  • Design (rulesets/design.xml)-Check various well-designed principles, such as: switch statements should have default blocks, should avoid deeply nested if blocks, should not be re-assigned to parameters, should not be equal to the double value comparison.
  • import Statement (rulesets/imports.xml)--Checks the import statement for problems, as if a class was imported two times or imported into a java.lang class.
  • JUnit Test (rulesets/junit.xml)--Find specific questions about test cases and test methods, such as the correct spelling of method names, and suite() whether the method is static and public.
  • string (rulesets/string.xml)--find common problems encountered when working with strings, such as repeating string scalars, calling String constructors, String calling methods on variables toString() .
  • parentheses (rulesets/braces.xml)-check for , if ,, while and else statement whether parentheses are used.
  • code size (rulesets/codesize.xml)--a method that tests too long, a class with too many methods, and a similar problem with refactoring.
  • Javabean (rulesets/javabeans.xml)--see if the Javabean component violates the Javabean encoding specification, such as a bean class that is not serialized.
  • finalization function (finalizer)--because in the Java language, finalize() methods are not so common (I have been writing this code many years ago), so their rules of use are very detailed, but people are relatively unfamiliar with them. Such checks look for finalize() various problems with methods, such as empty finalization functions, methods that call other methods finalize() , finalize() explicit calls to, and so on.
  • Clone (Rulesets/clone.xml)-- clone() A new rule for methods. clone()any class that overrides the method must be implemented Cloneable , the clone() method should be called super.clone() , and the clone() method should declare the exception to be thrown CloneNotSupportedException , even if it does not actually throw an exception.
  • coupling (rulesets/coupling.xml)-Find signs of over-coupling between classes, such as importing too much, using subclasses when the superclass or interface is sufficient, and too many fields, variables, and return types in the class.
  • Strict Exceptions (Rulesets/strictexception.xml)-Tests for exceptions: You should not declare the method without throwing an java.lang.Exception exception, you should not use exceptions for flow control, should not be captured Throwable , and so on.
  • Some of the rules of controversial (rulesets/controversial.xml)--PMD are acceptable to Java programmers who have the ability to do so. But there is still some controversy. This ruleset contains some more problematic tests, including assigning null to a variable, multiple return points in a method, and importing from a sun package.
  • log (Rulesets/logging-java.xml)- java.util.logging.Logger improper use of lookups, including non-final states (nonfinal), non-static loggers, and multiple loggers in a class.

If you want to detect the required rules at once, you can do the following

1, the existing rules using a comma to isolate, rulesets/java/strings.xml,rulesets/java/xx.xml, etc.

2. Define your own rule set

<?xml version= "1.0"? ><ruleset name= "Customruleset" >  <description>  Sample ruleset for DeveloperWorks article  </description>  <rule ref= "Rulesets/design.xml"/> <rule ref=  " Rulesets/naming.xml "/>  <rule ref=" Rulesets/basic.xml "/></ruleset>

A more granular set of rules:

<?xml version= "1.0"? ><ruleset name= "specific rules" >  <description>  Sample ruleset for DeveloperWorks article  </description>  <rule ref= "rulesets/design.xml/ Avoidreassigningparametersrule "/>  <rule ref=    " rulesets/design.xml/ Constructorcallsoverridablemethod "/>  <rule ref=" rulesets/design.xml/finalfieldcouldbestatic "/>  <rule ref= "rulesets/design.xml/defaultlabelnotlastinswitchstmt"/>  <rule ref= "rulesets/ Naming.xml/longvariable "/>  <rule ref=" Rulesets/naming.xml/shortmethodname "/> <rule  ref=" Rulesets/naming.xml/variablenamingconventions "/>  <rule ref=" rulesets/naming.xml/ Methodnamingconventions "/>  <rule ref=" rulesets/naming.xml/classnamingconventions "/>  <rule ref= "Rulesets/basic.xml/emptycatchblock"/>  <rule ref= "Rulesets/basic.xml/emptyfinallyblock"/>< /ruleset>

To exclude a specific rule:

<?xml version= "1.0"? ><ruleset name= "DW rules" >  <description>  Sample ruleset for DeveloperWorks article  </description>  <rule ref= "Rulesets/design.xml" >    <exclude name= " Switchstmtsshouldhavedefault "/>  </rule></ruleset>

You can export the results of a test to a place using the-R parameter

Online Documentation:

We have created our own facebankrules.xml files, simply containing some of the rules we consider important.

Modify the and add the Facebankrules.xml file to the compressed package

Facebank's own rules are as follows;

<?xml version= "1.0"?>
<ruleset name= "Facebankrules" >
Sample ruleset for Facebank developers
<rule ref= "Rulesets/java/unusedcode.xml"/>
<rule ref= "Rulesets/java/design.xml"/>
<rule ref= "Rulesets/java/imports.xml"/>
<rule ref= "Rulesets/java/strings.xml"/>
<rule ref= "Rulesets/java/braces.xml"/>
<rule ref= "Rulesets/java/codesize.xml"/>
<rule ref= "Rulesets/java/javabeans.xml"/>
<rule ref= "Rulesets/java/coupling.xml"/>
<rule ref= "Rulesets/java/strictexception.xml"/>
<rule ref= "Rulesets/java/logging-java.xml"/>
<rule ref= "Rulesets/java/sunsecure.xml"/>

Code Static resolution PMD

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.