#1364 FieldDeclarationsShouldBeAtStartOfClass false positive using multiple annotations

This commit is contained in:
Andreas Dangel
2015-05-31 10:29:42 +02:00
parent 00ff9f25ae
commit 19a13c0ab9
3 changed files with 44 additions and 5 deletions

View File

@ -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;
}
}

View File

@ -170,6 +170,30 @@ public class MyClass {
public void myPublicBean() {}
private static void myPrivateStatic() {}
}
]]></code>
</test-code>
<test-code>
<description>#1364 FieldDeclarationsShouldBeAtStartOfClass false positive using multiple annotations</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class MyEntity {
private static final String MY_STRING = "STRING";
@Id
@Column
private Long id;
@NotNull
@ManyToOne
@JoinColumn
@Valid
private RelationEntity relation;
public MyEntity() {
}
}
]]></code>
</test-code>

View File

@ -12,4 +12,6 @@
**Bugfixes:**
* [#1364](https://sourceforge.net/p/pmd/bugs/1364/): FieldDeclarationsShouldBeAtStartOfClass false positive using multiple annotations
**API Changes:**