Menu

#1126 False positive with FieldDeclarationsShouldBeAtStartOfClass for static enums

PMD-5.1.0
closed
None
PMD
3-Major
Bug
5.0.5
2014-02-02
2013-08-25
No

I am noticing that the FieldDeclarationsShouldBeAtStartOfClass rule is firing for cases where static enums are declared before non-static fields. Consider the following code:

public class Foo {
    private static final Logger LOGGER = LoggerFactory.getLogger(Foo.class);

    public static enum MyType {
        ABC, DEF, GHI, JHK
    };

    private int id;
    private MyType myType;
    private String name;

    // OK, now constructors, getters, setters, etc.
}

I do not find it objectionable to declare static enums before fields. In fact, I would prefer it because my fields reference the static enum... Also, I feel that the static declarations should be bunched together at the top.

I tried to find a reference to the official Java Code convention document that was the impetus for this rule, but I don't see that it specifically addresses this use-case. Technically, an enum is similar to an inner-class, but it is also declared statically in this case.

Can we agree that the above code is OK? If so, can the rule be relaxed such that this doesn't fire? If not, please provide an explanation as to why this is a problem.

Thank you,

Anthony

Discussion

  • Andreas Dangel

    Andreas Dangel - 2014-02-02
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-5.1.0
     
  • Andreas Dangel

    Andreas Dangel - 2014-02-02

    I've added a new rule property: "ignoreEnumDeclarations". This is by default true, so the rule is relaxed by default. If a more strict interpretation of this rule is needed, one can set this property to false, to have the old behavior.

     

Log in to post a comment.