[java] CloseResource: support try-with-resources
This commit is contained in:
@ -32,6 +32,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
|
|||||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
|
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
|
import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
|
import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
|
||||||
|
import net.sourceforge.pmd.lang.java.ast.ASTResourceSpecification;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
|
import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
|
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTTryStatement;
|
import net.sourceforge.pmd.lang.java.ast.ASTTryStatement;
|
||||||
@ -157,10 +158,12 @@ public class CloseResourceRule extends AbstractJavaRule {
|
|||||||
if (var.hasInitializer()) {
|
if (var.hasInitializer()) {
|
||||||
// figure out the runtime type. If the variable is initialized, take the type from there
|
// figure out the runtime type. If the variable is initialized, take the type from there
|
||||||
TypeNode runtimeType = var.getInitializer().getFirstChildOfType(ASTExpression.class);
|
TypeNode runtimeType = var.getInitializer().getFirstChildOfType(ASTExpression.class);
|
||||||
if (runtimeType != null && !isAllowedResourceType(runtimeType)) {
|
if (runtimeType != null && runtimeType.getType() != null) {
|
||||||
ids.put(var.getVariableId(), runtimeType);
|
type = runtimeType;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
if (!isAllowedResourceType(type)) {
|
||||||
ids.put(var.getVariableId(), type);
|
ids.put(var.getVariableId(), type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -358,6 +361,15 @@ public class CloseResourceRule extends AbstractJavaRule {
|
|||||||
if (closed) {
|
if (closed) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} else if (t.isTryWithResources()) {
|
||||||
|
// maybe the variable is used as a resource
|
||||||
|
List<ASTName> names = t.getFirstChildOfType(ASTResourceSpecification.class).findDescendantsOfType(ASTName.class);
|
||||||
|
for (ASTName potentialUsage : names) {
|
||||||
|
if (potentialUsage.hasImageEqualTo(variableToClose)) {
|
||||||
|
closed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -890,6 +890,27 @@ public class Foo {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
|
||||||
|
<test-code>
|
||||||
|
<description>closed with try-with-resources</description>
|
||||||
|
<expected-problems>0</expected-problems>
|
||||||
|
<code><![CDATA[
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class Foo {
|
||||||
|
public int bar() {
|
||||||
|
InputStream inputStream = getInputStreamFromSomewhere();
|
||||||
|
if (inputStream != null) {
|
||||||
|
try (InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8")) {
|
||||||
|
char c = reader.read();
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]]></code>
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
Reference in New Issue
Block a user