001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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.api.AuditEvent; 025import com.puppycrawl.tools.checkstyle.api.AutomaticBean; 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 files} - Define the RegExp for matching against the file name associated with 053 * an audit event. 054 * Type is {@code java.util.regex.Pattern}. 055 * Default value is {@code null}. 056 * </li> 057 * <li> 058 * Property {@code checks} - Define the RegExp for matching against the name of the check 059 * associated with an audit event. 060 * Type is {@code java.lang.String}. 061 * Validation type is {@code java.util.regex.Pattern}. 062 * Default value is {@code null}. 063 * </li> 064 * <li> 065 * Property {@code message} - Define the RegExp for matching against the message of the check 066 * associated with an audit event. 067 * Type is {@code java.util.regex.Pattern}. 068 * Default value is {@code null}. 069 * </li> 070 * <li> 071 * Property {@code id} - Specify a string matched against the ID of the check associated with 072 * an audit event. 073 * Type is {@code java.lang.String}. 074 * Default value is {@code null}. 075 * </li> 076 * <li> 077 * Property {@code lines} - Specify a comma-separated list of values, where each value is an 078 * integer or a range of integers denoted by integer-integer. 079 * Type is {@code java.lang.String}. 080 * Default value is {@code null}. 081 * </li> 082 * <li> 083 * Property {@code columns} - Specify a comma-separated list of values, where each value is an 084 * integer or a range of integers denoted by integer-integer. 085 * Type is {@code java.lang.String}. 086 * Default value is {@code null}. 087 * </li> 088 * </ul> 089 * <p> 090 * The following suppressions directs a {@code SuppressionSingleFilter} to reject 091 * {@code JavadocStyleCheck} violations for lines 82 and 108 to 122 of file 092 * {@code AbstractComplexityCheck.java}, and 093 * {@code MagicNumberCheck} violations for line 221 of file 094 * {@code JavadocStyleCheck.java}, and {@code 'Missing a Javadoc comment'} violations for all lines 095 * and files: 096 * </p> 097 * <pre> 098 * <module name="SuppressionSingleFilter"> 099 * <property name="checks" value="JavadocStyleCheck"/> 100 * <property name="files" value="AbstractComplexityCheck.java"/> 101 * <property name="lines" value="82,108-122"/> 102 * </module> 103 * <module name="SuppressionSingleFilter"> 104 * <property name="checks" value="MagicNumberCheck"/> 105 * <property name="files" value="JavadocStyleCheck.java"/> 106 * <property name="lines" value="221"/> 107 * </module> 108 * <module name="SuppressionSingleFilter"> 109 * <property name="message" value="Missing a Javadoc comment"/> 110 * </module> 111 * </pre> 112 * <p> 113 * Suppress check by <a href="https://checkstyle.org/config.html#Id">module id</a> when config 114 * have two instances on the same check: 115 * </p> 116 * <pre> 117 * <module name="SuppressionSingleFilter"> 118 * <property name="id" value="stringEqual"/> 119 * <property name="files" value="SomeTestCode.java"/> 120 * </module> 121 * </pre> 122 * <p> 123 * Suppress all checks for hidden files and folders: 124 * </p> 125 * <pre> 126 * <module name="SuppressionSingleFilter"> 127 * <property name="files" value="[/\\]\..+"/> 128 * <property name="checks" value=".*"/> 129 * </module> 130 * </pre> 131 * <p> 132 * Suppress all checks for Maven-generated code: 133 * </p> 134 * <pre> 135 * <module name="SuppressionSingleFilter"> 136 * <property name="files" value="[/\\]target[/\\]"/> 137 * <property name="checks" value=".*"/> 138 * </module> 139 * </pre> 140 * <p> 141 * Suppress all checks for archives, classes and other binary files: 142 * </p> 143 * <pre> 144 * <module name="SuppressionSingleFilter"> 145 * <property name="files" value=".+\.(?:jar|zip|war|class|tar|bin)$"/> 146 * <property name="checks" value=".*"/> 147 * </module> 148 * </pre> 149 * <p> 150 * Suppress all checks for image files: 151 * </p> 152 * <pre> 153 * <module name="SuppressionSingleFilter"> 154 * <property name="files" value=".+\.(?:png|gif|jpg|jpeg)$"/> 155 * <property name="checks" value=".*"/> 156 * </module> 157 * </pre> 158 * <p> 159 * Suppress all checks for non-java files: 160 * </p> 161 * <pre> 162 * <module name="SuppressionSingleFilter"> 163 * <property name="files" 164 * value=".+\.(?:txt|xml|csv|sh|thrift|html|sql|eot|ttf|woff|css|png)$"/> 165 * <property name="checks" value=".*"/> 166 * </module> 167 * </pre> 168 * <p> 169 * Suppress all checks in generated sources: 170 * </p> 171 * <pre> 172 * <module name="SuppressionSingleFilter"> 173 * <property name="files" value="com[\\/]mycompany[\\/]app[\\/]gen[\\/]"/> 174 * <property name="checks" value=".*"/> 175 * </module> 176 * </pre> 177 * <p> 178 * Suppress FileLength check on integration tests in certain folder: 179 * </p> 180 * <pre> 181 * <module name="SuppressionSingleFilter"> 182 * <property name="files" value="com[\\/]mycompany[\\/]app[\\/].*IT.java"/> 183 * <property name="checks" value="FileLength"/> 184 * </module> 185 * </pre> 186 * <p> 187 * Suppress naming violations on variable named 'log' in all files: 188 * </p> 189 * <pre> 190 * <module name="SuppressionSingleFilter"> 191 * <property name="message" value="Name 'log' must match pattern"/> 192 * </module> 193 * </pre> 194 * <p> 195 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker} 196 * </p> 197 * 198 * @since 8.23 199 */ 200public class SuppressionSingleFilter extends AutomaticBean implements Filter { 201 202 /** 203 * SuppressFilterElement instance. 204 */ 205 private SuppressFilterElement filter; 206 /** 207 * Define the RegExp for matching against the file name associated with an audit event. 208 */ 209 private Pattern files; 210 /** 211 * Define the RegExp for matching against the name of the check associated with an audit event. 212 */ 213 private Pattern checks; 214 /** 215 * Define the RegExp for matching against the message of the check associated with an audit 216 * event. 217 */ 218 private Pattern message; 219 /** 220 * Specify a string matched against the ID of the check associated with an audit event. 221 */ 222 private String id; 223 /** 224 * Specify a comma-separated list of values, where each value is an integer or a range of 225 * integers denoted by integer-integer. 226 */ 227 private String lines; 228 /** 229 * Specify a comma-separated list of values, where each value is an integer or a range of 230 * integers denoted by integer-integer. 231 */ 232 private String columns; 233 234 /** 235 * Setter to define the RegExp for matching against the file name associated with an audit 236 * event. 237 * 238 * @param files regular expression for filtered file names 239 */ 240 public void setFiles(Pattern files) { 241 this.files = files; 242 } 243 244 /** 245 * Setter to define the RegExp for matching against the name of the check associated with an 246 * audit event. 247 * 248 * @param checks the name of the check 249 */ 250 public void setChecks(String checks) { 251 this.checks = Pattern.compile(checks); 252 } 253 254 /** 255 * Setter to define the RegExp for matching against the message of the check associated with 256 * an audit event. 257 * 258 * @param message the message of the check 259 */ 260 public void setMessage(Pattern message) { 261 this.message = message; 262 } 263 264 /** 265 * Setter to specify a string matched against the ID of the check associated with an audit 266 * event. 267 * 268 * @param id the ID of the check 269 */ 270 public void setId(String id) { 271 this.id = id; 272 } 273 274 /** 275 * Setter to specify a comma-separated list of values, where each value is an integer or a 276 * range of integers denoted by integer-integer. 277 * 278 * @param lines the lines of the check 279 */ 280 public void setLines(String lines) { 281 this.lines = lines; 282 } 283 284 /** 285 * Setter to specify a comma-separated list of values, where each value is an integer or a 286 * range of integers denoted by integer-integer. 287 * 288 * @param columns the columns of the check 289 */ 290 public void setColumns(String columns) { 291 this.columns = columns; 292 } 293 294 @Override 295 protected void finishLocalSetup() { 296 filter = new SuppressFilterElement(files, checks, message, id, lines, columns); 297 } 298 299 @Override 300 public boolean accept(AuditEvent event) { 301 return filter.accept(event); 302 } 303 304}