[java] CloseResource: Fix NPE in case of "var"
This commit is contained in:
@ -32,6 +32,7 @@ AbstractTokenizer and the custom tokenizers of Fortran, Perl and Ruby are deprec
|
||||
* pmd-java
|
||||
* [#2708](https://github.com/pmd/pmd/issues/2708): \[java] False positive FinalFieldCouldBeStatic when using lombok Builder.Default
|
||||
* [#2738](https://github.com/pmd/pmd/issues/2738): \[java] Custom rule with @ExhaustiveEnumSwitch throws NPE
|
||||
* [#2755](https://github.com/pmd/pmd/issues/2755): \[java] \[6.27.0] Exception applying rule CloseResource on file ... java.lang.NullPointerException
|
||||
* [#2756](https://github.com/pmd/pmd/issues/2756): \[java] TypeTestUtil fails with NPE for anonymous class
|
||||
* [#2759](https://github.com/pmd/pmd/issues/2759): \[java] False positive in UnusedAssignment
|
||||
* [#2767](https://github.com/pmd/pmd/issues/2767): \[java] IndexOutOfBoundsException when parsing an initializer BlockStatement
|
||||
|
@ -175,7 +175,7 @@ public class CloseResourceRule extends AbstractJavaRule {
|
||||
Map<ASTVariableDeclarator, TypeNode> resVars = new HashMap<>();
|
||||
for (ASTVariableDeclarator var : vars) {
|
||||
TypeNode varType = getTypeOfVariable(var);
|
||||
if (isResourceTypeOrSubtype(varType)) {
|
||||
if (varType != null && isResourceTypeOrSubtype(varType)) {
|
||||
resVars.put(var, wrappedResourceTypeOrReturn(var, varType));
|
||||
}
|
||||
}
|
||||
@ -189,7 +189,7 @@ public class CloseResourceRule extends AbstractJavaRule {
|
||||
|
||||
private TypeNode getDeclaredTypeOfVariable(ASTVariableDeclarator var) {
|
||||
ASTLocalVariableDeclaration localVar = (ASTLocalVariableDeclaration) var.getParent();
|
||||
return localVar.getTypeNode();
|
||||
return localVar.getTypeNode(); // note: can be null, if type is inferred (var)
|
||||
}
|
||||
|
||||
private TypeNode getRuntimeTypeOfVariable(ASTVariableDeclarator var) {
|
||||
|
@ -1385,6 +1385,27 @@ public class Foo {
|
||||
}
|
||||
Closeables.close(stream, false);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>[java] [6.27.0] Exception applying rule CloseResource on file ... java.lang.NullPointerException #2755</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
import java.io.*;
|
||||
|
||||
public class Foo {
|
||||
public int bar() {
|
||||
var inputStream = getInputStreamFromSomewhere();
|
||||
if (inputStream != null) {
|
||||
try (InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8")) {
|
||||
char c = reader.read();
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
Reference in New Issue
Block a user