Class RegexpSinglelineJavaCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class RegexpSinglelineJavaCheck
    extends AbstractCheck

    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.

    • Property format - Specify the format of the regular expression to match. Type is java.util.regex.Pattern. Default value is "$.".
    • Property message - Specify the message which is used to notify about violations, if empty then default (hard-coded) message is used. Type is java.lang.String. Default value is null.
    • Property ignoreCase - Control whether to ignore case when searching. Type is boolean. Default value is false.
    • Property minimum - Specify the minimum number of matches required in each file. Type is int. Default value is 0.
    • Property maximum - Specify the maximum number of matches required in each file. Type is int. Default value is 0.
    • Property ignoreComments - Control whether to ignore text in comments when searching. Type is boolean. Default value is false.

    To configure the default check:

     <module name="RegexpSinglelineJava"/>
     

    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="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>
     

    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="RegexpSinglelineJava">
       <property name="format" value="debug"/>
       <property name="ignoreCase" value="true"/>
     </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="RegexpSinglelineJava">
       <property name="format" value="\.read(.*)|\.write(.*)"/>
       <property name="message" value="IO found"/>
     </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="RegexpSinglelineJava">
       <property name="format" value="\.log(.*)"/>
       <property name="maximum" value="2"/>
     </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="RegexpSinglelineJava">
       <property name="format" value="public"/>
       <property name="minimum" value="2"/>
       <property name="message" value="public member found"/>
       <property name="ignoreComments" value="true"/>
     </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
     }
     

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • regexp.exceeded
    • regexp.minimum
    Since:
    6.0
    • Field Detail

      • format

        private String format
        Specify the format of the regular expression to match.
      • message

        private String message
        Specify the message which is used to notify about violations, if empty then default (hard-coded) message is used.
      • minimum

        private int minimum
        Specify the minimum number of matches required in each file.
      • maximum

        private int maximum
        Specify the maximum number of matches required in each file.
      • ignoreCase

        private boolean ignoreCase
        Control whether to ignore case when searching.
      • ignoreComments

        private boolean ignoreComments
        Control whether to ignore text in comments when searching.
    • Method Detail

      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Description copied from class: AbstractCheck
        The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.
        Specified by:
        getAcceptableTokens in class AbstractCheck
        Returns:
        the token set this check is designed for.
        See Also:
        TokenTypes
      • beginTree

        public void beginTree​(DetailAST rootAST)
        Description copied from class: AbstractCheck
        Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.
        Overrides:
        beginTree in class AbstractCheck
        Parameters:
        rootAST - the root of the tree
      • setFormat

        public void setFormat​(String format)
        Setter to specify the format of the regular expression to match.
        Parameters:
        format - the format of the regular expression to match.
      • setMessage

        public void setMessage​(String message)
        Setter to specify the message which is used to notify about violations, if empty then default (hard-coded) message is used.
        Parameters:
        message - the message to report for a match.
      • setMinimum

        public void setMinimum​(int minimum)
        Setter to specify the minimum number of matches required in each file.
        Parameters:
        minimum - the minimum number of matches required in each file.
      • setMaximum

        public void setMaximum​(int maximum)
        Setter to specify the maximum number of matches required in each file.
        Parameters:
        maximum - the maximum number of matches required in each file.
      • setIgnoreCase

        public void setIgnoreCase​(boolean ignoreCase)
        Setter to control whether to ignore case when searching.
        Parameters:
        ignoreCase - whether to ignore case when searching.
      • setIgnoreComments

        public void setIgnoreComments​(boolean ignore)
        Setter to control whether to ignore text in comments when searching.
        Parameters:
        ignore - whether to ignore text in comments when searching.