Class RegexpSinglelineCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
-
- com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
,FileSetCheck
public class RegexpSinglelineCheck extends AbstractFileSetCheck
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.-
Property
format
- Specify the format of the regular expression to match. Type isjava.lang.String
. Default value is"$."
. -
Property
message
- Specify the message which is used to notify about violations, if empty then default (hard-coded) message is used. Type isjava.lang.String
. Default value isnull
. -
Property
ignoreCase
- Control whether to ignore case when searching. Type isboolean
. Default value isfalse
. -
Property
minimum
- Specify the minimum number of matches required in each file. Type isint
. Default value is0
. -
Property
maximum
- Specify the maximum number of matches required in each file. Type isint
. Default value is0
. -
Property
fileExtensions
- Specify the file type extension of files to process. Type isjava.lang.String[]
. Default value is""
.
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;
Parent is
com.puppycrawl.tools.checkstyle.Checker
Violation Message Keys:
-
regexp.exceeded
-
regexp.minimum
- Since:
- 5.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Constructor Summary
Constructors Constructor Description RegexpSinglelineCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beginProcessing(java.lang.String charset)
Called when about to be called to process a set of files.protected void
processFiltered(java.io.File file, FileText fileText)
Called to process a file that matches the specified file extensions.void
setFormat(java.lang.String format)
Setter to specify the format of the regular expression to match.void
setIgnoreCase(boolean ignoreCase)
Setter to control whether to ignore case when searching.void
setMaximum(int maximum)
Setter to specify the maximum number of matches required in each file.void
setMessage(java.lang.String message)
Setter to specify the message which is used to notify about violations, if empty then default (hard-coded) message is used.void
setMinimum(int minimum)
Setter to specify the minimum number of matches required in each file.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
addMessages, destroy, finishProcessing, fireErrors, getFileContents, getFileExtensions, getMessageDispatcher, getMessages, getTabWidth, init, log, log, process, setFileContents, setFileExtensions, setMessageDispatcher, setTabWidth
-
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
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Configurable
configure
-
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Contextualizable
contextualize
-
-
-
-
Method Detail
-
beginProcessing
public void beginProcessing(java.lang.String charset)
Description copied from interface:FileSetCheck
Called when about to be called to process a set of files.- Specified by:
beginProcessing
in interfaceFileSetCheck
- Overrides:
beginProcessing
in classAbstractFileSetCheck
- Parameters:
charset
- the character set used to read the files.
-
processFiltered
protected void processFiltered(java.io.File file, FileText fileText)
Description copied from class:AbstractFileSetCheck
Called to process a file that matches the specified file extensions.- Specified by:
processFiltered
in classAbstractFileSetCheck
- Parameters:
file
- the file to be processedfileText
- the contents of the file.
-
setFormat
public void setFormat(java.lang.String format)
Setter to specify the format of the regular expression to match.- Parameters:
format
- the format of the regular expression to match.
-
setMessage
public void setMessage(java.lang.String message)
Setter to specify the message which is used to notify about violations, if empty then default (hard-coded) message is used.- Parameters:
message
- the message to report for a match.
-
setMinimum
public void setMinimum(int minimum)
Setter to specify the minimum number of matches required in each file.- Parameters:
minimum
- the minimum number of matches required in each file.
-
setMaximum
public void setMaximum(int maximum)
Setter to specify the maximum number of matches required in each file.- Parameters:
maximum
- the maximum number of matches required in each file.
-
setIgnoreCase
public void setIgnoreCase(boolean ignoreCase)
Setter to control whether to ignore case when searching.- Parameters:
ignoreCase
- whether to ignore case when searching.
-
-