public class RegexpMultilineCheck extends AbstractFileSetCheck
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.
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
.
matchAcrossLines
- Control whether to match expressions
across multiple lines.
Type is boolean
.
Default value is false
.
fileExtensions
- Specify the file type extension of files to process.
Type is java.lang.String[]
.
Default value is all files
.
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>
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 of violation from the above config:
void method() { System.out. // violation print("Example"); System.out. print("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 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
AutomaticBean.OutputStreamOptions
Constructor and Description |
---|
RegexpMultilineCheck() |
Modifier and Type | Method and Description |
---|---|
void |
beginProcessing(String charset)
Called when about to be called to process a set of files.
|
protected void |
processFiltered(File file,
FileText fileText)
Called to process a file that matches the specified file extensions.
|
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 |
setMatchAcrossLines(boolean matchAcrossLines)
Setter to control whether to match expressions across multiple lines.
|
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.
|
addMessages, destroy, finishProcessing, fireErrors, getFileContents, getFileExtensions, getMessageDispatcher, getMessages, getTabWidth, init, log, log, process, setFileContents, setFileExtensions, setMessageDispatcher, setTabWidth
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
configure
contextualize
public RegexpMultilineCheck()
public void beginProcessing(String charset)
FileSetCheck
beginProcessing
in interface FileSetCheck
beginProcessing
in class AbstractFileSetCheck
charset
- the character set used to read the files.protected void processFiltered(File file, FileText fileText)
AbstractFileSetCheck
processFiltered
in class AbstractFileSetCheck
file
- the file to be processedfileText
- the contents of the file.public 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 setMatchAcrossLines(boolean matchAcrossLines)
matchAcrossLines
- whether to match expressions across multiple lines.Copyright © 2001–2020. All rights reserved.