forked from phoedos/pmd
85ca507481
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@3586 51baf565-9d33-0410-a72c-fc3788e3496d
2973 lines
104 KiB
XML
2973 lines
104 KiB
XML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<ruleset name="">
|
|
<description/>
|
|
<rule name="ExplicitCallToFinalize" message="Explicit call of finalize method" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> An explicit call was made to a finalize method. Finalize methods are meant to be executed at most once (by the garbage collector). Calling it explicitly could result in the method being executed twice for that object (once by you, once by the garbage collector). </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public void close() {
|
|
finalize(); // this is bad
|
|
foo.finalize(); // this is also bad
|
|
this.finalize(); // this is bad but currently not flagged
|
|
super.finalize(); // this is OK
|
|
foo.finalize(3); // this is arguably OK because the method is overloaded
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//PrimaryExpression[PrimarySuffix
|
|
/Arguments[count(*) = 0]]
|
|
/PrimaryPrefix
|
|
/Name[@Image = 'finalize' or ends-with(@Image, '.finalize')]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="TooManyFields" message="Too many fields" class="net.sourceforge.pmd.rules.design.TooManyFields">
|
|
<description> Classes that have too many fields could be redesigned to have less fields and some nested object grouping some of the information collected on the many fields. </description>
|
|
<example><![CDATA[
|
|
|
|
public Class Person {
|
|
String street;
|
|
int number;
|
|
int floor;
|
|
String postal;
|
|
String street;
|
|
long phone;
|
|
String city;
|
|
String state;
|
|
String Country;
|
|
// fields above should be in a class for Address or something similar
|
|
// that information does not really belong to a person, but to a place
|
|
// fields below are really from person
|
|
String firstname;
|
|
String lastname;
|
|
Date born;
|
|
Person father;
|
|
Person mother;
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="maximum" value="10"/>
|
|
</properties>
|
|
</rule>
|
|
<rule name="DontImportJavaLang" message="Avoid importing anything from the package 'java.lang'" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid importing anything from the package 'java.lang'. These classes are automatically imported (JLS 7.5.3). </description>
|
|
<example><![CDATA[
|
|
|
|
// this is bad
|
|
import java.lang.String;
|
|
public class Foo {}
|
|
|
|
// --- in another source code file...
|
|
|
|
// this is bad
|
|
import java.lang.*;
|
|
|
|
public class Foo {}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ImportDeclaration
|
|
[starts-with(Name/@Image, 'java.lang')]
|
|
[not(starts-with(Name/@Image, 'java.lang.ref'))]
|
|
[not(starts-with(Name/@Image, 'java.lang.reflect'))]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AssignmentToNonFinalStatic" message="Possible unsafe assignment to a non-final static field in a constructor." class="net.sourceforge.pmd.rules.design.AssignmentToNonFinalStatic" symboltable="true">
|
|
<description> Identifies a possible unsafe usage of a static field. </description>
|
|
<example><![CDATA[
|
|
|
|
public class StaticField {
|
|
static int x;
|
|
|
|
public FinalFields(int y) {
|
|
x = y;
|
|
}
|
|
|
|
}
|
|
Identifies the unasignment to x as possibly unsafe.
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="AbstractNaming" message="Abstract classes should be named 'AbstractXXX'" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Abstract classes should be named 'AbstractXXX'. </description>
|
|
<example><![CDATA[
|
|
|
|
public abstract class Foo { // should be AbstractFoo
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ClassOrInterfaceDeclaration
|
|
[@Abstract='true' and @Interface='false']
|
|
[starts-with(@Image,'Abstract') = 0]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="InstantiationToGetClass" message="Avoid instantiating an object just to call getClass() on it; use the .class public member instead" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid instantiating an object just to call getClass() on it; use the .class public member instead </description>
|
|
<example><![CDATA[
|
|
|
|
class Foo {
|
|
Class c = new String().getClass();
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//PrimarySuffix
|
|
[@Image='getClass']
|
|
[parent::PrimaryExpression
|
|
[PrimaryPrefix/AllocationExpression]
|
|
[count(PrimarySuffix) = 2]
|
|
]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="ExcessiveImports" message="A high number of imports can indicate a high degree of coupling within an object." class="net.sourceforge.pmd.rules.ExcessiveImports">
|
|
<description> A high number of imports can indicate a high degree of coupling within an object. Rule counts the number of unique imports and reports a violation if the count is above the user defined threshold. </description>
|
|
<example><![CDATA[
|
|
|
|
import blah.blah.Bardo;
|
|
import blah.blah.Hardo;
|
|
import blah.blah.Bar;
|
|
import blah.net.ObjectA;
|
|
//imports over some threshold
|
|
public class Foo {
|
|
public void doWork() {}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="minimum" value="30"/>
|
|
</properties>
|
|
</rule>
|
|
<rule name="MethodWithSameNameAsEnclosingClass" message="Classes should not have non-constructor methods with the same name as the class" class="net.sourceforge.pmd.rules.MethodWithSameNameAsEnclosingClass">
|
|
<description> Non-constructor methods should not have the same name as the enclosing class. </description>
|
|
<example><![CDATA[
|
|
|
|
public class MyClass {
|
|
// this is bad because it is a method
|
|
public void MyClass() {}
|
|
// this is OK because it is a constructor
|
|
public MyClass() {}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="CallSuperInConstructor" message="It is a good practice to call super() in a constructor" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> It is a good practice to call super() in a constructor. If super() is not called but another constructor, such as an overloaded constructor, of the class is called, this rule will not report it. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo extends Bar{
|
|
|
|
public Foo() {
|
|
// call the constructor of Bar
|
|
super();
|
|
}
|
|
|
|
public Foo(int code) {
|
|
// do something with code
|
|
this();
|
|
// no problem with this
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ConstructorDeclaration[
|
|
count(.//ExplicitConstructorInvocation)=0
|
|
]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="UseLocaleWithCaseConversions" message="When doing a String.toLowerCase()/toUpperCase() call, use a Locale" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> When doing a String.toLowerCase()/toUpperCase() call, use a Locale. This avoids problems with certain locales, i.e. Turkish. </description>
|
|
<example><![CDATA[
|
|
|
|
class Foo {
|
|
// BAD
|
|
if (x.toLowerCase().equals("list"))...
|
|
/*
|
|
This will not match "LIST" when in Turkish locale
|
|
The above could be
|
|
if (x.toLowerCase(Locale.US).equals("list")) ...
|
|
or simply
|
|
if (x.equalsIgnoreCase("list")) ...
|
|
*/
|
|
|
|
// GOOD
|
|
String z = a.toLowerCase(Locale.EN);
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//PrimaryExpression
|
|
[PrimaryPrefix/Name
|
|
[ends-with(@Image, 'toLowerCase') or ends-with(@Image,
|
|
'toUpperCase')]
|
|
]
|
|
[PrimarySuffix/Arguments[@ArgumentCount=0]]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="ExcessivePublicCount" message="A high number of public methods and attributes in an object can indicate the class may need to be broken up for exhaustive testing may prove difficult." class="net.sourceforge.pmd.rules.ExcessivePublicCount">
|
|
<description> A large amount of public methods and attributes declared in an object can indicate the class may need to be broken up as increased effort will be required to thoroughly test such a class. </description>
|
|
<example><![CDATA[
|
|
|
|
|
|
public class Foo {
|
|
public String value;
|
|
public Bar something;
|
|
public Variable var;
|
|
//more public attributes
|
|
public void doWork() {}
|
|
public void doMoreWork() {}
|
|
public void doWorkAgain() {}
|
|
public void doWorking() {}
|
|
public void doWorkIt() {}
|
|
public void doWorkingAgain() {}
|
|
public void doWorkAgainAgain() {}
|
|
public void doWorked() {}
|
|
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="minimum" value="45"/>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AvoidDollarSigns" message="Avoid using dollar signs in variable/method/class/interface names" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid using dollar signs in variable/method/class/interface names. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Fo$o { // yikes!
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ClassOrInterfaceDeclaration[contains(@Image, '$')]
|
|
|
|
|
//VariableDeclaratorId[contains(@Image, '$')]
|
|
|
|
|
//MethodDeclarator[contains(@Image, '$')]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="ForLoopsMustUseBraces" message="Avoid using 'for' statements without curly braces" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid using 'for' statements without using curly braces </description>
|
|
<example><![CDATA[
|
|
|
|
public void foo() {
|
|
for (int i=0; i<42;i++)
|
|
foo();
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ForStatement[not(Statement/Block)]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="SuspiciousEqualsMethodName" message="The method name and parameter number are suspiciously close to equals(Object)" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> The method name and parameter number are suspiciously close to equals(Object), which may mean you are trying (and failing) to override the equals(Object) method. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public int equals(Object o) {
|
|
// oops, this probably was supposed to be boolean equals
|
|
}
|
|
public boolean equals(String s) {
|
|
// oops, this probably was supposed to be equals(Object)
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//MethodDeclarator [
|
|
(
|
|
@Image = 'equals'
|
|
and count(FormalParameters/*) = 1
|
|
and not (FormalParameters/FormalParameter/Type/ReferenceType/ClassOrInterfaceType
|
|
[@Image = 'Object' or @Image = 'java.lang.Object'])
|
|
)
|
|
or
|
|
@Image='equal'
|
|
and count(FormalParameters/*) = 1
|
|
and (FormalParameters/FormalParameter/Type/ReferenceType/ClassOrInterfaceType
|
|
[@Image = 'Object' or @Image = 'java.lang.Object'])
|
|
|
|
]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AvoidCatchingThrowable" message="A catch statement should never catch throwable since it includes errors" class="net.sourceforge.pmd.rules.strictexception.AvoidCatchingThrowable">
|
|
<description> This is dangerous because if a java.lang.Error, for example OutOfMemmoryError, occurs then it will be caught. The container should handle java.lang.Error. If application code will catch them, try to log them (which will probably fail) and continue silently the situation will not be desirable. </description>
|
|
<example><![CDATA[
|
|
|
|
SimpleDateFormat sdf = null;
|
|
try {
|
|
sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
} catch (Throwable th) { //Should not catch throwable
|
|
th.printStackTrace();
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="ExceptionAsFlowControl" message="Avoid using exceptions as flow control" class="net.sourceforge.pmd.rules.design.ExceptionAsFlowControl">
|
|
<description> Using Exceptions as flow control leads to GOTOish code. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
void bar() {
|
|
try {
|
|
try {
|
|
} catch (Exception e) {
|
|
throw new WrapperException(e);
|
|
// this is essentially a GOTO to the WrapperException catch block
|
|
}
|
|
} catch (WrapperException e) {
|
|
// do some more stuff
|
|
}
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="FinalFieldCouldBeStatic" message="This final field could be made static" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> If a final field is assigned to a compile-time constant, it could be made static, thus saving overhead in each object </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public final int BAR = 42; // this could be static and save some space
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//FieldDeclaration
|
|
[not (../../../../ClassOrInterfaceDeclaration[@Interface='true'])]
|
|
[@Final='true' and @Static='false']
|
|
/VariableDeclarator/VariableInitializer/Expression
|
|
/PrimaryExpression/PrimaryPrefix/Literal
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="SuspiciousHashcodeMethodName" message="The method name and return type are suspiciously close to hashCode()" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> The method name and return type are suspiciously close to hashCode(), which may mean you are trying (and failing) to override the hashCode() method. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public int hashcode() {
|
|
// oops, this probably was supposed to be hashCode
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//MethodDeclaration
|
|
[ResultType
|
|
//PrimitiveType
|
|
[@Image='int']
|
|
[//MethodDeclarator
|
|
[@Image='hashcode' or @Image='HashCode' or @Image='Hashcode']
|
|
[not(FormalParameters/*)]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="UnusedModifier" message="Avoid modifiers which are implied by the context" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Fields in interfaces are automatically public static final, and methods are public abstract. Classes or interfaces nested in an interface are automatically public and static (all nested interfaces are automatically static). For historical reasons, modifiers which are implied by the context are accepted by the compiler, but are superfluous. </description>
|
|
<example><![CDATA[
|
|
|
|
public interface Foo {
|
|
public abstract void bar(); // both abstract and public are ignored by the compiler
|
|
public static final int X = 0; // public, static, and final all ignored
|
|
public static class Bar {} // public, static ignored
|
|
public static interface Baz {} // ditto
|
|
}
|
|
public class Bar {
|
|
public static interface Baz {} // static ignored
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ClassOrInterfaceDeclaration[@Interface='true']/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/MethodDeclaration
|
|
[@Public = 'true' or @Abstract = 'true']
|
|
[count(//Block) = 0]
|
|
| //ClassOrInterfaceDeclaration[@Interface='true']/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration
|
|
[@Public = 'true' or @Static = 'true' or @Final = 'true']
|
|
| //ClassOrInterfaceDeclaration[@Interface='true']//ClassOrInterfaceDeclaration[@Interface='false'][@Nested='true'][@Public = 'true' or @Static = 'true']
|
|
| //ClassOrInterfaceDeclaration[@Interface='true']//ClassOrInterfaceDeclaration[@Interface='true'][@Public = 'true' or @Static = 'true']
|
|
| //ClassOrInterfaceDeclaration[@Interface='false']//ClassOrInterfaceDeclaration[@Interface='true'][@Nested='true'][@Static = 'true']
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="ConstructorCallsOverridableMethod" message="Avoid calls to overridable methods during construction" class="net.sourceforge.pmd.rules.ConstructorCallsOverridableMethod">
|
|
<description> Calling overridable methods during construction poses a risk of invoking methods on an incompletely constructed object. This situation can be difficult to discern. It may leave the sub-class unable to construct its superclass or forced to replicate the construction process completely within itself, losing the ability to call super(). If the default constructor contains a call to an overridable method, the subclass may be completely uninstantiable. Note that this includes method calls throughout the control flow graph - i.e., if a constructor Foo() calls a private method bar() that calls a public method buz(), there's a problem. </description>
|
|
<example><![CDATA[
|
|
|
|
public class SeniorClass {
|
|
public SeniorClass(){
|
|
toString(); //may throw NullPointerException if overridden
|
|
}
|
|
public String toString(){
|
|
return "IAmSeniorClass";
|
|
}
|
|
}
|
|
public class JuniorClass extends SeniorClass {
|
|
private String name;
|
|
public JuniorClass(){
|
|
super(); //Automatic call leads to NullPointerException
|
|
name = "JuniorClass";
|
|
}
|
|
public String toString(){
|
|
return name.toUpperCase();
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="AbstractClassWithoutAbstractMethod" message="This abstract class does not have any abstract methods" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> The abstract class does not contain any abstract methods. An abstract class suggests an incomplete implementation, which is to be completed by subclasses implementing the abstract methods. If the class is intended to be used as a base class only (not to be instantiated direcly) a protected constructor can be provided prevent direct instantiation. </description>
|
|
<example><![CDATA[
|
|
|
|
public abstract class Foo {
|
|
void int method1() { ... }
|
|
void int method2() { ... }
|
|
// consider using abstract methods or removing
|
|
// the abstract modifier and adding protected constructors
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
//ClassOrInterfaceDeclaration
|
|
[@Abstract='true'
|
|
and
|
|
count( .//MethodDeclaration[@Abstract='true'] )=0 ]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="NonStaticInitializer" message="Non-static initializers are confusing" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> A nonstatic initializer block will be called any time a constructor is invoked (just prior to invoking the constructor). While this is a valid language construct, it is rarely used and is confusing. </description>
|
|
<example><![CDATA[
|
|
|
|
public class MyClass {
|
|
// this block gets run before any call to a constructor
|
|
{
|
|
System.out.println("I am about to construct myself");
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ClassOrInterfaceBodyDeclaration/Initializer[@Static='false']
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="UnusedLocalVariable" message="Avoid unused local variables such as ''{0}''" class="net.sourceforge.pmd.rules.UnusedLocalVariableRule" symboltable="true">
|
|
<description> Detects when a local variable is declared and/or assigned, but not used. </description>
|
|
<example><![CDATA[
|
|
|
|
public void doSomething() {
|
|
int i = 5; // Unused
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="SwitchDensity" message="A high ratio of statements to labels in a switch statement. Consider refactoring." class="net.sourceforge.pmd.rules.design.SwitchDensityRule">
|
|
<description> A high ratio of statements to labels in a switch statement implies that the switch statement is doing too much work. Consider moving the statements either into new methods, or creating subclasses based on the switch variable. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
private int x;
|
|
public void bar() {
|
|
switch (x) {
|
|
case 1: {
|
|
System.out.println("I am a fish.");
|
|
System.out.println("I am a fish.");
|
|
System.out.println("I am a fish.");
|
|
System.out.println("I am a fish.");
|
|
break;
|
|
}
|
|
|
|
case 2: {
|
|
System.out.println("I am a cow.");
|
|
System.out.println("I am a cow.");
|
|
System.out.println("I am a cow.");
|
|
System.out.println("I am a cow.");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="minimum" value="10"/>
|
|
</properties>
|
|
</rule>
|
|
<rule name="ExceptionTypeChecking" message="The catch clause shouldn't check the exception type - catch several exceptions instead" class="net.sourceforge.pmd.rules.strictexception.ExceptionTypeChecking">
|
|
<description> At some places Exception is caught and then a check with instanceof is performed. This result in messy code. It's considered better to catch all the specific exceptions instead. </description>
|
|
<example><![CDATA[
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
|
|
try {
|
|
returnString = sdf.format(value);
|
|
} catch (Exception ex) {
|
|
/* BAD STUFF !!!*/
|
|
if (ex instanceof NumberFormatException) {
|
|
System.out.println("NumberFormat exception!!!");
|
|
}
|
|
if (ex instanceof IllegalArgumentException) {
|
|
System.out.println("illegal argument...!!!");
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="EmptyFinallyBlock" message="Avoid empty finally blocks" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid empty finally blocks - these can be deleted. </description>
|
|
<example><![CDATA[
|
|
// this is bad
|
|
public void bar() {
|
|
try {
|
|
int x=2;
|
|
} finally {
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
//TryStatement[@Finally='true']/Block[position() = last()]
|
|
[count(*) = 0]
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="MoreThanOneLogger" message="Class contains more than one Logger" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Normally only one logger is used in each class. </description>
|
|
<example><![CDATA[
|
|
|
|
class Foo{
|
|
Logger log = Logger.getLogger(Foo.class.getName());
|
|
// It is very rare to see two loggers on a class, normally
|
|
// log information is multiplexed by levels
|
|
Logger log2= Logger.getLogger(Foo.class.getName());
|
|
}
|
|
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ClassOrInterfaceBody [
|
|
count(
|
|
//VariableDeclarator[../Type/ReferenceType/ClassOrInterfaceType[@Image='Logger']]
|
|
)>1
|
|
]
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="ReturnFromFinallyBlock" message="Avoid returning from a finally block" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid returning from a finally block - this can discard exceptions. </description>
|
|
<example><![CDATA[
|
|
public class Bar {
|
|
public String bugga() {
|
|
try {
|
|
throw new Exception( "My Exception" );
|
|
} catch (Exception e) {
|
|
throw e;
|
|
} finally {
|
|
return "A. O. K."; // Very bad.
|
|
}
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
//TryStatement[@Finally='true']/Block[position() = last()]//ReturnStatement
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="CyclomaticComplexity" message="The {0} ''{1}'' has a Cyclomatic Complexity of {2}." class="net.sourceforge.pmd.rules.CyclomaticComplexity">
|
|
<description> Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Scale: 1-4 (low complexity) 5-7 (moderate complexity) 8-10 (high complexity) 10+ (very high complexity) </description>
|
|
<example><![CDATA[
|
|
|
|
Cyclomatic Complexity = 12
|
|
|
|
public class Foo
|
|
{
|
|
1 public void example()
|
|
{
|
|
2 if (a == b)
|
|
{
|
|
3 if (a1 == b1)
|
|
{
|
|
do something;
|
|
}
|
|
4 else if a2 == b2)
|
|
{
|
|
do something;
|
|
}
|
|
else
|
|
{
|
|
do something;
|
|
}
|
|
}
|
|
5 else if (c == d)
|
|
{
|
|
6 while (c == d)
|
|
{
|
|
do something;
|
|
}
|
|
}
|
|
7 else if (e == f)
|
|
{
|
|
8 for (int n = 0; n < h; n++)
|
|
{
|
|
do something;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
switch (z)
|
|
{
|
|
9 case 1:
|
|
do something;
|
|
break;
|
|
|
|
10 case 2:
|
|
do something;
|
|
break;
|
|
|
|
11 case 3:
|
|
do something;
|
|
break;
|
|
|
|
12 default:
|
|
do something;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="reportLevel" value="10"/>
|
|
</properties>
|
|
</rule>
|
|
<rule name="StringToString" message="Avoid calling toString() on String objects; this is unnecessary" class="net.sourceforge.pmd.rules.StringToStringRule" symboltable="true">
|
|
<description> Avoid calling toString() on String objects; this is unnecessary </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
private String baz() {
|
|
String bar = "howdy";
|
|
return bar.toString();
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="AvoidInstanceofChecksInCatchClause" message="An instanceof check is being performed on the caught exception. Create a separate catch clause for this exception type." class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Each caught exception type should be handled in its own catch clause. </description>
|
|
<example><![CDATA[
|
|
|
|
// Avoid this
|
|
try {
|
|
...something...
|
|
}
|
|
catch (Exception ee) {
|
|
if (ee instanceof IOException) {
|
|
cleanup();
|
|
}
|
|
}
|
|
|
|
// Prefer this:
|
|
try {
|
|
...something...
|
|
}
|
|
catch (IOException ee) {
|
|
cleanup();
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//TryStatement/FormalParameter
|
|
/following-sibling::Block//InstanceOfExpression/PrimaryExpression/PrimaryPrefix
|
|
/Name[
|
|
@Image = ./ancestor::Block/preceding-sibling::FormalParameter
|
|
/VariableDeclaratorId/@Image
|
|
]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="UnusedImports" message="Avoid unused imports such as ''{0}''" class="net.sourceforge.pmd.rules.UnusedImportsRule">
|
|
<description> Avoid unused import statements. </description>
|
|
<example><![CDATA[
|
|
|
|
// this is bad
|
|
import java.io.File;
|
|
public class Foo {}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="EmptyTryBlock" message="Avoid empty try blocks" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid empty try blocks - what's the point? </description>
|
|
<example><![CDATA[
|
|
// this is bad
|
|
public void bar() {
|
|
try {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
//TryStatement/Block[1][count(*) = 0]
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="CloneMethodMustImplementCloneable" message="clone() method should be implemented only if implementing Cloneable interface" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> The method clone() should only be implemented if the class implements Cloneable interface </description>
|
|
<example><![CDATA[
|
|
|
|
public class MyClass {
|
|
// will cause an error
|
|
public Object clone() throws CloneNotSupportedException {
|
|
...
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ClassOrInterfaceDeclaration
|
|
[not(./ImplementsList/ClassOrInterfaceType
|
|
[@Image='Cloneable'])]
|
|
[.//MethodDeclaration/MethodDeclarator[@Image
|
|
= 'clone' and count(FormatParameters/*) = 0]]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="EmptyStatementNotInLoop" message="An empty statement (semicolon) not part of a loop" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> An empty statement (aka a semicolon by itself) that is not used as the sole body of a for loop or while loop is probably a bug. It could also be a double semicolon, which is useless and should be removed. </description>
|
|
<example><![CDATA[
|
|
public class MyClass {
|
|
public void doit()
|
|
{
|
|
// this is probably not what you meant to do
|
|
;
|
|
// the extra semicolon here this is not necessary
|
|
System.out.println("look at the extra semicolon");;
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//Statement/EmptyStatement
|
|
[not(
|
|
../../../ForStatement
|
|
or ../../../WhileStatement
|
|
or ../../../BlockStatement/ClassOrInterfaceDeclaration
|
|
or ../../../../../../ForStatement/Statement[1]
|
|
/Block[1]/BlockStatement[1]/Statement/EmptyStatement
|
|
or ../../../../../../WhileStatement/Statement[1]
|
|
/Block[1]/BlockStatement[1]/Statement/EmptyStatement)
|
|
]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="UnnecessaryParentheses" message="This return statement may have some unnecessary parentheses" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description>
|
|
Sometimes return statement expressions are wrapped in unnecessary parentheses,
|
|
making them look like a function call.
|
|
</description>
|
|
<example><![CDATA[
|
|
public class Foo {
|
|
boolean bar() {
|
|
return (true);
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ReturnStatement
|
|
/Expression
|
|
/PrimaryExpression
|
|
/PrimaryPrefix
|
|
/Expression[count(*)=1]
|
|
/PrimaryExpression
|
|
/PrimaryPrefix
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AvoidFieldNameMatchingMethodName" message="It is somewhat confusing to have a field name with the same name as a method" class="net.sourceforge.pmd.rules.AvoidFieldNameMatchingMethodName">
|
|
<description> It is somewhat confusing to have a field name with the same name as a method. While this is totally legal, having information (field) and actions (method) is not clear naming. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
Object bar;
|
|
// bar is data or an action or both?
|
|
void bar() {
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="DefaultLabelNotLastInSwitchStmt" message="The default label should be the last label in a switch statement" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> The default label in a switch statement should be the last label, by convention. Most programmers will expect the default label (if present) to be the last one. </description>
|
|
<example><![CDATA[
|
|
|
|
switch (a)
|
|
{
|
|
case 1:
|
|
// do something
|
|
break;
|
|
default:
|
|
// the default case should be last, by convention
|
|
break;
|
|
case 2:
|
|
break;
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//SwitchStatement
|
|
[not(SwitchLabel[position() = last()][count(*) = 0])]
|
|
[SwitchLabel[count(*) = 0]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AvoidSynchronizedAtMethodLevel" message="Use block level rather than method level synchronization" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Method level synchronization can backfire when new code is added to the method. Block-level synchronization helps to ensure that only the code that needs synchronization gets it. </description>
|
|
<example><![CDATA[
|
|
|
|
|
|
// Try to avoid this
|
|
synchronized void foo() {
|
|
}
|
|
|
|
// Prefer this:
|
|
void bar() {
|
|
synchronized(mutex) {
|
|
}
|
|
}
|
|
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//MethodDeclaration[@Synchronized='true']
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="SignatureDeclareThrowsException" message="A signature (constructor or method) shouldn't have Exception in throws declaration" class="net.sourceforge.pmd.rules.strictexception.ExceptionSignatureDeclaration">
|
|
<description> It is unclear which exceptions that can be thrown from the methods. It might be difficult to document and understand the vague interfaces. Use either a class derived from RuntimeException or a checked exception. </description>
|
|
<example><![CDATA[
|
|
|
|
public void methodThrowingException() throws Exception {
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="UnusedPrivateField" message="Avoid unused private fields such as ''{0}''" class="net.sourceforge.pmd.rules.UnusedPrivateFieldRule" symboltable="true">
|
|
<description> Detects when a private field is declared and/or assigned a value, but not used. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Something {
|
|
private static int FOO = 2; // Unused
|
|
private int i = 5; // Unused
|
|
private int j = 6;
|
|
public int addOne() {
|
|
return j++;
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="AccessorClassGeneration" message="Avoid instantiation through private constructors from outside of the constructor's class." class="net.sourceforge.pmd.rules.AccessorClassGeneration">
|
|
<description> Instantiation by way of private constructors from outside of the constructor's class often causes the generation of an accessor. A factory method, or non-privitization of the constructor can eliminate this situation. The generated class file is actually an interface. It gives the accessing class the ability to invoke a new hidden package scope constructor that takes the interface as a supplementary parameter. This turns a private constructor effectively into one with package scope, though not visible to the naked eye. </description>
|
|
<example><![CDATA[
|
|
|
|
public class OuterClass {
|
|
void method(){
|
|
InnerClass ic = new InnerClass();//Causes generation of accessor
|
|
}
|
|
public class InnerClass {
|
|
private InnerClass(){
|
|
}
|
|
}
|
|
}
|
|
|
|
public class OuterClass {
|
|
void method(){
|
|
InnerClass ic = new InnerClass();//OK, due to public constructor
|
|
}
|
|
public class InnerClass {
|
|
public InnerClass(){
|
|
}
|
|
}
|
|
}
|
|
|
|
public class OuterClass {
|
|
void method(){
|
|
InnerClass ic = InnerClass.getInnerClass();//OK
|
|
}
|
|
public static class InnerClass {
|
|
private InnerClass(){
|
|
}
|
|
private static InnerClass getInnerClass(){
|
|
return new InnerClass();
|
|
}
|
|
}
|
|
}
|
|
|
|
public class OuterClass {
|
|
private OuterClass(){
|
|
}
|
|
public class InnerClass {
|
|
void method(){
|
|
OuterClass oc = new OuterClass();//Causes generation of accessor
|
|
}
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="AvoidReassigningParameters" message="Avoid reassigning parameters such as ''{0}''" class="net.sourceforge.pmd.rules.AvoidReassigningParameters" symboltable="true">
|
|
<description> Reassigning values to parameters is a questionable practice. Use a temporary local variable instead. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
private void foo(String bar) {
|
|
bar = "something else";
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="MissingStaticMethodInNonInstantiatableClass" message="Class cannot be instantiated and does not provide any static methods" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> A class that has private constructors and does not have any static method cannot be used. </description>
|
|
<example><![CDATA[
|
|
|
|
/* This class is unusable, since it cannot be
|
|
instantiated (private constructor),
|
|
and no static method can be called.
|
|
*/
|
|
public class Foo {
|
|
|
|
private Foo() {}
|
|
|
|
void foo() {
|
|
}
|
|
|
|
}
|
|
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ClassOrInterfaceDeclaration[
|
|
( count(.//ConstructorDeclaration)>0
|
|
and count(.//ConstructorDeclaration) = count(.//ConstructorDeclaration[@Private='true']) )
|
|
and
|
|
count(.//MethodDeclaration[@Static='true'])=0
|
|
]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="ExcessiveMethodLength" message="Avoid really long methods." class="net.sourceforge.pmd.rules.design.LongMethodRule">
|
|
<description> Excessive Method Length usually means that the method is doing too much. There is usually quite a bit of Cut and Paste there as well. Try to reduce the method size by creating helper methods, and removing cut and paste. Default value is 2.5 sigma greater than the mean. There are three parameters available: minimum - Minimum Length before reporting. sigma - Std Deviations away from the mean before reporting. topscore - The Maximum Number of reports to generate. At this time, only one can be used at a time. </description>
|
|
<example><![CDATA[
|
|
|
|
public void doSomething() {
|
|
System.out.println("I am a fish.");
|
|
System.out.println("I am a fish.");
|
|
System.out.println("I am a fish.");
|
|
System.out.println("I am a fish.");
|
|
System.out.println("I am a fish.");
|
|
// 495 copies omitted for brevity.
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="minimum" value="100"/>
|
|
</properties>
|
|
</rule>
|
|
<rule name="ImportFromSamePackage" message="No need to import a type that's in the same package" class="net.sourceforge.pmd.rules.ImportFromSamePackageRule">
|
|
<description> No need to import a type that's in the same package. </description>
|
|
<example><![CDATA[
|
|
|
|
package foo;
|
|
import foo.Buz; // no need for this
|
|
public class Bar{}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="CouplingBetweenObjects" message="High amount of different objects as memebers donotes a high coupling" class="net.sourceforge.pmd.rules.CouplingBetweenObjects" symboltable="true">
|
|
<description> Rule counts unique attributes, local variables and return types within an object. An amount higher than specified threshold can indicate a high degree of couping with in an object </description>
|
|
<example><![CDATA[
|
|
|
|
import com.Blah;
|
|
import org.Bar;
|
|
import org.Bardo;
|
|
//
|
|
public class Foo {
|
|
private Blah var1;
|
|
private Bar var2;
|
|
//followed by many imports of unique objects
|
|
|
|
void ObjectC doWork() {
|
|
Bardo var55;
|
|
ObjectA var44;
|
|
ObjectZ var93;
|
|
return something;
|
|
}
|
|
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="threshold" value="20"/>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AvoidFieldNameMatchingTypeName" message="It is somewhat confusing to have a field name matching the declaring class name" class="net.sourceforge.pmd.rules.AvoidFieldNameMatchingTypeName">
|
|
<description> It is somewhat confusing to have a field name matching the declaring class name. This proabably means that type and or field names could be more precise. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo extends Bar {
|
|
// There's probably a better name for foo
|
|
int foo;
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="IdempotentOperations" message="Avoid idempotent operations (like assigning a variable to itself)" class="net.sourceforge.pmd.rules.IdempotentOperations">
|
|
<description> Avoid idempotent operations - they are silly. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public void bar() {
|
|
int x = 2;
|
|
x = x;
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="MethodNamingConventions" message="Method name does not begin with a lower case character." class="net.sourceforge.pmd.rules.MethodNamingConventions">
|
|
<description> Method names should always begin with a lower case character, and should not contain underscores. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public void fooStuff() {
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="OnlyOneReturn" message="A method should have only one exit point, and that should be the last statement in the method" class="net.sourceforge.pmd.rules.design.OnlyOneReturnRule">
|
|
<description> A method should have only one exit point, and that should be the last statement in the method. </description>
|
|
<example><![CDATA[
|
|
|
|
public class OneReturnOnly1 {
|
|
public void foo(int x) {
|
|
if (x > 0) {
|
|
return "hey"; // oops, multiple exit points!
|
|
}
|
|
return "hi";
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="OverrideBothEqualsAndHashcode" message="Ensure you override both equals() and hashCode()" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Override both public boolean Object.equals(Object other), and public int Object.hashCode(), or override neither. Even if you are inheriting a hashCode() from a parent class, consider implementing hashCode and explicitly delegating to your superclass. </description>
|
|
<example><![CDATA[
|
|
// this is bad
|
|
public class Bar {
|
|
public boolean equals(Object o) {
|
|
// do some comparison
|
|
}
|
|
}
|
|
|
|
// and so is this
|
|
public class Baz {
|
|
public int hashCode() {
|
|
// return some hash value
|
|
}
|
|
}
|
|
|
|
// this is OK
|
|
public class Foo {
|
|
public boolean equals(Object other) {
|
|
// do some comparison
|
|
}
|
|
public int hashCode() {
|
|
// return some hash value
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ClassOrInterfaceDeclaration[@Interface='false']//MethodDeclarator
|
|
[(@Image = 'equals' and count(FormalParameters/*) = 1
|
|
and not(//MethodDeclarator[count(FormalParameters/*) = 0][@Image = 'hashCode']))
|
|
or
|
|
(@Image='hashCode' and count(FormalParameters/*) = 0
|
|
and
|
|
not
|
|
(//MethodDeclarator
|
|
[count(
|
|
FormalParameters//Type/ReferenceType/ClassOrInterfaceType
|
|
[@Image = 'Object' or @Image = 'java.lang.Object']) = 1]
|
|
[@Image = 'equals']))]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="DuplicateImports" message="Avoid duplicate imports such as ''{0}''" class="net.sourceforge.pmd.rules.DuplicateImportsRule">
|
|
<description> Avoid duplicate import statements. </description>
|
|
<example><![CDATA[
|
|
|
|
// this is bad
|
|
import java.io.File;
|
|
import java.io.File;
|
|
public class Foo {}
|
|
|
|
// --- in another source code file...
|
|
|
|
// this is bad
|
|
import java.io.*;
|
|
import java.io.File;
|
|
|
|
public class Foo {}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="UnnecessaryConversionTemporary" message="Avoid unnecessary temporaries when converting primitives to Strings" class="net.sourceforge.pmd.rules.UnnecessaryConversionTemporary">
|
|
<description> Avoid unnecessary temporaries when converting primitives to Strings </description>
|
|
<example><![CDATA[
|
|
public String convert(int x) {
|
|
// this wastes an object
|
|
String foo = new Integer(x).toString();
|
|
// this is better
|
|
return Integer.toString(x);
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="CloneThrowsCloneNotSupportedException" message="clone() method should throw CloneNotSupportedException" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> The method clone() should throw a CloneNotSupportedException </description>
|
|
<example><![CDATA[
|
|
|
|
public class MyClass implements Cloneable{
|
|
public Object clone() // will cause an error {
|
|
MyClass clone = (MyClass)super.clone();
|
|
...
|
|
return clone;
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ClassOrInterfaceDeclaration
|
|
[@Final = 'false']
|
|
[.//MethodDeclaration[
|
|
MethodDeclarator/@Image = 'clone'
|
|
and count(MethodDeclarator/FormalParameters/*) = 0
|
|
and count(NameList/Name[contains
|
|
(@Image,'CloneNotSupportedException')]) = 0]]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="LooseCoupling" message="Avoid using implementation types like ''{0}''; use the interface instead" class="net.sourceforge.pmd.rules.design.LooseCoupling">
|
|
<description> Avoid using implementation types (i.e., HashSet); use the interface (i.e, Set) instead </description>
|
|
<example><![CDATA[
|
|
|
|
import java.util.*;
|
|
public class Bar {
|
|
|
|
// should be "private List list"
|
|
private ArrayList list = new ArrayList();
|
|
|
|
// should be "public Set getFoo()"
|
|
public HashSet getFoo() {
|
|
return new HashSet();
|
|
}
|
|
}
|
|
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="NonCaseLabelInSwitchStatement" message="A non-case label was present in a switch statement" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> A non-case label (e.g. a named break/continue label) was present in a switch statement. This legal, but confusing. It is easy to mix up the case labels and the non-case labels. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
void bar(int a) {
|
|
switch (a)
|
|
{
|
|
case 1:
|
|
// do something
|
|
break;
|
|
mylabel: // this is legal, but confusing!
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//SwitchStatement//BlockStatement/Statement/LabeledStatement
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="MissingBreakInSwitch" message="A switch statement does not contain a break" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> A switch statement without an enclosed break statement may be a bug. </description>
|
|
<example><![CDATA[
|
|
|
|
switch(status) {
|
|
case CANCELLED:
|
|
doCancelled();
|
|
// uncomment the next line if doNew() and doRemoved() should not be executed when status is cancelled
|
|
// break;
|
|
case NEW:
|
|
doNew();
|
|
case REMOVED:
|
|
doRemoved();
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//SwitchStatement[count(.//BreakStatement)=0]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="UseStringBufferForStringAppends" message="Prefer StringBuffer over += for concatenating strings" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description>
|
|
Finds usages of += for appending strings.
|
|
</description>
|
|
<example><![CDATA[
|
|
|
|
String a;
|
|
|
|
a = "foo";
|
|
a += " bar";
|
|
|
|
// better would be:
|
|
StringBuffer a = new StringBuffer("foo");
|
|
a.append(" bar);
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//StatementExpression
|
|
[PrimaryExpression/PrimaryPrefix/Name
|
|
[@Image = ancestor::MethodDeclaration//LocalVariableDeclaration
|
|
[./Type//ClassOrInterfaceType[@Image =
|
|
'String']]/VariableDeclarator/VariableDeclaratorId/@Image]]
|
|
//AssignmentOperator[@Compound='true']
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AssignmentInOperand" message="Avoid assigments in operands" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid assigments in operands; this can make code more complicated and harder to read. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public void bar() {
|
|
int x = 2;
|
|
if ((x = getX()) == 3) {
|
|
System.out.println("3!");
|
|
}
|
|
}
|
|
private int getX() {
|
|
return 3;
|
|
}
|
|
}
|
|
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//*[name()='WhileStatement' or name()='IfStatement'][Expression//AssignmentOperator]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="SimplifyBooleanExpressions" message="Avoid unnecessary comparisons in boolean expressions" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid unnecessary comparisons in boolean expressions - this makes simple code seem complicated. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Bar {
|
|
// can be simplified to
|
|
// bar = isFoo();
|
|
private boolean bar = (isFoo() == true);
|
|
|
|
public isFoo() { return false;}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//EqualityExpression/PrimaryExpression/PrimaryPrefix/Literal/BooleanLiteral
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="UnnecessaryConstructor" message="Avoid unnecessary constructors - the compiler will generate these for you" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Unnecessary constructor detects when a constructor is not necessary; i.e., when there's only one constructor, it's public, has an empty body, and takes no arguments. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public Foo() {}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ClassOrInterfaceDeclaration
|
|
/ClassOrInterfaceBody[count(ClassOrInterfaceBodyDeclaration/ConstructorDeclaration)=1]
|
|
/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration
|
|
[@Public='true']
|
|
[not(FormalParameters/*)]
|
|
[not(BlockStatement)]
|
|
[not(NameList)]
|
|
[count(ExplicitConstructorInvocation/Arguments/ArgumentList/Expression)=0]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="ExcessiveClassLength" message="Avoid really long Classes." class="net.sourceforge.pmd.rules.design.LongClassRule">
|
|
<description> Long Class files are indications that the class may be trying to do too much. Try to break it down, and reduce the size to something managable. Default value is 2.5 sigma greater than the mean. NOTE: In version 0.9 and higher, their are three parameters available: minimum - Minimum Length before reporting. sigma - Std Deviations away from the mean before reporting. topscore - The Maximum Number of reports to generate. At this time, only one can be used at a time. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public void bar() {
|
|
// 500 lines of code
|
|
}
|
|
|
|
public void baz() {
|
|
// 500 more lines of code
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="minimum" value="1000"/>
|
|
</properties>
|
|
</rule>
|
|
<rule name="ProperCloneImplementation" message="Object clone() should be implemented with super.clone()" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Object clone() should be implemented with super.clone() </description>
|
|
<example><![CDATA[
|
|
|
|
class Foo{
|
|
public Object clone(){
|
|
return new Foo(); // This is bad
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ClassOrInterfaceDeclaration//MethodDeclarator
|
|
[@Image = 'clone']
|
|
[count(FormalParameters/*) = 0]
|
|
[count(../Block//*[
|
|
(self::AllocationExpression) and
|
|
(./ClassOrInterfaceType/@Image = ancestor::
|
|
ClassOrInterfaceDeclaration[position()=last()]/@Image)
|
|
])> 0
|
|
]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="FinalizeShouldBeProtected" message="If you override finalize(), make it protected" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> If you override finalize(), make it protected. Otherwise, subclasses may not called your implementation of finalize. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public void finalize() {
|
|
// do something
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//MethodDeclaration[@Protected="false"]
|
|
/MethodDeclarator[@Image="finalize"]
|
|
[not(FormalParameters/*)]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="SimplifyConditional" message="No need to check for null before an instanceof" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description>
|
|
No need to check for null before an instanceof; the instanceof keyword returns false when given a null argument.
|
|
</description>
|
|
<example><![CDATA[
|
|
|
|
class Foo {
|
|
void bar(Object x) {
|
|
if (x != null && x instanceof Bar) {
|
|
// just drop the "x != null" check
|
|
}
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//Expression
|
|
[ConditionalOrExpression
|
|
[EqualityExpression[@Image='==']
|
|
//NullLiteral
|
|
and
|
|
UnaryExpressionNotPlusMinus
|
|
[@Image='!']//InstanceOfExpression[PrimaryExpression
|
|
//Name/@Image = ancestor::ConditionalOrExpression/EqualityExpression
|
|
//PrimaryPrefix/Name/@Image]]
|
|
or
|
|
ConditionalAndExpression
|
|
[EqualityExpression[@Image='!=']//NullLiteral
|
|
and
|
|
InstanceOfExpression
|
|
[PrimaryExpression
|
|
//Name/@Image = ancestor::ConditionalAndExpression
|
|
/EqualityExpression//PrimaryPrefix/Name/@Image]]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="SimplifyBooleanReturns" message="Avoid unnecessary if..then..else statements when returning a boolean" class="net.sourceforge.pmd.rules.SimplifyBooleanReturns">
|
|
<description> Avoid unnecessary if..then..else statements when returning a boolean </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
private int bar =2;
|
|
public boolean isBarEqualsTo(int x) {
|
|
// this bit of code
|
|
if (bar == x) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
// can be replaced with a simple
|
|
// return bar == x;
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="ConfusingTernary" message="Avoid if (x != y) ..; else ..;" class="net.sourceforge.pmd.rules.design.ConfusingTernary">
|
|
<description> In an "if" expression with an "else" clause, avoid negation in the test. For example, rephrase: if (x != y) diff(); else same(); as: if (x == y) same(); else diff(); Most "if (x != y)" cases without an "else" are often return cases, so consistent use of this rule makes the code easier to read. Also, this resolves trivial ordering problems, such as "does the error case go first?" or "does the common case go first?". </description>
|
|
<example><![CDATA[
|
|
|
|
return (x != y) ? diff : same;
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="UnnecessaryReturn" message="Avoid unnecessary return statements" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid unnecessary return statements </description>
|
|
<example><![CDATA[
|
|
// this is bad
|
|
public void bar() {
|
|
int x = 42;
|
|
return;
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
//ReturnStatement
|
|
[parent::Statement
|
|
[parent::BlockStatement
|
|
[parent::Block
|
|
[parent::MethodDeclaration/ResultType[@Void='true']
|
|
]
|
|
]
|
|
]
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="LoggerIsNotStaticFinal" message="The Logger variable declaration does not contain the static and final modifiers" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> In most cases, the Logger can be declared static and final. </description>
|
|
<example><![CDATA[
|
|
|
|
class Foo{
|
|
Logger log = Logger.getLogger(Foo.class.getName());
|
|
// It is much better to declare the logger as follows
|
|
// static final Logger log = Logger.getLogger(Foo.class.getName());
|
|
}
|
|
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//VariableDeclarator
|
|
[../Type/ReferenceType
|
|
/ClassOrInterfaceType[@Image='Logger']
|
|
and
|
|
(..[@Final='false'] or ..[@Static = 'false'] ) ]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="DontImportSun" message="Avoid importing anything from the 'sun.*' packages" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid importing anything from the 'sun.*' packages. These packages are not portable and are likely to change. </description>
|
|
<example><![CDATA[
|
|
|
|
import sun.misc.foo;
|
|
public class Foo {}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ImportDeclaration
|
|
[starts-with(Name/@Image, 'sun.')]
|
|
[not(starts-with(Name/@Image, 'sun.misc.Signal'))]
|
|
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="SingularField" message="Perhaps ''{0}'' could be replaced by a local variable." class="net.sourceforge.pmd.rules.SingularField">
|
|
<description>
|
|
A field that's only used by one method could perhaps be replaced by a local variable
|
|
</description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
private int x;
|
|
public void foo(int y) {
|
|
x = y + 5;
|
|
return x;
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="SuspiciousOctalEscape" message="Suspicious decimal characters following octal escape in string literal" class="net.sourceforge.pmd.rules.SuspiciousOctalEscape">
|
|
<description> A suspicious octal escape sequence was found inside a String literal. The Java language specification (section 3.10.6) says an octal escape sequence inside a literal String shall consist of a backslash followed by: OctalDigit | OctalDigit OctalDigit | ZeroToThree OctalDigit OctalDigit Any octal escape sequence followed by non-octal digits can be confusing, e.g. "\038" is interpreted as the octal escape sequence "\03" followed by the literal character "8". </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public void foo() {
|
|
// interpreted as octal 12, followed by character '8'
|
|
System.out.println("suspicious: \128");
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="EmptyCatchBlock" message="Avoid empty catch blocks" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Empty Catch Block finds instances where an exception is caught, but nothing is done. In most circumstances, this swallows an exception which should either be acted on or reported. </description>
|
|
<example><![CDATA[
|
|
public void doSomething() {
|
|
try {
|
|
FileInputStream fis = new FileInputStream("/tmp/bugger");
|
|
} catch (IOException ioe) {
|
|
// not good
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//TryStatement
|
|
[@Catch='true']
|
|
[FormalParameter/Type/ReferenceType/ClassOrInterfaceType
|
|
[@Image != 'InterruptedException' and @Image != 'CloneNotSupportedException']
|
|
]
|
|
/Block[position() > 1]
|
|
[count(*) = 0]
|
|
[../@Finally='false' or following-sibling::Block]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="ForLoopShouldBeWhileLoop" message="This for loop could be simplified to a while loop" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Some for loops can be simplified to while loops - this makes them more concise. </description>
|
|
<example><![CDATA[
|
|
public class Foo {
|
|
void bar() {
|
|
for (;true;) true; // No Init or Update part, may as well be: while (true)
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
//ForStatement[count(*) > 1][not(ForInit)][not(ForUpdate)]
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="UseArrayListInsteadOfVector" message="Use ArrayList instead of Vector" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> ArrayList is a much better Collection implementation than Vector. </description>
|
|
<example><![CDATA[
|
|
|
|
public class SimpleTest extends TestCase {
|
|
public void testX() {
|
|
Collection c = new Vector();
|
|
// This achieves the same with much better performance
|
|
// Collection c = new ArrayList();
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//AllocationExpression/ClassOrInterfaceType[@Image='Vector' or @Image='java.util.Vector']
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AvoidNonConstructorMethodsWithClassName" message="Method {0} is not a constructor but it can be confused with one" class="net.sourceforge.pmd.rules.AvoidNonConstructorMethodsWithClassName">
|
|
<description> It is very easy to confuse methods with classname with constructors. It is preferrable to name these non-constructor methods in a different way. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public void Foo() {
|
|
// not really a constructor...
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="SuspiciousConstantFieldName" message="The field name indicates a constants but its modifiers don't" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> A field name is all in uppercase characters, which in sun's java naming conventions indicate a constant. However, the field is not final. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
// this is bad, since someone could accidentally
|
|
// do PI = 2.71828; which is actualy e
|
|
// final double PI = 3.16; is ok
|
|
double PI = 3.16;
|
|
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//FieldDeclaration
|
|
[@Final='false']
|
|
[../../../../ClassOrInterfaceDeclaration[@Interface='false']]
|
|
/VariableDeclarator
|
|
/VariableDeclaratorId[upper-case(@Image)=@Image]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AvoidDuplicateLiterals" message="The String literal {0} appears {1} times in this file; the first occurrence is on line {2}" class="net.sourceforge.pmd.rules.AvoidDuplicateLiteralsRule">
|
|
<description> Code containing duplicate String literals can usually be improved by declaring the String as a constant field. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
private void bar() {
|
|
buz("Howdy");
|
|
buz("Howdy");
|
|
buz("Howdy");
|
|
buz("Howdy");
|
|
}
|
|
private void buz(String x) {}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="threshold" value="4"/>
|
|
</properties>
|
|
</rule>
|
|
<rule name="UnusedPrivateMethod" message="Avoid unused private methods such as ''{0}''" class="net.sourceforge.pmd.rules.UnusedPrivateMethodRule">
|
|
<description> Unused Private Method detects when a private method is declared but is unused. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Something {
|
|
private void foo() {} // unused
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="EmptyStaticInitializer" message="Empty static initializer was found" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> An empty static initializer was found. </description>
|
|
<example><![CDATA[
|
|
public class Foo {
|
|
// why are there no statements in this static block?
|
|
static {}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
//ClassOrInterfaceBodyDeclaration/Initializer[@Static='true']/Block[count(*)=0]
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="FinalizeOnlyCallsSuperFinalize" message="Finalize should do something besides just calling super.finalize()" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> If the finalize() is implemented, it should do something besides just calling super.finalize(). </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
protected void finalize() {
|
|
super.finalize();
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//MethodDeclaration[MethodDeclarator[@Image="finalize"][not(FormalParameters/*)]]
|
|
/Block[count(BlockStatement)=1]
|
|
/BlockStatement[Statement/StatementExpression/PrimaryExpression
|
|
/PrimaryPrefix[@Image="finalize"]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="OptimizableToArrayCall" message="This call to Collection.toArray() may be optimizable" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> A call to Collection.toArray can use the Collection's size vs an empty Array of the desired type. </description>
|
|
<example><![CDATA[
|
|
|
|
class Example {
|
|
void bar() {
|
|
// A bit inefficient, unlike...
|
|
x.toArray(new Foo[0]);
|
|
|
|
// ..this one, which sizes the destination array, avoiding
|
|
// a reflection call in some Collection implementations
|
|
x.toArray(new Foo[x.size()]);
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//PrimaryExpression
|
|
[PrimaryPrefix/Name[ends-with(@Image, 'toArray')]]
|
|
[
|
|
PrimarySuffix/Arguments/ArgumentList/Expression
|
|
/PrimaryExpression/PrimaryPrefix/AllocationExpression
|
|
/ArrayDimsAndInits/Expression/PrimaryExpression/PrimaryPrefix/Literal[@Image='0']
|
|
]
|
|
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="IfStmtsMustUseBraces" message="Avoid using if statements without curly braces" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid using if statements without using curly braces </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public void bar() {
|
|
int x = 0;
|
|
if (foo) x++;
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//IfStatement[count(*) < 3][not(Statement/Block)]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="EmptyWhileStmt" message="Avoid empty 'while' statements" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Empty While Statement finds all instances where a while statement does nothing. If it is a timing loop, then you should use Thread.sleep() for it; if it's a while loop that does a lot in the exit expression, rewrite it to make it clearer. </description>
|
|
<example><![CDATA[
|
|
while (a == b) {
|
|
// not good
|
|
}
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//WhileStatement/Statement[./Block[count(*) = 0] or ./EmptyStatement]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="EmptyFinalizer" message="Avoid empty finalize methods" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> If the finalize() method is empty, then it does not need to exist. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
protected void finalize() {}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//MethodDeclaration[MethodDeclarator[@Image='finalize'][not(FormalParameters/*)]]
|
|
/Block[count(*)=0]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="EmptySwitchStatements" message="Avoid empty switch statements" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid empty switch statements. </description>
|
|
<example><![CDATA[
|
|
public class Foo {
|
|
public void bar() {
|
|
int x = 2;
|
|
switch (x) {
|
|
// once there was code here
|
|
// but it's been commented out or something
|
|
}
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
//SwitchStatement[count(*) = 1]
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="BooleanInstantiation" message="Avoid instantiating Boolean objects; you can usually invoke Boolean.valueOf() instead." class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid instantiating Boolean objects, instead use Boolean.valueOf(). </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
private Boolean bar = new Boolean("true"); // just do a Boolean bar = Boolean.TRUE or Boolean.valueOf(true);
|
|
}
|
|
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//AllocationExpression[not (ArrayDimsAndInits)
|
|
and
|
|
(ClassOrInterfaceType/@Image='Boolean'
|
|
or
|
|
ClassOrInterfaceType/@Image='java.lang.Boolean')]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="JumbledIncrementer" message="Avoid modifying an outer loop incrementer in an inner loop for update expression" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid jumbled loop incrementers - it's usually a mistake, and it's confusing even if it's what's intended. </description>
|
|
<example><![CDATA[
|
|
public class JumbledIncrementerRule1 {
|
|
public void foo() {
|
|
for (int i = 0; i < 10; i++) {
|
|
for (int k = 0; k < 20; i++) {
|
|
System.out.println("Hello");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
//ForStatement
|
|
[
|
|
ForUpdate/StatementExpressionList/StatementExpression/PostfixExpression/PrimaryExpression/PrimaryPrefix/Name/@Image
|
|
=
|
|
ancestor::ForStatement/ForInit//VariableDeclaratorId/@Image
|
|
]
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="UseSingleton" message="All methods are static. Consider using Singleton instead. Alternatively, you could add a private constructor or make the class abstract to silence this warning." class="net.sourceforge.pmd.rules.design.UseSingleton">
|
|
<description> If you have a class that has nothing but static methods, consider making it a Singleton. Note that this doesn't apply to abstract classes, since their subclasses may well include non-static methods. Also, if you want this class to be a Singleton, remember to add a private constructor to prevent instantiation. </description>
|
|
<example><![CDATA[
|
|
|
|
public class MaybeASingleton {
|
|
public static void foo() {
|
|
// etc
|
|
}
|
|
public static void bar() {
|
|
// etc
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="SimpleDateFormatNeedsLocale" message="When instantiating a SimpleDateFormat object, specify a Locale" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Be sure to specify a Locale when creating a new instance of SimpleDateFormat. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
// Should specify Locale.US (or whatever)
|
|
private SimpleDateFormat sdf = new SimpleDateFormat("pattern");
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//AllocationExpression
|
|
[ClassOrInterfaceType[@Image='SimpleDateFormat']]
|
|
[Arguments[@ArgumentCount=1]]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="UnusedFormalParameter" message="Avoid unused formal parameters such as ''{0}''" class="net.sourceforge.pmd.rules.UnusedFormalParameterRule" symboltable="true">
|
|
<description> Avoid passing parameters to methods and then not using those parameters. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
private void bar(String howdy) {
|
|
// howdy is not used
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="UnconditionalIfStatement" message="Do not use 'if' statements that are always true or always false" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Do not use "if" statements that are always true or always false. </description>
|
|
<example><![CDATA[
|
|
public class Foo {
|
|
public void close() {
|
|
if (true) {
|
|
// ...
|
|
}
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
//IfStatement/Expression
|
|
[count(PrimaryExpression)=1]
|
|
/PrimaryExpression/PrimaryPrefix/Literal/BooleanLiteral
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AvoidCallingFinalize" message="Avoid calling finalize() explicitly" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> finalize() is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. </description>
|
|
<example><![CDATA[
|
|
|
|
|
|
void foo() {
|
|
Bar b = new Bar();
|
|
b.finalize();
|
|
}
|
|
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//Statement/StatementExpression/PrimaryExpression/PrimaryPrefix
|
|
[
|
|
./Name[
|
|
@Image='finalize'
|
|
or ends-with(@Image,'.finalize')
|
|
]
|
|
or @Image='finalize'
|
|
]
|
|
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AvoidProtectedFieldInFinalClass" message="Avoid protected fields in a final class. Change to private or package access." class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Do not use protected fields in final classes since they cannot be subclassed. Clarify your intent by using private or package access modifiers instead. </description>
|
|
<example><![CDATA[
|
|
public final class Bar {
|
|
private int x;
|
|
protected int y; // <-- Bar cannot be subclassed, so is y really private or package visible???
|
|
Bar() {}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ClassOrInterfaceDeclaration[@Final='true']//FieldDeclaration[@Protected='true']
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AvoidThrowingCertainExceptionTypes" message="Avoid throwing certain exception types." class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> 1) Avoid throwing certain exception types. Rather than throw a raw RuntimeException, Throwable, Exception, or Error, use a subclassed exception or error instead. 2) Avoid throwing a NullPointerException - it's confusing because most people will assume that the VM threw NPE. Consider using InvalidArgumentException("Null parameter") which will be clearly seen as a programmer initiated exception.. Use IllegalArgumentException or IllegalStateException instead. </description>
|
|
<example><![CDATA[
|
|
|
|
throw new Exception();
|
|
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//AllocationExpression
|
|
/ClassOrInterfaceType[
|
|
@Image='Throwable' |
|
|
@Image='Exception' |
|
|
@Image='Error' |
|
|
@Image='RuntimeException' |
|
|
@Image='NullPointerException']
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="MethodArgumentCouldBeFinal" message="Parameter ''{0}'' is not assigned and could be declared final" class="net.sourceforge.pmd.rules.optimization.MethodArgumentCouldBeFinal" symboltable="true">
|
|
<description> A method argument that is never assigned can be declared final. </description>
|
|
<example><![CDATA[
|
|
|
|
public void foo (String param) {
|
|
// do stuff with param never assigning it
|
|
// better: public void foo (final String param) {
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="AvoidCatchingNPE" message="Avoid catching NullPointerException; consider removing the cause of the NPE." class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Code should never throw NPE under normal circumstances. A catch block may hide the original error, causing other more subtle errors in its wake. </description>
|
|
<example><![CDATA[
|
|
try {
|
|
...
|
|
} catch (NullPointerException npe) {
|
|
...
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//TryStatement/FormalParameter/Type
|
|
/ReferenceType/ClassOrInterfaceType[@Image='NullPointerException']
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AvoidDeeplyNestedIfStmts" message="Deeply nested if..then statements are hard to read" class="net.sourceforge.pmd.rules.AvoidDeeplyNestedIfStmtsRule">
|
|
<description> Deeply nested if..then statements are hard to read. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public void bar() {
|
|
int x=2;
|
|
int y=3;
|
|
int z=4;
|
|
if (x>y) {
|
|
if (y>z) {
|
|
if (z==x) {
|
|
// this is officially out of control now
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="problemDepth" value="5"/>
|
|
</properties>
|
|
</rule>
|
|
<rule name="SimplifyStartsWith" message="This call to String.startsWith can be rewritten using String.charAt(0)" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description>
|
|
Since it passes in a literal of length 1, this call to String.startsWith can be rewritten using String.charAt(0) to save some time.
|
|
</description>
|
|
<example><![CDATA[
|
|
public class Foo {
|
|
boolean checkIt(String x) {
|
|
return x.startsWith("a");
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//PrimaryExpression
|
|
[PrimaryPrefix/Name
|
|
[ends-with(@Image, '.startsWith')]]
|
|
[PrimarySuffix/Arguments/ArgumentList
|
|
/Expression/PrimaryExpression/PrimaryPrefix
|
|
/Literal
|
|
[string-length(@Image)=3]
|
|
[starts-with(@Image, '"')]
|
|
[ends-with(@Image, '"')]
|
|
]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AvoidInstantiatingObjectsInLoops" message="Avoid instantiating new objects inside loops" class="net.sourceforge.pmd.rules.optimization.AvoidInstantiatingObjectsInLoops">
|
|
<description> Detects when a new object is created inside a loop </description>
|
|
<example><![CDATA[
|
|
|
|
public class Something {
|
|
public static void main( String as[] ) {
|
|
for (int i = 0; i < 10; i++) {
|
|
Foo f = new Foo(); //Avoid this whenever you can it's really expensive
|
|
}
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="ExcessiveParameterList" message="Avoid really long parameter lists." class="net.sourceforge.pmd.rules.design.LongParameterListRule">
|
|
<description> This checks to make sure that the Parameter Lists in the project aren't getting too long. If there are long parameter lists, then that is generally indicative that another object is hiding around there. Basically, try to group the parameters together. Default value is 2.5 sigma greater than the mean. NOTE: In version 0.9 and higher, their are three parameters available: minimum - Minimum Length before reporting. sigma - Std Deviations away from the mean before reporting. topscore - The Maximum Number of reports to generate. At this time, only one can be used at a time. </description>
|
|
<example><![CDATA[
|
|
|
|
public void addData(
|
|
int p00, int p01, int p02, int p03, int p04, int p05,
|
|
int p05, int p06, int p07, int p08, int p09, int p10) {
|
|
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="minimum" value="10"/>
|
|
</properties>
|
|
</rule>
|
|
<rule name="NullAssignment" message="Assigning an Object to null is a code smell. Consider refactoring." class="net.sourceforge.pmd.rules.design.NullAssignmentRule">
|
|
<description> Assigning a "null" to a variable (outside of its declaration) is usually in bad form. Some times, the assignment is an indication that the programmer doesn't completely understand what is going on in the code. NOTE: This sort of assignment may in rare cases be useful to encourage garbage collection. If that's what you're using it for, by all means, disregard this rule :-) </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public void bar() {
|
|
Object x = null; // This is OK.
|
|
x = new Object();
|
|
// Big, complex piece of code here.
|
|
x = null; // This is BAD.
|
|
// Big, complex piece of code here.
|
|
}
|
|
}
|
|
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="IfElseStmtsMustUseBraces" message="Avoid using 'if...else' statements without curly braces" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid using if..else statements without using curly braces </description>
|
|
<example><![CDATA[
|
|
|
|
|
|
public void doSomething() {
|
|
// this is OK
|
|
if (foo) x++;
|
|
|
|
// but this is not
|
|
if (foo)
|
|
x=x+1;
|
|
else
|
|
x=x-1;
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//Statement
|
|
[parent::IfStatement[@Else='true']]
|
|
[not(child::Block)]
|
|
[not(child::IfStatement)]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="StringInstantiation" message="Avoid instantiating String objects; this is usually unnecessary." class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid instantiating String objects; this is usually unnecessary. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
private String bar = new String("bar"); // just do a String bar = "bar";
|
|
}
|
|
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//AllocationExpression[ClassOrInterfaceType/@Image='String'][count(.//Expression) < 2][not(ArrayDimsAndInits)]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="EmptyIfStmt" message="Avoid empty 'if' statements" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Empty If Statement finds instances where a condition is checked but nothing is done about it. </description>
|
|
<example><![CDATA[
|
|
if (foo == 0) {
|
|
// why bother checking up on foo?
|
|
}
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
//IfStatement/Statement/Block[count(*) = 0]
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="WhileLoopsMustUseBraces" message="Avoid using 'while' statements without curly braces" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid using 'while' statements without using curly braces </description>
|
|
<example><![CDATA[
|
|
|
|
public void doSomething() {
|
|
while (true)
|
|
x++;
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//WhileStatement[not(Statement/Block)]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="EqualsNull" message="Don't use equals() to compare against null" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Newbie programmers sometimes get the comparison concepts confused and use equals() to compare to null. </description>
|
|
<example><![CDATA[
|
|
|
|
class Bar {
|
|
void foo() {
|
|
String x = "foo";
|
|
if (x.equals(null)) { // bad!
|
|
doSomething();
|
|
}
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//PrimaryExpression
|
|
[PrimaryPrefix/Name[ends-with(@Image, 'equals')]]
|
|
[PrimarySuffix/Arguments/ArgumentList
|
|
/Expression/PrimaryExpression/PrimaryPrefix
|
|
/Literal/NullLiteral]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="CloseConnection" message="Ensures that Connection objects are always closed after use" class="net.sourceforge.pmd.rules.CloseConnection">
|
|
<description> Ensures that Connection objects are always closed after use </description>
|
|
<example><![CDATA[
|
|
|
|
public void foo() {
|
|
Connection c = pool.getConnection();
|
|
try {
|
|
// do stuff
|
|
} catch (SQLException ex) {
|
|
// handle exception
|
|
} finally {
|
|
// oops, should close the connection using 'close'!
|
|
// c.close();
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="BadComparison" message="Avoid equality comparisons with Double.NaN" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid equality comparisons with Double.NaN - these are likely to be logic errors. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Bar {
|
|
int x = (y == Double.NaN);
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//EqualityExpression[@Image='==']
|
|
/PrimaryExpression/PrimaryPrefix
|
|
/Name[@Image='Double.NaN' or @Image='Float.NaN']
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="LongVariable" message="Avoid excessively long variable names like {0}" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Detects when a field, formal or local variable is declared with a long name. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Something {
|
|
int reallyLongIntName = -3; // VIOLATION - Field
|
|
|
|
public static void main( String argumentsList[] ) { // VIOLATION - Formal
|
|
int otherReallyLongName = -5; // VIOLATION - Local
|
|
|
|
for (int interestingIntIndex = 0; // VIOLATION - For
|
|
interestingIntIndex < 10;
|
|
interestingIntIndex ++ ) {
|
|
|
|
}
|
|
}
|
|
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[//VariableDeclaratorId[string-length(@Image) > 32]]]></value>
|
|
</property>
|
|
<property name="pluginname" value="true"/>
|
|
</properties>
|
|
</rule>
|
|
<rule name="ClassNamingConventions" message="Class names should begin with an uppercase character" class="net.sourceforge.pmd.rules.ClassNamingConventions">
|
|
<description> Class names should always begin with an upper case character. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {}
|
|
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="DoubleCheckedLocking" message="Double checked locking is not thread safe in Java." class="net.sourceforge.pmd.rules.DoubleCheckedLocking">
|
|
<description> Partially created objects can be returned by the Double Checked Locking pattern when used in Java. An optimizing JRE may assign a reference to the baz variable before it creates the object the reference is intended to point to. For more details see http://www.javaworld.com/javaworld/jw-02-2001/jw-0209-double.html. </description>
|
|
<example><![CDATA[
|
|
public class Foo {
|
|
Object baz;
|
|
Object bar() {
|
|
if(baz == null) { //baz may be non-null yet not fully created
|
|
synchronized(this){
|
|
if(baz == null){
|
|
baz = new Object();
|
|
}
|
|
}
|
|
}
|
|
return baz;
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="CollapsibleIfStatements" message="These nested if statements could be combined" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description>
|
|
Sometimes two 'if' statements can be consolidated by separating their conditions with a boolean short-circuit operator.
|
|
</description>
|
|
<example><![CDATA[
|
|
public class Foo {
|
|
void bar() {
|
|
if (x) {
|
|
if (y) {
|
|
// do stuff
|
|
}
|
|
}
|
|
}
|
|
}
|
|
]]></example>
|
|
<priority>5</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//IfStatement[@Else='false']/Statement
|
|
/IfStatement[@Else='false']
|
|
|
|
|
//IfStatement[@Else='false']/Statement
|
|
/Block[count(BlockStatement)=1]/BlockStatement
|
|
/Statement/IfStatement[@Else='false']
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="UseNotifyAllInsteadOfNotify" message="Call Thread.notifyAll() rather than Thread.notify()" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> notify() awakens a thread monitoring the object. If more than one thread is monitoring, then only one is chosen. The thread chosen is arbitrary; thus it's usually safer to call notifyAll() instead. </description>
|
|
<example><![CDATA[
|
|
|
|
x.notify();
|
|
// If many threads are monitoring x, only one (and you won't know which) will be notified.
|
|
// use instead:
|
|
x.notifyAll();
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//Statement/StatementExpression/PrimaryExpression/PrimaryPrefix
|
|
[
|
|
./Name[
|
|
@Image='notify'
|
|
or ends-with(@Image,'.notify')
|
|
]
|
|
or @Image='notify'
|
|
or ( ./AllocationExpression
|
|
and ../PrimarySuffix[@Image='notify'] )
|
|
]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="LocalVariableCouldBeFinal" message="Local variable could be declared final" class="net.sourceforge.pmd.rules.optimization.LocalVariableCouldBeFinal">
|
|
<description> A local variable assigned only once can be declared final. </description>
|
|
<example><![CDATA[
|
|
|
|
public void foo () {
|
|
String a = "a"; //if a will not be assigned again it is better to do this:
|
|
final String b = "b";
|
|
...
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="ShortMethodName" message="Avoid using short method names" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Detects when very short method names are used. </description>
|
|
<example><![CDATA[
|
|
|
|
public class ShortMethod {
|
|
public void a( int i ) { // Violation
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//MethodDeclarator[string-length(@Image) < 3]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="AvoidConcatenatingNonLiteralsInStringBuffer" message="Avoid concatenating non literals in a StringBuffer constructor or append()" class="net.sourceforge.pmd.rules.AvoidConcatenatingNonLiteralsInStringBuffer">
|
|
<description> Avoid concatenating non literals in a StringBuffer constructor or append(). </description>
|
|
<example><![CDATA[
|
|
|
|
|
|
// Avoid this
|
|
StringBuffer sb=new
|
|
StringBuffer("AAAAAAAAAA"+System.getProperty("java.io.tmpdir"));
|
|
|
|
// use instead something like this
|
|
StringBuffer sb = new StringBuffer("AAAAAAAAAA");
|
|
sb.append(System.getProperty("java.io.tmpdir"));
|
|
|
|
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties/>
|
|
</rule>
|
|
<rule name="SystemPrintln" message="System.out.println is used" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> System.(out|err).println is used, consider using a logger. </description>
|
|
<example><![CDATA[
|
|
|
|
class Foo{
|
|
Logger log = Logger.getLogger(Foo.class.getName());
|
|
|
|
public void testA () {
|
|
System.out.println("Entering test");
|
|
// Better use this
|
|
log.fine("Entering test");
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>2</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//Statement[
|
|
child:://Name[
|
|
starts-with(@Image, 'System.out.print')
|
|
or
|
|
starts-with(@Image, 'System.err.print')
|
|
]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="FinalizeDoesNotCallSuperFinalize" message="Last statement in finalize method should be a call to super.finalize()" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> If the finalize() is implemented, its last action should be to call super.finalize </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
protected void finalize() {
|
|
something();
|
|
// neglected to call super.finalize()
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
|
|
//MethodDeclaration[MethodDeclarator[@Image='finalize'][not(FormalParameters/*)]]
|
|
/Block
|
|
/BlockStatement[last()]
|
|
[not(Statement/StatementExpression/PrimaryExpression/PrimaryPrefix[@Image='finalize'])]
|
|
[not(Statement/TryStatement[@Finally='true']
|
|
/Block/BlockStatement/Statement/StatementExpression
|
|
/PrimaryExpression/PrimaryPrefix[@Image='finalize'])]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="EmptySynchronizedBlock" message="Avoid empty synchronized blocks" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Avoid empty synchronized blocks - they're useless. </description>
|
|
<example><![CDATA[
|
|
// this is bad
|
|
public void bar() {
|
|
synchronized (this) {}
|
|
}
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
//SynchronizedStatement/Block[1][count(*) = 0]
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="UnnecessaryFinalModifier" message="Unnecessary final modifier in final class" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> When a class has the final modifier, all the methods are marked finally. </description>
|
|
<example><![CDATA[
|
|
|
|
public final class Foo {
|
|
|
|
// This final modifier is not necessary, since the class is final
|
|
// and thus, all methods are final
|
|
private final void foo() {
|
|
}
|
|
|
|
}
|
|
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//ClassOrInterfaceDeclaration[@Final='true' and @Interface='false']
|
|
/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/MethodDeclaration[@Final='true']
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="SwitchStmtsShouldHaveDefault" message="Switch statements should have a default label" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Switch statements should have a default label. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
public void bar() {
|
|
int x = 2;
|
|
switch (x) {
|
|
case 2: int j = 8;
|
|
}
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//SwitchStatement[not(SwitchLabel[count(*) = 0])]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="FinalizeOverloaded" message="Finalize methods should not be overloaded" class="net.sourceforge.pmd.rules.XPathRule">
|
|
<description> Methods named finalize() should not have parameters. It is confusing and probably a bug to overload finalize(). It will not be called by the VM. </description>
|
|
<example><![CDATA[
|
|
|
|
public class Foo {
|
|
// this is confusing and probably a bug
|
|
protected void finalize(int a) {
|
|
}
|
|
}
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties>
|
|
<property name="xpath">
|
|
<value><![CDATA[
|
|
|
|
//MethodDeclaration
|
|
/MethodDeclarator[@Image='finalize'][FormalParameters[count(*)>0]]
|
|
|
|
]]></value>
|
|
</property>
|
|
</properties>
|
|
</rule>
|
|
<rule name="ImmutableField" message="Private field could be made final. It is only initialized in the declaration or constructor." class="net.sourceforge.pmd.rules.design.ImmutableField" symboltable="true">
|
|
<description> Identifies private fields whose values never change once they are initialized either in the declaration of the field or by a constructor. This aids in converting existing classes to immutable classes. </description>
|
|
<example><![CDATA[
|
|
|
|
public class FinalFields {
|
|
private int x;
|
|
|
|
public FinalFields() {
|
|
x = 7;
|
|
}
|
|
|
|
public void foo() {
|
|
int a = x + 2;
|
|
}
|
|
|
|
}
|
|
Identifies x as being eligible for making final.
|
|
|
|
]]></example>
|
|
<priority>4</priority>
|
|
<properties/>
|
|
</rule>
|
|
</ruleset>
|