BeforeExecutionExclusionFileFilter
Since Checkstyle 7.2
Description
BeforeExecutionExclusionFileFilter
decides which files should be
excluded from being processed by the utility.
By default, Checkstyle includes all files and subdirectories in a directory to be
processed and checked for violations. Users could have files that are in these
subdirectories that shouldn't be processed with their checkstyle configuration for
various reasons, one of which is a valid Java file that won't pass Checkstyle's parser.
When Checkstyle tries to parse a Java file and fails, it will throw an
Exception
and halt parsing any more files for violations. An example of a
valid Java file Checkstyle can't parse is JDK 9's module-info.java
.
This file filter will exclude these problem files from being parsed,
allowing the rest of the files to run normal and be validated.
Note: When a file is excluded from the utility, it is excluded from all Checks and no testing for violations will be performed on them.
Properties
name | description | type | default value | since |
---|---|---|---|---|
fileNamePattern | Define regular expression to match the file name against. | Pattern | null |
7.2 |
Examples
With the default configuration, no files are matched against the
fileNamePattern
, so the filter does not affect the audit:
<module name="Checker">
<module name="RegexpOnFilename">
<property name="fileNamePattern" value="^[A-Z].*"/>
<property name="match" value="false"/>
<message key="regexp.filename.mismatch"
value="File name must start with an uppercase."/>
</module>
<module name="BeforeExecutionExclusionFileFilter"/>
</module>
Example:
.../Example1.java
.../Example2.java
.../Example3.java
.../Example4.java
.../test/generated_TestCase1.java // violation, name must start with an uppercase
.../test/generated_StubBankRemote.java // violation, must start with an uppercase
.../test/MockPaymentRemote.java
.../module-info.java // violation, name must start with an uppercase
To configure the filter to exclude all 'module-info.java' files:
<module name="Checker">
<module name="RegexpOnFilename">
<property name="fileNamePattern" value="^[A-Z].*"/>
<property name="match" value="false"/>
<message key="regexp.filename.mismatch"
value="File name must start with an uppercase."/>
</module>
<module name="BeforeExecutionExclusionFileFilter">
<property name="fileNamePattern" value="module\-info\.java$"/>
</module>
</module>
Example:
.../Example1.java
.../Example2.java
.../Example3.java
.../Example4.java
.../test/generated_TestCase1.java // violation, name must start with an uppercase
.../test/generated_StubBankRemote.java // violation, must start with an uppercase
.../test/MockPaymentRemote.java
.../module-info.java // OK, the file is not audited
To configure the filter to run only on required files for example that ends with "Remote" or end with "Client" in names or named as "Remote.java" or "Client.java" use negative lookahead:
<module name="Checker">
<module name="RegexpOnFilename">
<property name="fileNamePattern" value="^[A-Z].*"/>
<property name="match" value="false"/>
<message key="regexp.filename.mismatch"
value="File name must start with an uppercase."/>
</module>
<module name="BeforeExecutionExclusionFileFilter">
<property name="fileNamePattern"
value="^(?!.*([\\/])?(Remote|Client)\.java$).*"/>
</module>
</module>
Example:
.../Example1.java
.../Example2.java
.../Example3.java
.../Example4.java
.../test/generated_TestCase1.java // OK, the file is not audited
.../test/generated_StubBankRemote.java // violation, must start with an uppercase
.../test/MockPaymentRemote.java
.../module-info.java // OK, the file is not audited
To configure the filter to exclude all 'test' folder files and all 'module-info.java' files:
<module name="Checker">
<module name="RegexpOnFilename">
<property name="fileNamePattern" value="^[A-Z].*"/>
<property name="match" value="false"/>
<message key="regexp.filename.mismatch"
value="File name must start with an uppercase."/>
</module>
<module name="BeforeExecutionExclusionFileFilter">
<property name="fileNamePattern"
value="(.*[\\/]test[\\/].*$)|(module\-info\.java$)"/>
</module>
</module>
Example:
.../Example1.java
.../Example2.java
.../Example3.java
.../Example4.java
.../test/generated_TestCase1.java // OK, 'test' folder is not audited
.../test/generated_StubBankRemote.java // OK, 'test' folder is not audited
.../test/MockPaymentRemote.java
.../module-info.java // OK, the file is not audited
Example of Usage
Package
com.puppycrawl.tools.checkstyle.filefilters