diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 96f236c32f..47fbf0c322 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -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 diff --git a/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CloseResourceRule.java b/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CloseResourceRule.java index d3c90711cb..969c452e15 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CloseResourceRule.java +++ b/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CloseResourceRule.java @@ -48,13 +48,14 @@ import net.sourceforge.pmd.lang.rule.properties.StringMultiProperty; public class CloseResourceRule extends AbstractJavaRule { private Set types = new HashSet(); + private Set simpleTypes = new HashSet(); private Set closeTargets = new HashSet(); 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); } diff --git a/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/CloseResource.xml b/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/CloseResource.xml index 9c2e659b4d..2fa53e4337 100644 --- a/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/CloseResource.xml +++ b/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/CloseResource.xml @@ -405,4 +405,24 @@ public class CloseResourceRuleBug { } ]]> + + + #992 Class java.beans.Statement triggered in CloseResource rule + 0 + +