Skip to content

Commit 2a39b2c

Browse files
authored
Fix UseSLF4JLoggerRule throws NPEs with Java 10+ var type (#382)
The PMD type resolution for inferred types using the `var` keyword is very simple (see [Java 10 Support](https://pmd.github.io/latest/pmd_release_notes_old.html#java-10-support). As a result it's usually not possible to get the type of such variables during analysis. So for now it's probably best to make sure not any NPEs occur whenever the type cannot be inferred. Since `var` can only be used with local variables, it's not likely loggers will be instantiated often this way. Fixes #379 Signed-off-by: Wouter Born <github@maindrain.net>
1 parent 5bb51f3 commit 2a39b2c

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

custom-checks/pmd/src/main/java/org/openhab/tools/analysis/pmd/UseSLF4JLoggerRule.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,17 @@ public Object visit(ASTImportDeclaration node, Object data) {
5757
@Override
5858
public Object visit(ASTVariableDeclarator node, Object data) {
5959
ASTType typeNode = node.getParent().getFirstChildOfType(ASTType.class);
60-
Node reftypeNode = typeNode.getChild(0);
61-
if (reftypeNode instanceof ASTReferenceType) {
62-
ASTClassOrInterfaceType classOrInterfaceType = reftypeNode
63-
.getFirstChildOfType(ASTClassOrInterfaceType.class);
64-
if (classOrInterfaceType != null) {
65-
String className = classOrInterfaceType.getImage();
60+
if (typeNode != null) {
61+
Node reftypeNode = typeNode.getChild(0);
62+
if (reftypeNode instanceof ASTReferenceType) {
63+
ASTClassOrInterfaceType classOrInterfaceType = reftypeNode
64+
.getFirstChildOfType(ASTClassOrInterfaceType.class);
65+
if (classOrInterfaceType != null) {
66+
String className = classOrInterfaceType.getImage();
6667

67-
if (isClassNameForbidden(className)) {
68-
addViolation(data, typeNode);
68+
if (isClassNameForbidden(className)) {
69+
addViolation(data, typeNode);
70+
}
6971
}
7072
}
7173
}

custom-checks/pmd/src/test/resources/org/openhab/tools/analysis/pmd/test/xml/UseSLF4JLoggerRule.xml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ class Foo{
6464
]]></code>
6565
</test-code>
6666
<test-code reinitializeRule="true" regressionTest="true">
67-
<description>Real smarthome class</description>
68-
<expected-problems>0</expected-problems>
69-
<code><![CDATA[
67+
<description>Real smarthome class</description>
68+
<expected-problems>0</expected-problems>
69+
<code><![CDATA[
7070
/**
7171
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation
7272
*
@@ -189,7 +189,20 @@ public class RawType implements PrimitiveType, State {
189189
}
190190
191191
}
192-
193-
]]></code>
192+
]]></code>
193+
</test-code>
194+
<test-code reinitializeRule="true" regressionTest="true">
195+
<description>Unresolved inferred var types</description>
196+
<expected-problems>0</expected-problems>
197+
<code><![CDATA[
198+
class Foo {
199+
Foo() {
200+
var oh = "openHAB";
201+
var obj1 = new Object();
202+
var obj2 = null;
203+
var i = 1;
204+
}
205+
}
206+
]]></code>
194207
</test-code>
195208
</test-data>

0 commit comments

Comments
 (0)