From cd98a36b66548aca5c5867283c99d1f39211643a Mon Sep 17 00:00:00 2001 From: Ollie Abbey Date: Wed, 26 Aug 2020 16:52:13 +0100 Subject: [PATCH 1/6] [java] Fix 2708: False positive FinalFieldCouldBeStatic --- .../main/resources/category/java/design.xml | 5 ++-- .../design/xml/FinalFieldCouldBeStatic.xml | 25 ++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/pmd-java/src/main/resources/category/java/design.xml b/pmd-java/src/main/resources/category/java/design.xml index 0a5c0485d1..7f71b9356c 100644 --- a/pmd-java/src/main/resources/category/java/design.xml +++ b/pmd-java/src/main/resources/category/java/design.xml @@ -740,8 +740,9 @@ in each object at runtime. diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml index ea8200e411..ac7d3db35b 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml @@ -1,8 +1,8 @@ + xmlns="http://pmd.sourceforge.net/rule-tests" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd"> simple failure case @@ -98,6 +98,25 @@ interface Test { + + + + #2708 - False positive with @Builder.Default fields + 0 + From 363c3a30ff59b5d7f16e6e28ba90ed8ffc0f92bc Mon Sep 17 00:00:00 2001 From: Ollie Abbey Date: Thu, 27 Aug 2020 08:45:53 +0100 Subject: [PATCH 2/6] Trigger for non lombok Builder.Default --- .../main/resources/category/java/design.xml | 3 ++- .../design/xml/FinalFieldCouldBeStatic.xml | 21 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pmd-java/src/main/resources/category/java/design.xml b/pmd-java/src/main/resources/category/java/design.xml index 7f71b9356c..e1a944b912 100644 --- a/pmd-java/src/main/resources/category/java/design.xml +++ b/pmd-java/src/main/resources/category/java/design.xml @@ -742,7 +742,8 @@ in each object at runtime. [@Final= true() and @Static= false()] [VariableDeclarator/VariableInitializer/Expression /PrimaryExpression[not(PrimarySuffix)]/PrimaryPrefix/Literal] -[not(preceding-sibling::Annotation/MarkerAnnotation/Name[@Image="Builder.Default"])] + [not(preceding-sibling::Annotation/MarkerAnnotation/Name[@Image="Builder.Default"] + and //ImportDeclaration/Name[@Image="lombok.Builder"])] ]]> diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml index ac7d3db35b..7df1479d54 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml @@ -103,7 +103,7 @@ public @interface MetricType { - #2708 - False positive with @Builder.Default fields + #2708 - False positive with lombok @Builder.Default fields 0 + + + + #2708 - Should trigger with non-lombok @Builder.Default fields + 1 + Date: Tue, 1 Sep 2020 08:55:52 +0100 Subject: [PATCH 3/6] Addressed review comments --- pmd-java/src/main/resources/category/java/design.xml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pmd-java/src/main/resources/category/java/design.xml b/pmd-java/src/main/resources/category/java/design.xml index bba97ab2b2..5d7dfcb081 100644 --- a/pmd-java/src/main/resources/category/java/design.xml +++ b/pmd-java/src/main/resources/category/java/design.xml @@ -740,10 +740,11 @@ in each object at runtime. From 85754378f72f069510c39179789086799443b95f Mon Sep 17 00:00:00 2001 From: Ollie Abbey Date: Tue, 1 Sep 2020 09:02:48 +0100 Subject: [PATCH 4/6] Added test for multiple VariableDecoratorIds --- .../rule/design/xml/FinalFieldCouldBeStatic.xml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml index 7df1479d54..c7f8c74dbf 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml @@ -136,6 +136,19 @@ public class ExampleClass { @Builder.Default private final long exampleField = 0L; +} + ]]> + + + + Should trigger multiple times for fields declared on one line + 2 + From b3e0c54a80af931ef4369ac01baa261da7b15b28 Mon Sep 17 00:00:00 2001 From: Ollie Abbey Date: Tue, 1 Sep 2020 09:03:28 +0100 Subject: [PATCH 5/6] Fixed a typo --- .../pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml index c7f8c74dbf..ebd108cc17 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml @@ -148,7 +148,7 @@ package com.example; public class ExampleClass { - private final String one = "one", two = "two; + private final String one = "one", two = "two"; } ]]> From 3ce68f49779ab5185e85e34cf732c353a1d71125 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Tue, 1 Sep 2020 14:18:07 +0200 Subject: [PATCH 6/6] dogfood - update PMD to 6.27.0 --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 9949d0cd95..186208bfcb 100644 --- a/pom.xml +++ b/pom.xml @@ -405,12 +405,12 @@ net.sourceforge.pmd pmd-core - 6.26.0 + 6.27.0 net.sourceforge.pmd pmd-java - 6.26.0 + 6.27.0