IllegalIdentifierName

Since Checkstyle 8.36

Description

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.

Properties

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; // 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
}
        

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

Parent Module

TreeWalker