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.filters; 021 022import java.util.regex.Pattern; 023 024import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean; 025import com.puppycrawl.tools.checkstyle.api.AuditEvent; 026import com.puppycrawl.tools.checkstyle.api.Filter; 027 028/** 029 * <div> 030 * Filter {@code SuppressionSingleFilter} suppresses audit events for Checks violations in the 031 * specified file, class, checks, message, module id, lines, and columns. 032 * </div> 033 * 034 * <p> 035 * Rationale: To allow users to use suppressions configured in the same config as other modules. 036 * {@code SuppressionFilter} and {@code SuppressionXpathFilter} require a separate file. 037 * </p> 038 * 039 * <p> 040 * Advice: If checkstyle configuration is used for several projects, single suppressions on common 041 * files/folders is better to put in checkstyle configuration as common rule. All suppression that 042 * are for specific file names is better to keep in project specific config file. 043 * </p> 044 * 045 * <p> 046 * Attention: This filter only supports single suppression, and will need multiple instances if 047 * users wants to suppress multiple violations. 048 * </p> 049 * 050 * <p> 051 * Notes: 052 * {@code SuppressionSingleFilter} can suppress Checks that have {@code Treewalker} or 053 * {@code Checker} as parent module. 054 * </p> 055 * <ul> 056 * <li> 057 * Property {@code checks} - Define the RegExp for matching against the name of the check 058 * associated with an audit event. 059 * Type is {@code java.util.regex.Pattern}. 060 * Default value is {@code null}. 061 * </li> 062 * <li> 063 * Property {@code columns} - Specify a comma-separated list of values, where each value is an 064 * integer or a range of integers denoted by integer-integer. 065 * Type is {@code java.lang.String}. 066 * Default value is {@code null}. 067 * </li> 068 * <li> 069 * Property {@code files} - Define the RegExp for matching against the file name associated with 070 * an audit event. 071 * Type is {@code java.util.regex.Pattern}. 072 * Default value is {@code null}. 073 * </li> 074 * <li> 075 * Property {@code id} - Specify a string matched against the ID of the check associated with 076 * an audit event. 077 * Type is {@code java.lang.String}. 078 * Default value is {@code null}. 079 * </li> 080 * <li> 081 * Property {@code lines} - Specify a comma-separated list of values, where each value is an 082 * integer or a range of integers denoted by integer-integer. 083 * Type is {@code java.lang.String}. 084 * Default value is {@code null}. 085 * </li> 086 * <li> 087 * Property {@code message} - Define the RegExp for matching against the message of the check 088 * associated with an audit event. 089 * Type is {@code java.util.regex.Pattern}. 090 * Default value is {@code null}. 091 * </li> 092 * </ul> 093 * 094 * <p> 095 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker} 096 * </p> 097 * 098 * @since 8.23 099 */ 100public class SuppressionSingleFilter extends AbstractAutomaticBean implements Filter { 101 102 /** 103 * SuppressFilterElement instance. 104 */ 105 private SuppressFilterElement filter; 106 /** 107 * Define the RegExp for matching against the file name associated with an audit event. 108 */ 109 private Pattern files; 110 /** 111 * Define the RegExp for matching against the name of the check associated with an audit event. 112 */ 113 private Pattern checks; 114 /** 115 * Define the RegExp for matching against the message of the check associated with an audit 116 * event. 117 */ 118 private Pattern message; 119 /** 120 * Specify a string matched against the ID of the check associated with an audit event. 121 */ 122 private String id; 123 /** 124 * Specify a comma-separated list of values, where each value is an integer or a range of 125 * integers denoted by integer-integer. 126 */ 127 private String lines; 128 /** 129 * Specify a comma-separated list of values, where each value is an integer or a range of 130 * integers denoted by integer-integer. 131 */ 132 private String columns; 133 134 /** 135 * Setter to define the RegExp for matching against the file name associated with an audit 136 * event. 137 * 138 * @param files regular expression for filtered file names 139 * @since 8.23 140 */ 141 public void setFiles(Pattern files) { 142 this.files = files; 143 } 144 145 /** 146 * Setter to define the RegExp for matching against the name of the check associated with an 147 * audit event. 148 * 149 * @param checks the name of the check 150 * @since 8.23 151 */ 152 public void setChecks(String checks) { 153 this.checks = Pattern.compile(checks); 154 } 155 156 /** 157 * Setter to define the RegExp for matching against the message of the check associated with 158 * an audit event. 159 * 160 * @param message the message of the check 161 * @since 8.23 162 */ 163 public void setMessage(Pattern message) { 164 this.message = message; 165 } 166 167 /** 168 * Setter to specify a string matched against the ID of the check associated with an audit 169 * event. 170 * 171 * @param id the ID of the check 172 * @since 8.23 173 */ 174 public void setId(String id) { 175 this.id = id; 176 } 177 178 /** 179 * Setter to specify a comma-separated list of values, where each value is an integer or a 180 * range of integers denoted by integer-integer. 181 * 182 * @param lines the lines of the check 183 * @since 8.23 184 */ 185 public void setLines(String lines) { 186 this.lines = lines; 187 } 188 189 /** 190 * Setter to specify a comma-separated list of values, where each value is an integer or a 191 * range of integers denoted by integer-integer. 192 * 193 * @param columns the columns of the check 194 * @since 8.23 195 */ 196 public void setColumns(String columns) { 197 this.columns = columns; 198 } 199 200 @Override 201 protected void finishLocalSetup() { 202 filter = new SuppressFilterElement(files, checks, message, id, lines, columns); 203 } 204 205 @Override 206 public boolean accept(AuditEvent event) { 207 return filter.accept(event); 208 } 209 210}