diff --git a/config/checkstyle_checks.xml b/config/checkstyle_checks.xml
index 9f6d7289c..0529f55ce 100644
--- a/config/checkstyle_checks.xml
+++ b/config/checkstyle_checks.xml
@@ -18,6 +18,9 @@
+
+
+
diff --git a/config/pmd.xml b/config/pmd.xml
index a91e36620..7776103f5 100644
--- a/config/pmd.xml
+++ b/config/pmd.xml
@@ -43,6 +43,12 @@
+
+
+
+
+
+
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java b/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java
index b633e0527..0b775df09 100644
--- a/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java
@@ -37,6 +37,8 @@ import org.apache.commons.logging.LogFactory;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.AuditListener;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
+import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter;
+import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilterSet;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.api.Context;
@@ -72,6 +74,10 @@ public class Checker extends AutomaticBean implements MessageDispatcher {
/** Vector of fileset checks. */
private final List fileSetChecks = new ArrayList<>();
+ /** The audit event before execution file filters. */
+ private final BeforeExecutionFileFilterSet beforeExecutionFileFilters =
+ new BeforeExecutionFileFilterSet();
+
/** The audit event filters. */
private final FilterSet filters = new FilterSet();
@@ -136,6 +142,14 @@ public class Checker extends AutomaticBean implements MessageDispatcher {
cache.load();
}
+ /**
+ * Removes before execution file filter.
+ * @param filter before execution file filter to remove.
+ */
+ public void removeBeforeExecutionFileFilter(BeforeExecutionFileFilter filter) {
+ beforeExecutionFileFilters.removeBeforeExecutionFileFilter(filter);
+ }
+
/**
* Removes filter.
* @param filter filter to remove.
@@ -147,6 +161,7 @@ public class Checker extends AutomaticBean implements MessageDispatcher {
/** Cleans up the object. **/
public void destroy() {
listeners.clear();
+ beforeExecutionFileFilters.clear();
filters.clear();
if (cache != null) {
try {
@@ -259,7 +274,8 @@ public class Checker extends AutomaticBean implements MessageDispatcher {
final String fileName = file.getAbsolutePath();
final long timestamp = file.lastModified();
if (cache != null && cache.isInCache(fileName, timestamp)
- || !CommonUtils.matchesFileExtension(file, fileExtensions)) {
+ || !CommonUtils.matchesFileExtension(file, fileExtensions)
+ || !acceptFileStarted(fileName)) {
continue;
}
fireFileStarted(fileName);
@@ -307,6 +323,18 @@ public class Checker extends AutomaticBean implements MessageDispatcher {
return fileMessages;
}
+ /**
+ * Check if all before execution file filters accept starting the file.
+ *
+ * @param fileName
+ * the file to be audited
+ * @return {@code true} if the file is accepted.
+ */
+ private boolean acceptFileStarted(String fileName) {
+ final String stripped = CommonUtils.relativizeAndNormalizePath(basedir, fileName);
+ return beforeExecutionFileFilters.accept(stripped);
+ }
+
/**
* Notify all listeners about the beginning of a file audit.
*
@@ -408,6 +436,10 @@ public class Checker extends AutomaticBean implements MessageDispatcher {
fsc.init();
addFileSetCheck(fsc);
}
+ else if (child instanceof BeforeExecutionFileFilter) {
+ final BeforeExecutionFileFilter filter = (BeforeExecutionFileFilter) child;
+ addBeforeExecutionFileFilter(filter);
+ }
else if (child instanceof Filter) {
final Filter filter = (Filter) child;
addFilter(filter);
@@ -432,6 +464,14 @@ public class Checker extends AutomaticBean implements MessageDispatcher {
fileSetChecks.add(fileSetCheck);
}
+ /**
+ * Adds a before execution file filter to the end of the event chain.
+ * @param filter the additional filter
+ */
+ public void addBeforeExecutionFileFilter(BeforeExecutionFileFilter filter) {
+ beforeExecutionFileFilters.addBeforeExecutionFileFilter(filter);
+ }
+
/**
* Adds a filter to the end of the audit event filter chain.
* @param filter the additional filter
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/api/BeforeExecutionFileFilter.java b/src/main/java/com/puppycrawl/tools/checkstyle/api/BeforeExecutionFileFilter.java
new file mode 100644
index 000000000..904444795
--- /dev/null
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/api/BeforeExecutionFileFilter.java
@@ -0,0 +1,33 @@
+////////////////////////////////////////////////////////////////////////////////
+// checkstyle: Checks Java source code for adherence to a set of rules.
+// Copyright (C) 2001-2016 the original author or authors.
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+////////////////////////////////////////////////////////////////////////////////
+
+package com.puppycrawl.tools.checkstyle.api;
+
+/**
+ * An interface for before execution file filtering events.
+ * @author Richard Veach
+ */
+public interface BeforeExecutionFileFilter {
+ /**
+ * Determines whether or not a before execution file filtered event is accepted.
+ * @param uri the uri to filter.
+ * @return true if the event is accepted.
+ */
+ boolean accept(String uri);
+}
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/api/BeforeExecutionFileFilterSet.java b/src/main/java/com/puppycrawl/tools/checkstyle/api/BeforeExecutionFileFilterSet.java
new file mode 100644
index 000000000..5b6a6e45e
--- /dev/null
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/api/BeforeExecutionFileFilterSet.java
@@ -0,0 +1,82 @@
+////////////////////////////////////////////////////////////////////////////////
+// checkstyle: Checks Java source code for adherence to a set of rules.
+// Copyright (C) 2001-2016 the original author or authors.
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+////////////////////////////////////////////////////////////////////////////////
+
+package com.puppycrawl.tools.checkstyle.api;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * A before execution file filter set applies filters to events.
+ * If a before execution file filter in the set rejects an event, then the
+ * event is rejected. Otherwise, the event is accepted.
+ * @author Richard Veach
+ */
+public final class BeforeExecutionFileFilterSet
+ implements BeforeExecutionFileFilter {
+ /** Filter set. */
+ private final Set beforeExecutionFileFilters = new HashSet<>();
+
+ /**
+ * Adds a Filter to the set.
+ * @param filter the Filter to add.
+ */
+ public void addBeforeExecutionFileFilter(BeforeExecutionFileFilter filter) {
+ beforeExecutionFileFilters.add(filter);
+ }
+
+ /**
+ * Removes filter.
+ * @param filter filter to remove.
+ */
+ public void removeBeforeExecutionFileFilter(BeforeExecutionFileFilter filter) {
+ beforeExecutionFileFilters.remove(filter);
+ }
+
+ /**
+ * Returns the Filters of the filter set.
+ * @return the Filters of the filter set.
+ */
+ public Set getBeforeExecutionFileFilters() {
+ return Collections.unmodifiableSet(beforeExecutionFileFilters);
+ }
+
+ @Override
+ public String toString() {
+ return beforeExecutionFileFilters.toString();
+ }
+
+ @Override
+ public boolean accept(String uri) {
+ boolean result = true;
+ for (BeforeExecutionFileFilter filter : beforeExecutionFileFilters) {
+ if (!filter.accept(uri)) {
+ result = false;
+ break;
+ }
+ }
+ return result;
+ }
+
+ /** Clears the BeforeExecutionFileFilterSet. */
+ public void clear() {
+ beforeExecutionFileFilters.clear();
+ }
+}
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filefilters/BeforeExecutionExclusionFileFilter.java b/src/main/java/com/puppycrawl/tools/checkstyle/filefilters/BeforeExecutionExclusionFileFilter.java
new file mode 100644
index 000000000..d4158a953
--- /dev/null
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/filefilters/BeforeExecutionExclusionFileFilter.java
@@ -0,0 +1,92 @@
+////////////////////////////////////////////////////////////////////////////////
+// checkstyle: Checks Java source code for adherence to a set of rules.
+// Copyright (C) 2001-2016 the original author or authors.
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+////////////////////////////////////////////////////////////////////////////////
+
+package com.puppycrawl.tools.checkstyle.filefilters;
+
+import java.util.regex.Pattern;
+
+import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
+import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter;
+import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
+
+/**
+ *
+ * File filter BeforeExecutionExclusionFileFilter decides which files should be
+ * excluded from being processed by the utility.
+ *
+ *
+ *
+ * By default Checkstyle includes all files and sub-directories in a directory to be processed and
+ * checked for violations. Users could have files that are in these sub-directories 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.
+ *
+ *
+ *
+ * Check have following options:
+ *
+ *
+ *
+ * fileNamePattern - Regular expression to match the file name against. Default value is null.
+ *
+ *
+ *
+ *
+ * To configure the filter to exclude all 'module-info.java' files:
+ *
+ *
+ * @author Richard Veach
+ */
+public final class BeforeExecutionExclusionFileFilter extends AutomaticBean
+ implements BeforeExecutionFileFilter {
+
+ /** Filename of exclusion. */
+ private Pattern fileNamePattern;
+
+ /**
+ * Sets regular expression of the file to exclude.
+ *
+ * @param fileNamePattern regular expression of the excluded file.
+ * @throws org.apache.commons.beanutils.ConversionException if unable to
+ * create Pattern object.
+ */
+ public void setFileNamePattern(String fileNamePattern) {
+ this.fileNamePattern = CommonUtils.createPattern(fileNamePattern);
+ }
+
+ @Override
+ public boolean accept(String uri) {
+ return fileNamePattern == null || !fileNamePattern.matcher(uri).find();
+ }
+}
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filefilters/package-info.java b/src/main/java/com/puppycrawl/tools/checkstyle/filefilters/package-info.java
new file mode 100644
index 000000000..750fbc690
--- /dev/null
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/filefilters/package-info.java
@@ -0,0 +1,23 @@
+////////////////////////////////////////////////////////////////////////////////
+// checkstyle: Checks Java source code for adherence to a set of rules.
+// Copyright (C) 2001-2016 the original author or authors.
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+////////////////////////////////////////////////////////////////////////////////
+
+/**
+ * Contains the before execution file filters that are bundled with the main distribution.
+ */
+package com.puppycrawl.tools.checkstyle.filefilters;
diff --git a/src/main/resources/checkstyle_packages.xml b/src/main/resources/checkstyle_packages.xml
index bba646f80..681dda645 100644
--- a/src/main/resources/checkstyle_packages.xml
+++ b/src/main/resources/checkstyle_packages.xml
@@ -22,6 +22,7 @@
+
diff --git a/src/site/site.xml b/src/site/site.xml
index 8dbdead27..48a8ba6c5 100644
--- a/src/site/site.xml
+++ b/src/site/site.xml
@@ -60,6 +60,7 @@
+
@@ -95,6 +96,7 @@
+
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java
index 62257d475..607a3b3a0 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java
@@ -65,6 +65,14 @@ public class CheckerTest extends BaseCheckTestSupport {
@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
+ private static Method getAcceptFileStarted() throws NoSuchMethodException {
+ final Class checkerClass = Checker.class;
+ final Method acceptFileStarted = checkerClass.getDeclaredMethod("acceptFileStarted",
+ String.class);
+ acceptFileStarted.setAccessible(true);
+ return acceptFileStarted;
+ }
+
private static Method getFireAuditFinished() throws NoSuchMethodException {
final Class checkerClass = Checker.class;
final Method fireAuditFinished = checkerClass.getDeclaredMethod("fireAuditFinished");
@@ -178,6 +186,33 @@ public class CheckerTest extends BaseCheckTestSupport {
}
+ @Test
+ public void testAddBeforeExecutionFileFilter() throws Exception {
+ final Checker checker = new Checker();
+ final TestBeforeExecutionFileFilter filter = new TestBeforeExecutionFileFilter();
+
+ checker.addBeforeExecutionFileFilter(filter);
+
+ filter.resetFilter();
+ getAcceptFileStarted().invoke(checker, "Test.java");
+ assertTrue("Checker.acceptFileStarted() doesn't call filter", filter.wasCalled());
+ }
+
+ @Test
+ public void testRemoveBeforeExecutionFileFilter() throws Exception {
+ final Checker checker = new Checker();
+ final TestBeforeExecutionFileFilter filter = new TestBeforeExecutionFileFilter();
+ final TestBeforeExecutionFileFilter f2 = new TestBeforeExecutionFileFilter();
+ checker.addBeforeExecutionFileFilter(filter);
+ checker.addBeforeExecutionFileFilter(f2);
+ checker.removeBeforeExecutionFileFilter(filter);
+
+ f2.resetFilter();
+ getAcceptFileStarted().invoke(checker, "Test.java");
+ assertTrue("Checker.acceptFileStarted() doesn't call filter", f2.wasCalled());
+ assertFalse("Checker.acceptFileStarted() does call removed filter", filter.wasCalled());
+ }
+
@Test
public void testAddFilter() {
final Checker checker = new Checker();
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/PackageNamesLoaderTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/PackageNamesLoaderTest.java
index 8ffc8b71c..f26da2f78 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/PackageNamesLoaderTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/PackageNamesLoaderTest.java
@@ -79,8 +79,8 @@ public class PackageNamesLoaderTest {
"com.puppycrawl.tools.checkstyle.checks.regexp.",
"com.puppycrawl.tools.checkstyle.checks.sizes.",
"com.puppycrawl.tools.checkstyle.checks.whitespace.",
+ "com.puppycrawl.tools.checkstyle.filefilters.",
"com.puppycrawl.tools.checkstyle.filters.",
-
};
assertEquals("pkgNames.length.", checkstylePackages.length,
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/TestBeforeExecutionFileFilter.java b/src/test/java/com/puppycrawl/tools/checkstyle/TestBeforeExecutionFileFilter.java
new file mode 100644
index 000000000..a705fe6c9
--- /dev/null
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/TestBeforeExecutionFileFilter.java
@@ -0,0 +1,40 @@
+////////////////////////////////////////////////////////////////////////////////
+// checkstyle: Checks Java source code for adherence to a set of rules.
+// Copyright (C) 2001-2016 the original author or authors.
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+////////////////////////////////////////////////////////////////////////////////
+
+package com.puppycrawl.tools.checkstyle;
+
+import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter;
+
+class TestBeforeExecutionFileFilter implements BeforeExecutionFileFilter {
+ private boolean called;
+
+ @Override
+ public boolean accept(String uri) {
+ called = true;
+ return true;
+ }
+
+ public boolean wasCalled() {
+ return called;
+ }
+
+ public void resetFilter() {
+ called = false;
+ }
+}
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/filefilters/ExclusionBeforeExecutionFileFilterTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/filefilters/ExclusionBeforeExecutionFileFilterTest.java
new file mode 100644
index 000000000..6c86ec521
--- /dev/null
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/filefilters/ExclusionBeforeExecutionFileFilterTest.java
@@ -0,0 +1,92 @@
+////////////////////////////////////////////////////////////////////////////////
+// checkstyle: Checks Java source code for adherence to a set of rules.
+// Copyright (C) 2001-2016 the original author or authors.
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+////////////////////////////////////////////////////////////////////////////////
+
+package com.puppycrawl.tools.checkstyle.filefilters;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
+import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
+import com.puppycrawl.tools.checkstyle.api.Configuration;
+import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
+
+public class ExclusionBeforeExecutionFileFilterTest extends BaseCheckTestSupport {
+ @Test
+ public void testAccept() {
+ final String fileName = "BAD";
+ final BeforeExecutionExclusionFileFilter filter =
+ createExclusionBeforeExecutionFileFilter(fileName);
+
+ assertTrue(filter.accept("ATest.java"));
+ }
+
+ @Test
+ public void testAcceptOnNullFile() {
+ final String fileName = null;
+ final BeforeExecutionExclusionFileFilter filter =
+ createExclusionBeforeExecutionFileFilter(fileName);
+
+ assertTrue(filter.accept("AnyJava.java"));
+ }
+
+ @Test
+ public void testReject() {
+ final String fileName = "Test";
+ final BeforeExecutionExclusionFileFilter filter =
+ createExclusionBeforeExecutionFileFilter(fileName);
+
+ assertFalse(filter.accept("ATest.java"));
+ }
+
+ @Test
+ public void testRejectBadFile() throws Exception {
+ final DefaultConfiguration filterConfig =
+ createBeforeExecutionFileFilterConfig(BeforeExecutionExclusionFileFilter.class);
+ filterConfig.addAttribute("fileNamePattern", "IncorrectClass\\.java");
+
+ final String[] violations = CommonUtils.EMPTY_STRING_ARRAY;
+ verify(createChecker(filterConfig), getNonCompilablePath("InputIncorrectClass.java"),
+ violations);
+ }
+
+ private static BeforeExecutionExclusionFileFilter
+ createExclusionBeforeExecutionFileFilter(String fileName) {
+ final BeforeExecutionExclusionFileFilter exclusionBeforeExecutionFileFilter =
+ new BeforeExecutionExclusionFileFilter();
+ if (fileName != null) {
+ exclusionBeforeExecutionFileFilter.setFileNamePattern(fileName);
+ }
+ return exclusionBeforeExecutionFileFilter;
+ }
+
+ private static DefaultConfiguration createBeforeExecutionFileFilterConfig(Class> aClass) {
+ return new DefaultConfiguration(aClass.getName());
+ }
+
+ @Override
+ protected DefaultConfiguration createCheckerConfig(Configuration config) {
+ final DefaultConfiguration dc =
+ new DefaultConfiguration("configuration");
+ dc.addChild(config);
+ return dc;
+ }
+}
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/filters/BeforeExecutionFileFilterSetTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/filters/BeforeExecutionFileFilterSetTest.java
new file mode 100644
index 000000000..b8f1ad0f5
--- /dev/null
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/filters/BeforeExecutionFileFilterSetTest.java
@@ -0,0 +1,68 @@
+////////////////////////////////////////////////////////////////////////////////
+// checkstyle: Checks Java source code for adherence to a set of rules.
+// Copyright (C) 2001-2016 the original author or authors.
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+////////////////////////////////////////////////////////////////////////////////
+
+package com.puppycrawl.tools.checkstyle.filters;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilterSet;
+import com.puppycrawl.tools.checkstyle.filefilters.BeforeExecutionExclusionFileFilter;
+
+public class BeforeExecutionFileFilterSetTest {
+ @Test
+ public void testAccept() {
+ final String fileName = "BAD";
+ final BeforeExecutionExclusionFileFilter filter = new BeforeExecutionExclusionFileFilter();
+ filter.setFileNamePattern(fileName);
+ final BeforeExecutionFileFilterSet set = new BeforeExecutionFileFilterSet();
+ set.addBeforeExecutionFileFilter(filter);
+
+ assertTrue(set.accept("ATest.java"));
+ }
+
+ @Test
+ public void testReject() {
+ final String fileName = "Test";
+ final BeforeExecutionExclusionFileFilter filter = new BeforeExecutionExclusionFileFilter();
+ filter.setFileNamePattern(fileName);
+ final BeforeExecutionFileFilterSet set = new BeforeExecutionFileFilterSet();
+ set.addBeforeExecutionFileFilter(filter);
+
+ assertFalse(set.accept("ATest.java"));
+ }
+
+ @Test
+ public void testGetFilters2() {
+ final BeforeExecutionFileFilterSet filterSet = new BeforeExecutionFileFilterSet();
+ filterSet.addBeforeExecutionFileFilter(new BeforeExecutionExclusionFileFilter());
+ assertEquals("size is the same", 1, filterSet.getBeforeExecutionFileFilters().size());
+ }
+
+ @Test
+ public void testToString2() {
+ final BeforeExecutionFileFilterSet filterSet = new BeforeExecutionFileFilterSet();
+ filterSet.addBeforeExecutionFileFilter(new BeforeExecutionExclusionFileFilter());
+ assertNotNull("size is the same", filterSet.toString());
+ }
+}
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/CheckUtil.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/CheckUtil.java
index 7a84dcce3..a1971a7f0 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/CheckUtil.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/CheckUtil.java
@@ -41,6 +41,7 @@ import org.w3c.dom.NodeList;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.ClassPath;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
+import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter;
import com.puppycrawl.tools.checkstyle.api.Filter;
import com.puppycrawl.tools.checkstyle.checks.regexp.RegexpMultilineCheck;
import com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck;
@@ -184,7 +185,7 @@ public final class CheckUtil {
* Checks whether a class may be considered as the checkstyle module.
* Checkstyle's modules are nonabstract classes which names end with 'Check',
* do not contain the word 'Input' (are not input files for UTs),
- * checkstyle's filters and SuppressWarningsHolder class.
+ * checkstyle's filters, checkstyle's file filters and SuppressWarningsHolder class.
* @param loadedClass class to check.
* @return true if the class may be considered as the checkstyle module.
*/
@@ -192,6 +193,7 @@ public final class CheckUtil {
final String className = loadedClass.getSimpleName();
return isCheckstyleNonAbstractCheck(loadedClass, className)
|| isFilterModule(loadedClass, className)
+ || isFileFilterModule(loadedClass, className)
|| "SuppressWarningsHolder".equals(className)
|| "FileContentsHolder".equals(className);
}
@@ -210,6 +212,20 @@ public final class CheckUtil {
&& className.endsWith("Filter");
}
+ /**
+ * Checks whether a class may be considered as the checkstyle file filter.
+ * Checkstyle's file filters are classes which are subclasses of AutomaticBean,
+ * implement 'BeforeExecutionFileFilter' interface, and which names end with 'FileFilter'.
+ * @param loadedClass class to check.
+ * @param className class name to check.
+ * @return true if a class may be considered as the checkstyle file filter.
+ */
+ private static boolean isFileFilterModule(Class> loadedClass, String className) {
+ return BeforeExecutionFileFilter.class.isAssignableFrom(loadedClass)
+ && AutomaticBean.class.isAssignableFrom(loadedClass)
+ && className.endsWith("FileFilter");
+ }
+
/**
* Get's the check's messages.
* @param module class to examine.
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java
index 789506a5c..ae4493c6f 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java
@@ -99,6 +99,7 @@ public class XDocsPagesTest {
"name=\"SuppressionCommentFilter\"",
"name=\"SuppressWithNearbyCommentFilter\"",
"name=\"SuppressWarningsFilter\"",
+ "name=\"BeforeExecutionExclusionFileFilter\"",
"name=\"RegexpHeader\"",
"name=\"RegexpOnFilename\"",
"name=\"RegexpSingleline\"",
diff --git a/src/xdocs/config.xml b/src/xdocs/config.xml
index 9f0dfd446..af1bd2b5f 100644
--- a/src/xdocs/config.xml
+++ b/src/xdocs/config.xml
@@ -659,6 +659,20 @@
+
+
+
+ A Checker module has a Before Execution File Filter submodule to filter
+ files from being processed by the utility. A Before Execution File Filter
+ can accept or reject a file by its name. If all Before Execution File Filters
+ accept a file, then Checker will process and validate the file.
+ If any Before Execution File Filter rejects an file, then Checker skips over
+ the file and acts like it does not exist.
+ For more information, please visit the file filters page.
+
+ File filter BeforeExecutionExclusionFileFilter decides which files should be
+ excluded from being processed by the utility.
+
+
+ By default Checkstyle includes all files and sub-directories in a directory to be
+ processed and checked for violations. Users could have files that are in these
+ sub-directories 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.
+
+
+
+
+
+
name
+
description
+
type
+
default value
+
+
+
fileNamePattern
+
Regular expression to match the file name against.
+ 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.
+
+
+
+
+
+ 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.
+
+ 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:
+
+ 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.
+
+
+
+
+
+ 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.
+