diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/FieldDeclarationsShouldBeAtStartOfClassRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/FieldDeclarationsShouldBeAtStartOfClassRule.java index 3f5fdfcf8a..b6fa242e28 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/FieldDeclarationsShouldBeAtStartOfClassRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/FieldDeclarationsShouldBeAtStartOfClassRule.java @@ -51,11 +51,7 @@ public class FieldDeclarationsShouldBeAtStartOfClassRule extends AbstractJavaRul for (int i = 0; i < parent.jjtGetNumChildren(); i++) { Node child = parent.jjtGetChild(i); if (child.jjtGetNumChildren() > 0) { - if (!(child.jjtGetChild(0) instanceof ASTAnnotation) || child.jjtGetNumChildren() == 1) { - child = child.jjtGetChild(0); - } else { - child = child.jjtGetChild(1); - } + child = skipAnnotations(child); } if (child.equals(node)) { break; @@ -80,4 +76,21 @@ public class FieldDeclarationsShouldBeAtStartOfClassRule extends AbstractJavaRul } return data; } + + /** + * Ignore all annotations, until anything, that is not an annotation and + * return this node + * @param child the node from where to start the search + * @return the first child or the first child after annotations + */ + private Node skipAnnotations(Node child) { + Node nextChild = child.jjtGetChild(0); + for (int j = 0; j < child.jjtGetNumChildren(); j++) { + if (!(child.jjtGetChild(j) instanceof ASTAnnotation)) { + nextChild = child.jjtGetChild(j); + break; + } + } + return nextChild; + } } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FieldDeclarationsShouldBeAtStartOfClass.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FieldDeclarationsShouldBeAtStartOfClass.xml index afc92632fa..dfaaf8c3af 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FieldDeclarationsShouldBeAtStartOfClass.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FieldDeclarationsShouldBeAtStartOfClass.xml @@ -170,6 +170,30 @@ public class MyClass { public void myPublicBean() {} private static void myPrivateStatic() {} +} + ]]> + + + #1364 FieldDeclarationsShouldBeAtStartOfClass false positive using multiple annotations + 0 + diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 77e4e9587d..e081295c14 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -12,4 +12,6 @@ **Bugfixes:** +* [#1364](https://sourceforge.net/p/pmd/bugs/1364/): FieldDeclarationsShouldBeAtStartOfClass false positive using multiple annotations + **API Changes:**