[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:

committed by
Andreas Dangel

parent
4b6196a8f5
commit
630b92b64e
@ -53,7 +53,7 @@ public class ASTFieldDeclaration extends AbstractJavaAccessTypeNode implements D
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isFinal() {
|
public boolean isFinal() {
|
||||||
if (isInterfaceMember()) {
|
if (isAnnotationMember() || isInterfaceMember()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return super.isFinal();
|
return super.isFinal();
|
||||||
@ -61,7 +61,7 @@ public class ASTFieldDeclaration extends AbstractJavaAccessTypeNode implements D
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isPrivate() {
|
public boolean isPrivate() {
|
||||||
if (isInterfaceMember()) {
|
if (isAnnotationMember() || isInterfaceMember()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return super.isPrivate();
|
return super.isPrivate();
|
||||||
@ -69,7 +69,7 @@ public class ASTFieldDeclaration extends AbstractJavaAccessTypeNode implements D
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isPackagePrivate() {
|
public boolean isPackagePrivate() {
|
||||||
if (isInterfaceMember()) {
|
if (isAnnotationMember() || isInterfaceMember()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return super.isPackagePrivate();
|
return super.isPackagePrivate();
|
||||||
@ -77,12 +77,19 @@ public class ASTFieldDeclaration extends AbstractJavaAccessTypeNode implements D
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isProtected() {
|
public boolean isProtected() {
|
||||||
if (isInterfaceMember()) {
|
if (isAnnotationMember() || isInterfaceMember()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return super.isProtected();
|
return super.isProtected();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isAnnotationMember() {
|
||||||
|
if (jjtGetParent().jjtGetParent() instanceof ASTAnnotationTypeBody) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isInterfaceMember() {
|
public boolean isInterfaceMember() {
|
||||||
if (jjtGetParent().jjtGetParent() instanceof ASTEnumBody) {
|
if (jjtGetParent().jjtGetParent() instanceof ASTEnumBody) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -51,6 +51,15 @@ public class ASTFieldDeclarationTest extends ParserTst {
|
|||||||
" String[] foo;" + PMD.EOL +
|
" 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 =
|
private static final String TEST2 =
|
||||||
"class Foo {" + PMD.EOL +
|
"class Foo {" + PMD.EOL +
|
||||||
" String[][][] foo;" + PMD.EOL +
|
" String[][][] foo;" + PMD.EOL +
|
||||||
@ -67,6 +76,8 @@ public class ASTFieldDeclarationTest extends ParserTst {
|
|||||||
" private int x;" + PMD.EOL +
|
" private int x;" + PMD.EOL +
|
||||||
"}";
|
"}";
|
||||||
|
|
||||||
|
private static final String TEST5 = "public @interface Foo {" + PMD.EOL + " int BAR = 6;" + PMD.EOL + "}";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetVariableName() {
|
public void testGetVariableName() {
|
||||||
int id = 0;
|
int id = 0;
|
||||||
|
@ -1309,6 +1309,16 @@ public class SomeClass {
|
|||||||
<code><![CDATA[
|
<code><![CDATA[
|
||||||
public class SomeClass {
|
public class SomeClass {
|
||||||
private float someNumber = 0.1f;
|
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>
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
Reference in New Issue
Block a user