From 5d15889fb51a12bf01885a2d0ac32a47f4549b67 Mon Sep 17 00:00:00 2001 From: LynnBroe <109954313+LynnBroe@users.noreply.github.com> Date: Wed, 3 May 2023 19:39:13 +0800 Subject: [PATCH 1/2] Fix issue4510 --- .../ConstructorCallsOverridableMethodRule.java | 11 ++++++++++- .../xml/ConstructorCallsOverridableMethod.xml | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/ConstructorCallsOverridableMethodRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/ConstructorCallsOverridableMethodRule.java index 771fc6d5ab..4ef336e6dc 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/ConstructorCallsOverridableMethodRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/ConstructorCallsOverridableMethodRule.java @@ -4,11 +4,14 @@ package net.sourceforge.pmd.lang.java.rule.errorprone; +import static net.sourceforge.pmd.util.CollectionUtil.setOf; + import java.lang.reflect.Modifier; import java.util.Deque; import java.util.HashMap; import java.util.LinkedList; import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; import org.checkerframework.checker.nullness.qual.NonNull; @@ -29,6 +32,7 @@ import net.sourceforge.pmd.lang.java.symbols.JMethodSymbol; import net.sourceforge.pmd.lang.java.types.JMethodSig; import net.sourceforge.pmd.lang.java.types.OverloadSelectionResult; + /** * Searches through all methods and constructors called from constructors. It * marks as dangerous any call to overridable methods from non-private @@ -54,6 +58,11 @@ public final class ConstructorCallsOverridableMethodRule extends AbstractJavaRul private static final Deque EMPTY_STACK = new LinkedList<>(); + private static final Set MAKE_FIELD_FINAL_CLASS_ANNOT = + setOf( + "lombok.Value" + ); + public ConstructorCallsOverridableMethodRule() { super(ASTConstructorDeclaration.class); } @@ -66,7 +75,7 @@ public final class ConstructorCallsOverridableMethodRule extends AbstractJavaRul @Override public Object visit(ASTConstructorDeclaration node, Object data) { - if (node.getEnclosingType().isFinal()) { + if (node.getEnclosingType().isFinal() || JavaAstUtils.hasAnyAnnotation(node.getEnclosingType(), MAKE_FIELD_FINAL_CLASS_ANNOT)) { return null; // then cannot be overridden } for (ASTMethodCall call : node.getBody().descendants(ASTMethodCall.class)) { diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/ConstructorCallsOverridableMethod.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/ConstructorCallsOverridableMethod.xml index feb2c14f31..dc277ca1be 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/ConstructorCallsOverridableMethod.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/ConstructorCallsOverridableMethod.xml @@ -614,6 +614,21 @@ public class MyTimestamp { return t; } } +]]> + + + + [java]A false positive about ConstructorCallsOverridableMethod and @Value #4510 + 0 + From 0f1453cb6d0dc1d116cf1129d54c5a0ad680b9c1 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sun, 28 May 2023 12:08:53 +0200 Subject: [PATCH 2/2] [doc] Update release notes (#4510, #4542) --- docs/pages/release_notes.md | 4 ++++ .../rule/errorprone/xml/ConstructorCallsOverridableMethod.xml | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 48783b1fc0..4fc5f7a5ab 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -66,6 +66,7 @@ See [the example report]({{ baseurl }}report-examples/cpdhtml-v2.html). * java-errorprone * [#4063](https://github.com/pmd/pmd/issues/4063): \[java] AvoidBranchingStatementAsLastInLoop: False-negative about try/finally block * [#4457](https://github.com/pmd/pmd/issues/4457): \[java] OverrideBothEqualsAndHashcode: false negative with anonymous classes + * [#4510](https://github.com/pmd/pmd/issues/4510): \[java] ConstructorCallsOverridableMethod: false positive with lombok's @Value * [#4546](https://github.com/pmd/pmd/issues/4546): \[java] OverrideBothEqualsAndHashCode ignores records #### API Changes @@ -98,6 +99,7 @@ See [the example report]({{ baseurl }}report-examples/cpdhtml-v2.html). * [#4537](https://github.com/pmd/pmd/pull/4537): \[java] Fix #4455: A false positive about FieldNamingConventions and UtilityClass - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) * [#4538](https://github.com/pmd/pmd/pull/4538): \[java] Fix #4456: A false positive about FinalFieldCouldBeStatic and UtilityClass - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) * [#4540](https://github.com/pmd/pmd/pull/4540): \[java] Fix #4457: false negative about OverrideBothEqualsAndHashcode - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) +* [#4542](https://github.com/pmd/pmd/pull/4542): \[java] Fix #4510: A false positive about ConstructorCallsOverridableMethod and @Value - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) ### 🚀 Major Features and Enhancements @@ -588,6 +590,7 @@ Language specific fixes: * [#4457](https://github.com/pmd/pmd/issues/4457): \[java] OverrideBothEqualsAndHashcode: false negative with anonymous classes * [#4493](https://github.com/pmd/pmd/issues/4493): \[java] MissingStaticMethodInNonInstantiatableClass: false-positive about @Inject * [#4505](https://github.com/pmd/pmd/issues/4505): \[java] ImplicitSwitchFallThrough NPE in PMD 7.0.0-rc1 + * [#4510](https://github.com/pmd/pmd/issues/4510): \[java] ConstructorCallsOverridableMethod: false positive with lombok's @Value * [#4513](https://github.com/pmd/pmd/issues/4513): \[java] UselessOperationOnImmutable various false negatives with String * [#4514](https://github.com/pmd/pmd/issues/4514): \[java] AvoidLiteralsInIfCondition false positive and negative for String literals when ignoreExpressions=true * [#4546](https://github.com/pmd/pmd/issues/4546): \[java] OverrideBothEqualsAndHashCode ignores records @@ -643,6 +646,7 @@ Language specific fixes: * [#4537](https://github.com/pmd/pmd/pull/4537): \[java] Fix #4455: A false positive about FieldNamingConventions and UtilityClass - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) * [#4538](https://github.com/pmd/pmd/pull/4538): \[java] Fix #4456: A false positive about FinalFieldCouldBeStatic and UtilityClass - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) * [#4540](https://github.com/pmd/pmd/pull/4540): \[java] Fix #4457: false negative about OverrideBothEqualsAndHashcode - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) +* [#4542](https://github.com/pmd/pmd/pull/4542): \[java] Fix #4510: A false positive about ConstructorCallsOverridableMethod and @Value - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) ### 📈 Stats * 4557 commits diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/ConstructorCallsOverridableMethod.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/ConstructorCallsOverridableMethod.xml index dc277ca1be..5581a7b041 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/ConstructorCallsOverridableMethod.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/ConstructorCallsOverridableMethod.xml @@ -618,14 +618,14 @@ public class MyTimestamp { - [java]A false positive about ConstructorCallsOverridableMethod and @Value #4510 + [java] ConstructorCallsOverridableMethod: false positive with lombok's @Value #4510 0