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