1 //////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code for adherence to a set of rules. 3 // Copyright (C) 2001-2019 the original author or authors. 4 // 5 // This library is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 2.1 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 //////////////////////////////////////////////////////////////////////////////// 19 20 package com.puppycrawl.tools.checkstyle.filefilters; 21 22 import java.util.regex.Pattern; 23 24 import com.puppycrawl.tools.checkstyle.api.AutomaticBean; 25 import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter; 26 27 /** 28 * <p> 29 * File filter {@code BeforeExecutionExclusionFileFilter} decides which files should be 30 * excluded from being processed by the utility. 31 * </p> 32 * 33 * <p> 34 * By default Checkstyle includes all files and sub-directories in a directory to be processed and 35 * checked for violations. Users could have files that are in these sub-directories that shouldn't 36 * be processed with their checkstyle configuration for various reasons, one of which is a valid 37 * Java file that won't pass Checkstyle's parser. When Checkstyle tries to parse a Java file and 38 * fails, it will throw an {@code Exception} and halt parsing any more files for violations. 39 * An example of a valid Java file Checkstyle can't parse is JDK 9's {@code module-info.java}. 40 * This file filter will exclude these problem files from being parsed, allowing the rest of the 41 * files to run normal and be validated. 42 * </p> 43 * 44 * <p> 45 * <b>Note:</b> When a file is excluded from the utility, it is excluded from all Checks and no 46 * testing for violations will be performed on them. 47 * </p> 48 * 49 * <p> 50 * Check have following options: 51 * </p> 52 * <ul> 53 * <li> 54 * fileNamePattern - Regular expression to match the file name against. Default value is null.</li> 55 * </ul> 56 * <br> 57 * 58 * <p> 59 * To configure the filter to exclude all 'module-info.java' files: 60 * </p> 61 * 62 * <pre> 63 * <module name="BeforeExecutionExclusionFileFilter"> 64 * <property name="fileNamePattern" value="module\-info\.java$"/> 65 * </module> 66 * </pre> 67 * 68 */ 69 public final class BeforeExecutionExclusionFileFilter extends AutomaticBean 70 implements BeforeExecutionFileFilter { 71 72 /** Filename of exclusion. */ 73 private Pattern fileNamePattern; 74 75 /** 76 * Sets regular expression of the file to exclude. 77 * 78 * @param fileNamePattern regular expression of the excluded file. 79 */ 80 public void setFileNamePattern(Pattern fileNamePattern) { 81 this.fileNamePattern = fileNamePattern; 82 } 83 84 @Override 85 protected void finishLocalSetup() { 86 // No code by default 87 } 88 89 @Override 90 public boolean accept(String uri) { 91 return fileNamePattern == null || !fileNamePattern.matcher(uri).find(); 92 } 93 94 }