Test Tool PMD Use

Source: Internet
Author: User
Tags basic html page xslt
First, basic knowledge
PMD is a tool for parsing Java code errors. Unlike other analysis tools, PMD learns code errors through static analysis. That is, you report an error without running the Java program. PMD comes with a number of rules that can be used directly, using these rules to identify many problems with Java source programs, such as unused variables, extra variable creation operations, empty catch blocks, and so on. In addition, users can define their own rules to check whether Java code conforms to certain coding specifications.
PMD scans the Java code and looks for potential problems: 1. Possible bugs-null Try/catch/finally/switch statement 2. Invalid code-unused variables, parameters, and private methods 3. Non-optimal code-resource-intensive string/ StringBuffer usage 4. Overly complex expression-unnecessary if statement, or should be a while for loop 5. Duplicate code-copy/paste code means copy/paste bug
(2)
PMD Rules:
Such as:
C:\TOOLS\PMD-4.1\BIN>PMD c:\project\***\src html>a.html Rulese
Ts/unusedcode.xml,rulesets/basic.xml
Another example:
C:\Documents and settings\904031\ Desktop \findbugs+pmd\pmd-4.2.5\bin>
PMD F:\s2is\dm\dm\java xml>c:\1.xml rulesets/unusedcode.xml PMD contains 16 rule sets that cover various common problems with Java, some of which are more controversial than other rules:
Basic (Rulesets/basic.xml)--a basic collection of rules that most developers may disagree with: a catch block should not be empty, rewrite hashcode () whenever you rewrite Equals (), and so on.
Name (rulesets/naming.xml)--Test for 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, methods 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 invoked, and so on.
Design (rulesets/design.xml)--check for a variety of well-designed principles, such as: switch statements should have a default block, you should avoid deep nested if block, should not be assigned to the 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 issues with test cases and test methods, such as the correct spelling of method names, and whether the suite () method is static and public.
String (rulesets/string.xml)--find common problems encountered when handling strings, such as repeating string literals, calling the string constructor, and calling the ToString () method on a string variable.
Parentheses (Rulesets/braces.xml)-Checks whether the for, if, while and ELSE statements use parentheses.
Code size (rulesets/codesize.xml)--Tests for too-long methods, classes with too many methods, and similar problems 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.
End functions (finalizer)-Because the Finalize () method is not so common in the Java language (I wrote this code a few years ago), their rules of use are very detailed, but people are relatively unfamiliar with them. Such checks look for various problems with the Finalize () method, such as empty finalization functions, the Finalize () method of calling other methods, explicit calls to finalize (), and so on.
Clone (Rulesets/clone.xml)--The new rule for the Clone () method. All classes overriding the Clone () method must implement Cloneable, the Clone () method should call Super.clone (), and the Clone () method should declare the clonenotsupportedexception exception thrown. This is true even if the exception is not actually thrown.
Coupling (Rulesets/coupling.xml)--find signs of excessive coupling between classes, such as too many imported content, use subclass types 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 and throw java.lang.Exception exceptions, you should not use exceptions for flow control, you should not capture throwable, and so on.
Some of the rules of the controversial (Rulesets/controversial.xml)--PMD are acceptable to capable Java programmers. But there is still some controversy. This rule set contains some more problematic tests, including assigning null to variables, multiple return points in methods, and importing from sun packs.
Log (Rulesets/logging-java.xml)--finds improper use of java.util.logging.Logger, including non-final status (nonfinal), non-static loggers, and multiple loggers in a class.
(3)
PMD various monitors:
PMD comes with 4 differents type of Renderers:1.text, which does a basic text ouput of PMD ' s;
2.xml, which format as XML;
3.html, which format the result as a very basic HTML page;
4.nicehtml, which is ' better ' HTML view of the result. As this feature use a XSLT transformation, it also possible to override using Stylesheet The-xslt.
Such as:
Java net.sourceforge.pmd.pmd/path/to/source nicehtml basic,imports,unusedcode-xslt my-own-stylesheet.xsl (3)
Error level:
PMD Priority 1-> severity error, priority high
PMD Priority 2-> severity error, priority normal
PMD Priority 3-> severity warning, priority high
PMD Priority 4-> severity warning, priority normal
PMD Priority 5-> severity information, priority normal
That
PMD Priority 1-"serious error, priority high
PMD Priority 2-"Critical error, priority normal
PMD's Priority 3-"critical warning, high priority
PMD's Priority 4-"critical warning, priority normal
PMD's Priority 5-"serious information, precedence even if PMD Priority 1, does not mean code error, but code is not specification." Such as:
The static final variable is not all uppercase.
Static final Sysloger---static final sysloger----------------------------------------
PMD detection code and generate HTML test report 1. Download the PMD compression pack latest Pmd4.2.5.zip 2. Extract 3. Create the following script file (Checkall.bat), placed in the Unpacked Bin directory: @echo off REM AUTHOR:HWJ 2010-01-27 REM The files and paths to check
Path of Set filepath=c:\12 REM report output
Set reportpath=c:\code-check-report\ REM Gets the current date and time seconds
Set nowtime=%date:~0,10% ""%time:~1,7% REM Replacement: for-
Set nowtime=%nowtime::=-% REM Synthesis report file path name
Set outfile=%reportpath%%nowtime%.html rem Specifies output rendener
REM Set xslt= ". /etc/xslt/corley-pmd-report.xslt "
REM Set xslt= ". /etc/xslt/only-prio1.xslt "
REM Set xslt= ". /etc/xslt/pmd-report.xslt "
REM Set xslt= ". /etc/xslt/only-prio1.xslt "
Set xslt= ". /etc/xslt/wz-pmd-report.xslt "
REM Specifies the check rule, which includes all the rules of the officially launched Java
Set ruleset= "Rulesets/internal/all-java.xml" rem execution check and output report
PMD%filepath% nicehtml%ruleset%-xslt%xslt%-reportfile%outfile%
The above script is set to only Filepath,reportpath. Generate HTML, as follows:----------------------------------------
Integrate PMD into Ant. 1. Optionally, copy the jar files under the PMD lib to the PMD directory in the project's build directory. This is set directly to PMD's Lib directory 2. Open Build.xml, add the following section:--here, create a new file directly Checkall.xml
<project name= "PMD" default= "PMD" basedir=. /">
<path id= "Pmd.classpath" >
<pathelement location= "${basedir}/"/>
<fileset dir= "lib/" >
<include name= "*.jar"/>
</fileset>
</path> <target name= "PMD" >
<taskdef name= "PMD" classname= "Net.sourceforge.pmd.ant.PMDTask" classpathref= "Pmd.classpath"/> &LT;PMD Shortfilenames= "true" >
<ruleset>unusedcode</ruleset>
<ruleset>basic</ruleset>
<ruleset>imports</ruleset> <formatter type= "html" tofile= "pmd_report.html" toconsole= "true"/> <fileset dir= "C:\12" >
<include name= "**/*.java"/>
</fileset>
</pmd>
</target>
</project> where the path section is the directory that defines the jars needed for PMD is Pmd.classpath,${basedir} is the directory where the project is located, and the target part is the part that performs the PMD, Taskdef defines the class that executes the task as Net.sourceforge.pmd.ant.PMDTask, and its classpath is Pmd.classpath. The PMD section is the PMD implementation setting,<ruleset> specifies its check rules, formatter is the result output file. Fileset is the file target path that is checked by PMD. After you save the Checkall.xml, you can use Ant-buidlfile checkall.xml to perform the check. ----------------------------------------
Integrate PMD into eclipse.
Just unpack this package:pmd-eclipse-1.8.0.zip--> in plugins in Eclipse, reboot eclipse--> Right click Select Project-->pmd--> Select first option Toggle PMD Nature--> produces PMD check results in question in eclipse--> to disappear, then select Project-->pmd--> Select the first option toggle PMD nature.

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: info-contact@alibabacloud.com 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.