pmd: fix #1027 PMD Ant: java.lang.ClassCastException

This commit is contained in:
Andreas Dangel 2013-03-17 11:59:00 +01:00
parent 43ecf1e110
commit b6c6899741
5 changed files with 110 additions and 2 deletions

View File

@ -11,6 +11,7 @@ Fixed bug 997: Rule NonThreadSafeSingleton gives analysis problem
Fixed bug 999: Law of Demeter: False positives and negatives
Fixed bug 1002: False +: FinalFieldCouldBeStatic on inner class
Fixed bug 1005: False + for ConstructorCallsOverridableMethod - overloaded methods
Fixed bug 1027: PMD Ant: java.lang.ClassCastException
Fixed bug 1032: ImmutableField Rule: Private field in inner class gives false positive
Fixed bug 1064: Exception running PrematureDeclaration
Fixed bug 1068: CPD fails on broken symbolic links

View File

@ -289,7 +289,12 @@ public class PMDTask extends Task {
log("Using the normal ClassLoader", Project.MSG_VERBOSE);
} else {
log("Using the AntClassLoader", Project.MSG_VERBOSE);
configuration.setClassLoader(new AntClassLoader(getProject(), classpath));
// must be true, otherwise you'll get ClassCastExceptions as classes are loaded twice
// and exist in multiple class loaders
boolean parentFirst = true;
configuration.setClassLoader(
new AntClassLoader(Thread.currentThread().getContextClassLoader(), getProject(),
classpath, parentFirst));
}
try {
/*

View File

@ -110,7 +110,12 @@ public class PMDTaskTest extends BuildFileTest {
assertOutputContaining("Avoid using global variables");
assertOutputContaining("Use ===/!== to compare with true/false or Numbers");
}
@Test
public void testClasspath() {
executeTarget("testClasspath");
}
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(PMDTaskTest.class);
}

View File

@ -0,0 +1,86 @@
<?xml version="1.0"?>
<ruleset name="Basic"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
The Basic ruleset contains a collection of good practices which should be followed.
</description>
<rule name="JumbledIncrementer"
language="java"
since="1.0"
message="Avoid modifying an outer loop incrementer in an inner loop for update expression"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/basic.html#JumbledIncrementer">
<description>
Avoid jumbled loop incrementers - its usually a mistake, and is confusing even if intentional.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ForStatement
[
ForUpdate/StatementExpressionList/StatementExpression/PostfixExpression/PrimaryExpression/PrimaryPrefix/Name/@Image
=
ancestor::ForStatement/ForInit//VariableDeclaratorId/@Image
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class JumbledIncrementerRule1 {
public void foo() {
for (int i = 0; i < 10; i++) { // only references 'i'
for (int k = 0; k < 20; i++) { // references both 'i' and 'k'
System.out.println("Hello");
}
}
}
}
]]>
</example>
</rule>
<rule name="OverrideBothEqualsAndHashcode"
language="java"
since="0.4"
message="Ensure you override both equals() and hashCode()"
class="net.sourceforge.pmd.lang.java.rule.basic.OverrideBothEqualsAndHashcodeRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/basic.html#OverrideBothEqualsAndHashcode">
<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>
<priority>3</priority>
<example>
<![CDATA[
public class Bar { // poor, missing a hashcode() method
public boolean equals(Object o) {
// do some comparison
}
}
public class Baz { // poor, missing an equals() method
public int hashCode() {
// return some hash value
}
}
public class Foo { // perfect, both methods provided
public boolean equals(Object other) {
// do some comparison
}
public int hashCode() {
// return some hash value
}
}
]]>
</example>
</rule>
</ruleset>

View File

@ -127,4 +127,15 @@
</fileset>
</pmd>
</target>
<target name="testClasspath">
<pmd>
<ruleset>classpathtest/ruleset.xml</ruleset>
<classpath>
<pathelement path="${pmd.home}/target/classes"/>
<pathelement path="${pmd.home}/target/test-classes"/>
<pathelement path="${pmd.home}/target/test-classes/net/sourceforge/pmd/ant"/>
</classpath>
</pmd>
</target>
</project>