RegexpSinglelineJava

Since Checkstyle 6.0

Description

Checks that a specified pattern matches a single-line in Java files.

This class is variation on RegexpSingleline for detecting single-lines that match a supplied regular expression in Java files. It supports suppressing matches in Java comments.

Properties

name description type default value since
format Specify the format of the regular expression to match. Pattern "$." 5.0
ignoreCase Control whether to ignore case when searching. boolean false 5.0
ignoreComments Control whether to ignore text in comments when searching. boolean false 5.0
maximum Specify the maximum number of matches required in each file. int 0 5.0
message Specify the message which is used to notify about violations, if empty then default (hard-coded) message is used. String null 6.0
minimum Specify the minimum number of matches required in each file. int 0 5.0

Examples

To configure the default check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="RegexpSinglelineJava"/>
  </module>
</module>
        

Example1:

class Example1 {

  private void testMethod1() {
    int debug = 0;
    System.out.println("");
    System.out.
    println("");
  }

  private void testMethod2() throws IOException {
    FileReader in = new FileReader("path/to/input");
    int ch = in.read();
    while(ch != -1) {
      System.out.print((char)ch);
      ch = in.read();
    }

    FileWriter out = new FileWriter("path/to/output");
    out.write("something");
  }

  public void testMethod3(){
    final Logger logger = Logger.getLogger(Example1.class.getName());
    logger.info("first");
    logger.info("second");
    logger.info("third");
    System.out.println("fourth");
    logger.info("fifth");
  }
}
        

To configure the check for calls to System.out.println, except in comments:

<module name="Checker">
  <module name="TreeWalker">
    <module name="RegexpSinglelineJava">
      <!-- . matches any character, so we need to
           escape it and use \. to match dots. -->
      <property name="format" value="System\.out\.println"/>
      <property name="ignoreComments" value="true"/>
    </module>
  </module>
</module>
        

Example2:

class Example2 {

  private void testMethod1() {
    int debug = 0;
    System.out.println(""); // violation, 'Line matches the illegal pattern'
    System.out.
    println("");
  }

  private void testMethod2() throws IOException {
    FileReader in = new FileReader("path/to/input");
    int ch = in.read();
    while(ch != -1) {
      System.out.print((char)ch);
      ch = in.read();
    }

    FileWriter out = new FileWriter("path/to/output");
    out.write("something");
  }

  public void testMethod3(){
    final Logger logger = Logger.getLogger(Example2.class.getName());
    logger.info("first");
    logger.info("second");
    logger.info("third");
    System.out.println("fourth"); // violation, 'Line matches the illegal pattern'
    logger.info("fifth");
  }
}
        

To configure the check to find case-insensitive occurrences of "debug":

<module name="Checker">
  <module name="TreeWalker">
    <module name="RegexpSinglelineJava">
      <property name="format" value="debug"/>
      <property name="ignoreCase" value="true"/>
      <property name="ignoreComments" value="true"/>
    </module>
  </module>
</module>
        

Example3:

class Example3 {

  private void testMethod1() {
    int debug = 0; // violation, 'Line matches the illegal pattern'
    System.out.println("");
    System.out.
    println("");
  }

  private void testMethod2() throws IOException {
    FileReader in = new FileReader("path/to/input");
    int ch = in.read();
    while(ch != -1) {
      System.out.print((char)ch);
      ch = in.read();
    }

    FileWriter out = new FileWriter("path/to/output");
    out.write("something");
  }

  public void testMethod3(){
    final Logger logger = Logger.getLogger(Example3.class.getName());
    logger.info("first");
    logger.info("second");
    logger.info("third");
    System.out.println("fourth");
    logger.info("fifth");
  }
}
        

To configure the check to find occurrences of "\.read(.*)|\.write(.*)" and display "IO found" for each violation.

<module name="Checker">
  <module name="TreeWalker">
    <module name="RegexpSinglelineJava">
      <property name="format" value="\.read(.*)|\.write(.*)"/>
      <property name="message" value="IO found"/>
    </module>
  </module>
</module>
        

Example4:

class Example4 {

  private void testMethod1() {
    int debug = 0;
    System.out.println("");
    System.out.
    println("");
  }

