Skip to content

Commit

Permalink
Merge pull request pmd#4260 from adangel:issue-4166-UnusedPrivateFiel…
Browse files Browse the repository at this point in the history
…d-annotations

[java] UnusedPrivateField - add new property reportForAnnotations pmd#4260
adangel committed Dec 31, 2022
2 parents 0dcefd1 + bef6080 commit 67230ed
Showing 3 changed files with 73 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/pages/release_notes.md
Original file line number Diff line number Diff line change
@@ -14,11 +14,20 @@ This is a {{ site.pmd.release_type }} release.

### New and noteworthy

#### Modified rules

* The Java rule {% rule java/bestpractices/UnusedPrivateField %} has a new property `reportForAnnotations`.
This is a list of fully qualified names of the annotation types that should be reported anyway. If an unused field
has any of these annotations, then it is reported. If it has any other annotation, then it is still considered
to be used and is not reported.

### Fixed Issues
* core
* [#4248](https://github.com/pmd/pmd/issues/4248): \[core] Can't analyze sources in zip files
* apex-security
* [#4146](https://github.com/pmd/pmd/issues/4146): \[apex] ApexCRUDViolation: Recognize User Mode in SOQL + DML
* java-bestpractices
* [#4166](https://github.com/pmd/pmd/issues/4166): \[java] UnusedPrivateField doesn't find annotated unused private fields anymore
* java-multithreading
* [#4210](https://github.com/pmd/pmd/issues/4210): \[java] DoNotUseThreads report duplicate warnings

Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
import java.util.logging.Logger;

import net.sourceforge.pmd.PMDVersion;
import net.sourceforge.pmd.lang.java.ast.ASTAnnotation;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
@@ -28,6 +29,7 @@
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
import net.sourceforge.pmd.properties.PropertyDescriptor;
@@ -43,16 +45,26 @@ public class UnusedPrivateFieldRule extends AbstractJavaRule {
+ "This property has been deprecated since PMD 6.50.0 and will be completely ignored.")
.defaultValue(new ArrayList<String>())
.build();

private static boolean warnedAboutDeprecatedIgnoredAnnotationsProperty = false;
private static final PropertyDescriptor<List<String>> IGNORED_FIELD_NAMES =
PropertyFactory.stringListProperty("ignoredFieldNames")
.defaultValues("serialVersionUID", "serialPersistentFields")
.desc("Field Names that are ignored from the unused check")
.build();

private static final PropertyDescriptor<List<String>> REPORT_FOR_ANNOTATIONS_DESCRIPTOR
= stringListProperty("reportForAnnotations")
.desc("Fully qualified names of the annotation types that should be reported anyway. If an unused field "
+ "has any of these annotations, then it is reported. If it has any other annotation, then "
+ "it is still considered to used and is not reported.")
.defaultValue(new ArrayList<String>())
.build();

public UnusedPrivateFieldRule() {
definePropertyDescriptor(IGNORED_ANNOTATIONS_DESCRIPTOR);
definePropertyDescriptor(IGNORED_FIELD_NAMES);
definePropertyDescriptor(REPORT_FOR_ANNOTATIONS_DESCRIPTOR);
}

@Override
@@ -93,6 +105,14 @@ public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
}

private boolean hasAnyAnnotation(Annotatable node) {
List<ASTAnnotation> annotations = node.getDeclaredAnnotations();
for (String reportAnnotation : getProperty(REPORT_FOR_ANNOTATIONS_DESCRIPTOR)) {
for (ASTAnnotation annotation : annotations) {
if (TypeTestUtil.isA(reportAnnotation, annotation)) {
return false;
}
}
}
return !node.getDeclaredAnnotations().isEmpty();
}

Original file line number Diff line number Diff line change
@@ -767,6 +767,50 @@ public class C {
@ToString.Include
private int a; // Should not report a warning in this line
}
]]></code>
</test-code>

<test-code>
<description>[java] UnusedPrivateField doesn't find annotated unused private fields anymore #4166 (default)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.WebElement;
class ClassWithUnusedField {
@FindBy(id = "myId")
private WebElement myElement;
}
]]></code>
</test-code>

<test-code>
<description>[java] UnusedPrivateField doesn't find annotated unused private fields anymore #4166 (default, other annotation)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
class ClassWithUnusedField {
@Deprecated
private String unusedField;
}
]]></code>
</test-code>

<test-code>
<description>[java] UnusedPrivateField doesn't find annotated unused private fields anymore #4166 (configuration)</description>
<rule-property name="reportForAnnotations">java.lang.Deprecated|org.openqa.selenium.support.FindBy</rule-property>
<expected-problems>2</expected-problems>
<expected-linenumbers>6,9</expected-linenumbers>
<code><![CDATA[
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
class ClassWithUnusedField {
@Deprecated
private String unusedField;
@FindBy(id = "whatEverId")
private WebElement myElement;
}
]]></code>
</test-code>
</test-data>

0 comments on commit 67230ed

Please sign in to comment.