Class FieldDeclarationsShouldBeAtStartOfClassRule

  • All Implemented Interfaces:
    JavaParserVisitor, ImmutableLanguage, PropertySource, Rule

    public class FieldDeclarationsShouldBeAtStartOfClassRule
    extends AbstractJavaRule
    Detects fields that are declared after methods, constructors, etc. It was a XPath rule, but the Java version is much faster. The XPath rule for reference:
    //ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration
    [not(.//ClassOrInterfaceBodyDeclaration) or $ignoreAnonymousClassDeclarations = 'false']
    [../preceding-sibling::ClassOrInterfaceBodyDeclaration
        [  count(ClassOrInterfaceDeclaration) > 0
        or count(ConstructorDeclaration) > 0
        or count(MethodDeclaration) > 0
        or count(AnnotationTypeDeclaration) > 0
        or ($ignoreEnumDeclarations = 'false' and count(EnumDeclaration) > 0)
        ]
    ]