PMD and CPD in Maven

PMD

PMD is an Open Source, rule based, static source code analyzed that analyzes java source code based on the evaluative rules that have been enabled during given execuetion. The tool comes with a default set of rules which can be used to unearth, common development mistakes such as having emty try catch blocks, variables , that never used, objects that are unecessary etc.

Regardless of how experienced and talented programmer may be, he/she is likely to make mistakes while writing programs. Unfortunatly while for smaller program these mistakes are usually few and offen have limited infact only. As program grow, even small mistakes start having unexpectedly severe impact.

CPD

Duplicate code can be hard to find, especially in a large project. But PMD's Copy/Paste Detector (CPD) can find it for you!
CPD will not identify duplicates based on the logic and will identify based on the String which are matichig. For suppose if we have same logic with different variable will not identify as vioaltion by CPD.

Note that CPD works with Java, JSP, C, C++, Fortran and PHP code. CPD is included with PMD


PMD checks source code against rules and produces a report. Like this:

  • Something passes a file name and a RuleSet into PMD
  • PMD hands an InputStream to the file off to a JavaCC-generated parser
  • PMD gets a reference to an Abstract Syntax Tree back from the parser
  • PMD hands the AST off to the symbol table layer which builds scopes, finds declarations, and find usages.
  • If any rules need data flow analysis, PMD hands the AST over to the DFA layer for building control flow graphs and data flow nodes.
  • Each Rule in the RuleSet gets to traverse the AST and check for problems. The rules can also poke around the symbol table and DFA nodes.
  • The Report is now filled with RuleViolations, and those get printed out in XML or HTML or whatever

MORE ABOUT PMD and CPD

     »  Maven setup for PMD and CPD

     »  PMD plugin for Eclipse

     »  How to make a pmd rule set in maven

     »  How to create PMD custom rule in ruleset