IllegalIdentifierName
Since Checkstyle 8.36
Description
Checks identifiers against a regular expression pattern to detect illegal names. Since
this check uses a pattern to define valid identifiers, users will need to use
negative lookaheads to explicitly ban certain names (e.g., "var") or patterns
(e.g., any identifier containing $) while still allowing all other valid identifiers.
Properties
name | description | type | default value | since |
---|---|---|---|---|
format | Sets the pattern to match valid identifiers. | Pattern | "^(?!var$|\S*\$)\S+$" |
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 |
Examples
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;
String yield = "yield";
String test$stuff = "test"; // violation, 'must match pattern'
String when = "today";
record Record(Record r){}
record R(Record record){}
String yieldString = "yieldString";
record MyRecord(){}
Integer variable = 2;
int open = 4;
Object transitive = "transitive";
int openInt = 4;
Object transitiveObject = "transitiveObject";
}
To configure the check to include some of JLS Keywords in the set of illegal identifiers (See Java Language Specification for the full list of JLS keywords):
Configuration:
<module name="Checker">
<module name="TreeWalker">
<module name="IllegalIdentifierName">
<property name="format"
value="(?i)^(?!(when|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'
String test$stuff = "test"; // violation, 'must match pattern'
String when = "today"; // violation, 'Name 'when' 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
}
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.naming