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 2606609 - False "UnusedImports" positive in package-info.java
Fixed bug 2645268 - ClassCastException in UselessOperationOnImmutable.getDeclaration
Fixed bug 2724653 - AvoidThreadGroup reports false positives
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

View File

@ -1,42 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description><![CDATA[
<test-code>
<description><![CDATA[
bad, using new ThreadGroup()
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
ThreadGroup t = new ThreadGroup("my tg");
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
</test-code>
<test-code>
<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()
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
ThreadGroup t = Thread.currentThread().getThreadGroup();
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
</test-code>
<test-code>
<description><![CDATA[
bad, using System.getSecurityManager().getThreadGroup()
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
ThreadGroup t = System.getSecurityManager().getThreadGroup();
}
}
]]></code>
</test-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>
</test-code>
</test-data>

View File

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

View File

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