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.FilterUtil; 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 * @noinspection NonFinalFieldReferenceInEquals, NonFinalFieldReferencedInHashCode 040 */ 041public class SuppressionFilter extends AutomaticBean implements Filter, ExternalResourceHolder { 042 043 /** Filename of suppression file. */ 044 private String file; 045 /** Tells whether config file existence is optional. */ 046 private boolean optional; 047 /** Set of individual suppresses. */ 048 private FilterSet filters = new FilterSet(); 049 050 /** 051 * Sets name of the suppression file. 052 * @param fileName name of the suppressions file. 053 */ 054 public void setFile(String fileName) { 055 file = fileName; 056 } 057 058 /** 059 * Sets whether config file existence is optional. 060 * @param optional tells if config file existence is optional. 061 */ 062 public void setOptional(boolean optional) { 063 this.optional = optional; 064 } 065 066 @Override 067 public boolean accept(AuditEvent event) { 068 return filters.accept(event); 069 } 070 071 @Override 072 public boolean equals(Object obj) { 073 if (this == obj) { 074 return true; 075 } 076 if (obj == null || getClass() != obj.getClass()) { 077 return false; 078 } 079 final SuppressionFilter suppressionFilter = (SuppressionFilter) obj; 080 return Objects.equals(filters, suppressionFilter.filters); 081 } 082 083 @Override 084 public int hashCode() { 085 return Objects.hash(filters); 086 } 087 088 @Override 089 protected void finishLocalSetup() throws CheckstyleException { 090 if (file != null) { 091 if (optional) { 092 if (FilterUtil.isFileExists(file)) { 093 filters = SuppressionsLoader.loadSuppressions(file); 094 } 095 else { 096 filters = new FilterSet(); 097 } 098 } 099 else { 100 filters = SuppressionsLoader.loadSuppressions(file); 101 } 102 } 103 } 104 105 @Override 106 public Set<String> getExternalResourceLocations() { 107 return Collections.singleton(file); 108 } 109 110}