Class RegexpCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- 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 singleline 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
format- Specify the pattern to match against. Type isjava.util.regex.Pattern. Default value is"^$". -
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. -
Property
illegalPattern- Control whether the pattern is required or illegal. Type isboolean. Default value isfalse. -
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
ignoreComments- Control whether to ignore matches found within comments. Type isboolean. Default value isfalse.
To configure the check:
The following examples are mainly copied from the other 3 checks mentioned above, to show how the same results can be achieved using this check in place of them.
To use like Required Regexp check:
An example of how to configure the check to make sure a copyright statement is included in the file:
The statement.
// This code is copyrighted
The check.
<module name="Regexp"> <property name="format" value="// This code is copyrighted"/> </module>
Your statement may be multiline.
// This code is copyrighted // (c) MyCompany
Then the check would be.
<module name="Regexp"> <property name="format" value="// This code is copyrighted\n// \(c\) MyCompany"/> </module>
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.
And to make sure it appears only once:
<module name="Regexp"> <property name="format" value="// This code is copyrighted\n// \(c\) MyCompany"/> <property name="duplicateLimit" value="0"/> </module>
It can also be useful to attach a meaningful message to the check:
<module name="Regexp"> <property name="format" value="// This code is copyrighted\n// \(c\) MyCompany"/> <property name="message" value="Copyright"/> </module>
To use like illegal regexp check:
An example of how to configure the check to make sure there are no calls to
System.out.println:<module name="Regexp"> <!-- . matches any character, so we need to escape it and use \. to match dots. --> <property name="format" value="System\.out\.println"/> <property name="illegalPattern" value="true"/> </module>
You may want to make the above check ignore comments, like this:
<module name="Regexp"> <property name="format" value="System\.out\.println"/> <property name="illegalPattern" value="true"/> <property name="ignoreComments" value="true"/> </module>
An example of how to configure the check to find trailing whitespace at the end of a line:
<module name="Regexp"> <property name="format" value="[ \t]+$"/> <property name="illegalPattern" value="true"/> <property name="message" value="Trailing whitespace"/> </module>
An example of how to configure the check to find case-insensitive occurrences of "debug":
<module name="Regexp"> <property name="format" value="(?i)debug"/> <property name="illegalPattern" value="true"/> </module>
Note: The (?i) at the beginning of the regular expression tells the regexp engine to ignore the case.
There is also a feature to limit the number of violations reported. When the limit is reached the check aborts with a message reporting that the limit has been reached. The default limit setting is 100, but this can be change as shown in the following example.
<module name="Regexp"> <property name="format" value="(?i)debug"/> <property name="illegalPattern" value="true"/> <property name="errorLimit" value="1000"/> </module>
To use like RegexpHeader:
To configure the check to verify that each file starts with the following multiline header.
Note the following:
- \A means the start of the file.
- The date can be any 4 digit number.
// Copyright (C) 2004 MyCompany // All rights reserved
<module name="Regexp"> <property name="format" value="\A// Copyright \(C\) \d\d\d\d MyCompany\n// All rights reserved"/> </module>A more complex example. Note how the import and javadoc multilines are handled, there can be any number of them.
/////////////////////////////////////////////////////////////////////// // checkstyle: // Checks Java source code for adherence to a set of rules. // Copyright (C) 2004 Oliver Burn // Last modification by $Author A.N.Other$ /////////////////////////////////////////////////////////////////////// package com.puppycrawl.checkstyle; import java.util.thing1; import java.util.thing2; import java.util.thing3; /** * javadoc line 1 * javadoc line 2 * javadoc line 3 */
<module name="Regexp"> <property name="format" value="\A/{71}\n// checkstyle:\n// Checks Java source code for adherence to a set of rules\.\n// Copyright \(C\) \d\d\d\d Oliver Burn\n // Last modification by \$Author.*\$\n/{71}\n\npackage [\w\.]*;\n\n (import [\w\.]*;\n)*\n/\*\*\n( \*[^/]*\n)* \*/"/> </module>More examples:
The next 2 examples deal with the following example Java source file:
/* * PID.java * * Copyright (c) 2001 ACME * 123 Some St. * Somewhere. * * This software is the confidential and proprietary information of ACME. * ("Confidential Information"). You shall not disclose such * Confidential Information and shall use it only in accordance with * the terms of the license agreement you entered into with ACME. * * $Log: config_misc.xml,v $ * Revision 1.7 2007/01/16 12:16:35 oburn * Removing all reference to mailing lists * * Revision 1.6 2005/12/25 16:13:10 o_sukhodolsky * Fix for rfe 1248106 (TYPECAST is now accepted by NoWhitespaceAfter) * * Fix for rfe 953266 (thanks to Paul Guyot (pguyot) for submitting patch) * IllegalType can be configured to accept some abstract classes which * matches to regexp of illegal type names (property legalAbstractClassNames) * * TrailingComment now can be configured to accept some trailing comments * (such as NOI18N) (property legalComment, rfe 1385344). * * Revision 1.5 2005/11/06 11:54:12 oburn * Incorporate excellent patch [ 1344344 ] Consolidation of regexp checks. * * Revision 1.3.8.1 2005/10/11 14:26:32 someone * Fix for bug 251. The broken bit is fixed */ package com.acme.tools; import com.acme.thing1; import com.acme.thing2; import com.acme.thing3; /** * * <P> * <I>This software is the confidential and proprietary information of * ACME (<B>"Confidential Information"</B>). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with ACME.</I> * </P> * * © copyright 2002 ACME * * @author Some Body */ public class PID extends StateMachine implements WebObject.Constants { /** javadoc. */ public static final int A_SETPOINT = 1; . . . } // class PIDThis checks for the presence of the header, the first 16 lines.
Note the following:
- Line 2 and 13 contain the file name. These are checked to make sure they are the same, and that they match the class name.
- The date can be any 4 digit number.
<module name="Regexp"> <property name="format" value="\A/\*\n \* (\w*)\.java\n \*\n \* Copyright \(c\) \d\d\d\d ACME\n \* 123 Some St\.\n \* Somewhere\.\n \*\n \* This software is the confidential and proprietary information of ACME\.\n \* \("Confidential Information"\)\. You shall not disclose such\n \* Confidential Information and shall use it only in accordance with\n \* the terms of the license agreement you entered into with ACME\.\n \*\n \* \$Log: config_misc\.xml,v $ \* Revision 1\.7 2007/01/16 12:16:35 oburn \* Removing all reference to mailing lists \* \ \* Revision 1.6 2005/12/25 16:13:10 o_sukhodolsky \* Fix for rfe 1248106 \(TYPECAST is now accepted by NoWhitespaceAfter\) \* \ \* Fix for rfe 953266 \(thanks to Paul Guyot \(pguyot\) for submitting patch\) \* IllegalType can be configured to accept some abstract classes which \* matches to regexp of illegal type names \(property legalAbstractClassNames\) \* \* TrailingComment now can be configured to accept some trailing comments \* \(such as NOI18N\) \(property legalComment, rfe 1385344\). \* \* Revision 1.5 2005/11/06 11:54:12 oburn \* Incorporate excellent patch \[ 1344344 \] Consolidation of regexp checks. \* \\n(.*\n)*([\w|\s]*( class | interface )\1)"/> <property name="message" value="Correct header not found"/> </module>This checks for the presence of a copyright notice within the class javadoc, lines 24 to 37.
<module name="Regexp"> <property name="format" value="(/\*\*\n)( \*.*\n)*( \* <P>\n \* <I> This software is the confidential and proprietary information of\n \* ACME \(<B>"Confidential Information"</B> \)\. You shall not\n \* disclose such Confidential Information and shall use it only in\n \* accordance with the terms of the license agreement you entered into\n \* with ACME\.</I>\n \* </P>\n \*\n \* © copyright \d\d\d\d ACME\n \*\n \* @author .*)(\n\s\*.*)*/\n[\w|\s]*( class | interface )"/> <property name="message" value="Copyright in class/interface Javadoc"/> <property name="duplicateLimit" value="0"/> </module>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.
Parent is
com.puppycrawl.tools.checkstyle.TreeWalkerViolation Message Keys:
-
duplicate.regexp -
illegal.regexp -
required.regexp
- Since:
- 4.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private booleancheckForDuplicatesBoolean to say if we should check for duplicates.private static intDEFAULT_DUPLICATE_LIMITDefault duplicate limit.private static intDEFAULT_ERROR_LIMITDefault error report limit.private intduplicateLimitControl 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 java.lang.StringERROR_LIMIT_EXCEEDED_MESSAGEError count exceeded message.private interrorCountTracks number of errors.private interrorLimitSpecify the maximum number of violations before the check will abort.private java.util.regex.PatternformatSpecify the pattern to match against.private booleanignoreCommentsControl whether to ignore matches found within comments.private booleanillegalPatternControl whether the pattern is required or illegal.private intmatchCountTracks number of matches made.private java.util.regex.MatchermatcherThe matcher.private java.lang.StringmessageSpecify message which is used to notify about violations, if empty then the default (hard-coded) message is used.static java.lang.StringMSG_DUPLICATE_REGEXPA key is pointing to the warning message text in "messages.properties" file.static java.lang.StringMSG_ILLEGAL_REGEXPA key is pointing to the warning message text in "messages.properties" file.static java.lang.StringMSG_REQUIRED_REGEXPA 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 voidbeginTree(DetailAST rootAST)Called before the starting to process a tree.private booleancanContinueValidation(boolean ignore)Check if we can stop validation.private voidfindMatch()Recursive method that finds the matches.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.private booleanisIgnore(int startLine, FileText text, LineColumn start)Detect ignore situation.private voidlogMessage(int lineNumber)Displays the right message.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.voidsetFormat(java.util.regex.Pattern pattern)Setter 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(java.lang.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, 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.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_ILLEGAL_REGEXP
public static final java.lang.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 java.lang.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 java.lang.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 java.lang.String ERROR_LIMIT_EXCEEDED_MESSAGE
Error count exceeded message.- See Also:
- Constant Field Values
-
message
private java.lang.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.
-
format
private java.util.regex.Pattern format
Specify the pattern to match against.
-
matcher
private java.util.regex.Matcher matcher
The matcher.
-
-
Constructor Detail
-
RegexpCheck
public RegexpCheck()
-
-
Method Detail
-
setMessage
public void setMessage(java.lang.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.
-
setIgnoreComments
public void setIgnoreComments(boolean ignoreComments)
Setter to control whether to ignore matches found within comments.- Parameters:
ignoreComments- True if comments should be ignored.
-
setIllegalPattern
public void setIllegalPattern(boolean illegalPattern)
Setter to control whether the pattern is required or illegal.- Parameters:
illegalPattern- True if pattern is not allowed.
-
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.
-
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.
-
setFormat
public final void setFormat(java.util.regex.Pattern pattern)
Setter to specify the pattern to match against.- Parameters:
pattern- the new pattern
-
getDefaultTokens
public int[] 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:
TokenTypes
-
getAcceptableTokens
public int[] 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:
TokenTypes
-
getRequiredTokens
public int[] 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:
TokenTypes
-
beginTree
public void beginTree(DetailAST rootAST)
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
-
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.
-
-