Class RegexpMultilineCheck
- All Implemented Interfaces:
Configurable,Contextualizable,FileSetCheck
Checks that a specified pattern matches across multiple lines in any file type.
Rationale: This check can be used to when the regular expression can be span multiple lines.
-
Property
format- Specify the format of the regular expression to match. Type isjava.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 isjava.lang.String. Default value isnull. -
Property
ignoreCase- Control whether to ignore case when searching. Type isboolean. Default value isfalse. -
Property
minimum- Specify the minimum number of matches required in each file. Type isint. Default value is0. -
Property
maximum- Specify the maximum number of matches required in each file. Type isint. Default value is0. -
Property
matchAcrossLines- Control whether to match expressions across multiple lines. Type isboolean. Default value isfalse. -
Property
fileExtensions- Specify the file type extension of files to process. Type isjava.lang.String[]. Default value is"".
To run the check with its default configuration (no matches will be):
<module name="RegexpMultiline"/>
Example:
void method() {
int i = 5; // OK
System.out.println(i); // OK
}
To configure the check to find calls to print to the console:
<module name="RegexpMultiline">
<property name="format" value="System\.(out)|(err)\.print(ln)?\("/>
</module>
Example:
void method() {
System.out.print("Example"); // violation
System.err.println("Example"); // violation
System.out.print
("Example"); // violation
System.err.println
("Example"); // OK
System
.out.print("Example"); // OK
System
.err.println("Example"); // violation
System.
out.print("Example"); // OK
System.
err.println("Example"); // violation
}
To configure the check to match text that spans multiple lines, like normal code in a Java file:
<module name="RegexpMultiline">
<property name="matchAcrossLines" value="true"/>
<property name="format" value="System\.out.*?print\("/>
</module>
Example:
void method() {
System.out.print("Example"); // violation
System.err.println("Example");
System.out.print // violation
("Example");
System.err.println
("Example");
System
.out.print("Example");
System
.err.println("Example");
System.
out.print("Example");
System.
err.println("Example");
}
Note: Beware of the greedy regular expression used in the above example.
.* will match as much as possible and not produce multiple violations
in the file if multiple groups of lines could match the expression. To prevent
an expression being too greedy, avoid overusing matching all text or allow it
to be optional, like .*?. Changing the example expression to not be
greedy will allow multiple violations in the example to be found in the same file.
To configure the check to match a maximum of three test strings:
<module name="RegexpMultiline"> <property name="format" value="Test #[0-9]+:[A-Za-z ]+"/> <property name="ignoreCase" value="true"/> <property name="maximum" value="3"/> </module>
Example:
void method() {
System.out.println("Test #1: this is a test string"); // OK
System.out.println("TeSt #2: This is a test string"); // OK
System.out.println("TEST #3: This is a test string"); // OK
int i = 5;
System.out.println("Value of i: " + i);
System.out.println("Test #4: This is a test string"); // violation
System.out.println("TEst #5: This is a test string"); // violation
}
To configure the check to match a minimum of two test strings:
<module name="RegexpMultiline"> <property name="format" value="Test #[0-9]+:[A-Za-z ]+"/> <property name="minimum" value="2"/> </module>
Example:
void method() {
System.out.println("Test #1: this is a test string"); // violation
System.out.println("TEST #2: This is a test string"); // OK, "ignoreCase" is false by default
int i = 5;
System.out.println("Value of i: " + i);
System.out.println("Test #3: This is a test string"); // violation
System.out.println("Test #4: This is a test string"); // violation
}
To configure the check to restrict an empty file:
<module name="RegexpMultiline">
<property name="format" value="^\s*$" />
<property name="matchAcrossLines" value="true" />
<property name="message" value="Empty file is not allowed" />
</module>
Example of violation from the above config:
/var/tmp$ cat -n Test.java 1 2 3 4
Result:
/var/tmp/Test.java // violation, a file must not be empty.
Parent is com.puppycrawl.tools.checkstyle.Checker
Violation Message Keys:
-
regexp.StackOverflowError -
regexp.empty -
regexp.exceeded -
regexp.minimum
- Since:
- 5.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate MultilineDetectorThe detector to use.private StringSpecify the format of the regular expression to match.private booleanControl whether to ignore case when searching.private booleanControl whether to match expressions across multiple lines.private intSpecify the maximum number of matches required in each file.private StringSpecify the message which is used to notify about violations, if empty then default (hard-coded) message is used.private intSpecify the minimum number of matches required in each file. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbeginProcessing(String charset) Called when about to be called to process a set of files.private intRetrieves the compile flags for the regular expression being built based onmatchAcrossLines.protected voidprocessFiltered(File file, FileText fileText) Called to process a file that matches the specified file extensions.voidSetter to specify the format of the regular expression to match.voidsetIgnoreCase(boolean ignoreCase) Setter to control whether to ignore case when searching.voidsetMatchAcrossLines(boolean matchAcrossLines) Setter to control whether to match expressions across multiple lines.voidsetMaximum(int maximum) Setter to specify the maximum number of matches required in each file.voidsetMessage(String message) Setter to specify the message which is used to notify about violations, if empty then default (hard-coded) message is used.voidsetMinimum(int minimum) Setter to specify the minimum number of matches required in each file.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
addViolations, destroy, finishProcessing, fireErrors, getFileContents, getFileExtensions, getMessageDispatcher, getTabWidth, getViolations, init, log, log, process, setFileContents, setFileExtensions, setMessageDispatcher, setTabWidthMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChildMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.puppycrawl.tools.checkstyle.api.Configurable
configureMethods inherited from interface com.puppycrawl.tools.checkstyle.api.Contextualizable
contextualize
-
Field Details
-
format
Specify the format of the regular expression to match. -
message
Specify the message which is used to notify about violations, if empty then default (hard-coded) message is used. -
minimum
private int minimumSpecify the minimum number of matches required in each file. -
maximum
private int maximumSpecify the maximum number of matches required in each file. -
ignoreCase
private boolean ignoreCaseControl whether to ignore case when searching. -
matchAcrossLines
private boolean matchAcrossLinesControl whether to match expressions across multiple lines. -
detector
The detector to use.
-
-
Constructor Details
-
RegexpMultilineCheck
public RegexpMultilineCheck()
-
-
Method Details
-
beginProcessing
Description copied from interface:FileSetCheckCalled when about to be called to process a set of files.- Specified by:
beginProcessingin interfaceFileSetCheck- Overrides:
beginProcessingin classAbstractFileSetCheck- Parameters:
charset- the character set used to read the files.
-
processFiltered
Description copied from class:AbstractFileSetCheckCalled to process a file that matches the specified file extensions.- Specified by:
processFilteredin classAbstractFileSetCheck- Parameters:
file- the file to be processedfileText- the contents of the file.
-
getRegexCompileFlags
private int getRegexCompileFlags()Retrieves the compile flags for the regular expression being built based onmatchAcrossLines.- Returns:
- The compile flags.
-
setFormat
Setter to specify the format of the regular expression to match.- Parameters:
format- the format of the regular expression to match.
-
setMessage
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.
-
setMatchAcrossLines
public void setMatchAcrossLines(boolean matchAcrossLines) Setter to control whether to match expressions across multiple lines.- Parameters:
matchAcrossLines- whether to match expressions across multiple lines.
-