Reputation: 6583
We want to enforce naming convention for enum types in our project to start with E letter (so enums should be named e.g. EType
, EColor
etc).
I see the way to enforce naming convention for interfaces and classes types (using "Class declaration" and "Interface declaration" tokens in TypeName rule).
Is there a way to do this for enums?
Upvotes: 4
Views: 1211
Reputation: 1252
It is possible now with the TypeName rule mentioned by OP, using the ENUM_DEF
token.
Something like this should work (based on the rule's example, not tested) :
<module name="TypeName">
<property name="format" value="^E[A-Z][a-zA-Z0-9]+$"/>
<property name="tokens" value="ENUM_DEF"/>
</module>
Upvotes: 0
Reputation: 17494
There is no corresponding rule in the naming conventions set. But you could achieve the desired result using a RegExp check (explanation of the regex):
<module name="Regexp">
<property name="format" value="\benum\s+\S\S(?<!E[A-Z])[a-zA-Z0-9]+"/>
<property name="message"
value="Enums must start with a capital 'E', e.g. EMyEnum"/>
<property name="illegalPattern" value="true"/>
<property name="ignoreComments" value="true"/>
</module>
This ignores matches in comments (like when an enum declaration was commented out) and also works if there is a newline between the enum
keyword and the identifier. Since enum
is a keyword in Java, there should not be many false positives.
Upvotes: 1
Reputation: 2234
I think there is no easy way to do this with checkstyle so you need to write your own check to do this.
See: Extending Checkstyle - Writing checks
Upvotes: 1
Reputation: 431
As far as i know there is not. But you can define your own rule. See http://checkstyle.sourceforge.net/writingchecks.html
Upvotes: 0