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() {
    // violation below 'Token text matches the illegal pattern 'a href'.'
    String test  = "a href";

    String test2 = "A href"; // ok, case is sensitive
    String link = "href";
    final String quote = """
            \"""";
    int num1 = 0;
    int num2 = 0x111;
    int num3 = 0X111;
    int num4 = 010;
    long num5 = 0L;
    long num6 = 010L;
  }
}

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() {
    // violation below 'Token text matches the illegal pattern 'a href'.'
    String test  = "a href";
    // violation below 'Token text matches the illegal pattern 'a href'.'
    String test2 = "A href";
    String link = "href";
    final String quote = """
            \"""";
    int num1 = 0;
    int num2 = 0x111;
    int num3 = 0X111;
    int num4 = 010;
    long num5 = 0L;
    long num6 = 010L;
  }
}

To configure the check with a custom violation message using the message property:


<module name="Checker">
  <module name="TreeWalker">
    <module name="IllegalTokenText">
      <property name="tokens" value="STRING_LITERAL"/>
      <property name="format" value="href"/>
      <property name="message" value="Custom illegal text found"/>
    </module>
  </module>
</module>

Example:


public class Example5 {
  public void myTest() {

    String test  = "a href"; // violation 'Custom illegal text found'

    String test2 = "A href"; // violation 'Custom illegal text found'
    String link = "href";    // violation 'Custom illegal text found'
    final String quote = """
            \"""";
    int num1 = 0;
    int num2 = 0x111;
    int num3 = 0X111;
    int num4 = 010;
    long num5 = 0L;
    long num6 = 010L;
  }
}

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() {

    String test  = "a href";

    String test2 = "A href";
    String link = "href";
    final String quote = """
            \""""; // violation above 'Token text matches the illegal pattern '"'.'
    int num1 = 0;
    int num2 = 0x111;
    int num3 = 0X111;
    int num4 = 010;
    long num5 = 0L;
    long num6 = 010L;
  }
}

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() {

    String test  = "a href";

    String test2 = "A href";
    String link = "href";
    final String quote = """
            \"""";
    int num1 = 0;
    int num2 = 0x111;
    int num3 = 0X111; // ok, case is ignored
    int num4 = 010;     // violation 'Token text matches the illegal pattern'
    long num5 = 0L;
    long num6 = 010L;   // violation 'Token text matches the illegal pattern'
  }
}

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.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.coding.IllegalTokenTextCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker