001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.filefilters; 021 022import java.util.regex.Pattern; 023 024import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean; 025import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter; 026 027/** 028 * <div> 029 * File filter {@code BeforeExecutionExclusionFileFilter} decides which files should be 030 * excluded from being processed by the utility. 031 * </div> 032 * 033 * <p> 034 * By default, Checkstyle includes all files and subdirectories in a directory to be processed and 035 * checked for violations. Users could have files that are in these subdirectories that shouldn't 036 * be processed with their checkstyle configuration for various reasons, one of which is a valid 037 * Java file that won't pass Checkstyle's parser. When Checkstyle tries to parse a Java file and 038 * fails, it will throw an {@code Exception} and halt parsing any more files for violations. 039 * An example of a valid Java file Checkstyle can't parse is JDK 9's {@code module-info.java}. 040 * This file filter will exclude these problem files from being parsed, allowing the rest of the 041 * files to run normal and be validated. 042 * </p> 043 * 044 * <p> 045 * <b>Note:</b> When a file is excluded from the utility, it is excluded from all Checks and no 046 * testing for violations will be performed on them. 047 * </p> 048 * 049 * @since 7.2 050 */ 051public final class BeforeExecutionExclusionFileFilter extends AbstractAutomaticBean 052 implements BeforeExecutionFileFilter { 053 054 /** Define regular expression to match the file name against. */ 055 private Pattern fileNamePattern; 056 057 /** 058 * Setter to define regular expression to match the file name against. 059 * 060 * @param fileNamePattern regular expression of the excluded file. 061 * @since 7.2 062 */ 063 public void setFileNamePattern(Pattern fileNamePattern) { 064 this.fileNamePattern = fileNamePattern; 065 } 066 067 @Override 068 protected void finishLocalSetup() { 069 // No code by default 070 } 071 072 @Override 073 public boolean accept(String uri) { 074 return fileNamePattern == null || !fileNamePattern.matcher(uri).find(); 075 } 076 077}