PMD is a static code analysis tool. Static code analysis is a way of analysing computer software without actually executing the code. (Analysis on running programs is known as "dynamic analysis".) Usually static analysis is performed by an automated tool, such as PMD.
Why should I care?
The main issues with this type of tool are false positives. But you can handle those by disabling rules, on a per-rule basis (completely or for specific situations). And, with the source code available, you can improve any rule to correct bugs.
What does PMD look for?
PMD scans Java source code for potential problems such as:
empty try/catch/finally/switch statements
unused local variables, parameters, and private methods
wasteful String/StringBuffer usage
unnecessary if statements, for loops that could be while loops
copied/pasted code — which could mean copied/pasted bugs
PMD provides a rich and complete set of rules that apply to any Java application. Please check the PMD website for more information.
PMD is easy to use, integrate, and extend PMD's analysis can be integrated with most popular IDEs. (PMD is integrated with JDeveloper, Eclipse, JEdit, JBuilder, BlueJ, CodeGuide, NetBeans/Sun Java Studio Enterprise/Creator, IntelliJ IDEA, TextPad, Maven, Ant, Gel, JCreator, and Emacs.) You can also use it to build custom tools, and extend it with custom rules — such as those created for JSLEE.
What are the JSLEE PMD rules?
The JSLEE PMD rules are a set of static code analysis rules to prevent common bugs and enforce best practices when developing JSLEE applications. Release 0.7 includes design, naming, tracing rules, and CMP specific rules.. (Note that the categories and rule names are subject to change until release 1.0.)
Each rule includes a priority, as follows. P1 Change absolutely required. Behavior is critically broken/buggy. P2 Change highly recommended. Behavior is quite likely to be broken/buggy. P3 Change recommended. Behavior is confusing, perhaps buggy, and/or against standards/best practices. P4 Change optional. Behavior is not likely to be buggy, but more just flies in the face of standards/style/good taste. P5 Change highly optional. Nice to have, such as a consistent naming policy for package/class/fields...
JSLEE PMD source code The source code of the JSLEE PMD rules is available here.
Prefer the Tracer Interface instead of proprietary logging frameworks.
Rulesets group rules with common characteristics in sets that you can configure. At the moment there is a jainslee ruleset with all the rules, and one ruleset for each kind of SLEE component (sbb, event, profile, ratype, and ra).
Create a new fileset or update the above one to define it as the classpath of that task, and include the JSLEE PMD library file (jainslee-pmd-rules-0.7.jar) and the Java source files to analyze. Define the rulesets as necessary.
If you execute the command ant jsleepmd, PMD will analyze the source code and write a report in HTML with the results to pmd/pmd.html. Many other options are available to customize the analysis (for example, minimum priority of rules) and the output type (such as text or xml).
To configure JSLEE PMD Rules with Maven, add the dependency of the Maven PMD Plugin and the dependency of the JSLEE PMD Rules to the pom.xml file of the project. For example:
Note that for Maven to automatically resolve the JSLEE PMD Rules dependency, you'll need to have the OpenCloud Maven repository configured.
You can search for violation directly from a command-line console. The following example command instruments the classes in the directory "src" with the "profile" and "sbb" rulesets, and writes an HTML report file:
java -jar jainslee-pmd-rules-x.x.jar src html profile.xml,sbb.xml -reportfile report.html
Execute the jar file without arguments to see the help text. See here full information about the arguments.
Tips and tricks
Below are the following tips and tricks for using PMD:
Please report to OpenCloud using the form below any improvement you think it can be made to particular rules and false positives that are annoying you.
If you are an advanced user, the source code of the rules is available on the public source code repository here. Please check out the source code, modify and build your own rules jar, and contribute the changes back!