From 98b23d542043773a46f2b38ddf737472b2f1cf95 Mon Sep 17 00:00:00 2001 From: Robbie Martinus <12573669+rmartinus@users.noreply.github.com> Date: Wed, 2 Jan 2019 18:27:51 +1100 Subject: [PATCH 1/4] InvalidSlf4jMessageFormatRule should not throw NPE for enums Fixes gh-1549 --- .../InvalidSlf4jMessageFormatRule.java | 3 +- .../xml/InvalidSlf4jMessageFormat.xml | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) 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 f7cd1683e4..6534669cba 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 @@ -20,6 +20,7 @@ import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.java.ast.ASTArgumentList; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType; +import net.sourceforge.pmd.lang.java.ast.ASTEnumBody; import net.sourceforge.pmd.lang.java.ast.ASTExpression; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTInitializer; @@ -175,7 +176,7 @@ public class InvalidSlf4jMessageFormatRule extends AbstractJavaRule { if (count == 0) { // look if the message is defined in a field - final List fieldlist = node.getFirstParentOfType(ASTClassOrInterfaceBody.class) + final List fieldlist = node.getFirstParentOfAnyType(ASTClassOrInterfaceBody.class, ASTEnumBody.class) .findDescendantsOfType(ASTFieldDeclaration.class); // only look for ASTVariableDeclarator that are Fields final List fields = new ArrayList<>(fieldlist.size()); 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 994de283ab..05dd883ff4 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 @@ -335,4 +335,48 @@ public final class Main { } ]]> + + + NPE in enums (see #1549) + 0 + + + + + missing argument in enum + 1 + 11 + + \ No newline at end of file From d35e7398bac06d4a3aa89fb1477f3f86b2f38bf2 Mon Sep 17 00:00:00 2001 From: Robbie Martinus <12573669+rmartinus@users.noreply.github.com> Date: Wed, 2 Jan 2019 21:14:37 +1100 Subject: [PATCH 2/4] Moved logger to a field Fixes gh-1549 --- .../java/rule/errorprone/xml/InvalidSlf4jMessageFormat.xml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 05dd883ff4..bacf2019b5 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 @@ -362,7 +362,7 @@ public final class Main { missing argument in enum 1 - 11 + 12 Date: Thu, 3 Jan 2019 05:58:59 +1100 Subject: [PATCH 3/4] Moved logger message pattern to a field Fixes gh-1549 --- .../java/rule/errorprone/xml/InvalidSlf4jMessageFormat.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 bacf2019b5..486a1351fc 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 @@ -370,10 +370,10 @@ public final class Main { public enum LoggerHelper { INSTANCE; - private final Logger logger = LoggerFactory.getLogger(loggerName); + private static final String pattern = "log: {}"; public static void main(String[] args) { - final String pattern = "log: {}"; + final Logger logger = LoggerFactory.getLogger(LoggerHelper.class); logger.info(pattern, 1, 2); } From a58f46686a410d1051a27a8fb4ea6992ab807e9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Tue, 8 Jan 2019 20:42:18 -0300 Subject: [PATCH 4/4] Update changelog, refs #1551 --- docs/pages/release_notes.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index b6f74252f8..67b3a05736 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -48,8 +48,9 @@ This is a {{ site.pmd.release_type }} release. * [#1517](https://github.com/pmd/pmd/issues/1517): \[java] New Rule: UseDiamondOperator * java-errorprone * [#1035](https://github.com/pmd/pmd/issues/1035): \[java] ReturnFromFinallyBlock: False positive on lambda expression in finally block + * [#1549](https://github.com/pmd/pmd/issues/1549): \[java] NPE in PMD 6.8.0 InvalidSlf4jMessageFormat * plsql - * [#1508](https://github.com/pmd/pmd/issues/1508): \[plsql] Parse Exception when using SELECT COUNT(*) + * [#1508](https://github.com/pmd/pmd/issues/1508): \[plsql] Parse Exception when using SELECT COUNT(\*) * [#1509](https://github.com/pmd/pmd/issues/1509): \[plsql] Parse Exception with OUTER/INNER Joins * [#1511](https://github.com/pmd/pmd/issues/1511): \[plsql] Parse Exception with IS NOT NULL @@ -64,6 +65,7 @@ This is a {{ site.pmd.release_type }} release. * [#1521](https://github.com/pmd/pmd/pull/1521): \[java] Upgrade to ASM7 for JDK 11 support - [Mark Pritchard](https://github.com/markpritchard) * [#1530](https://github.com/pmd/pmd/pull/1530): \[java] New rule: AvoidReassigningLoopVariables - [Kris Scheibe](https://github.com/kris-scheibe) * [#1534](https://github.com/pmd/pmd/pull/1534): \[java] This is the change regarding the usediamondoperator #1517 - [hemanshu070](https://github.com/hemanshu070) +* [#1551](https://github.com/pmd/pmd/pull/1551): \[java] InvalidSlf4jMessageFormatRule should not throw NPE for enums - [Robbie Martinus](https://github.com/rmartinus) {% endtocmaker %}