Skip to content

Commit

Permalink
Merge pull request #4231 from adangel:issue-4175-ImmutableField-depre…
Browse files Browse the repository at this point in the history
…cate-ignoredAnnotations

[java] ImmutableField - deprecate property ignoredAnnotations #4231
  • Loading branch information
adangel committed Nov 25, 2022
2 parents 93587f0 + a28705a commit 8b71fe6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
8 changes: 8 additions & 0 deletions docs/pages/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ This is a {{ site.pmd.release_type }} release.
either inherit from JUnit 3 TestCase or have at least one method annotated with the Test annotations from
JUnit4/5 or TestNG.

* The property `ignoredAnnotations` of rule {% rule java/design/ImmutableField %} has been deprecated and doesn't
have any effect anymore.
Since PMD 6.47.0, the rule only considers fields, that are initialized once and never changed. If the field is just
declared but never explicitly initialized, it won't be reported. That's the typical case when a framework sets
the field value by reflection. Therefore, the property is not needed anymore. If there is a special case where
this rule misidentifies fields as immutable, then the rule should be suppressed for these fields explicitly.

### Fixed Issues
* cli
* [#4215](https://github.com/pmd/pmd/discussions/4215): NullPointerException when trying to open designer
Expand All @@ -51,6 +58,7 @@ This is a {{ site.pmd.release_type }} release.
* [#2867](https://github.com/pmd/pmd/issues/2867): \[java] Separate pattern for test classes in ClassNamingConventions rule for Java
* [#4201](https://github.com/pmd/pmd/issues/4201): \[java] CommentDefaultAccessModifier should consider lombok's @<!-- -->Value
* java-design
* [#4175](https://github.com/pmd/pmd/issues/4175): \[java] ImmutableField - deprecate property `ignoredAnnotations`
* [#4177](https://github.com/pmd/pmd/issues/4177): \[java] New Rule InvalidJavaBean
* [#4188](https://github.com/pmd/pmd/issues/4188): \[java] ClassWithOnlyPrivateConstructorsShouldBeFinal false positive with Lombok's @<!-- -->NoArgsConstructor
* [#4189](https://github.com/pmd/pmd/issues/4189): \[java] AbstractClassWithoutAnyMethod should consider lombok's @<!-- -->AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ public abstract class AbstractIgnoredAnnotationRule extends AbstractJavaRule {

private final PropertyDescriptor<List<String>> ignoredAnnotationsDescriptor
= stringListProperty("ignoredAnnotations")
.desc("Fully qualified names of the annotation types that should be ignored by this rule")
.desc(defaultIgnoredAnnotationsDescription())
.defaultValue(defaultSuppressionAnnotations())
.build();

protected Collection<String> defaultSuppressionAnnotations() {
return Collections.emptyList();
}

protected String defaultIgnoredAnnotationsDescription() {
return "Fully qualified names of the annotation types that should be ignored by this rule";
}

protected AbstractIgnoredAnnotationRule() {
definePropertyDescriptor(ignoredAnnotationsDescriptor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package net.sourceforge.pmd.lang.java.rule.design;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand All @@ -22,7 +23,6 @@
import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer;
import net.sourceforge.pmd.lang.java.ast.ASTWhileStatement;
import net.sourceforge.pmd.lang.java.ast.AccessNode;
import net.sourceforge.pmd.lang.java.ast.Annotatable;
import net.sourceforge.pmd.lang.java.rule.AbstractLombokAwareRule;
import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
Expand All @@ -33,6 +33,17 @@
*/
public class ImmutableFieldRule extends AbstractLombokAwareRule {


@Override
protected String defaultIgnoredAnnotationsDescription() {
return "deprecated! " + super.defaultIgnoredAnnotationsDescription();
}

@Override
protected Collection<String> defaultSuppressionAnnotations() {
return Collections.emptyList();
}

@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
Object result = super.visit(node, data);
Expand All @@ -45,8 +56,7 @@ public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
AccessNode accessNodeParent = field.getAccessNodeParent();
if (accessNodeParent.isStatic() || !accessNodeParent.isPrivate() || accessNodeParent.isFinal()
|| accessNodeParent.isVolatile()
|| hasLombokAnnotation(node)
|| hasIgnoredAnnotation((Annotatable) accessNodeParent)) {
|| hasLombokAnnotation(node)) {
continue;
}

Expand Down
6 changes: 6 additions & 0 deletions pmd-java/src/main/resources/category/java/design.xml
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,12 @@ of the field or by a constructor. This helps in converting existing classes to b
Note that this rule does not enforce referenced object to be immutable itself. A class can still be mutable, even
if all its member fields are declared final. This is referred to as shallow immutability. For more information on
mutability, see Effective Java, 3rd Edition, Item 17: Minimize mutability.

Note: The property `ignoredAnnotations` is deprecated since PMD 6.52.0 and doesn't have any effect anymore.
Since PMD 6.47.0, the rule only considers fields, that are initialized once and never changed. If the field is just
declared but never explicitly initialized, it won't be reported. That's the typical case when a framework sets
the field value by reflection. Therefore, the property is not needed anymore. If there is a special case where
this rule misidentifies fields as immutable, then the rule should be suppressed for these fields explicitly.
</description>
<priority>3</priority>
<example>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ class Foo {
<code><![CDATA[
public class Foo {
@Deprecated
@SuppressWarnings("PMD.ImmutableField")
private int x;
public Foo() {
Expand Down

0 comments on commit 8b71fe6

Please sign in to comment.