<!-- New rules Jan 2010-->
<rule name="NPathComplexity"
since="3.9"
message="The method {0}() has an NPath complexity of {1}"
class="net.sourceforge.pmd.rules.design.NpathComplexity"
externalInfoUrl="http://pmd.sourceforge.net/rules/codesize.html#NPathComplexity">
<description>
The NPath complexity of a method is the number of acyclic execution paths through that method.
A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
</description>
<priority>2</priority> <!-- Priority 1 to 2. Changed by ATP3RXS -->
<properties>
<property name="minimum" description="The npath reporting threshold" value="200"/>
</properties>
<example>
<![CDATA[
public class Foo {
void bar() {
// lots of complicated code
}
}
]]>
</example>
</rule>
<rule name="ExcessiveMethodLength"
since="0.6"
message="Avoid really long methods."
class="net.sourceforge.pmd.rules.design.LongMethodRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/codesize.html#ExcessiveMethodLength">
<description>
Violations of this rule usually indicate that the method is doing
too much. Try to reduce the method size by creating helper methods and removing any copy/pasted code.
</description>
<priority>2</priority> <!-- Priority 3 to 2. Changed by ATP3RXS -->
<properties>
<property name="minimum" description="The method size reporting threshold" value="60"/>
</properties>
<example>
<![CDATA[
public class Foo {
public void doSomething() {
System.out.println("Hello world!");
System.out.println("Hello world!");
// 98 copies omitted for brevity.
}
}
]]>
</example>
</rule>
<rule name="ExcessiveClassLength"
since="0.6"
message="Avoid really long classes."
class="net.sourceforge.pmd.rules.design.LongClassRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/codesize.html#ExcessiveClassLength">
<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
manageable.
</description>
<priority>2</priority> <!-- Priority 1 to 2. Changed by ATP3RXS -->
<properties>
<property name="minimum" description="The class size reporting threshold" value="1000"/>
</properties>
<example>
<![CDATA[
public class Foo {
public void bar() {
// 1000 lines of code
}
}
]]>
</example>
</rule>
<rule name="CyclomaticComplexity"
since="1.03"
message = "The {0} ‘‘{1}‘‘ has a Cyclomatic Complexity of {2}."
class="net.sourceforge.pmd.rules.CyclomaticComplexity"
externalInfoUrl="http://pmd.sourceforge.net/rules/codesize.html#CyclomaticComplexity">
<description>
<![CDATA[
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‘. Generally,
1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity,
and 11+ is very high complexity.
]]>
</description>
<priority>2</priority> <!-- Priority 1 to 2. Changed by ATP3RXS -->
<properties>
<property name="reportLevel" description="The Cyclomatic Complexity reporting threshold" value="10"/>
<property name="showClassesComplexity"
description="Indicate if class average violation should be added to the report"
value="true"/>
<property name="showMethodsComplexity"
description="Indicate if class average violation should be added to the report"
value="true"/>
</properties>
<example>
<![CDATA[
// Cyclomatic Complexity = 12
public class Foo {
1 public void example() {
2 if (a == b) {
3 if (a1 == b1) {
fiddle();
4 } else if a2 == b2) {
fiddle();
} else {
fiddle();
}
5 } else if (c == d) {
6 while (c == d) {
fiddle();
}
7 } else if (e == f) {
8 for (int n = 0; n < h; n++) {
fiddle();
}
} else{
switch (z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
12 default:
fiddle();
break;
}
}
}
}
]]>
</example>
</rule>
<rule name="NcssMethodCount" message="The method {0}() has an NCSS line count of {1}"
since="3.9"
class="net.sourceforge.pmd.rules.codesize.NcssMethodCount"
externalInfoUrl="http://pmd.sourceforge.net/rules/codesize.html#NcssMethodCount">
<description>
This rule uses the NCSS (Non Commenting Source Statements) algorithm to determine the number of lines
of code for a given method. NCSS ignores comments, and counts actual statements. Using this algorithm,
lines of code that are split are counted as one.
</description>
<priority>1</priority>
<properties>
<property name="minimum" description="The method NCSS count reporting threshold" value="100"/>
</properties>
<example>
<![CDATA[
public class Foo extends Bar {
public int methd() {
super.methd();
//this method only has 1 NCSS lines
return 1;
}
}
]]>
</example>
</rule>
<rule name="AvoidReassigningParameters"
since="1.0"
message="Avoid reassigning parameters such as ‘‘{0}‘‘"
class="net.sourceforge.pmd.rules.AvoidReassigningParameters"
externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#AvoidReassigningParameters">
<description>
Reassigning values to parameters is a questionable practice. Use a temporary local variable instead.
</description>
<priority>2</priority>
<example>
<![CDATA[
public class Foo {
private void foo(String bar) {
bar = "something else";
}
}
]]>
</example>
</rule>
<rule name="EqualsNull"
since="1.9"
message="Avoid using equals() to compare against null"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#EqualsNull">
<description>
Inexperienced programmers sometimes confuse comparison concepts
and use equals() to compare to null.
</description>
<priority>1</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryExpression
[
PrimaryPrefix/Name[ends-with(@Image, ‘equals‘)]
or
PrimarySuffix[ends-with(@Image, ‘equals‘)]
]
[PrimarySuffix/Arguments/ArgumentList[count(Expression)=1]
/Expression/PrimaryExpression/PrimaryPrefix
/Literal/NullLiteral]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
class Bar {
void foo() {
String x = "foo";
if (x.equals(null)) { // bad!
doSomething();
}
}
}
]]>
</example>
</rule>
<rule name="InstantiationToGetClass"
since="2.0"
message="Avoid instantiating an object just to call getClass() on it; use the .class public member instead"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#InstantiationToGetClass">
<description>
Avoid instantiating an object just to call getClass() on it; use the .class public member instead.
</description>
<priority>2</priority> <!-- Priority 4 to 2. Changed by ATP3RXS -->
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimarySuffix
[@Image=‘getClass‘]
[parent::PrimaryExpression
[PrimaryPrefix/AllocationExpression]
[count(PrimarySuffix) = 2]
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
// Replace this
Class c = new String().getClass();
// with this:
Class c = String.class;
}
]]>
</example>
</rule>
<rule name="MissingBreakInSwitch"
since="3.0"
message="A switch statement does not contain a break"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#MissingBreakInSwitch">
<description>
A switch statement without an enclosed break statement may be a bug.
</description>
<priority>2</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//SwitchStatement
[count(.//BreakStatement)=0]
[count(SwitchLabel) > 0]
[count(BlockStatement/Statement/ReturnStatement)
+ count(BlockStatement/Statement/ThrowStatement)
< count (SwitchLabel)]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
public void bar(int status) {
switch(status) {
case CANCELLED:
doCancelled();
// break; hm, should this be commented out?
case NEW:
doNew();
case REMOVED:
doRemoved();
}
}
}
]]>
</example>
</rule>
<rule name="AbstractClassWithoutAbstractMethod"
since="3.0"
message="This abstract class does not have any abstract methods"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#AbstractClassWithoutAbstractMethod">
<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>
<priority>3</priority>
<properties>
<property name="xpath">
<value><![CDATA[
//ClassOrInterfaceDeclaration
[@Abstract=‘true‘
and count( .//MethodDeclaration[@Abstract=‘true‘] )=0 ]
[count(ImplementsList)=0]
[count(.//ExtendsList)=0]
]]>
</value>
</property>
</properties>
<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>
</rule>
<rule name="AvoidConstantsInterface"
since="3.5"
message="An Interface should be used only to model a behaviour; consider converting this to a class."
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#AvoidConstantsInterface">
<description>
An interface should be used only to model a behaviour of a
class: using an interface as a container of constants is a poor usage pattern.
</description>
<priority>2</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceDeclaration[@Interface="true"]
[
count(.//MethodDeclaration)=0
and
count(.//FieldDeclaration)>0
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public interface ConstantsInterface {
public static final int CONSTANT1=0;
public static final String CONSTANT2="1";
}
]]>
</example>
</rule>
<rule name="AbstractClassWithoutAnyMethod"
since="4.2"
class="net.sourceforge.pmd.rules.XPathRule"
message="No abstract method which means that the keyword is most likely used to prevent instantiation. use a private or protected constructor instead."
externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#AbstractClassWithoutAnyMethod">
<description>
<![CDATA[
If the abstract class does not provides any methods, it may be just a data container that is not to be instantiated. In this case, it‘s probably
better to use a private or a protected constructor in order to prevent instantiation than make the class misleadingly abstract.
]]>
</description>
<priority>2</priority> <!-- Priority 1 to 2. Changed by ATP3RXS -->
<properties>
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceDeclaration[
(@Abstract = ‘true‘)
and
(count(//MethodDeclaration) + count(//ConstructorDeclaration) = 0)
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class abstract Example {
String field;
int otherField;
}
]]>
</example>
</rule>
<rule name="MisplacedNullCheck"
since="3.5"
message="The null check here is misplaced; if the variable is null there‘ll be a NullPointerException"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/basic.html#MisplacedNullCheck">
<description>
The null check here is misplaced. if the variable is null you‘ll get a NullPointerException.
Either the check is useless (the variable will never be "null") or it‘s incorrect.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Expression
/*[self::ConditionalOrExpression or self::ConditionalAndExpression]
/descendant::PrimaryExpression/PrimaryPrefix
/Name[starts-with(@Image,
concat(ancestor::PrimaryExpression/following-sibling::EqualityExpression
[./PrimaryExpression/PrimaryPrefix/Literal/NullLiteral]
/PrimaryExpression/PrimaryPrefix
/Name[count(../../PrimarySuffix)=0]/@Image,"."))
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
void bar() {
if (a.equals(baz) && a != null) {}
}
}
]]>
</example>
<example><![CDATA[
public class Foo {
void bar() {
if (a.equals(baz) || a == null) {}
}
}
]]></example>
</rule>
<rule name="AvoidThrowingNullPointerException"
since="1.8"
message="Avoid throwing null pointer exceptions."
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/strictexception.html#AvoidThrowingNullPointerException">
<description>
Avoid throwing a NullPointerException - it‘s confusing because most people will assume that the
virtual machine threw it. Consider using an IllegalArgumentException instead; this will be
clearly seen as a programmer-initiated exception.
</description>
<priority>1</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//AllocationExpression/ClassOrInterfaceType[@Image=‘NullPointerException‘]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
void bar() {
throw new NullPointerException();
}
}
]]>
</example>
</rule>
<rule name="AvoidRethrowingException"
since="3.8"
message="A catch statement that catches an exception only to rethrow it should be avoided."
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/strictexception.html#AvoidRethrowingException">
<description>
Catch blocks that merely rethrow a caught exception only add to code size and runtime complexity.
</description>
<priority>1</priority> <!-- Priority 3 to 1. Changed by ATP3RXS -->
<properties>
<property name="xpath">
<value>
<![CDATA[
//CatchStatement[FormalParameter
/VariableDeclaratorId/@Image = Block/BlockStatement/Statement
/ThrowStatement/Expression/PrimaryExpression[count(PrimarySuffix)=0]/PrimaryPrefix/Name/@Image
and count(Block/BlockStatement/Statement) =1]
]]>
</value>
</property>
</properties>
<example> <![CDATA[
public class Foo {
void bar() {
try {
// do something
} catch (SomeException se) {
throw se;
}
}
}
]]>
</example>
</rule>
<rule
name="DoNotExtendJavaLangError"
since="4.0"
message="Exceptions should not extend java.lang.Error"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/strictexception.html#DoNotExtendJavaLangError">
<description>
<![CDATA[
Errors are system exceptions. Do not extend them.
]]>
</description>
<priority>1</priority> <!-- Priority 3 to 1. Changed by ATP1AXR -->
<properties>
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceDeclaration/ExtendsList/ClassOrInterfaceType
[@Image="Error" or @Image="java.lang.Error"]
]]>
</value>
</property>
</properties>
<example><![CDATA[
public class Foo extends Error { }
]]></example>
</rule>
<rule name="DoNotThrowExceptionInFinally"
since="4.2"
message="A throw statement in a finally block makes the control flow hard to understand."
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/strictexception.html#DoNotThrowExceptionInFinally">
<description>
<![CDATA[
Throwing exception in a finally block is confusing. It may mask exception or a defect of the code,
it also render code cleanup uninstable.
Note: This is a PMD implementation of the Lint4j rule "A throw in a finally block"
]]>
</description>
<priority>1</priority> <!-- Priority 4 to 1. Changed by ATP1AXR -->
<properties>
<property name="xpath">
<value>
<![CDATA[
//FinallyStatement[descendant::ThrowStatement]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo
{
public void bar()
{
try {
// Here do some stuff
}
catch( Exception e) {
// Handling the issue
}
finally
{
// is this really a good idea ?
throw new Exception();
}
}
}
]]>
</example>
</rule>
<rule name="UseStringBufferLength"
since="3.4"
message="This is an inefficient use of StringBuffer.toString; call StringBuffer.length instead."
class="net.sourceforge.pmd.rules.strings.UseStringBufferLength"
externalInfoUrl="http://pmd.sourceforge.net/rules/strings.html#UseStringBufferLength">
<description>
Use StringBuffer.length() to determine StringBuffer length rather than using StringBuffer.toString().equals("")
or StringBuffer.toString().length() ==.
</description>
<priority>2</priority> <!-- Priority 3 to 2. Changed by ATP3RXS -->
<example>
<![CDATA[
public class Foo {
void bar() {
StringBuffer sb = new StringBuffer();
// this is bad
if(sb.toString().equals("")) {}
// this is good
if(sb.length() == 0) {}
}
}
]]>
</example>
</rule>
<rule name="UseStringBufferForStringAppends"
since="3.1"
message="Prefer StringBuffer over += for concatenating strings"
class="net.sourceforge.pmd.rules.optimization.UseStringBufferForStringAppends"
externalInfoUrl="http://pmd.sourceforge.net/rules/optimizations.html#UseStringBufferForStringAppends">
<description>
Finds usages of += for appending strings.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
void bar() {
String a;
a = "foo";
a += " bar";
// better would be:
// StringBuffer a = new StringBuffer("foo");
// a.append(" bar);
}
}
]]>
</example>
</rule>
<!-- END new rules Jan 2010-->
</ruleset>