RegexpSingleline
Since Checkstyle 5.0
Description
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.
Properties
| name | description | type | default value | since |
|---|---|---|---|---|
| fileExtensions | Specify the file extensions of the files to process. | String[] | all files |
5.0 |
| format | Specify the format of the regular expression to match. | Pattern | $. |
5.0 |
| ignoreCase | Control whether to ignore case when searching. | boolean | false |
5.0 |
| maximum | Specify the maximum number of matches required in each file. | int | 0 |
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 |
| minimum | Specify the minimum number of matches required in each file. | int | 0 |
5.0 |
Examples
To configure the default check:
<module name="Checker">
<module name="RegexpSingleline" />
</module>
Example:
/**
* This file is copyrighted under CC.
*/
public class Example1 {
void myFunction() {
try {
doSomething();
System.exit(0);
} catch (Exception e) {
System.exit(1);
}
}
void doSomething() {}
}
To configure the check to find occurrences of 'System.exit(' with some slack of allowing only one occurrence per file:
<module name="Checker">
<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>
</module>
Example:
/**
* This file is copyrighted under CC.
*/
public class Example2 {
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="Checker">
<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>
</module>
Example:
/**
* This file is copyrighted under CC.
*/
public class Example4 {
void myFunction() {
try {
doSomething();
System.exit(0);
} catch (Exception e) {
System.exit(1);
}
}
void doSomething() {}
}
An example of how to configure the check to make sure sql files contains the term 'license'.
<module name="Checker">
<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 SQL extension. -->
<property name="fileExtensions" value="sql"/>
</module>
</module>
Example:
/**
* This file is copyrighted under CC.
*/
public class Example6 {
void myFunction() {
try {
doSomething();
System.exit(0);
} catch (Exception e) {
System.exit(1);
}
}
void doSomething() {}
}
Example:
/*
AP 2.0 License. // ok, Check ignores the case of the term.
*/
CREATE DATABASE MyDB;
An example of how to configure the check to make sure sql files contains the term 'license'.
<module name="Checker">
<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 SQL extension. -->
<property name="fileExtensions" value="sql"/>
</module>
</module>
Example with violation:
/**
* This file is copyrighted under CC.
*/
public class Example7 {
void myFunction() {
try {
doSomething();
System.exit(0);
} catch (Exception e) {
System.exit(1);
}
}
void doSomething() {}
}
Example with violation:
/* // violation, file doesn't contain the term.
Example sql file.
*/
CREATE DATABASE MyDB;
This configuration does not match to anything, so we do not provide any code example for it as no violation will ever be reported.
Use Cases
<module name="Checker">
<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>
</module>
Example with violation:
/**
* This file is copyrighted under CC.
*/
public class UseCase1 {
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="Checker">
<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"/>
<property name="message"
value="File must contain copyright statement"/>
</module>
</module>
Example with violation:
/**
* MyClass as a configuration example.
*/
public class UseCase2 {
void myFunction() {
try {
doSomething();
System.exit(0);
} catch (Exception e) {
System.exit(1);
}
}
void doSomething() {}
}
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Fully Qualified Name
com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck
Use this fully qualified class name in configuration when an exact class reference is required.






