Since Checkstyle 3.0
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 .
name | description | type | default value | since |
---|---|---|---|---|
format | Specifies valid identifiers. | Pattern | "^[a-z][a-zA-Z0-9]*$" |
3.0 |
ignoreOverridden | Allows to skip methods with Override annotation from validation. | boolean | false |
6.12.1 |
accessModifiers | Access modifiers of methods where parameters are checked. | AccessModifierOption[] | public, protected, package, private |
7.5 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="ParameterName"/> </module> </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]*$' }
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="Checker"> <module name="TreeWalker"> <module name="ParameterName"> <property name="format" value="^[a-z][_a-zA-Z0-9]+$"/> </module> </module> </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="Checker"> <module name="TreeWalker"> <module name="ParameterName"> <property name="ignoreOverridden" value="true"/> </module> </module> </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="Checker"> <module name="TreeWalker"> <module name="ParameterName"> <property name="format" value="^[a-z][a-zA-Z0-9]+$"/> </module> </module> </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="Checker"> <module name="TreeWalker"> <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> </module> </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]*$' }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.naming