A check that makes sure 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.\A
.$
means the end of a line, as opposed to the end of the input.\Z
.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.
name | description | type | default value |
---|---|---|---|
format | pattern | Regular Expression | "$^" (empty) |
message | message which is used to notify about violations, if empty then the default (hard-coded) message is used. | String | "" (empty) |
illegalPattern | Controls whether the pattern is required or illegal. | Boolean | false |
duplicateLimit | Controls 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 errors will be logged. | Integer | 0 |
errorLimit | Controls the maximum number of errors before the check will abort. | Integer | 100 |
ignoreComments | Controls whether to ignore matches found within comments. | Boolean | false |
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.
The check.
Your statement may be multiline.
Then the check would be.
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:
It can also be useful to attach a meaningful message to the check:
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
:
You may want to make the above check ignore comments, like this:
An example of how to configure the check to find trailing whitespace at the end of a line:
An example of how to configure the check to find case-insensitive occurrences of "debug":
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 errors 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.
To use like RegexpHeader :
To configure the check to verify that each file starts with the following multiline header.
Note the following:
A more complex example. Note how the import and javadoc multilines are handled, there can be any number of them.
More examples:
The next 2 examples deal with the following example Java source file:
This checks for the presence of the header, the first 16 lines.
Note the following:
This checks for the presence of a copyright notice within the class javadoc, lines 24 to 37.
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.
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.regexp
A check for detecting that matches across multiple lines. Works with any file type.
Rationale: This check can be used to when the regular expression can be span multiple lines.
name | description | type | default value |
---|---|---|---|
format | illegal pattern | Regular Expression | "$." |
message | message which is used to notify about violations, if empty then default(hard-coded) message is used. | String | "" (empty) |
ignoreCase | Controls whether to ignore case when searching. | Boolean | false |
minimum | The minimum number of matches required in each file. | Integer | 0 |
maximum | The maximum number of matches required in each file. | Integer | 0 |
fileExtensions | file type extension of files to process | String Set | {} |
To configure the check to find calls to print to the console:
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.regexp
Implementation of a check that looks for a file name and/or path match (or mis-match) against specified patterns. It can also be used to verify files match specific naming patterns not covered by other checks (Ex: properties, xml, etc.).
When customizing the check, the properties are applied in a specific order. The fileExtensions property first picks only files that match any of the specific extensions supplied. Once files are matched against the fileExtensions, the match property is then used in conjunction with the patterns to determine if the check is looking for a match or mis-match on those files. If the fileNamePattern is supplied, the matching is only applied to the fileNamePattern and not the folderPattern. If no fileNamePattern is supplied, then matching is applied to the folderPattern only and will result in all files in a folder to be reported on violations. If no folderPattern is supplied, then all folders that checkstyle finds are examined for violations. The ignoreFileNameExtensions property drops the file extension and applies the fileNamePattern only to the rest of file name. For example, if the file is named 'test.java' and this property is turned on, the pattern is only applied to 'test'.
If this check is configured with no properties, then the default behavior of this check is to report file names with spaces in them. When at least one pattern property is supplied, the entire check is under the user's control to allow them to fully customize the behavior.
It is recommended that if you create your own pattern, to also specify a custom error message. This allows the error message printed to be clear what the violation is, especially if multiple RegexpOnFilename checks are used. Argument 0 for the message populates the check's folderPattern. Argument 1 for the message populates the check's fileNamePattern. The file name is not passed as an argument since it is part of CheckStyle's default error messages.
name | description | type | default value |
---|---|---|---|
folderPattern | Regular expression to match the folder path against. | Regular Expression | null |
fileNamePattern | Regular expression to match the file name against. | Regular Expression | null |
match | Whether to look for a match or mis-match on the file name, if the fileNamePattern is supplied, otherwise it is applied on the folderPattern. | Boolean | true |
ignoreFileNameExtensions | Whether to ignore the file extension for the file name match. | Boolean | false |
fileExtensions | File type extension of files to process. If this is specified, then only files that match these types are examined with the other patterns. | String Set | {} |
To configure the check to report file names that contain a space:
To configure the check to force picture files to not be 'gif':
OR:
To configure the check to only allow property and xml files to be located in the resource folder:
To configure the check to only allow Java and XML files in your folders use the below.
To configure the check to only allow Java and XML files only in your source folder
and ignore any other folders:
Note: 'folderPattern' must be specified if checkstyle is analyzing more than
the normal source folder, like the 'bin' folder where class files can be located.
To configure the check to only allow file names to be camel case:
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.regexp
A check for detecting single lines that match a supplied regular expression. Works with any file type.
Rationale: This check can be used to prototype checks and to
find common bad practice such as calling ex.printStacktrace()
,
System.out.println()
, System.exit()
, etc.
name | description | type | default value |
---|---|---|---|
format | illegal pattern | Regular Expression | "$." |
message | message which is used to notify about violations, if empty then default(hard-coded) message is used. | String | "" (empty) |
ignoreCase | Controls whether to ignore case when searching. | Boolean | false |
minimum | The minimum number of matches required in each file. | Integer | 0 |
maximum | The maximum number of matches required in each file. | Integer | 0 |
fileExtensions | file type extension of files to process | String Set | {} |
To configure the check to find trailing whitespace at the end of a line:
To configure the check to find trailing whitespace at the end of a line, with some slack of allowing two occurrences per file:
An example of how to configure the check to make sure a copyright statement is included in the file:
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.regexp
This class is variation on RegexpSingleline for detecting single lines that match a supplied regular expression in Java files. It supports suppressing matches in Java comments.
name | description | type | default value |
---|---|---|---|
format | illegal pattern | Regular Expression | "$." |
message | message which is used to notify about violations, if empty then default(hard-coded) message is used. | String | "" (empty) |
ignoreCase | Controls whether to ignore case when searching. | Boolean | false |
minimum | The minimum number of matches required in each file. | Integer | 0 |
maximum | The maximum number of matches required in each file. | Integer | 0 |
ignoreComments | Controls whether to ignore text in comments when searching. | Boolean | false |
To configure the check for calls to System.out.println
, except in comments:
To configure the check to find case-insensitive occurrences of "debug":
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.regexp