public class ConstantNameCheck extends AbstractAccessControlNameCheck
Checks that constant names conform to a specified pattern. A constant is a static and final field or an interface/annotation field, except serialVersionUID and serialPersistentFields .
format
- Specifies valid identifiers.
Type is java.util.regex.Pattern
.
Default value is "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"
.
applyToPublic
- Controls whether to apply the check to public member.
Type is boolean
.
Default value is true
.
applyToProtected
- Controls whether to apply the check to protected member.
Type is boolean
.
Default value is true
.
applyToPackage
- Controls whether to apply the check to package-private member.
Type is boolean
.
Default value is true
.
applyToPrivate
- Controls whether to apply the check to private member.
Type is boolean
.
Default value is true
.
An example of how to configure the check is:
<module name="ConstantName"/>
The following configuration apart from names allowed by default allows log
or logger
:
<module name="ConstantName"> <property name="format" value="^log(ger)?$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/> </module>
Code Example:
class MyClass { final static int log = 10; // OK final static int logger = 50; // OK final static int logMYSELF = 10; // violation, name 'logMYSELF' must match // pattern '^log(ger)?$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$' final static int loggerMYSELF = 5; // violation, name 'loggerMYSELF' must match // pattern '^log(ger)?$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$' final static int MYSELF = 100; // OK final static int myselfConstant = 1; // violation, name 'myselfConstant' must match pattern // '^log(ger)?$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$' }
The following configuration skip validation on public constant field and protected constant field.
<module name="ConstantName"> <property name="applyToPublic" value="false"/> <property name="applyToProtected" value="false"/> </module>
Code Example:
class MyClass { public final static int firstConstant = 10; // OK protected final static int secondConstant = 100; // OK final static int thirdConstant = 1000; // violation, name 'thirdConstant' must // match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$' private final static int fourthConstant = 50; // violation, name 'fourthConstant' must match // pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$' }
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
name.invalidPattern
AutomaticBean.OutputStreamOptions
MSG_INVALID_PATTERN
Constructor and Description |
---|
ConstantNameCheck()
Creates a new
ConstantNameCheck instance. |
Modifier and Type | Method and Description |
---|---|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
protected boolean |
mustCheckName(DetailAST ast)
Decides whether the name of an AST should be checked against
the format regexp.
|
setApplyToPackage, setApplyToPrivate, setApplyToProtected, setApplyToPublic, shouldCheckInScope
setFormat, visitToken
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public ConstantNameCheck()
ConstantNameCheck
instance.public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
protected final boolean mustCheckName(DetailAST ast)
AbstractNameCheck
mustCheckName
in class AbstractAccessControlNameCheck
ast
- the AST to check.Copyright © 2001–2020. All rights reserved.