  private void testMethod2() throws IOException {
    FileReader in = new FileReader("path/to/input");
    int ch = in.read(); // violation, 'IO found'
    while(ch != -1) {
      System.out.print((char)ch);
      ch = in.read(); // violation, 'IO found'
    }

    FileWriter out = new FileWriter("path/to/output");
    out.write("something"); // violation, 'IO found'
  }

  public void testMethod3(){
    final Logger logger = Logger.getLogger(Example3.class.getName());
    logger.info("first");
    logger.info("second");
    logger.info("third");
    System.out.println("fourth");
    logger.info("fifth");
  }
}
        

To configure the check to find occurrences of "\.log(.*)". We want to allow a maximum of 2 occurrences.

<module name="Checker">
  <module name="TreeWalker">
    <module name="RegexpSinglelineJava">
      <property name="format" value="\.info(.*)"/>
      <property name="maximum" value="2"/>
    </module>
  </module>
</module>
        

Example5:

class Example5 {

  private void testMethod1() {
    int debug = 0;
    System.out.println("");
    System.out.
    println("");
  }

  private void testMethod2() throws IOException {
    FileReader in = new FileReader("path/to/input");
    int ch = in.read();
    while(ch != -1) {
      System.out.print((char)ch);
      ch = in.read();
    }

    FileWriter out = new FileWriter("path/to/output");
    out.write("something");
  }

  public void testMethod3(){
    final Logger logger = Logger.getLogger(Example5.class.getName());
    logger.info("first");
    logger.info("second"); // violation, 'Line matches the illegal pattern'
    logger.info("third");  // violation, 'Line matches the illegal pattern'
    System.out.println("fourth");
    logger.info("fifth");  // violation, 'Line matches the illegal pattern'
  }
}
        

To configure the check to find all occurrences of "public". We want to ignore comments, display "public member found" for each violation and say if less than 1 occurrences.

<module name="Checker">
  <module name="TreeWalker">
    <module name="RegexpSinglelineJava">
      <property name="format" value="public"/>
      <property name="minimum" value="1"/>
      <property name="message" value="public member found"/>
      <property name="ignoreComments" value="true"/>
    </module>
  </module>
</module>
        

Example6:

class Example6 {

  private void testMethod1() {
    int debug = 0;
    System.out.println("");
    System.out.
    println("");
  }

  private void testMethod2() throws IOException {
    FileReader in = new FileReader("path/to/input");
    int ch = in.read();
    while(ch != -1) {
      System.out.print((char)ch);
      ch = in.read();
    }

    FileWriter out = new FileWriter("path/to/output");
    out.write("something");
  }
  // violation below, 'public member found'
  public void testMethod3(){
    final Logger logger = Logger.getLogger(Example1.class.getName());
    logger.info("first");
    logger.info("second");
    logger.info("third");
    System.out.println("fourth");
    logger.info("fifth");
  }
}
        

To configure the check to find all occurrences of "private". We want to ignore comments, display "private member found" for each violation and say if less than 2 occurrences.

<module name="Checker">
  <module name="TreeWalker">
    <module name="RegexpSinglelineJava">
      <property name="format" value="private"/>
      <property name="minimum" value="2"/>
      <property name="message" value="private member found"/>
      <property name="ignoreComments" value="true"/>
    </module>
  </module>
</module>
        

Example7:

class Example7 {
  // violation below, 'private member found'
  private void testMethod1() {
    int debug = 0;
    System.out.println("");
    System.out.
    println("");
  }
  // violation below, 'private member found'
  private void testMethod2() throws IOException {
    FileReader in = new FileReader("path/to/input");
    int ch = in.read();
    while(ch != -1) {
      System.out.print((char)ch);
      ch = in.read();
    }

    FileWriter out = new FileWriter("path/to/output");
    out.write("something");
  }

  public void testMethod3(){
    final Logger logger = Logger.getLogger(Example1.class.getName());
    logger.info("first");
    logger.info("second");
    logger.info("third");
    System.out.println("fourth");
    logger.info("fifth");
  }
}
        

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.regexp

Parent Module

TreeWalker