public class PatternVariableNameCheck extends AbstractNameCheck
Checks that pattern variable names conform to a specified pattern.
format - Specifies valid identifiers.
Type is java.util.regex.Pattern.
Default value is "^[a-z][a-zA-Z0-9]*$".
To configure the check:
<module name="PatternVariableName"/>
Code Example:
class MyClass {
MyClass(Object o1){
if (o1 instanceof String STRING) { // violation, name 'STRING' must
// match pattern '^[a-z][a-zA-Z0-9]*$'
}
if (o1 instanceof Integer num) { // OK
}
}
}
An example of how to configure the check for names that have a lower case letter, followed by letters and digits, optionally separated by underscore:
<module name="PatternVariableName"> <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/> </module>
Code Example:
class MyClass {
MyClass(Object o1){
if (o1 instanceof String STR) { // violation, name 'STR' must
// match pattern '^[a-z](_?[a-zA-Z0-9]+)*$'
}
if (o1 instanceof Integer num) { // OK
}
if (o1 instanceof Integer num_1) { // OK
}
}
}
An example of how to configure the check to that all variables have 3 or more chars in name:
<module name="PatternVariableName">
<property name="format" value="^[a-z][_a-zA-Z0-9]{2,}$"/>
</module>
Code Example:
class MyClass {
MyClass(Object o1){
if (o1 instanceof String s) { // violation, name 's' must
// match pattern '^[a-z][_a-zA-Z0-9]{2,}$'
}
if (o1 instanceof Integer num) { // OK
}
}
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
name.invalidPattern
AutomaticBean.OutputStreamOptionsMSG_INVALID_PATTERN| Constructor and Description |
|---|
PatternVariableNameCheck()
Creates a new
PatternVariableNameCheck 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.
|
setFormat, visitTokenbeginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic PatternVariableNameCheck()
PatternVariableNameCheck instance.public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypesprotected final boolean mustCheckName(DetailAST ast)
AbstractNameCheckmustCheckName in class AbstractNameCheckast - the AST to check.Copyright © 2001-2020. All Rights Reserved.