Since Checkstyle 8.36
Checks that pattern variable names conform to a specified pattern.
name | description | type | default value | since |
---|---|---|---|---|
format | Sets the pattern to match valid identifiers. | Pattern | "^[a-z][a-zA-Z0-9]*$" |
8.36 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="PatternVariableName"/> </module> </module>
Code Example:
class Example1 { void foo(Object o1){ if (o1 instanceof String STRING) {} // violation if (o1 instanceof Integer num) {} if (o1 instanceof Integer num_1) {} // violation if (o1 instanceof Integer n) {} } }
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="Checker"> <module name="TreeWalker"> <module name="PatternVariableName"> <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/> </module> </module> </module>
Code Example:
class Example2 { void foo(Object o1){ if (o1 instanceof String STRING) {} // violation if (o1 instanceof Integer num) {} if (o1 instanceof Integer num_1) {} if (o1 instanceof Integer n) {} } }
An example of how to configure the check to that all variables have 3 or more chars in name:
<module name="Checker"> <module name="TreeWalker"> <module name="PatternVariableName"> <property name="format" value="^[a-z][_a-zA-Z0-9]{2,}$"/> </module> </module> </module>
Code Example:
class Example3 { void foo(Object o1){ if (o1 instanceof String STRING) {} // violation if (o1 instanceof Integer num) {} if (o1 instanceof Integer num_1) {} if (o1 instanceof Integer n) {} // violation } }
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