Class RegexpCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
public class RegexpCheck extends AbstractCheck
Checks that a specified pattern exists, exists less than a set number of times, or does not exist in the file.
This check combines all the functionality provided by RegexpHeader except supplying the regular expression from a file.
It differs from them in that it works in multiline mode. Its regular expression can span multiple lines and it checks this against the whole file at once. The others work in single-line mode. Their single or multiple regular expressions can only span one line. They check each of these against each line in the file in turn.
Note: Because of the different mode of operation there may be some changes in the regular expressions used to achieve a particular end.
In multiline mode...
-
^
means the beginning of a line, as opposed to beginning of the input. -
For beginning of the input use
\A
. -
$
means the end of a line, as opposed to the end of the input. -
For end of input use
\Z
. - Each line in the file is terminated with a line feed character.
Note: Not all regular expression engines are created equal. Some provide extra functions that others do not and some elements of the syntax may vary. This check makes use of the java.util.regex package; please check its documentation for details of how to construct a regular expression to achieve a particular goal.
Note: When entering a regular expression as a parameter in the XML config file you must also take into account the XML rules. e.g. if you want to match a < symbol you need to enter <. The regular expression should be entered on one line.
-
Property
duplicateLimit
- Control whether to check for duplicates of a required pattern, any negative value means no checking for duplicates, any positive value is used as the maximum number of allowed duplicates, if the limit is exceeded violations will be logged. Type isint
. Default value is0
. -
Property
errorLimit
- Specify the maximum number of violations before the check will abort. Type isint
. Default value is100
. -
Property
format
- Specify the pattern to match against. Type isjava.util.regex.Pattern
. Default value is"^$"
. -
Property
ignoreComments
- Control whether to ignore matches found within comments. Type isboolean
. Default value isfalse
. -
Property
illegalPattern
- Control whether the pattern is required or illegal. Type isboolean
. Default value isfalse
. -
Property
message
- Specify message which is used to notify about violations, if empty then the default (hard-coded) message is used. Type isjava.lang.String
. Default value isnull
.
Parent is
com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
duplicate.regexp
-
illegal.regexp
-
required.regexp
- Since:
- 4.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
checkForDuplicates
Boolean to say if we should check for duplicates.private static int
DEFAULT_DUPLICATE_LIMIT
Default duplicate limit.private static int
DEFAULT_ERROR_LIMIT
Default error report limit.private int
duplicateLimit
Control whether to check for duplicates of a required pattern, any negative value means no checking for duplicates, any positive value is used as the maximum number of allowed duplicates, if the limit is exceeded violations will be logged.private static String
ERROR_LIMIT_EXCEEDED_MESSAGE
Error count exceeded message.private int
errorCount
Tracks number of errors.private int
errorLimit
Specify the maximum number of violations before the check will abort.private Pattern
format
Specify the pattern to match against.private boolean
ignoreComments
Control whether to ignore matches found within comments.private boolean
illegalPattern
Control whether the pattern is required or illegal.private int
matchCount
Tracks number of matches made.private Matcher
matcher
The matcher.private String
message
Specify message which is used to notify about violations, if empty then the default (hard-coded) message is used.static String
MSG_DUPLICATE_REGEXP
A key is pointing to the warning message text in "messages.properties" file.static String
MSG_ILLEGAL_REGEXP
A key is pointing to the warning message text in "messages.properties" file.static String
MSG_REQUIRED_REGEXP
A key is pointing to the warning message text in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description RegexpCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beginTree(DetailAST rootAST)
Called before the starting to process a tree.private boolean
canContinueValidation(boolean ignore)
Check if we can stop validation.private void
findMatch()
Recursive method that finds the matches.int[]
getAcceptableTokens()
The configurable token set.int[]
getDefaultTokens()
Returns the default token a check is interested in.private String
getMessage()
Provide right message.int[]
getRequiredTokens()
The tokens that this check must be registered for.private boolean
isIgnore(int startLine, FileText text, LineColumn start)
Detect ignore situation.private void
logMessage(int lineNumber)
Displays the right message.void
setDuplicateLimit(int duplicateLimit)
Setter to control whether to check for duplicates of a required pattern, any negative value means no checking for duplicates, any positive value is used as the maximum number of allowed duplicates, if the limit is exceeded violations will be logged.void
setErrorLimit(int errorLimit)
Setter to specify the maximum number of violations before the check will abort.void
setFormat(Pattern pattern)
Setter to specify the pattern to match against.void
setIgnoreComments(boolean ignoreComments)
Setter to control whether to ignore matches found within comments.void
setIllegalPattern(boolean illegalPattern)
Setter to control whether the pattern is required or illegal.void
setMessage(String message)
Setter to specify message which is used to notify about violations, if empty then the default (hard-coded) message is used.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens, visitToken
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_ILLEGAL_REGEXP
public static final String MSG_ILLEGAL_REGEXP
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_REQUIRED_REGEXP
public static final String MSG_REQUIRED_REGEXP
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_DUPLICATE_REGEXP
public static final String MSG_DUPLICATE_REGEXP
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
DEFAULT_DUPLICATE_LIMIT
private static final int DEFAULT_DUPLICATE_LIMIT
Default duplicate limit.- See Also:
- Constant Field Values
-
DEFAULT_ERROR_LIMIT
private static final int DEFAULT_ERROR_LIMIT
Default error report limit.- See Also:
- Constant Field Values
-
ERROR_LIMIT_EXCEEDED_MESSAGE
private static final String ERROR_LIMIT_EXCEEDED_MESSAGE
Error count exceeded message.- See Also:
- Constant Field Values
-
message
private String message
Specify message which is used to notify about violations, if empty then the default (hard-coded) message is used.
-
ignoreComments
private boolean ignoreComments
Control whether to ignore matches found within comments.
-
illegalPattern
private boolean illegalPattern
Control whether the pattern is required or illegal.
-
errorLimit
private int errorLimit
Specify the maximum number of violations before the check will abort.
-
duplicateLimit
private int duplicateLimit
Control whether to check for duplicates of a required pattern, any negative value means no checking for duplicates, any positive value is used as the maximum number of allowed duplicates, if the limit is exceeded violations will be logged.
-
checkForDuplicates
private boolean checkForDuplicates
Boolean to say if we should check for duplicates.
-
matchCount
private int matchCount
Tracks number of matches made.
-
errorCount
private int errorCount
Tracks number of errors.
-
-
Constructor Detail
-
RegexpCheck
public RegexpCheck()
-
-
Method Detail
-
setMessage
public void setMessage(String message)
Setter to specify message which is used to notify about violations, if empty then the default (hard-coded) message is used.- Parameters:
message
- custom message which should be used in report.- Since:
- 4.0
-
setIgnoreComments
public void setIgnoreComments(boolean ignoreComments)
Setter to control whether to ignore matches found within comments.- Parameters:
ignoreComments
- True if comments should be ignored.- Since:
- 4.0
-
setIllegalPattern
public void setIllegalPattern(boolean illegalPattern)
Setter to control whether the pattern is required or illegal.- Parameters:
illegalPattern
- True if pattern is not allowed.- Since:
- 4.0
-
setErrorLimit
public void setErrorLimit(int errorLimit)
Setter to specify the maximum number of violations before the check will abort.- Parameters:
errorLimit
- the number of errors to report.- Since:
- 4.0
-
setDuplicateLimit
public void setDuplicateLimit(int duplicateLimit)
Setter to control whether to check for duplicates of a required pattern, any negative value means no checking for duplicates, any positive value is used as the maximum number of allowed duplicates, if the limit is exceeded violations will be logged.- Parameters:
duplicateLimit
- negative values mean no duplicate checking, any positive value is used as the limit.- Since:
- 4.0
-
setFormat
public final void setFormat(Pattern pattern)
Setter to specify the pattern to match against.- Parameters:
pattern
- the new pattern- Since:
- 4.0
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
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 classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered 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 classAbstractCheck
- Parameters:
rootAST
- the root of the tree
-
findMatch
private void findMatch()
Recursive method that finds the matches.
-
canContinueValidation
private boolean canContinueValidation(boolean ignore)
Check if we can stop validation.- Parameters:
ignore
- flag- Returns:
- true is we can continue
-
isIgnore
private boolean isIgnore(int startLine, FileText text, LineColumn start)
Detect ignore situation.- Parameters:
startLine
- position of linetext
- file textstart
- line column- Returns:
- true is that need to be ignored
-
logMessage
private void logMessage(int lineNumber)
Displays the right message.- Parameters:
lineNumber
- the line number the message relates to.
-
getMessage
private String getMessage()
Provide right message.- Returns:
- message for violation.
-
-