Coverage Report - com.puppycrawl.tools.checkstyle.filters.SuppressionFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
SuppressionFilter
100%
22/22
100%
12/12
2.143
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2017 the original author or authors.
 4  
 //
 5  
 // This library is free software; you can redistribute it and/or
 6  
 // modify it under the terms of the GNU Lesser General Public
 7  
 // License as published by the Free Software Foundation; either
 8  
 // version 2.1 of the License, or (at your option) any later version.
 9  
 //
 10  
 // This library is distributed in the hope that it will be useful,
 11  
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // Lesser General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU Lesser General Public
 16  
 // License along with this library; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 ////////////////////////////////////////////////////////////////////////////////
 19  
 
 20  
 package com.puppycrawl.tools.checkstyle.filters;
 21  
 
 22  
 import java.util.Collections;
 23  
 import java.util.Objects;
 24  
 import java.util.Set;
 25  
 
 26  
 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
 27  
 import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
 28  
 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
 29  
 import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder;
 30  
 import com.puppycrawl.tools.checkstyle.api.Filter;
 31  
 import com.puppycrawl.tools.checkstyle.api.FilterSet;
 32  
 import com.puppycrawl.tools.checkstyle.utils.FilterUtils;
 33  
 
 34  
 /**
 35  
  * <p>
 36  
  * This filter accepts AuditEvents according to file, check, line, and
 37  
  * column, as specified in a suppression file.
 38  
  * </p>
 39  
  * @author Rick Giles
 40  
  * @author <a href="mailto:piotr.listkiewicz@gmail.com">liscju</a>
 41  
  * @noinspection NonFinalFieldReferenceInEquals, NonFinalFieldReferencedInHashCode
 42  
  */
 43  15
 public class SuppressionFilter extends AutomaticBean implements Filter, ExternalResourceHolder {
 44  
 
 45  
     /** Filename of suppression file. */
 46  
     private String file;
 47  
     /** Tells whether config file existence is optional. */
 48  
     private boolean optional;
 49  
     /** Set of individual suppresses. */
 50  15
     private FilterSet filters = new FilterSet();
 51  
 
 52  
     /**
 53  
      * Sets name of the suppression file.
 54  
      * @param fileName name of the suppressions file.
 55  
      */
 56  
     public void setFile(String fileName) {
 57  13
         file = fileName;
 58  13
     }
 59  
 
 60  
     /**
 61  
      * Sets whether config file existence is optional.
 62  
      * @param optional tells if config file existence is optional.
 63  
      */
 64  
     public void setOptional(boolean optional) {
 65  8
         this.optional = optional;
 66  8
     }
 67  
 
 68  
     @Override
 69  
     public boolean accept(AuditEvent event) {
 70  6
         return filters.accept(event);
 71  
     }
 72  
 
 73  
     @Override
 74  
     public boolean equals(Object obj) {
 75  100
         if (this == obj) {
 76  2
             return true;
 77  
         }
 78  98
         if (obj == null || getClass() != obj.getClass()) {
 79  5
             return false;
 80  
         }
 81  93
         final SuppressionFilter suppressionFilter = (SuppressionFilter) obj;
 82  93
         return Objects.equals(filters, suppressionFilter.filters);
 83  
     }
 84  
 
 85  
     @Override
 86  
     public int hashCode() {
 87  38
         return Objects.hash(filters);
 88  
     }
 89  
 
 90  
     @Override
 91  
     protected void finishLocalSetup() throws CheckstyleException {
 92  14
         if (file != null) {
 93  12
             if (optional) {
 94  4
                 if (FilterUtils.isFileExists(file)) {
 95  2
                     filters = SuppressionsLoader.loadSuppressions(file);
 96  
                 }
 97  
                 else {
 98  2
                     filters = new FilterSet();
 99  
                 }
 100  
             }
 101  
             else {
 102  8
                 filters = SuppressionsLoader.loadSuppressions(file);
 103  
             }
 104  
         }
 105  12
     }
 106  
 
 107  
     @Override
 108  
     public Set<String> getExternalResourceLocations() {
 109  5
         return Collections.singleton(file);
 110  
     }
 111  
 }