From f0830eb973aef7ddbb496bffb426305dbe10ef00 Mon Sep 17 00:00:00 2001 From: Phokham Nonava Date: Fri, 1 Feb 2019 11:02:42 +0100 Subject: [PATCH 1/2] Update instead of override classHasLombokAnnotation flag Fixes issues where inner classes would override this flag. --- .../java/rule/AbstractLombokAwareRule.java | 12 ++++-- .../java/rule/design/ImmutableFieldRule.java | 2 +- .../java/rule/design/xml/SingularField.xml | 41 +++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractLombokAwareRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractLombokAwareRule.java index 710f374aee..e0f8fd5345 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractLombokAwareRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractLombokAwareRule.java @@ -65,21 +65,27 @@ public class AbstractLombokAwareRule extends AbstractIgnoredAnnotationRule { @Override public Object visit(ASTClassOrInterfaceDeclaration node, Object data) { + boolean oldValue = classHasLombokAnnotation; classHasLombokAnnotation = hasLombokAnnotation(node); - return super.visit(node, data); + Object result = super.visit(node, data); + classHasLombokAnnotation = oldValue; + return result; } @Override public Object visit(ASTEnumDeclaration node, Object data) { + boolean oldValue = classHasLombokAnnotation; classHasLombokAnnotation = hasLombokAnnotation(node); - return super.visit(node, data); + Object result = super.visit(node, data); + classHasLombokAnnotation = oldValue; + return result; } /** * Returns whether there have been class level Lombok annotations found. * Note: this can only be queried after the class declaration node has been * processed. - * + * * @return true if a lombok annotation at the class level has * been found */ diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ImmutableFieldRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ImmutableFieldRule.java index d409c66d90..a4385b640f 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ImmutableFieldRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ImmutableFieldRule.java @@ -55,7 +55,7 @@ public class ImmutableFieldRule extends AbstractLombokAwareRule { AccessNode accessNodeParent = field.getAccessNodeParent(); if (accessNodeParent.isStatic() || !accessNodeParent.isPrivate() || accessNodeParent.isFinal() || accessNodeParent.isVolatile() - || hasClassLombokAnnotation() + || hasLombokAnnotation(node) || hasIgnoredAnnotation((Annotatable) accessNodeParent)) { continue; } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/SingularField.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/SingularField.xml index 5b9ff1086a..4caef8662a 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/SingularField.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/SingularField.xml @@ -573,6 +573,47 @@ public class MyClass { ]]> + + [java] SingularField: Lombok false positive with inner class and annotated outer class + 0 + + + + + [java] SingularField: Lombok false positive with annotated inner class + 1 + + #177 [java] SingularField with lambdas as final fields From 6d17dc2e45f047d88df30b679f5dfae43a6ba776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Fri, 1 Feb 2019 19:25:35 -0300 Subject: [PATCH 2/2] Update changelog, refs #1640 --- docs/pages/release_notes.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index bbe45d6b90..a9d8201b0a 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -21,6 +21,8 @@ This is a {{ site.pmd.release_type }} release. * java-codestyle * [#1543](https://github.com/pmd/pmd/issues/1543): \[java] LinguisticNaming should ignore overriden methods * [#1547](https://github.com/pmd/pmd/issues/1547): \[java] AtLeastOneConstructorRule: false-positive with lombok.AllArgsConstructor +* java-design + * [#1641](https://github.com/pmd/pmd/issues/1641): \[java] False-positive with Lombok and inner classes * java-errorprone * [#780](https://github.com/pmd/pmd/issues/780): \[java] BeanMembersShouldSerializeRule does not recognize lombok accessors * java-multithreading @@ -35,6 +37,7 @@ This is a {{ site.pmd.release_type }} release. * [#1628](https://github.com/pmd/pmd/pull/1628): \[java] LinguisticNaming should ignore overriden methods - [Shubham](https://github.com/Shubham-2k17) * [#1634](https://github.com/pmd/pmd/pull/1634): \[java] BeanMembersShouldSerializeRule does not recognize lombok accessors - [Shubham](https://github.com/Shubham-2k17) * [#1635](https://github.com/pmd/pmd/pull/1635): \[java] UnsynchronizedStaticFormatter reports commons lang FastDateFormat - [Shubham](https://github.com/Shubham-2k17) +* [#1640](https://github.com/pmd/pmd/pull/1640): \[java] Update instead of override classHasLombokAnnotation flag - [Phokham Nonava](https://github.com/fluxroot) {% endtocmaker %}