From 52b8be6601329f1f695217f651e49182b64ed944 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 26 Jan 2013 15:43:46 +0100 Subject: [PATCH] pmd: fixed #947 CloseResource rule fails if field is marked with annotation --- pmd/etc/changelog.txt | 1 + .../java/rule/design/CloseResourceRule.java | 4 +-- .../java/rule/design/xml/CloseResource.xml | 31 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index f316f53afb..16eb0c9949 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -2,6 +2,7 @@ Fixed bug 878: False positive: UnusedFormalParameter for abstract methods Fixed bug 913: SignatureDeclareThrowsException is raised twice +Fixed bug 947: CloseResource rule fails if field is marked with annotation Fixed bug 1007: Parse Exception with annotation Fixed bug 1011: CloseResource Rule ignores Constructors Fixed bug 1012: False positive: Useless parentheses. 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 c6eb82bb1b..d3c90711cb 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 @@ -97,7 +97,7 @@ public class CloseResourceRule extends AbstractJavaRule { if (ref.jjtGetChild(0) instanceof ASTClassOrInterfaceType) { ASTClassOrInterfaceType clazz = (ASTClassOrInterfaceType) ref.jjtGetChild(0); if (types.contains(clazz.getImage())) { - ASTVariableDeclaratorId id = (ASTVariableDeclaratorId) var.jjtGetChild(1).jjtGetChild(0); + ASTVariableDeclaratorId id = var.getFirstDescendantOfType(ASTVariableDeclaratorId.class); ids.add(id); } } @@ -227,7 +227,7 @@ public class CloseResourceRule extends AbstractJavaRule { // if all is not well, complain if (!closed) { - ASTType type = (ASTType) var.jjtGetChild(0); + ASTType type = var.getFirstChildOfType(ASTType.class); ASTReferenceType ref = (ASTReferenceType) type.jjtGetChild(0); ASTClassOrInterfaceType clazz = (ASTClassOrInterfaceType) ref.jjtGetChild(0); addViolation(data, id, clazz.getImage()); 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 6ed1a11866..9c2e659b4d 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 @@ -374,4 +374,35 @@ public class Test { } ]]> + + #1029 No instance level check in the close resource rule + 0 + + + + #947 CloseResource rule fails if field is marked with annotation + 2 + +