PatternVariableName

Since Checkstyle 8.36

Description

Checks that pattern variable names conform to a specified pattern.

Properties

name description type default value since
format Sets the pattern to match valid identifiers. Pattern "^([a-z][a-zA-Z0-9]*|_)$" 8.36

Examples

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

An example of how to configure the check with different format for final and non-final pattern variables:

<module name="Checker">
  <module name="TreeWalker">
    <module name="PatternVariableName">
        <property name="id" value="FinalPatternVariableName"/>
        <property name="format" value="^[A-Z][A-Z0-9]*$"/>
    </module>

    <module name="PatternVariableName">
        <property name="id" value="NonFinalPatternVariableName"/>
        <property name="format" value="^([a-z][a-zA-Z0-9]*|_)$"/>
    </module>

    <module name="SuppressionXpathSingleFilter">
      <property name="id" value="FinalPatternVariableName"/>
      <property name="query" value="//PATTERN_VARIABLE_DEF[
            not(./MODIFIERS/FINAL)]/IDENT"/>
    </module>

    <module name="SuppressionXpathSingleFilter">
      <property name="id" value="NonFinalPatternVariableName"/>
      <property name="query" value="//PATTERN_VARIABLE_DEF[
            (./MODIFIERS/FINAL)]/IDENT"/>
    </module>
  </module>
</module>
        

Code Example:

class Example4 {
  void foo(Object o1){
    if (o1 instanceof String BAD) {} // violation
    if (o1 instanceof String good) {}
    if (o1 instanceof final String GOOD) {}
    if (o1 instanceof final String bad) {} // violation
  }
}
        

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