001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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.Collections; 023import java.util.Objects; 024import java.util.Set; 025 026import com.puppycrawl.tools.checkstyle.api.AuditEvent; 027import com.puppycrawl.tools.checkstyle.api.AutomaticBean; 028import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 029import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder; 030import com.puppycrawl.tools.checkstyle.api.Filter; 031import com.puppycrawl.tools.checkstyle.api.FilterSet; 032import com.puppycrawl.tools.checkstyle.utils.FilterUtils; 033 034/** 035 * <p> 036 * This filter accepts AuditEvents according to file, check, line, and 037 * column, as specified in a suppression file. 038 * </p> 039 * @author Rick Giles 040 * @author <a href="mailto:piotr.listkiewicz@gmail.com">liscju</a> 041 * @noinspection NonFinalFieldReferenceInEquals, NonFinalFieldReferencedInHashCode 042 */ 043public class SuppressionFilter extends AutomaticBean implements Filter, ExternalResourceHolder { 044 045 /** Filename of suppression file. */ 046 private String file; 047 /** Tells whether config file existence is optional. */ 048 private boolean optional; 049 /** Set of individual suppresses. */ 050 private FilterSet filters = new FilterSet(); 051 052 /** 053 * Sets name of the suppression file. 054 * @param fileName name of the suppressions file. 055 */ 056 public void setFile(String fileName) { 057 file = fileName; 058 } 059 060 /** 061 * Sets whether config file existence is optional. 062 * @param optional tells if config file existence is optional. 063 */ 064 public void setOptional(boolean optional) { 065 this.optional = optional; 066 } 067 068 @Override 069 public boolean accept(AuditEvent event) { 070 return filters.accept(event); 071 } 072 073 @Override 074 public boolean equals(Object obj) { 075 if (this == obj) { 076 return true; 077 } 078 if (obj == null || getClass() != obj.getClass()) { 079 return false; 080 } 081 final SuppressionFilter suppressionFilter = (SuppressionFilter) obj; 082 return Objects.equals(filters, suppressionFilter.filters); 083 } 084 085 @Override 086 public int hashCode() { 087 return Objects.hash(filters); 088 } 089 090 @Override 091 protected void finishLocalSetup() throws CheckstyleException { 092 if (file != null) { 093 if (optional) { 094 if (FilterUtils.isFileExists(file)) { 095 filters = SuppressionsLoader.loadSuppressions(file); 096 } 097 else { 098 filters = new FilterSet(); 099 } 100 } 101 else { 102 filters = SuppressionsLoader.loadSuppressions(file); 103 } 104 } 105 } 106 107 @Override 108 public Set<String> getExternalResourceLocations() { 109 return Collections.singleton(file); 110 } 111 112}