From 8f1791c5afad498b0619631e743972ecba0b8dca Mon Sep 17 00:00:00 2001 From: Kristian Scheibe Date: Wed, 5 Dec 2018 09:33:29 +0100 Subject: [PATCH 1/2] Fix InvalidSlf4jMessageFormatRule fails with a NPE if a logger call with a variable as parameter is not inside a method or constructor. Added test-cases for (static) initializer block and lambda expression block, which also can contain local variable declarations. --- .../InvalidSlf4jMessageFormatRule.java | 14 ++-- .../xml/InvalidSlf4jMessageFormat.xml | 66 +++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidSlf4jMessageFormatRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidSlf4jMessageFormatRule.java index 0a5e1402c4..f7cd1683e4 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidSlf4jMessageFormatRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidSlf4jMessageFormatRule.java @@ -22,6 +22,8 @@ import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType; import net.sourceforge.pmd.lang.java.ast.ASTExpression; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; +import net.sourceforge.pmd.lang.java.ast.ASTInitializer; +import net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression; import net.sourceforge.pmd.lang.java.ast.ASTLiteral; import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTName; @@ -31,6 +33,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix; import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator; import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId; import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer; +import net.sourceforge.pmd.lang.java.ast.JavaNode; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration; import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper; @@ -163,11 +166,12 @@ public class InvalidSlf4jMessageFormatRule extends AbstractJavaRule { count = countPlaceholders(node); } else if (node.getFirstDescendantOfType(ASTName.class) != null) { final String variableName = node.getFirstDescendantOfType(ASTName.class).getImage(); - // look if the message is defined locally - final List localVariables = node - .getFirstParentOfType(ASTMethodOrConstructorDeclaration.class) - .findDescendantsOfType(ASTVariableDeclarator.class); - count = getAmountOfExpectedArguments(variableName, localVariables); + // look if the message is defined locally in a method/constructor, initializer block or lambda expression + final JavaNode parentBlock = node.getFirstParentOfAnyType(ASTMethodOrConstructorDeclaration.class, ASTInitializer.class, ASTLambdaExpression.class); + if (parentBlock != null) { + final List localVariables = parentBlock.findDescendantsOfType(ASTVariableDeclarator.class); + count = getAmountOfExpectedArguments(variableName, localVariables); + } if (count == 0) { // look if the message is defined in a field diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidSlf4jMessageFormat.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidSlf4jMessageFormat.xml index 0cbc4296a5..9df5189f19 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidSlf4jMessageFormat.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidSlf4jMessageFormat.xml @@ -269,4 +269,70 @@ public final class Main { } ]]> + + + NPE in static block + 0 + + + + + missing argument in static block + 1 + 8 + + + + + NPE in lambda call + 0 + list = someMethod(message -> logger.info(message)); + } + ]]> + + + + missing argument in lambda call + 1 + 9 + list = someMethod(message -> { + final String pattern = "log: {}"; + + logger.info(pattern, 1, 2); + }); + } + ]]> + \ No newline at end of file From c77345a8867f79eda486bedef60a578534d7bf4c Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 7 Dec 2018 19:40:34 +0100 Subject: [PATCH 2/2] Update release notes, fixes #1512, refs #1504 --- docs/pages/release_notes.md | 3 +++ .../java/rule/errorprone/xml/InvalidSlf4jMessageFormat.xml | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index a6d3e00d55..eb848a61a1 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -50,6 +50,8 @@ This means, you can use CPD to find duplicated code in your Kotlin projects. * java-design * [#1151](https://github.com/pmd/pmd/issues/1151): \[java] ImmutableField false positive with multiple constructors * [#1483](https://github.com/pmd/pmd/issues/1483): \[java] Cyclo metric should count conditions of for statements correctly +* java-errorprone + * [#1512](https://github.com/pmd/pmd/issues/1512): \[java] InvalidSlf4jMessageFormatRule causes NPE in lambda and static blocks * plsql * [#1454](https://github.com/pmd/pmd/issues/1454): \[plsql] ParseException for IF/CASE statement with >=, <=, != @@ -246,6 +248,7 @@ removal: * [#1464](https://github.com/pmd/pmd/pull/1464): \[doc] Fix XSS on documentation web page - [Maxime Robert](https://github.com/marob) * [#1469](https://github.com/pmd/pmd/pull/1469): \[core] Configurable max loops in DAAPathFinder - [Alberto Fernández](https://github.com/albfernandez) * [#1494](https://github.com/pmd/pmd/pull/1494): \[java] 1151: Rephrase ImmutableField documentation in design.xml - [Robbie Martinus](https://github.com/rmartinus) +* [#1504](https://github.com/pmd/pmd/pull/1504): \[java] NPE in InvalidSlf4jMessageFormatRule if a logger call with a variable as parameter is not inside a method or constructor - [kris-scheibe](https://github.com/kris-scheibe) {% endtocmaker %} diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidSlf4jMessageFormat.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidSlf4jMessageFormat.xml index 9df5189f19..994de283ab 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidSlf4jMessageFormat.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidSlf4jMessageFormat.xml @@ -271,7 +271,7 @@ public final class Main { - NPE in static block + NPE in static block (see #1512) 0 - NPE in lambda call + NPE in lambda call (see #1512) 0