IllegalTokenText

Since Checkstyle 3.2

Description

Checks specified tokens text for matching an illegal pattern. By default, no tokens are specified.

Properties

name description type default value since
format Define the RegExp for illegal pattern. Pattern "^$" 3.2
ignoreCase Control whether to ignore case when matching. boolean false 3.2
message Define the message which is used to notify about violations; if empty then the default message is used. String "" 3.2
tokens tokens to check subset of tokens NUM_DOUBLE , NUM_FLOAT , NUM_INT , NUM_LONG , IDENT , COMMENT_CONTENT , STRING_LITERAL , CHAR_LITERAL , TEXT_BLOCK_CONTENT . empty 3.2

Examples

To configure the check to forbid String literals containing "a href":

<module name="Checker">
  <module name="TreeWalker">
    <module name="IllegalTokenText">
      <property name="tokens" value="STRING_LITERAL"/>
      <property name="format" value="a href"/>
    </module>
  </module>
</module>
        

Example:

public class Example1 {
  public void myTest() {
    String test  = "a href"; // violation
    String test2 = "A href"; // OK, case is sensitive
  }
}
        

To configure the check to forbid String literals containing "a href" for the ignoreCase mode:

<module name="Checker">
  <module name="TreeWalker">
    <module name="IllegalTokenText">
      <property name="tokens" value="STRING_LITERAL"/>
      <property name="format" value="a href"/>
      <property name="ignoreCase" value="true"/>
    </module>
  </module>
</module>
        

Example:

public class Example2 {
  public void myTest() {
    String test  = "a href"; // violation
    String test2 = "A href"; // violation
  }
}
        

To configure the check to forbid string literal text blocks containing ":

<module name="Checker">
  <module name="TreeWalker">
    <module name="IllegalTokenText">
        <property name="tokens" value="TEXT_BLOCK_CONTENT"/>
        <property name="format" value='"'/>
    </module>
  </module>
</module>
        

Example:

public class Example3 {
  public void myTest() {
    final String quote = """
            \""""; // violation above
  }
}
        

To configure the check to forbid leading zeros in an integer literal, other than zero and a hex literal:

<module name="Checker">
  <module name="TreeWalker">
    <module name="IllegalTokenText">
      <property name="tokens" value="NUM_INT,NUM_LONG"/>
      <property name="format" value="^0[^lx]"/>
      <property name="ignoreCase" value="true"/>
    </module>
  </module>
</module>
        

Example:

public class Example4 {
  public void myTest() {
    int test1 = 0; // OK
    int test2 = 0x111; // OK
    int test3 = 0X111; // OK, case is ignored
    int test4 = 010; // violation
    long test5 = 0L; // OK
    long test6 = 010L; // 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.coding

Parent Module

TreeWalker