[java] Treat annotation members as public static final

- This fixes #215
 - I created a new method so we didn't change public APIs
This commit is contained in:
Juan Martín Sotuyo Dodero
2017-01-30 12:59:07 -03:00
committed by Andreas Dangel
parent 4b6196a8f5
commit 630b92b64e
3 changed files with 32 additions and 4 deletions

View File

@ -53,7 +53,7 @@ public class ASTFieldDeclaration extends AbstractJavaAccessTypeNode implements D
@Override
public boolean isFinal() {
if (isInterfaceMember()) {
if (isAnnotationMember() || isInterfaceMember()) {
return true;
}
return super.isFinal();
@ -61,7 +61,7 @@ public class ASTFieldDeclaration extends AbstractJavaAccessTypeNode implements D
@Override
public boolean isPrivate() {
if (isInterfaceMember()) {
if (isAnnotationMember() || isInterfaceMember()) {
return false;
}
return super.isPrivate();
@ -69,7 +69,7 @@ public class ASTFieldDeclaration extends AbstractJavaAccessTypeNode implements D
@Override
public boolean isPackagePrivate() {
if (isInterfaceMember()) {
if (isAnnotationMember() || isInterfaceMember()) {
return false;
}
return super.isPackagePrivate();
@ -77,12 +77,19 @@ public class ASTFieldDeclaration extends AbstractJavaAccessTypeNode implements D
@Override
public boolean isProtected() {
if (isInterfaceMember()) {
if (isAnnotationMember() || isInterfaceMember()) {
return false;
}
return super.isProtected();
}
public boolean isAnnotationMember() {
if (jjtGetParent().jjtGetParent() instanceof ASTAnnotationTypeBody) {
return true;
}
return false;
}
public boolean isInterfaceMember() {
if (jjtGetParent().jjtGetParent() instanceof ASTEnumBody) {
return false;

View File

@ -51,6 +51,15 @@ public class ASTFieldDeclarationTest extends ParserTst {
" String[] foo;" + PMD.EOL +
"}";
@Test
public void testWithAnnotation() {
ASTCompilationUnit cu = parseJava15(TEST5);
ASTFieldDeclaration node = cu.findDescendantsOfType(ASTFieldDeclaration.class).get(0);
assertFalse(node.isInterfaceMember());
assertTrue(node.isAnnotationMember());
}
private static final String TEST2 =
"class Foo {" + PMD.EOL +
" String[][][] foo;" + PMD.EOL +
@ -67,6 +76,8 @@ public class ASTFieldDeclarationTest extends ParserTst {
" private int x;" + PMD.EOL +
"}";
private static final String TEST5 = "public @interface Foo {" + PMD.EOL + " int BAR = 6;" + PMD.EOL + "}";
@Test
public void testGetVariableName() {
int id = 0;

View File

@ -1309,6 +1309,16 @@ public class SomeClass {
<code><![CDATA[
public class SomeClass {
private float someNumber = 0.1f;
}
]]></code>
</test-code>
<test-code>
<description>#215 - False positive for annotation fields</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public @interface SomeAnnotation {
int CONSTANT = 0;
}
]]></code>
</test-code>