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>
        

This configuration does not match to anything, so we do not provide any code example for it as no violation will ever be reported.

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>
        

Example:

System.out.println(""); // violation, instruction matches illegal pattern
System.out.
println(""); // OK
/* System.out.println */ // OK, comments are ignored
        

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"/>
    </module>
  </module>
</module>
        

Example:

int debug = 0; // violation, variable name matches illegal pattern
public class Debug { // violation, class name matches illegal pattern
/* this is for de
   bug only; */ // OK
        

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>
        

Example:

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

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

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="\.log(.*)"/>
      <property name="maximum" value="2"/>
    </module>
  </module>
</module>
        

Example:

public class Foo{
  public void bar(){
    Logger.log("first"); // OK, first occurrence is allowed
    Logger.log("second"); // OK, second occurrence is allowed
    Logger.log("third"); // violation
    System.out.println("fourth");
    Logger.log("fifth"); // violation
  }
}
        

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 2 occurrences.

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

Example:

class Foo{ // violation, file contains less than 2 occurrences of "public"
  private int a;
  /* public comment */ // OK, comment is ignored
  private void bar1() {}
  public  void bar2() {} // violation
}
        

Example:

class Foo{
  private int a;
  /* public comment */ // OK, comment is ignored
  public void bar1() {} // violation
  public void bar2() {} // 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.regexp

Parent Module

TreeWalker