Fixed bug 2724653 - AvoidThreadGroup reports false positives

Changed Rule to use Type Resolution.


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@6918 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Ryan Gustafson 2009-04-24 02:51:55 +00:00
parent c168ecc6e5
commit ae499b9132
4 changed files with 68 additions and 21 deletions

View File

@ -4,7 +4,9 @@ Fixed bug 2590258 - NPE with nicerhtml output
Fixed bug 2317099 - False + in SimplifyCondition Fixed bug 2317099 - False + in SimplifyCondition
Fixed bug 2606609 - False "UnusedImports" positive in package-info.java Fixed bug 2606609 - False "UnusedImports" positive in package-info.java
Fixed bug 2645268 - ClassCastException in UselessOperationOnImmutable.getDeclaration Fixed bug 2645268 - ClassCastException in UselessOperationOnImmutable.getDeclaration
Fixed bug 2724653 - AvoidThreadGroup reports false positives
Correct -benchmark reporting of Rule visits via the RuleChain Correct -benchmark reporting of Rule visits via the RuleChain
Fix issue with Type Resolution incorrectly handling of Classes with same name as a java.lang Class.
Android ruleset: CallSuperLast rule now also checks for finish() redefinitions Android ruleset: CallSuperLast rule now also checks for finish() redefinitions

View File

@ -15,6 +15,19 @@ public class Foo {
</test-code> </test-code>
<test-code> <test-code>
<description><![CDATA[ <description><![CDATA[
bad, using fully qualified java.lang.ThreadGroup()
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
java.lang.ThreadGroup t = new java.lang.ThreadGroup();
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
bad, using Thread.getThreadGroup() bad, using Thread.getThreadGroup()
]]></description> ]]></description>
<expected-problems>1</expected-problems> <expected-problems>1</expected-problems>
@ -36,6 +49,37 @@ public class Foo {
void bar() { void bar() {
ThreadGroup t = System.getSecurityManager().getThreadGroup(); ThreadGroup t = System.getSecurityManager().getThreadGroup();
} }
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
ThreadGroup() but not java.lang.ThreadGroup
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import some.pkg.ThreadGroup;
public class Foo {
void bar() {
ThreadGroup t = new ThreadGroup();
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
MyThreadGroup() not java.lang.ThreadGroup
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.lang.ThreadGroup;
public class Foo {
public class MyThreadGroup{}
void bar() {
MyThreadGroup t = new MyThreadGroup();
}
} }
]]></code> ]]></code>
</test-code> </test-code>

View File

@ -942,11 +942,12 @@ if (method1().equals(a)) {
<rule name="AvoidThreadGroup" <rule name="AvoidThreadGroup"
since="3.6" since="3.6"
message="Avoid using ThreadGroup; it is not thread safe" message="Avoid using java.lang.ThreadGroup; it is not thread safe"
class="net.sourceforge.pmd.rules.XPathRule" class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/basic.html#AvoidThreadGroup"> externalInfoUrl="http://pmd.sourceforge.net/rules/basic.html#AvoidThreadGroup"
typeResolution="true">
<description> <description>
Avoid using ThreadGroup; although it is intended to be used in a threaded environment Avoid using java.lang.ThreadGroup; although it is intended to be used in a threaded environment
it contains methods that are not thread safe. it contains methods that are not thread safe.
</description> </description>
<priority>3</priority> <priority>3</priority>
@ -954,7 +955,7 @@ Avoid using ThreadGroup; although it is intended to be used in a threaded enviro
<property name="xpath"> <property name="xpath">
<value> <value>
<![CDATA[ <![CDATA[
//AllocationExpression/ClassOrInterfaceType[contains(@Image,'ThreadGroup')] | //AllocationExpression/ClassOrInterfaceType[typeof(@Image, 'java.lang.ThreadGroup')]|
//PrimarySuffix[contains(@Image, 'getThreadGroup')] //PrimarySuffix[contains(@Image, 'getThreadGroup')]
]]> ]]>
</value> </value>

View File

@ -644,6 +644,8 @@ public class ClassTypeResolver extends JavaParserVisitorAdapter {
List<ASTImportDeclaration> theImportDeclarations = node.findChildrenOfType(ASTImportDeclaration.class); List<ASTImportDeclaration> theImportDeclarations = node.findChildrenOfType(ASTImportDeclaration.class);
importedClasses = new HashMap<String, String>(); importedClasses = new HashMap<String, String>();
importedClasses.putAll(myJavaLang);
// go through the imports // go through the imports
for (ASTImportDeclaration anImportDeclaration : theImportDeclarations) { for (ASTImportDeclaration anImportDeclaration : theImportDeclarations) {
String strPackage = anImportDeclaration.getPackageName(); String strPackage = anImportDeclaration.getPackageName();
@ -655,8 +657,6 @@ public class ClassTypeResolver extends JavaParserVisitorAdapter {
importedClasses.put(strName.substring(strPackage.length() + 1), strName); importedClasses.put(strName.substring(strPackage.length() + 1), strName);
} }
} }
importedClasses.putAll(myJavaLang);
} }
private void populateClassName(ASTCompilationUnit node, String className) throws ClassNotFoundException { private void populateClassName(ASTCompilationUnit node, String className) throws ClassNotFoundException {