From a1ab0d6657ea4636f126de4052fbc1657e0cb905 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 5 Sep 2015 19:53:05 +0200 Subject: [PATCH] #1399 False positive for VariableNamingConventions with annotation @interface --- .../java/rule/naming/VariableNamingConventionsRule.java | 8 ++++++-- .../java/rule/naming/xml/VariableNamingConventions.xml | 9 +++++++++ src/site/markdown/overview/changelog.md | 1 + 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/naming/VariableNamingConventionsRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/naming/VariableNamingConventionsRule.java index cfab2b9444..d80e97b9e9 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/naming/VariableNamingConventionsRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/naming/VariableNamingConventionsRule.java @@ -5,6 +5,7 @@ package net.sourceforge.pmd.lang.java.rule.naming; import net.sourceforge.pmd.PropertyDescriptor; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; @@ -111,9 +112,12 @@ public class VariableNamingConventionsRule extends AbstractJavaRule { } boolean isStatic = node.isStatic(); boolean isFinal = node.isFinal(); + + Node type = node.jjtGetParent().jjtGetParent().jjtGetParent(); // Anything from an interface is necessarily static and final - if (node.jjtGetParent().jjtGetParent().jjtGetParent() instanceof ASTClassOrInterfaceDeclaration - && ((ASTClassOrInterfaceDeclaration) node.jjtGetParent().jjtGetParent().jjtGetParent()).isInterface()) { + // Anything inside an annotation type is also static and final + if (type instanceof ASTClassOrInterfaceDeclaration && ((ASTClassOrInterfaceDeclaration) type).isInterface() + || type instanceof ASTAnnotationTypeDeclaration) { isStatic = true; isFinal = true; } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/naming/xml/VariableNamingConventions.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/naming/xml/VariableNamingConventions.xml index 96733c3741..e0b65b2d33 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/naming/xml/VariableNamingConventions.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/naming/xml/VariableNamingConventions.xml @@ -355,6 +355,15 @@ public class X { public class X { private final String _projection; private final String _projectionID; +} + ]]> + + + #1399 False positive for VariableNamingConventions with annotation @interface + 0 + diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 44d4857567..f07bf6603e 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -15,6 +15,7 @@ * [#1370](https://sourceforge.net/p/pmd/bugs/1370/): ConsecutiveAppendsShouldReuse not detected properly on StringBuffer * [#1371](https://sourceforge.net/p/pmd/bugs/1371/): InsufficientStringBufferDeclaration not detected properly on StringBuffer * [#1384](https://sourceforge.net/p/pmd/bugs/1384/): NullPointerException in ConsecutiveLiteralAppendsRule +* [#1399](https://sourceforge.net/p/pmd/bugs/1399/): False positive for VariableNamingConventions with annotation @interface * [#1400](https://sourceforge.net/p/pmd/bugs/1400/): False positive with JUnit4TestShouldUseBeforeAnnotation * [#1401](https://sourceforge.net/p/pmd/bugs/1401/): False positive for StringBuilder.append called with constructor * [#1402](https://sourceforge.net/p/pmd/bugs/1402/): Windows-Only: File exclusions are not case insensitive