Content

Overview

A Checker has a set of BeforeExecutionFileFilters that decide which files the Checker processes. Interface BeforeExecutionFileFilter and class BeforeExecutionFileFilterSet are intended to support general file uri filtering using a set of BeforeExecutionFileFilters.

A BeforeExecutionFileFilter has boolean method accept(String uri) that returns true if the BeforeExecutionFileFilter accepts the file uri parameter and returns false if the file uri rejects it.

A BeforeExecutionFileFilterSet is a particular BeforeExecutionFileFilter that contains a set of BeforeExecutionFileFilters. A BeforeExecutionFileFilterSet accepts an file uri if and only if all BeforeExecutionFileFilters in the set accept the file uri.

Writing Before Execution File Filters

The BeforeExecutionFileFilter that we demonstrate here rejects file uris whose name matches a regular expression. In order to enable the specification of the file name pattern as a property in a configuration file, the BeforeExecutionFileFilter is an AutomaticBean with mutator method setFiles(String) that receives the file name pattern. An AutomaticBean uses JavaBean introspection to set JavaBean properties such as files.

package com.mycompany.filefilters;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter;
import com.puppycrawl.tools.checkstyle.api.Utils;

public class FilesBeforeExecutionFileFilter
    extends AutomaticBean
    implements BeforeExecutionFileFilter
{
  private Pattern mFileRegexp;

  public FilesBeforeExecutionFileFilter()
      throws PatternSyntaxException
  {
    setFiles("^$");
  }

  public boolean accept(String uri)
  {
    return ((uri == null) || !mFileRegexp.matcher(uri).find());
  }

  public void setFiles(String aFilesPattern)
      throws PatternSyntaxException
  {
    mFileRegexp = Utils.getPattern(aFilesPattern);
  }
}
      

Using Before Execution File Filters

To incorporate a BeforeExecutionFileFilter in the before execution file filter set for a Checker, include a module element for the BeforeExecutionFileFilter in the configuration file. For example, to configure a Checker so that it uses custom filter FilesBeforeExecutionFileFilter to prevent processing files whose name contains "Generated", include the following module in the configuration file:

<module name="com.mycompany.filters.FilesBeforeExecutionFileFilter">
  <property name="files" value="Generated"/>
</module>
      

Huh? I can't figure it out!

That's probably our fault, and it means that we have to provide better documentation. Please do not hesitate to ask questions on the user mailing list, this will help us to improve this document. Please ask your questions as precisely as possible. We will not be able to answer questions like "I want to write a filter but I don't know how, can you help me?". Tell us what you are trying to do (the purpose of the filter), what you have understood so far, and what exactly you are getting stuck on.

Contributing

We need your help to keep improving Checkstyle. Whenever you write a before execution file filter that you think is generally useful, please consider contributing it to the Checkstyle community and submit it for inclusion in the next release of Checkstyle.