public class ParameterNameCheck extends AbstractNameCheck
Checks that method parameter names conform to a specified pattern.
By using accessModifiers property it is possible
to specify different formats for methods at different visibility levels.
To validate catch parameters please use
CatchParameterName.
To validate lambda parameters please use LambdaParameterName.
format - Specifies valid identifiers.
Type is java.util.regex.Pattern.
Default value is "^[a-z][a-zA-Z0-9]*$".
ignoreOverridden - Allows to skip methods with Override annotation from
validation. For example, the following method's parameter will be skipped from validation,
if ignoreOverridden is true:
@Override
public boolean equals(Object o) {
return super.equals(o);
}
Type is boolean.
Default value is false.
accessModifiers - Access modifiers of methods where parameters are
checked.
Type is com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption[].
Default value is public, protected, package, private.
An example of how to configure the check:
<module name="ParameterName"/>
Code Example:
class MyClass {
void method1(int v1) {} // OK
void method2(int V2) {} // violation, name 'V2' must match pattern '^[a-z][a-zA-Z0-9]*$'
}
An example of how to configure the check for names that begin with a lower case letter, followed by letters, digits, and underscores:
<module name="ParameterName"> <property name="format" value="^[a-z][_a-zA-Z0-9]+$"/> </module>
Code Example:
class MyClass {
void method1(int v1) {} // OK
void method2(int v_2) {} // OK
void method3(int V3) {} // violation, name 'V3' must match pattern '^[a-z][_a-zA-Z0-9]+$'
}
An example of how to configure the check to skip methods with Override annotation from validation:
<module name="ParameterName"> <property name="ignoreOverridden" value="true"/> </module>
Code Example:
class MyClass {
void method1(int v1) {} // OK
void method2(int V2) {} // violation, name 'V2' must match pattern '^[a-z][a-zA-Z0-9]*$'
@Override
public boolean equals(Object V3) { // OK
return true;
}
}
An example of how to configure the check for names that begin with a lower case letter, followed by letters and digits is:
<module name="ParameterName"> <property name="format" value="^[a-z][a-zA-Z0-9]+$"/> </module>
Code Example:
class MyClass {
void method1(int v1) {} // OK
void method2(int v_2) {} // violation, name 'v_2' must match pattern '^[a-z][a-zA-Z0-9]+$'
void method3(int V3) {} // violation, name 'V3' must match pattern '^[a-z][a-zA-Z0-9]+$'
}
The following configuration checks that the parameters always start with two lowercase characters and, in addition, that public method parameters cannot be one character long:
<module name="ParameterName">
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
<property name="accessModifiers"
value="protected, package, private"/>
<message key="name.invalidPattern"
value="Parameter name ''{0}'' must match pattern ''{1}''"/>
</module>
<module name="ParameterName">
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
<property name="accessModifiers" value="public"/>
<message key="name.invalidPattern"
value="Parameter name ''{0}'' must match pattern ''{1}''"/>
</module>
Code Example:
class MyClass {
void method1(int v1) {} // OK
protected method2(int V2) {} // violation, Parameter name 'V2'
// must match pattern '^[a-z]([a-z0-9][a-zA-Z0-9]*)?$'
private method3(int a) {} // OK
public method4(int b) {} // violation, Parameter name 'b'
// must match pattern '^[a-z][a-z0-9][a-zA-Z0-9]*$'
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
name.invalidPattern
AutomaticBean.OutputStreamOptionsMSG_INVALID_PATTERN| Constructor and Description |
|---|
ParameterNameCheck()
Creates a new
ParameterNameCheck 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.
|
void |
setAccessModifiers(AccessModifierOption... accessModifiers)
Setter to access modifiers of methods where parameters are checked.
|
void |
setIgnoreOverridden(boolean ignoreOverridden)
Setter to allows to skip methods with Override annotation from validation.
|
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 ParameterNameCheck()
ParameterNameCheck instance.public void setIgnoreOverridden(boolean ignoreOverridden)
@Override
public boolean equals(Object o) {
return super.equals(o);
}
ignoreOverridden - Flag for skipping methods with Override annotation.public void setAccessModifiers(AccessModifierOption... accessModifiers)
accessModifiers - access modifiers of methods which should be checked.public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypesprotected boolean mustCheckName(DetailAST ast)
AbstractNameCheckmustCheckName in class AbstractNameCheckast - the AST to check.Copyright © 2001–2020. All rights reserved.