Since Checkstyle 4.0
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.\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 | since |
---|---|---|---|---|
format | Specify the pattern to match against. | Pattern | "^$" |
4.0 |
message | Specify message which is used to notify about violations, if empty then the default (hard-coded) message is used. | String | null |
4.0 |
illegalPattern | Control whether the pattern is required or illegal. | boolean | false |
4.0 |
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. | int | 0 |
4.0 |
errorLimit | Specify the maximum number of violations before the check will abort. | int | 100 |
4.0 |
ignoreComments | Control whether to ignore matches found within comments. | boolean | false |
4.0 |
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:
// 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 PID
This checks for the presence of the header, the first 16 lines.
Note the following:
<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.
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
Since Checkstyle 5.0
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.
name | description | type | default value | since |
---|---|---|---|---|
format | Specify the format of the regular expression to match. | Regular Expression | "$." |
5.0 |
message | Specify the message which is used to notify about violations, if empty then default (hard-coded) message is used. | String | null |
5.0 |
ignoreCase | Control whether to ignore case when searching. | boolean | false |
5.0 |
minimum | Specify the minimum number of matches required in each file. | int | 0 |
5.0 |
maximum | Specify the maximum number of matches required in each file. | int | 0 |
5.0 |
matchAcrossLines | Control whether to match expressions across multiple lines. | boolean | false |
8.25 |
fileExtensions | Specify the file type extension of files to process. | String[] | all files |
5.0 |
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.
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
Since Checkstyle 6.15
Checks that a specified pattern matches based on file and/or folder path. 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 violation message. This allows the violation 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 violation messages.
name | description | type | default value | since |
---|---|---|---|---|
folderPattern | Specify the regular expression to match the folder path against. | Pattern | null |
6.15 |
fileNamePattern | Specify the regular expression to match the file name against. | Pattern | null |
6.15 |
match | Control 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 |
6.15 |
ignoreFileNameExtensions | Control whether to ignore the file extension for the file name match. | boolean | false |
6.15 |
fileExtensions | Specify the 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[] | all files |
6.15 |
To configure the check to report file names that contain a space:
<module name="RegexpOnFilename"/>
Example:
src/xdocs/config_regexp.xml //OK, contains no whitespace src/xdocs/"config regexp".xml //violation, contains whitespace
To configure the check to forbid 'gif' files in folders:
<module name="RegexpOnFilename"> <property name="fileNamePattern" value="\.gif$"/> </module>
Example:
src/site/resources/images/favicon.png //OK src/site/resources/images/logo.jpg //OK src/site/resources/images/groups.gif //violation, .gif images not allowed
To configure the check to forbid 'md' files except 'README.md file' in folders, with custom message:
<module name="RegexpOnFilename"> <property name="fileNamePattern" value="README"/> <property name="fileExtensions" value="md"/> <property name="match" value="false"/> <message key="regexp.filename.mismatch" value="No '*.md' files other then 'README.md'"/> </module>
Example:
src/site/resources/README.md //OK src/site/resources/Logo.png //OK src/site/resources/Text.md //violation, .md files other than 'README.md' are not allowed
To configure the check to only allow property and xml files to be located in the resource folder:
<module name="RegexpOnFilename"> <property name="folderPattern" value="[\\/]src[\\/]\w+[\\/]resources[\\/]"/> <property name="match" value="false"/> <property name="fileExtensions" value="properties, xml"/> </module>
Example:
src/main/resources/sun_checks.xml //OK src/main/resources/check_properties.properties //OK src/main/resources/JavaClass.java //violation, xml|property files are allowed in resource folder
To configure the check to only allow Java and XML files in your folders use the below.
<module name="RegexpOnFilename"> <property name="fileNamePattern" value="\.(java|xml)$"/> <property name="match" value="false"/> </module>
Example:
src/main/java/JavaClass.java //OK src/main/MainClass.java //OK src/main/java/java_xml.xml //OK src/main/main_xml.xml //OK src/main/java/image.png //violation, folders should only contain java or xml files src/main/check_properties.properties //violation, folders should only contain java or xml files
To configure the check to only allow Java and XML files only in your source folder and ignore any other folders:
<module name="RegexpOnFilename"> <property name="folderPattern" value="[\\/]src[\\/]"/> <property name="fileNamePattern" value="\.(java|xml)$"/> <property name="match" value="false"/> </module>
Example:
src/SourceClass.java //OK src/source_xml.xml //OK src/image.png //violation, only java and xml files are allowed in src folder src/main/main_properties.properties //OK, this check only applies to src folder
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:
<module name="RegexpOnFilename"> <property name="fileNamePattern" value="^([A-Z][a-z0-9]+\.?)+$"/> <property name="match" value="false"/> <property name="ignoreFileNameExtensions" value="true"/> </module>
Example:
src/main/java/JavaClass.java //OK src/main/MainClass.java //OK src/main/java/java_class.java //violation, file names should be in Camel Case src/main/main_class.java //violation, file names should be in 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
Since Checkstyle 5.0
Checks that a specified pattern matches a single line in 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 | since |
---|---|---|---|---|
format | Specify the format of the regular expression to match. | Regular Expression | "$." |
5.0 |
message | Specify the message which is used to notify about violations, if empty then default (hard-coded) message is used. | String | null |
5.0 |
ignoreCase | Control whether to ignore case when searching. | boolean | false |
5.0 |
minimum | Specify the minimum number of matches required in each file. | int | 0 |
5.0 |
maximum | Specify the maximum number of matches required in each file. | int | 0 |
5.0 |
fileExtensions | Specify the file type extension of files to process. | String[] | all files |
5.0 |
To configure the default check:
<module name="RegexpSingleline" />
This configuration does not match to anything, so we do not provide any code example for it as no violation will ever be reported.
To configure the check to find occurrences of 'System.exit(' with some slack of allowing only one occurrence per file:
<module name="RegexpSingleline"> <property name="format" value="System.exit\("/> <!-- next line not required as 0 is the default --> <property name="minimum" value="0"/> <property name="maximum" value="1"/> </module>
Example:
class MyClass { void myFunction() { try { doSomething(); } catch (Exception e) { System.exit(1); // OK, as only there is only one occurrence. } } void doSomething(){}; }
class MyClass { void myFunction() { try { doSomething(); System.exit(0); } catch (Exception e) { System.exit(1); // Violation, as there are more than one occurrence. } } void doSomething(){}; }
An example of how to configure the check to make sure a copyright statement is included in the file:
<module name="RegexpSingleline"> <property name="format" value="This file is copyrighted"/> <property name="minimum" value="1"/> <!-- Need to specify a maximum, so 10 times is more than enough. --> <property name="maximum" value="10"/> </module>
Example:
/** * This file is copyrighted under CC. // Ok, as the file contains a copyright statement. */ class MyClass { }
/** // violation, as the file doesn't contain a copyright statement. * MyClass as a configuration example. */ class MyClass { }
An example of how to configure the check to make sure sql files contains the term 'license'.
<module name="RegexpSingleline"> <property name="format" value="license"/> <property name="minimum" value="1"/> <property name="maximum" value="9999"/> <property name="ignoreCase" value="true"/> <!-- Configure a message to be shown on violation of the Check. --> <property name="message" value="File must contain at least one occurrence of 'license' term"/> <!-- Perform the Check only on files with java extension. --> <property name="fileExtensions" value="sql"/> </module>
Example:
/* AP 2.0 License. // Ok, Check ignores the case of the term. */ CREATE DATABASE MyDB;
/* // violation, file doesn't contain the term. Example sql file. */ CREATE DATABASE MyDB;
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
Since Checkstyle 6.0
Checks that a specified pattern matches a single line in Java files.
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 | since |
---|---|---|---|---|
format | Specify the format of the regular expression to match. | Regular Expression | "$." |
5.0 |
message | Specify the message which is used to notify about violations, if empty then default (hard-coded) message is used. | String | null |
6.0 |
ignoreCase | Control whether to ignore case when searching. | boolean | false |
5.0 |
minimum | Specify the minimum number of matches required in each file. | int | 0 |
5.0 |
maximum | Specify the maximum number of matches required in each file. | int | 0 |
5.0 |
ignoreComments | Control whether to ignore text in comments when searching. | boolean | false |
5.0 |
To configure the default check:
<module name="RegexpSinglelineJava"/>
This configuration does not match to anything, so we do not provide any code example for it as no violation will ever be reported.
To configure the check for calls to System.out.println
, except in comments:
<module name="RegexpSinglelineJava"> <!-- . matches any character, so we need to escape it and use \. to match dots. --> <property name="format" value="System\.out\.println"/> <property name="ignoreComments" value="true"/> </module>
Example:
System.out.println(""); // violation, instruction matches illegal pattern System.out. println(""); // OK /* System.out.println */ // OK, comments are ignored
To configure the check to find case-insensitive occurrences of "debug":
<module name="RegexpSinglelineJava"> <property name="format" value="debug"/> <property name="ignoreCase" value="true"/> </module>
Example:
int debug = 0; // violation, variable name matches illegal pattern public class Debug { // violation, class name matches illegal pattern /* this is for de bug only; */ // OK
To configure the check to find occurrences of "\.read(.*)|\.write(.*)" and display "IO found" for each violation.
<module name="RegexpSinglelineJava"> <property name="format" value="\.read(.*)|\.write(.*)"/> <property name="message" value="IO found"/> </module>
Example:
FileReader in = new FileReader("path/to/input"); int ch = in.read(); // violation while(ch != -1) { System.out.print((char)ch); ch = in.read(); // violation } FileWriter out = new FileWriter("path/to/output"); out.write("something"); // violation
To configure the check to find occurrences of "\.log(.*)". We want to allow a maximum of 2 occurrences.
<module name="RegexpSinglelineJava"> <property name="format" value="\.log(.*)"/> <property name="maximum" value="2"/> </module>
Example:
public class Foo{ public void bar(){ Logger.log("first"); // OK, first occurrence is allowed Logger.log("second"); // OK, second occurrence is allowed Logger.log("third"); // violation System.out.println("fourth"); Logger.log("fifth"); // violation } }
To configure the check to find all occurrences of "public". We want to ignore comments, display "public member found" for each violation and say if less than 2 occurrences.
<module name="RegexpSinglelineJava"> <property name="format" value="public"/> <property name="minimum" value="2"/> <property name="message" value="public member found"/> <property name="ignoreComments" value="true"/> </module>
Example:
class Foo{ // violation, file contains less than 2 occurrences of "public" private int a; /* public comment */ // OK, comment is ignored private void bar1() {} public void bar2() {} // violation }
Example:
class Foo{ private int a; /* public comment */ // OK, comment is ignored public void bar1() {} // violation public void bar2() {} // violation }
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