pmd: fix #992 Class java.beans.Statement triggered in CloseResource rule

This commit is contained in:
Andreas Dangel 2013-03-16 11:40:15 +01:00
parent c00ca9e44c
commit 589ec3c6da
3 changed files with 42 additions and 2 deletions

View File

@ -1,5 +1,6 @@
????? ??, 2013 - 5.0.3:
Fixed bug 992: Class java.beans.Statement triggered in CloseResource rule
Fixed bug 1002: False +: FinalFieldCouldBeStatic on inner class
Fixed bug 1005: False + for ConstructorCallsOverridableMethod - overloaded methods
Fixed bug 1032: ImmutableField Rule: Private field in inner class gives false positive

View File

@ -48,13 +48,14 @@ import net.sourceforge.pmd.lang.rule.properties.StringMultiProperty;
public class CloseResourceRule extends AbstractJavaRule {
private Set<String> types = new HashSet<String>();
private Set<String> simpleTypes = new HashSet<String>();
private Set<String> closeTargets = new HashSet<String>();
private static final StringMultiProperty CLOSE_TARGETS_DESCRIPTOR = new StringMultiProperty("closeTargets",
"Methods which may close this resource", new String[]{}, 1.0f, ',');
private static final StringMultiProperty TYPES_DESCRIPTOR = new StringMultiProperty("types",
"Affected types", new String[]{"Connection","Statement","ResultSet"}, 2.0f, ',');
"Affected types", new String[]{"java.sql.Connection","java.sql.Statement","java.sql.ResultSet"}, 2.0f, ',');
public CloseResourceRule() {
definePropertyDescriptor(CLOSE_TARGETS_DESCRIPTOR);
@ -69,9 +70,23 @@ public class CloseResourceRule extends AbstractJavaRule {
if (types.isEmpty() && getProperty(TYPES_DESCRIPTOR) != null) {
types.addAll(Arrays.asList(getProperty(TYPES_DESCRIPTOR)));
}
if (simpleTypes.isEmpty() && getProperty(TYPES_DESCRIPTOR) != null) {
for (String type : getProperty(TYPES_DESCRIPTOR)) {
simpleTypes.add(toSimpleType(type));
}
}
return super.visit(node, data);
}
private static String toSimpleType(String fullyQualifiedClassName) {
int lastIndexOf = fullyQualifiedClassName.lastIndexOf('.');
if (lastIndexOf > -1) {
return fullyQualifiedClassName.substring(lastIndexOf + 1);
} else {
return fullyQualifiedClassName;
}
}
@Override
public Object visit(ASTConstructorDeclaration node, Object data) {
checkForResources(node, data);
@ -96,7 +111,11 @@ public class CloseResourceRule extends AbstractJavaRule {
ASTReferenceType ref = (ASTReferenceType) type.jjtGetChild(0);
if (ref.jjtGetChild(0) instanceof ASTClassOrInterfaceType) {
ASTClassOrInterfaceType clazz = (ASTClassOrInterfaceType) ref.jjtGetChild(0);
if (types.contains(clazz.getImage())) {
if (clazz.getType() != null && types.contains(clazz.getType().getName())
|| (clazz.getType() == null && simpleTypes.contains(toSimpleType(clazz.getImage())))
|| types.contains(clazz.getImage())) {
ASTVariableDeclaratorId id = var.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
ids.add(id);
}

View File

@ -405,4 +405,24 @@ public class CloseResourceRuleBug {
}
]]></code>
</test-code>
<test-code>
<description>#992 Class java.beans.Statement triggered in CloseResource rule</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.beans.Statement;
public class Test {
public void foo() {
Statement stmt = new Statement(vo, "set" + prop, new Object[] { vector });
try {
stmt.execute();
} catch (Exception e) {
throw new RuntimeException("Could not set property prop: " + prop + "of type:" + pd.getPropertyType(), e);
}
}
}
]]></code>
</test-code>
</test-data>