diff --git a/pmd/rulesets/basic-jsf.xml b/pmd/rulesets/basic-jsf.xml index 71284a8d87..028ffb398f 100644 --- a/pmd/rulesets/basic-jsf.xml +++ b/pmd/rulesets/basic-jsf.xml @@ -1,6 +1,7 @@ Empty Catch Block finds instances where an exception is caught, @@ -24,7 +23,7 @@ which should either be acted on or reported. Empty If Statement finds instances where a condition is checked but nothing is done about it. @@ -63,7 +61,7 @@ Empty If Statement finds instances where a condition is checked but nothing is d @@ -85,8 +83,7 @@ public class Foo { Empty While Statement finds all instances where a while statement @@ -97,7 +94,7 @@ it's a while loop that does a lot in the exit expression, rewrite it to make it @@ -120,8 +117,7 @@ public class Foo { Avoid empty try blocks - what's the point? @@ -130,7 +126,7 @@ Avoid empty try blocks - what's the point? @@ -153,8 +149,7 @@ public class Foo { Avoid empty finally blocks - these can be deleted. @@ -163,7 +158,7 @@ Avoid empty finally blocks - these can be deleted. @@ -188,8 +183,7 @@ public class Foo { Avoid empty switch statements. @@ -198,7 +192,7 @@ Avoid empty switch statements. @@ -222,8 +216,7 @@ public class Foo { Avoid jumbled loop incrementers - it's usually a mistake, and it's confusing even if it's what's intended. @@ -232,7 +225,7 @@ Avoid jumbled loop incrementers - it's usually a mistake, and it's confusing eve Some for loops can be simplified to while loops - this makes them more concise. @@ -270,7 +262,7 @@ Some for loops can be simplified to while loops - this makes them more concise. 1] [not(ForInit)] [not(ForUpdate)] @@ -382,8 +374,7 @@ public class Foo { Avoid returning from a finally block - this can discard exceptions. @@ -392,7 +383,7 @@ Avoid returning from a finally block - this can discard exceptions. @@ -418,8 +409,7 @@ public class Bar { Avoid empty synchronized blocks - they're useless. @@ -428,7 +418,7 @@ public class Bar { @@ -472,8 +462,7 @@ public class Foo { An empty static initializer was found. @@ -482,7 +471,7 @@ An empty static initializer was found. @@ -501,8 +490,7 @@ public class Foo { Do not use "if" statements that are always true or always false. @@ -511,7 +499,7 @@ Do not use "if" statements that are always true or always false. @@ -534,8 +522,7 @@ public class Foo { An empty statement (aka a semicolon by itself) that is not used @@ -547,7 +534,7 @@ removed. When a class has the final modifier, all the methods are automatically final. @@ -606,7 +592,7 @@ When a class has the final modifier, all the methods are automatically final. @@ -629,8 +615,7 @@ public final class Foo { Sometimes two 'if' statements can be consolidated by separating their conditions with a boolean short-circuit operator. @@ -639,10 +624,10 @@ Sometimes two 'if' statements can be consolidated by separating their conditions @@ -686,8 +671,7 @@ public String foo() { if you need to get an array of a class from your Collection, @@ -699,7 +683,7 @@ ClassCastException. One might assume that "new BigDecimal(.1)" is exactly equal @@ -759,7 +742,7 @@ if executed) The null check here is misplaced. if the variable is null you'll get a NullPointerException. @@ -830,7 +812,7 @@ class Test { After checking an object reference for null, you should invoke equals() on that object rather than passing it to another object's equals() method. @@ -874,7 +855,7 @@ public class Foo { The null check is broken since it will throw a Nullpointer itself. @@ -980,7 +960,7 @@ It is likely that you used || instead of && or vice versa. 1]//PrimaryPrefix/Name/@Image, diff --git a/pmd/rulesets/braces.xml b/pmd/rulesets/braces.xml index 5d90aebc96..113593941c 100644 --- a/pmd/rulesets/braces.xml +++ b/pmd/rulesets/braces.xml @@ -11,8 +11,7 @@ The Braces Ruleset contains a collection of braces rules. Avoid using if statements without using curly braces. @@ -21,7 +20,7 @@ Avoid using if statements without using curly braces. @@ -42,8 +41,7 @@ Avoid using if statements without using curly braces. Avoid using 'while' statements without using curly braces. @@ -52,7 +50,7 @@ Avoid using 'while' statements without using curly braces. @@ -71,8 +69,7 @@ public void doSomething() { Avoid using if..else statements without using curly braces. @@ -81,7 +78,7 @@ Avoid using if..else statements without using curly braces. Avoid using 'for' statements without using curly braces. @@ -118,7 +114,7 @@ Avoid using 'for' statements without using curly braces. diff --git a/pmd/rulesets/clone.xml b/pmd/rulesets/clone.xml index 79c2d7f41d..c3e0ff908f 100644 --- a/pmd/rulesets/clone.xml +++ b/pmd/rulesets/clone.xml @@ -11,8 +11,7 @@ The Clone Implementation ruleset contains a collection of rules that find questi Object clone() should be implemented with super.clone(). @@ -21,7 +20,7 @@ Object clone() should be implemented with super.clone(). The method clone() should throw a CloneNotSupportedException. @@ -59,7 +57,7 @@ The method clone() should throw a CloneNotSupportedException. The method clone() should only be implemented if the class implements the Cloneable interface with the exception of a final method that only throws CloneNotSupportedException. @@ -98,7 +95,7 @@ The method clone() should only be implemented if the class implements the Clonea This rule detects when a constructor is not necessary; i.e., when there's only one constructor, @@ -26,7 +25,7 @@ it's public, has an empty body, and takes no arguments. Each class should declare at least one constructor. @@ -164,7 +162,7 @@ Each class should declare at least one constructor. @@ -228,8 +226,7 @@ public class Foo { It is a good practice to call super() in a constructor. If super() is not called but @@ -239,7 +236,7 @@ It is a good practice to call super() in a constructor. If super() is not called 0 ] +//ClassOrInterfaceDeclaration[ count (ExtendsList/*) > 0 ] /ClassOrInterfaceBody /ClassOrInterfaceBodyDeclaration /ConstructorDeclaration[ count (.//ExplicitConstructorInvocation)=0 ] @@ -267,8 +264,7 @@ public class Foo extends Bar{ Sometimes expressions are wrapped in unnecessary parentheses, @@ -278,7 +274,7 @@ making them look like a function call. Use explicit scoping instead of the default package private level. @@ -331,7 +326,7 @@ Use explicit scoping instead of the default package private level. Use bitwise inversion to invert boolean values - it's the fastest way to do this. @@ -357,7 +351,7 @@ See http://www.javaspecialists.co.za/archive/newsletter.do?issue=042&locale= diff --git a/pmd/rulesets/design.xml b/pmd/rulesets/design.xml index c65c3cc18f..5f382e649d 100644 --- a/pmd/rulesets/design.xml +++ b/pmd/rulesets/design.xml @@ -61,8 +61,7 @@ public class Foo { Avoid unnecessary comparisons in boolean expressions - this complicates simple code. @@ -71,7 +70,7 @@ Avoid unnecessary comparisons in boolean expressions - this complicates simple c @@ -93,8 +92,7 @@ public class Bar { Switch statements should have a default label. @@ -103,7 +101,7 @@ Switch statements should have a default label. @@ -270,8 +268,7 @@ public class Outer { If a final field is assigned to a compile-time constant, it could be @@ -281,7 +278,7 @@ made static, thus saving overhead in each object at runtime. A nonstatic initializer block will be called any time a constructor @@ -345,7 +341,7 @@ is a valid language construct, it is rarely used and is confusing. @@ -365,8 +361,7 @@ public class MyClass { By convention, the default label should be the last label in a switch statement. @@ -375,7 +370,7 @@ By convention, the default label should be the last label in a switch statement. @@ -402,8 +397,7 @@ public class Foo { A non-case label (e.g. a named break/continue label) was present in a switch statement. @@ -413,7 +407,7 @@ This legal, but confusing. It is easy to mix up the case labels and the non-case @@ -440,8 +434,7 @@ public class Foo { A call to Collection.toArray can use the Collection's size vs an empty Array of the desired type. @@ -450,7 +443,7 @@ A call to Collection.toArray can use the Collection's size vs an empty Array of Avoid equality comparisons with Double.NaN - these are likely to be logic errors. @@ -491,7 +483,7 @@ Avoid equality comparisons with Double.NaN - these are likely to be logic errors @@ -510,8 +502,7 @@ public class Bar { Inexperienced programmers sometimes confuse comparison concepts @@ -521,7 +512,7 @@ and use equals() to compare to null. Avoid instantiating an object just to call getClass() on it; use the .class public member instead. @@ -599,7 +589,7 @@ public class Foo { Be sure to specify a Locale when creating a new instance of SimpleDateFormat. @@ -645,7 +634,7 @@ Be sure to specify a Locale when creating a new instance of SimpleDateFormat. @@ -690,8 +679,7 @@ public class Foo { When doing a String.toLowerCase()/toUpperCase() call, use a Locale. This avoids @@ -719,7 +707,7 @@ class Foo { Do not use protected fields in final classes since they cannot be subclassed. @@ -744,7 +731,7 @@ Clarify your intent by using private or package access modifiers instead. @@ -785,8 +772,7 @@ public class StaticField { A class that has private constructors and does not have any static methods or fields cannot be used. @@ -795,7 +781,7 @@ A class that has private constructors and does not have any static methods or fi 0 and count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration) = count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[@Private='true']) ) and @@ -826,8 +812,7 @@ public class Foo { Method level synchronization can backfire when new code is added to the method. Block-level @@ -837,7 +822,7 @@ public class Foo { @@ -861,8 +846,7 @@ public class Foo { A switch statement without an enclosed break statement may be a bug. @@ -871,7 +855,7 @@ A switch statement without an enclosed break statement may be a bug. 0] [count(BlockStatement/Statement/ReturnStatement) @@ -904,8 +888,7 @@ public class Foo { Thread.notify() awakens a thread monitoring the object. If more than one thread is monitoring, then only @@ -915,7 +898,7 @@ one is chosen. The thread chosen is arbitrary; thus it's usually safer to call Each caught exception type should be handled in its own catch clause. @@ -955,7 +937,7 @@ Each caught exception type should be handled in its own catch clause. The abstract class does not contain any abstract methods. An abstract class suggests @@ -998,7 +979,7 @@ direcly) a protected constructor can be provided prevent direct instantiation. No need to check for null before an instanceof; the instanceof keyword returns false when given a null argument. @@ -1031,7 +1011,7 @@ No need to check for null before an instanceof; the instanceof keyword returns f Position literals first in String comparisons - that way if the String is null you won't get a NullPointerException, it'll just return false. @@ -1097,7 +1076,7 @@ class Foo { @@ -1175,8 +1154,7 @@ public static Foo getFoo() { Uncommented Empty Method finds instances where a method does not contain @@ -1188,7 +1166,7 @@ empty methods. @@ -1204,8 +1182,7 @@ public void doSomething() { Uncommented Empty Constructor finds instances where a constructor does not @@ -1217,7 +1194,7 @@ and unintentional empty constructors. @@ -1237,8 +1214,7 @@ public Foo() { + class="net.sourceforge.pmd.rules.XPathRule"> 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. @@ -1247,7 +1223,7 @@ public Foo() { If the finalize() method is empty, then it does not need to exist. @@ -22,7 +21,7 @@ If the finalize() method is empty, then it does not need to exist. @@ -40,8 +39,7 @@ public class Foo { If the finalize() is implemented, it should do something besides just calling @@ -51,7 +49,7 @@ super.finalize(). Methods named finalize() should not have parameters. It is @@ -87,7 +84,7 @@ not be called by the VM. 0]] ]]> @@ -107,8 +104,7 @@ public class Foo { If the finalize() is implemented, its last action should be to call super.finalize. @@ -119,7 +115,7 @@ If the finalize() is implemented, its last action should be to call super.finali If you override finalize(), make it protected. If you make @@ -156,7 +151,7 @@ If you override finalize(), make it protected. If you make diff --git a/pmd/rulesets/j2ee.xml b/pmd/rulesets/j2ee.xml index b6c4148095..6623a9bda3 100644 --- a/pmd/rulesets/j2ee.xml +++ b/pmd/rulesets/j2ee.xml @@ -8,8 +8,7 @@ + class="net.sourceforge.pmd.rules.XPathRule"> In J2EE getClassLoader() might not work as expected. Use Thread.currentThread().getContextClassLoader() instead. @@ -17,7 +16,7 @@ diff --git a/pmd/rulesets/javabeans.xml b/pmd/rulesets/javabeans.xml index 1890100ff3..9a6ee8e50c 100644 --- a/pmd/rulesets/javabeans.xml +++ b/pmd/rulesets/javabeans.xml @@ -49,8 +49,7 @@ provide getFoo and setFoo methods. Classes that are serializable should provide a serialVersionUID field. @@ -59,7 +58,7 @@ Classes that are serializable should provide a serialVersionUID field. The suite() method in a JUnit test needs to be both public and static. @@ -21,7 +20,7 @@ The suite() method in a JUnit test needs to be both public and static. @@ -43,8 +42,7 @@ public class Foo extends TestCase { Some JUnit framework methods are easy to misspell. @@ -53,7 +51,7 @@ Some JUnit framework methods are easy to misspell. A JUnit test assertion with a boolean literal is unnecessary since it always will eval to the same thing. @@ -158,7 +155,7 @@ statements like assertTrue(true) and assertFalse(false). If you just want a tes This rule detects JUnit assertions in object equality. These assertions @@ -204,7 +200,7 @@ should be made by more specific methods, like assertEquals. This rule detects JUnit assertions in object references equality. These assertions @@ -241,7 +236,7 @@ should be made by more specific methods, like assertSame, assertNotSame. This rule detects JUnit assertions in object references equality. These assertions @@ -279,7 +273,7 @@ public class FooTest extends TestCase { Avoid negation in an assertTrue or assertFalse test. @@ -322,7 +315,7 @@ assertFalse(expr); To make sure the full stacktrace is printed out, use the logging statement with 2 arguments: a String and a Throwable. @@ -20,7 +19,7 @@ To make sure the full stacktrace is printed out, use the logging statement with Logger should normally be defined private static final and have the correct class. @@ -59,7 +57,7 @@ public class Main { In most cases, the Logger can be declared static and final. @@ -42,7 +41,7 @@ In most cases, the Logger can be declared static and final. System.(out|err).print is used, consider using a logger. @@ -112,7 +110,7 @@ System.(out|err).print is used, consider using a logger. Avoid printStackTrace(); use a logger call instead. @@ -148,7 +145,7 @@ Avoid printStackTrace(); use a logger call instead. diff --git a/pmd/rulesets/migrating.xml b/pmd/rulesets/migrating.xml index 11acccf2a9..a9e880fe0b 100644 --- a/pmd/rulesets/migrating.xml +++ b/pmd/rulesets/migrating.xml @@ -13,8 +13,7 @@ Contains rules about migrating from one JDK version to another. Don't use these Consider replacing Vector usages with the newer java.util.ArrayList if expensive threadsafe operation is not required. @@ -23,7 +22,7 @@ Contains rules about migrating from one JDK version to another. Don't use these @@ -42,8 +41,7 @@ public class Foo { Consider replacing this Hashtable with the newer java.util.Map @@ -52,7 +50,7 @@ public class Foo { @@ -71,8 +69,7 @@ public class Foo { Consider replacing this Enumeration with the newer java.util.Iterator @@ -81,7 +78,7 @@ public class Foo { @@ -104,15 +101,14 @@ public class Foo implements Enumeration { Finds all places 'enum' is used as an identifier is used. @@ -131,15 +127,14 @@ public class Foo implements Enumeration { Finds all places 'assert' is used as an identifier is used. @@ -158,14 +153,13 @@ public class Foo implements Enumeration { + class="net.sourceforge.pmd.rules.XPathRule"> In JDK 1.5, calling new Integer() causes memory allocation. Integer.valueOf() is more memory friendly. Detects when a field, local, or parameter has a very short name. @@ -22,7 +21,7 @@ Detects when a field, local, or parameter has a very short name. @@ -47,8 +46,7 @@ public class Something { Detects when a field, formal or local variable is declared with a long name. @@ -59,7 +57,7 @@ Detects when a field, formal or local variable is declared with a long name. $minimum] +//VariableDeclaratorId[string-length(@Image) > $minimum] ]]> @@ -81,8 +79,7 @@ public class Something { Detects when very short method names are used. @@ -92,7 +89,7 @@ Detects when very short method names are used. @@ -170,8 +167,7 @@ public class Foo {} Abstract classes should be named 'AbstractXXX'. @@ -181,7 +177,7 @@ Abstract classes should be named 'AbstractXXX'. @@ -254,8 +250,7 @@ public class Foo { A field name is all in uppercase characters, which in Sun's Java naming @@ -265,7 +260,7 @@ conventions indicate a constant. However, the field is not final. The method name and parameter number are suspiciously close to @@ -301,7 +295,7 @@ method. Detects when a class or interface does not have a package definition. @@ -407,7 +400,7 @@ Detects when a class or interface does not have a package definition. @@ -424,8 +417,7 @@ public class ClassInDefaultPackage { Detects when a package definition contains upper case characters. @@ -434,7 +426,7 @@ public class ClassInDefaultPackage { @@ -451,8 +443,7 @@ public class SomeClass { Detects when a non-field has a name starting with 'm_'. This usually @@ -462,7 +453,7 @@ indicates a field and thus is confusing. diff --git a/pmd/rulesets/optimizations.xml b/pmd/rulesets/optimizations.xml index 6a01cb345c..572bc864c4 100644 --- a/pmd/rulesets/optimizations.xml +++ b/pmd/rulesets/optimizations.xml @@ -70,8 +70,7 @@ public class Something { ArrayList is a much better Collection implementation than Vector. @@ -80,7 +79,7 @@ ArrayList is a much better Collection implementation than Vector. @@ -102,8 +101,7 @@ public class SimpleTest extends TestCase { 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. @@ -112,7 +110,7 @@ Since it passes in a literal of length 1, this call to String.startsWith can be The class java.util.Arrays has a "asList" method that @@ -179,7 +176,7 @@ public class Foo { Instead of copying data between two arrays, use @@ -242,7 +238,7 @@ public class Foo { Code should never throw NPE under normal circumstances. A catch block may hide the original error, causing other more subtle errors in its wake. @@ -101,7 +100,7 @@ public class Foo { @@ -111,8 +110,7 @@ public class Foo { Avoid throwing certain exception types. Rather than throw a raw RuntimeException, Throwable, @@ -132,7 +130,7 @@ public void bar() throws Exception { Avoid throwing a NullPointerException - it's confusing because most people will assume that the @@ -168,7 +165,7 @@ public class Foo { @@ -177,8 +174,7 @@ public class Foo { + class="net.sourceforge.pmd.rules.XPathRule"> Catch blocks that merely rethrow a caught exception only add to code size and runtime complexity. @@ -199,7 +195,7 @@ public class Foo { StringBuffer sb = new StringBuffer('c'); The @@ -303,7 +302,7 @@ StringBuffer size. 1) { SimpleVariableContext vc = new SimpleVariableContext(); for (Iterator i = properties.entrySet().iterator(); i.hasNext();) { diff --git a/pmd/src/net/sourceforge/pmd/rules/XPathRule.java b/pmd/src/net/sourceforge/pmd/rules/XPathRule.java index d3b7492b41..7ad1148614 100644 --- a/pmd/src/net/sourceforge/pmd/rules/XPathRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/XPathRule.java @@ -10,6 +10,7 @@ import net.sourceforge.pmd.ast.Node; import net.sourceforge.pmd.ast.SimpleNode; import net.sourceforge.pmd.jaxen.DocumentNavigator; import net.sourceforge.pmd.jaxen.MatchesFunction; + import org.jaxen.BaseXPath; import org.jaxen.JaxenException; import org.jaxen.SimpleVariableContext; @@ -29,6 +30,25 @@ import java.util.Map.Entry; */ public class XPathRule extends CommonAbstractRule { + public static Class loadClass(ClassLoader classloader, String xpath, String name) { + if (xpath.indexOf('|') != -1) { + //System.err.println(name + " not a dynamic rule: " + xpath.trim().replaceAll("\n", "")); + return XPathRule.class; + } + String part = xpath.trim(); + + // Need to use DOTALL mode because of potential line terminators + if (!part.matches("(?s)//\\w+\\W.*")) { + //System.err.println(name + " not a dynamic rule: " + xpath.trim().replaceAll("\n", "")); + return XPathRule.class; + } + + String tail = part.replaceFirst("^//\\w+", ""); + String nodeName = part.substring(2, part.indexOf(tail)); + + return DynamicXPathRule.loadClass(classloader, nodeName); + } + private XPath xpath; private boolean regexpFunctionRegistered;