Class RegexpCheck
- All Implemented Interfaces:
Configurable,Contextualizable
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.
Note: To search for parentheses () in a regular expression you must escape them like \(\). This is required by the regexp engine, otherwise it will think they are special instruction characters.
Note: To search for things that mean something in XML, like < you need to escape them like <. This is required so the XML parser does not act on them, but instead passes the correct character to the regexp engine.
- Since:
- 4.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanBoolean to say if we should check for duplicates.private static final intDefault duplicate limit.private static final intDefault error report limit.private intControl 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 final StringError count exceeded message.private intSpecify the maximum number of violations before the check will abort.private PatternSpecify the pattern to match against.private booleanControl whether to ignore matches found within comments.private booleanControl whether the pattern is required or illegal.private StringSpecify message which is used to notify about violations, if empty then the default (hard-coded) message is used.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCalled before the starting to process a tree.int[]The configurable token set.int[]Returns the default token a check is interested in.private StringgetMessage(int errorCount) Provide right message.int[]The tokens that this check must be registered for.private booleanisIgnore(int startLine, FileText text, LineColumn start, Matcher matcher) Detect ignore situation.private voidlogMessage(int lineNumber, int errorCount) Displays the right message.private voidProcesses the regexp matches and logs the number of errors in the file.voidsetDuplicateLimit(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.voidsetErrorLimit(int errorLimit) Setter to specify the maximum number of violations before the check will abort.final voidSetter to specify the pattern to match against.voidsetIgnoreComments(boolean ignoreComments) Setter to control whether to ignore matches found within comments.voidsetIllegalPattern(boolean illegalPattern) Setter to control whether the pattern is required or illegal.voidsetMessage(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, visitTokenMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_ILLEGAL_REGEXP
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_REQUIRED_REGEXP
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_DUPLICATE_REGEXP
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
DEFAULT_DUPLICATE_LIMIT
Default duplicate limit.- See Also:
-
DEFAULT_ERROR_LIMIT
Default error report limit.- See Also:
-
ERROR_LIMIT_EXCEEDED_MESSAGE
Error count exceeded message.- See Also:
-
message
Specify message which is used to notify about violations, if empty then the default (hard-coded) message is used. -
ignoreComments
Control whether to ignore matches found within comments. -
illegalPattern
Control whether the pattern is required or illegal. -
errorLimit
Specify the maximum number of violations before the check will abort. -
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
Boolean to say if we should check for duplicates. -
format
Specify the pattern to match against.
-
-
Constructor Details
-
RegexpCheck
public RegexpCheck()
-
-
Method Details
-
setMessage
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
Setter to control whether to ignore matches found within comments.- Parameters:
ignoreComments- True if comments should be ignored.- Since:
- 4.0
-
setIllegalPattern
Setter to control whether the pattern is required or illegal.- Parameters:
illegalPattern- True if pattern is not allowed.- Since:
- 4.0
-
setErrorLimit
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
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
Setter to specify the pattern to match against.- Parameters:
pattern- the new pattern- Since:
- 4.0
-
getDefaultTokens
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe 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:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
beginTree
Description copied from class:AbstractCheckCalled before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTreein classAbstractCheck- Parameters:
rootAST- the root of the tree
-
processRegexpMatches
Processes the regexp matches and logs the number of errors in the file. -
isIgnore
Detect ignore situation.- Parameters:
startLine- position of linetext- file textstart- line columnmatcher- The matcher- Returns:
- true is that need to be ignored
-
logMessage
Displays the right message.- Parameters:
lineNumber- the line number the message relates to.errorCount- number of errors in the file.
-
getMessage
Provide right message.- Parameters:
errorCount- number of errors in the file.- Returns:
- message for violation.
-