Merge pull request #4542 from LynnBroe:issue4510

[java] Fix #4510: A false positive about ConstructorCallsOverridableMethod and @Value #4542
This commit is contained in:
Andreas Dangel
2023-05-28 12:10:53 +02:00
3 changed files with 29 additions and 1 deletions

View File

@ -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
* java-performance
* [#4458](https://github.com/pmd/pmd/issues/4458): \[java] RedundantFieldInitializer: false positive with lombok's @<!-- -->Value
@ -101,6 +102,7 @@ See [the example report]({{ baseurl }}report-examples/cpdhtml-v2.html).
* [#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)
* [#4541](https://github.com/pmd/pmd/pull/4541): \[java] Fix #4458: A false positive about RedundantFieldInitializer and @<!-- -->Value - [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
@ -591,6 +593,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
@ -648,6 +651,7 @@ Language specific fixes:
* [#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)
* [#4541](https://github.com/pmd/pmd/pull/4541): \[java] Fix #4458: A false positive about RedundantFieldInitializer and @<!-- -->Value - [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

View File

@ -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<JMethodSymbol> EMPTY_STACK = new LinkedList<>();
private static final Set<String> 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)) {

View File

@ -614,6 +614,21 @@ public class MyTimestamp {
return t;
}
}
]]></code>
</test-code>
<test-code>
<description>[java] ConstructorCallsOverridableMethod: false positive with lombok's @Value #4510</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import lombok.Value;
@Value
class Foo1 {
public Foo1(String bar) {
bar(); // false positive warning
}
public void bar() {}
}
]]></code>
</test-code>
</test-data>