public class UniformEnumConstantNameCheck
extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
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.
| Modifier and Type | Field and Description |
|---|---|
static String |
CAMEL_PATTERN
Camel notation regular expression.
|
static String[] |
DEFAULT_PATTERN
Default pattern for enumeration values.
|
static String |
MSG_NOT_VALID_MULTI
Message code for format violations.
|
static String |
MSG_NOT_VALID_SINGLE
Message code for format violations.
|
static String |
UPPERCASE_PATTERN
Upper case notation regular expression.
|
| Constructor and Description |
|---|
UniformEnumConstantNameCheck()
Constructs check with the default pattern.
|
| Modifier and Type | Method and 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) |
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeveritypublic static final String MSG_NOT_VALID_MULTI
public static final String MSG_NOT_VALID_SINGLE
public static final String CAMEL_PATTERN
public static final String UPPERCASE_PATTERN
public static final String[] DEFAULT_PATTERN
public UniformEnumConstantNameCheck()
public final void setFormats(String... regexps)
regexps - format to check againstpublic int[] getDefaultTokens()
getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getAcceptableTokens()
getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getRequiredTokens()
getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckCopyright © 2021. All rights reserved.