[java] Property ignoredAnnotations does not work for SingularField and ImmutableField

Fixes #1056
This commit is contained in:
Andreas Dangel
2018-04-27 22:26:45 +02:00
parent 9ee838758c
commit 95f4ac62e8
7 changed files with 75 additions and 2 deletions

View File

@@ -137,6 +137,8 @@ from the comunity during the processm but if you have a legitimate use case for
* java-codestyle
* [#1003](https://github.com/pmd/pmd/issues/1003): \[java] UnnecessaryConstructor triggered on required empty constructor (Dagger @Inject)
* [#1023](https://github.com/pmd/pmd/issues/1023): \[java] False positive for useless parenthesis
* java-design
* [#1056](https://github.com/pmd/pmd/issues/1056): \[java] Property ignoredAnnotations does not work for SingularField and ImmutableField
* java-errorprone
* [#629](https://github.com/pmd/pmd/issues/629): \[java] NullAssignment false positive
* [#816](https://github.com/pmd/pmd/issues/816): \[java] SingleMethodSingleton false positives with inner classes

View File

@@ -23,6 +23,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTTryStatement;
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;
@@ -54,7 +55,8 @@ public class ImmutableFieldRule extends AbstractLombokAwareRule {
AccessNode accessNodeParent = field.getAccessNodeParent();
if (accessNodeParent.isStatic() || !accessNodeParent.isPrivate() || accessNodeParent.isFinal()
|| accessNodeParent.isVolatile()
|| hasClassLombokAnnotation()) {
|| hasClassLombokAnnotation()
|| hasIgnoredAnnotation((Annotatable) accessNodeParent)) {
continue;
}

View File

@@ -51,7 +51,7 @@ public class SingularFieldRule extends AbstractLombokAwareRule {
boolean checkInnerClasses = getProperty(CHECK_INNER_CLASSES);
boolean disallowNotAssignment = getProperty(DISALLOW_NOT_ASSIGNMENT);
if (node.isPrivate() && !node.isStatic() && !hasClassLombokAnnotation() && !hasLombokAnnotation(node)) {
if (node.isPrivate() && !node.isStatic() && !hasClassLombokAnnotation() && !hasIgnoredAnnotation(node)) {
for (ASTVariableDeclarator declarator : node.findChildrenOfType(ASTVariableDeclarator.class)) {
ASTVariableDeclaratorId declaration = (ASTVariableDeclaratorId) declarator.jjtGetChild(0);
List<NameOccurrence> usages = declaration.getUsages();

View File

@@ -1579,4 +1579,28 @@ class Foo {
}
]]></code>
</test-code>
<test-code>
<description>Verify property ignoredAnnotations is used</description>
<rule-property name="ignoredAnnotations">java.lang.Override</rule-property>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
@Override // well override doesn't really make sense, but it's good enough for the test case
private void foo() {}
}
]]></code>
</test-code>
<test-code>
<description>Verify property ignoredAnnotations is used - 2</description>
<rule-property name="ignoredAnnotations"></rule-property>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
@Deprecated
private void foo() {}
}
]]></code>
</test-code>
</test-data>

View File

@@ -216,4 +216,17 @@ public enum Foo {
}
]]></code>
</test-code>
<test-code>
<description>Verify ignoredAnnotations property is used</description>
<rule-property name="ignoredAnnotations">java.lang.Deprecated</rule-property>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
@Deprecated
public Foo() {
}
}
]]></code>
</test-code>
</test-data>

View File

@@ -466,4 +466,20 @@
}
]]></code>
</test-code>
<test-code>
<description>#1056 [java] Property ignoredAnnotations does not work for SingularField and ImmutableField</description>
<rule-property name="ignoredAnnotations">java.lang.Deprecated</rule-property>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
@Deprecated
private int x;
public Foo() {
x = 2;
}
}
]]></code>
</test-code>
</test-data>

View File

@@ -628,4 +628,20 @@ public enum Foo {
]]></code>
</test-code>
<test-code>
<description>#1056 [java] Property ignoredAnnotations does not work for SingularField and ImmutableField</description>
<rule-property name="ignoredAnnotations">java.lang.Deprecated</rule-property>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Source {
@Deprecated
private Object o; //violation!
Object m() {
o = new Object();
return o;
}
}
]]></code>
</test-code>
</test-data>