Class UniformEnumConstantNameCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public class UniformEnumConstantNameCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
    Check forces enum constants to match one of the specified patterns and forces all the values to follow only one of the specified patterns.

    By default both CamelCase and UPPER_CASE are allowed, so check validates, whether all the values conform the either of them.

    For example, both enums are allowed by the check:

     public enum EnumOne {
        FirstElement, SecondElement, ThirdElement;
     }
     public enum EnumTwo {
        FIRST_ELEMENT, SECOND_ELEMENT, THIRD_ELEMENT;
     }
    But the following enum, is violated, because values conform different notations:
     public enum EnumThree {
        FirstElement, SECOND_ELEMENT, ThirdElement;
     }
     

    To use only CamelCase, use the following configuration:

     <module name="UniformEnumConstantNameCheck">
        <property name="format" value="^[A-Z][a-zA-Z0-9]*$"/>
     </module>
     

    If both CamelCase and UPPER_CASE are allowed, use the following configuration (this is the default):

     <module name="UniformEnumConstantNameCheck">
        <property name="format" value="^[A-Z][a-zA-Z0-9]*$,^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>
     </module>
     

    Only first violation is reported for each enumeration because of the nature of the check: it's impossible to determine which specific pattern user should follow for this certain enumeration, as multiple patterns have been specified. The only thing that this check reports is whether there is at least one pattern (among specified in the configuration), which all the enum constant conform or there is no.

    Since:
    1.21.0
    Author:
    Pavel Baranchikov
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void setFormats​(String... regexps)
      Method sets format to match Class Enumeration names.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

        finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Field Detail

      • MSG_NOT_VALID_MULTI

        public static final String MSG_NOT_VALID_MULTI
        Message code for format violations. Used, when more than one format violated.
        See Also:
        Constant Field Values
      • MSG_NOT_VALID_SINGLE

        public static final String MSG_NOT_VALID_SINGLE
        Message code for format violations. Used, when exactly one format violated.
        See Also:
        Constant Field Values
      • UPPERCASE_PATTERN

        public static final String UPPERCASE_PATTERN
        Upper case notation regular expression.
        See Also:
        Constant Field Values
      • DEFAULT_PATTERN

        public static final String[] DEFAULT_PATTERN
        Default pattern for enumeration values.
    • Constructor Detail

      • UniformEnumConstantNameCheck

        public UniformEnumConstantNameCheck()
        Constructs check with the default pattern.
    • Method Detail

      • setFormats

        public final void setFormats​(String... regexps)
        Method sets format to match Class Enumeration names.
        Parameters:
        regexps - format to check against
      • getDefaultTokens

        public int[] getDefaultTokens()
        Specified by:
        getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck