Since Checkstyle 8.36
Checks identifiers with a pattern for a set of illegal names, such as those that are restricted or contextual keywords. Examples include "yield", "record", and "var". Please read more at Java Language Specification to get to know more about restricted keywords. Since this check uses a pattern to specify valid identifiers, users can also prohibit the usage of certain symbols, such as "$", or any non-ascii character.
name | description | type | default value | since |
---|---|---|---|---|
format | Sets the pattern to match valid identifiers. | Pattern | "(?i)^(?!(record|yield|var|permits|sealed)$).+$" |
8.36 |
tokens | tokens to check | subset of tokens CLASS_DEF , INTERFACE_DEF , ENUM_DEF , ANNOTATION_DEF , ANNOTATION_FIELD_DEF , PARAMETER_DEF , VARIABLE_DEF , METHOD_DEF , ENUM_CONSTANT_DEF , PATTERN_VARIABLE_DEF , RECORD_DEF , RECORD_COMPONENT_DEF , LAMBDA . | CLASS_DEF , INTERFACE_DEF , ENUM_DEF , ANNOTATION_DEF , ANNOTATION_FIELD_DEF , PARAMETER_DEF , VARIABLE_DEF , METHOD_DEF , ENUM_CONSTANT_DEF , PATTERN_VARIABLE_DEF , RECORD_DEF , RECORD_COMPONENT_DEF , LAMBDA . | 8.36 |
To configure the check:
Configuration:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalIdentifierName"/> </module> </module>
Example:
public class Example1 { Integer var = 4; // violation, 'Name 'var' must match pattern' int record = 15; // violation, 'Name 'record' must match pattern' String yield = "yield"; // violation above, 'Name 'yield' must match pattern' record Record(Record r){} // violation, 'Name 'Record' must match pattern' record R(Record record){} // violation, 'Name 'record' must match pattern' String yieldString = "yieldString"; // ok above, word 'yield' is not used as an identifier by itself record MyRecord(){} // ok above, word 'Record' is not used as an identifier by itself Integer variable = 2; // ok above, word 'var' is not used as an identifier by itself int open = 4; // ok, word 'open' can be used as an identifier Object transitive = "transitive"; // ok above, word 'transitive' can be used as an identifier int openInt = 4; // ok above, word 'openInt' can be used as an identifier Object transitiveObject = "transitiveObject"; // ok above, word 'transitiveObject' can be used as an identifier }
To configure the check to include "open" and "transitive" in the set of illegal identifiers:
Configuration:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalIdentifierName"> <property name="format" value="(?i)^(?!(record|yield|var|permits|sealed|open|transitive|_)$).+$"/> </module> </module> </module>
Example:
public class Example2 { Integer var = 4; // violation, 'Name 'var' must match pattern' int record = 15; // violation, 'Name 'record' must match pattern' String yield = "yield"; // violation above, 'Name 'yield' must match pattern' record Record(Record r){} // violation, 'Name 'Record' must match pattern' record R(Record record){} // violation, 'Name 'record' must match pattern' String yieldString = "yieldString"; // ok above, word 'yield' is not used as an identifier by itself record MyRecord(){} // ok above, word 'Record' is not used as an identifier by itself Integer variable = 2; // ok above, word 'var' is not used as an identifier by itself int open = 4; // violation, 'Name 'open' must match pattern' Object transitive = "transitive"; // violation above, 'Name 'transitive' must match pattern' int openInt = 4; // ok above, word 'open' is not used as an identifier by itself Object transitiveObject = "transitiveObject"; // ok above, word 'transitive' is not used as an identifier by itself }
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