From cc43e6f8be9ec1dd7f1d757ac9f901b7372048d5 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 15 May 2015 16:06:29 +0200 Subject: [PATCH] #1354 Complex FieldDeclarationsShouldBeAtStartOfClass false positive with Spring annotations --- ...dDeclarationsShouldBeAtStartOfClassRule.java | 7 ++++++- .../FieldDeclarationsShouldBeAtStartOfClass.xml | 17 +++++++++++++++++ src/site/markdown/overview/changelog.md | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) 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 f81d1e7f64..3f5fdfcf8a 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 @@ -4,6 +4,7 @@ package net.sourceforge.pmd.lang.java.rule.design; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.java.ast.ASTAnnotation; import net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; @@ -50,7 +51,11 @@ public class FieldDeclarationsShouldBeAtStartOfClassRule extends AbstractJavaRul for (int i = 0; i < parent.jjtGetNumChildren(); i++) { Node child = parent.jjtGetChild(i); if (child.jjtGetNumChildren() > 0) { - child = child.jjtGetChild(0); + if (!(child.jjtGetChild(0) instanceof ASTAnnotation) || child.jjtGetNumChildren() == 1) { + child = child.jjtGetChild(0); + } else { + child = child.jjtGetChild(1); + } } if (child.equals(node)) { break; 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 495193385d..afc92632fa 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 @@ -153,6 +153,23 @@ public class MyClass { // something } }; +} + ]]> + + + #1354 Complex FieldDeclarationsShouldBeAtStartOfClass false positive with Spring annotations + 0 + diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index c23aaf7115..b604c38368 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -17,5 +17,6 @@ * [#1343](https://sourceforge.net/p/pmd/bugs/1343/): MethodNamingConventions for overrided methods * [#1345](https://sourceforge.net/p/pmd/bugs/1345/): UseCollectionIsEmpty throws NullPointerException * [#1353](https://sourceforge.net/p/pmd/bugs/1353/): False positive "Only One Return" with lambda +* [#1354](https://sourceforge.net/p/pmd/bugs/1354/): Complex FieldDeclarationsShouldBeAtStartOfClass false positive with Spring annotations **API Changes:**