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