CloseResource: a method call as an allocation argument bug fix

This commit is contained in:
Mykhailo Palahuta committed 2020-07-27 23:45:02 +03:00
1 parent 4bb3759371
commit aa3271978f
2 files changed
+27 -10

No files matched your search

@@ -187,15 +187,6 @@ public class CloseResourceRule extends AbstractJavaRule {
return null;
}
private boolean isNotMethodCall(ASTExpression expr) {
return !isMethodCall(expr);
}
private boolean isMethodCall(ASTExpression expression) {
return expression != null && expression.getNumChildren() > 0
&& expression.getChild(0).getFirstChildOfType(ASTPrimarySuffix.class) != null;
}
private TypeNode wrappedResourceTypeOrReturn(ASTVariableDeclarator var, TypeNode defaultVal) {
if (var.hasInitializer()) {
TypeNode wrappedResType = getWrappedResourceType(var);
@@ -221,7 +212,7 @@ public class CloseResourceRule extends AbstractJavaRule {
ASTArgumentList argsList = allocation.getFirstDescendantOfType(ASTArgumentList.class);
if (argsList != null) {
ASTExpression firstArg = argsList.getFirstChildOfType(ASTExpression.class);
return isNotLiteral(firstArg) ? firstArg : null;
return isNotLiteral(firstArg) && isNotMethodCall(firstArg) ? firstArg : null;
}
}
return null;
@@ -241,6 +232,15 @@ public class CloseResourceRule extends AbstractJavaRule {
return literal == null;
}
private boolean isNotMethodCall(ASTExpression expr) {
return !isMethodCall(expr);
}
private boolean isMethodCall(ASTExpression expression) {
return expression != null && expression.getNumChildren() > 0
&& expression.getChild(0).getFirstChildOfType(ASTPrimarySuffix.class) != null;
}
private boolean isNotAllowedResourceType(TypeNode varType) {
return !isAllowedResourceType(varType);
}
@@ -1313,6 +1313,23 @@ public class Foo {
}
};
}
}
]]></code>
</test-code>
<test-code>
<description>wrapped ByteArrayInputStream false-negative test</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>5</expected-linenumbers>
<code><![CDATA[
import java.io.*;
public class Foo {
public void bar() {
ByteArrayOutputStream bos = new ByteArrayOutputStream(new byte[10]);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
int c = ois.read();
bos.close();
}
}
]]></code>
</test-code>