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.
format - Specify the format of the regular expression to match.
Type is java.lang.String.
Default value is "$.".
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.
ignoreCase - Control whether to ignore case when searching.
Type is boolean.
Default value is false.
minimum - Specify the minimum number of matches required in each file.
Type is int.
Default value is 0.
maximum - Specify the maximum number of matches required in each file.
Type is int.
Default value is 0.
ignoreComments - Control whether to ignore text in comments when searching.
Type is boolean.
Default value is false.
To configure the check with default values:
<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
AutomaticBean.OutputStreamOptions| Constructor and Description |
|---|
RegexpSinglelineJavaCheck() |
| Modifier and Type | Method and Description |
|---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
void |
setFormat(String format)
Setter to specify the format of the regular expression to match.
|
void |
setIgnoreCase(boolean ignoreCase)
Setter to control whether to ignore case when searching.
|
void |
setIgnoreComments(boolean ignore)
Setter to control whether to ignore text in comments when searching.
|
void |
setMaximum(int maximum)
Setter to specify the maximum number of matches required in each file.
|
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.
|
void |
setMinimum(int minimum)
Setter to specify the minimum number of matches required in each file.
|
clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens, visitTokenfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic RegexpSinglelineJavaCheck()
public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void beginTree(DetailAST rootAST)
AbstractCheckbeginTree in class AbstractCheckrootAST - the root of the treepublic void setFormat(String format)
format - the format of the regular expression to match.public void setMessage(String message)
message - the message to report for a match.public void setMinimum(int minimum)
minimum - the minimum number of matches required in each file.public void setMaximum(int maximum)
maximum - the maximum number of matches required in each file.public void setIgnoreCase(boolean ignoreCase)
ignoreCase - whether to ignore case when searching.public void setIgnoreComments(boolean ignore)
ignore - whether to ignore text in comments when searching.Copyright © 2001–2020. All rights reserved.