Is there a way to enforce enumeration type naming convention with Checkstyle?

We want to enforce the naming convention for enumeration types in our project to start with the letter E (therefore, enumerations must be specified, for example, EType , EColor , etc.).

I see a way to apply a naming convention for interfaces and class types (using the "Class Declaration" and "Interface Declaration" tokens in the TypeName rule ).

Is there a way to do this for listings?

+4
source share
4 answers

I think there is no easy way to do this with checkstyle, so you need to write your own check to do this.

See: Checkstyle extension - Recording verification

+1
source

There is no corresponding rule in the naming rules. But you could achieve the desired result using RegExp check ( explanation of regular expression):

 <module name="Regexp"> <property name="format" value="\benum\s+\S\S(?&lt;!E[AZ])[a-zA-Z0-9]+"/> <property name="message" value="Enums must start with a capital 'E', eg EMyEnum"/> <property name="illegalPattern" value="true"/> <property name="ignoreComments" value="true"/> </module> 

This ignores the matches in the comments (for example, when the enumeration declaration has been commented out), and also works if there is a new line between the enum keyword and the identifier. Since enum is a keyword in Java, there should not be many false positives.

+1
source

As far as I know, no. But you can define your own rule. See http://checkstyle.sourceforge.net/writingchecks.html

0
source

Now you can use the TypeName rule specified by OP using the ENUM_DEF token.

Something like this should work (based on an example rule, not tested):

 <module name="TypeName"> <property name="format" value="^E[AZ][a-zA-Z0-9]+$"/> <property name="tokens" value="ENUM_DEF"/> </module> 
0
source

Source: https://habr.com/ru/post/1487432/


All Articles