From c2a45c64caad35aed004ea8c8fd95979c8ae2460 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 29 Jun 2019 15:01:24 +0200 Subject: [PATCH] [java] CloseResource: verify first argument for chained streams --- .../rule/errorprone/CloseResourceRule.java | 19 +++++++++++++- .../rule/errorprone/xml/CloseResource.xml | 25 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloseResourceRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloseResourceRule.java index 589825fe01..d51bda2cc0 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloseResourceRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloseResourceRule.java @@ -42,7 +42,9 @@ import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId; import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer; import net.sourceforge.pmd.lang.java.ast.TypeNode; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; +import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration; import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper; +import net.sourceforge.pmd.lang.symboltable.NameOccurrence; import net.sourceforge.pmd.properties.PropertyDescriptor; /** @@ -187,10 +189,25 @@ public class CloseResourceRule extends AbstractJavaRule { private ASTExpression getAllocationFirstArgument(ASTExpression expression) { List allocations = expression.findDescendantsOfType(ASTAllocationExpression.class); + ASTExpression firstArgument = null; + if (!allocations.isEmpty()) { ASTArgumentList argumentList = allocations.get(allocations.size() - 1).getFirstDescendantOfType(ASTArgumentList.class); if (argumentList != null) { - return argumentList.getFirstChildOfType(ASTExpression.class); + firstArgument = argumentList.getFirstChildOfType(ASTExpression.class); + } + } + + // the argument must not be a literal, it needs to be a Name referring to a variable + if (firstArgument != null && firstArgument.getFirstDescendantOfType(ASTName.class) != null) { + ASTName name = firstArgument.getFirstDescendantOfType(ASTName.class); + + Map> vars = firstArgument.getScope() + .getDeclarations(VariableNameDeclaration.class); + for (VariableNameDeclaration nameDecl : vars.keySet()) { + if (nameDecl.getName().equals(name.getImage())) { + return firstArgument; + } } } return null; diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/CloseResource.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/CloseResource.xml index 95c8409c30..98df84b9f8 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/CloseResource.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/CloseResource.xml @@ -1038,6 +1038,31 @@ public class CloseResourcePrintWriter { pw.println("Foo"); return sw.toString(); } +} + ]]> + + + + Correctly determine the type for the message + 2 + 7,8 + + Ensure that resources like this FileInputStream object are closed after use + Ensure that resources like this Scanner object are closed after use + +