Coverage Report - com.puppycrawl.tools.checkstyle.filters.SuppressElement
 
Classes in this File Line Coverage Branch Coverage Complexity
SuppressElement
80%
52/65
69%
46/66
6.222
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  Oliver Burn
 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  
 package com.puppycrawl.tools.checkstyle.filters;
 20  
 
 21  
 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
 22  
 import com.puppycrawl.tools.checkstyle.api.Filter;
 23  
 import com.puppycrawl.tools.checkstyle.api.Utils;
 24  
 import java.util.regex.Pattern;
 25  
 import java.util.regex.PatternSyntaxException;
 26  
 
 27  
 /**
 28  
  * This filter processes {@link com.puppycrawl.tools.checkstyle.api.AuditEvent}
 29  
  * objects based on the criteria of file, check, module id, line, and
 30  
  * column. It rejects an AuditEvent if the following match:
 31  
  * <ul>
 32  
  *   <li>the event's file name; and
 33  
  *   <li>the check name or the module identifier; and
 34  
  *   <li>(optionally) the event's line is in the filter's line CSV; and
 35  
  *   <li>(optionally) the check's columns is in the filter's column CSV.
 36  
  * </ul>
 37  
  *
 38  
  * @author Rick Giles
 39  
  */
 40  
 public class SuppressElement
 41  
     implements Filter
 42  
 {
 43  
     /** hash function multiplicand */
 44  
     private static final int HASH_MULT = 29;
 45  
 
 46  
     /** the regexp to match file names against */
 47  
     private final Pattern mFileRegexp;
 48  
 
 49  
     /** the pattern for file names*/
 50  
     private final String mFilePattern;
 51  
 
 52  
     /** the regexp to match check names against */
 53  
     private Pattern mCheckRegexp;
 54  
 
 55  
     /** the pattern for check class names*/
 56  
     private String mCheckPattern;
 57  
 
 58  
     /** module id filter. */
 59  
     private String mModuleId;
 60  
 
 61  
     /** line number filter */
 62  
     private CSVFilter mLineFilter;
 63  
 
 64  
     /** CSV for line number filter */
 65  
     private String mLinesCSV;
 66  
 
 67  
     /** column number filter */
 68  
     private CSVFilter mColumnFilter;
 69  
 
 70  
     /** CSV for column number filter */
 71  
     private String mColumnsCSV;
 72  
 
 73  
     /**
 74  
      * Constructs a <code>SuppressElement</code> for a
 75  
      * file name pattern. Must either call {@link #setColumns(String)} or
 76  
      * {@link #setModuleId(String)} before using this object.
 77  
      * @param aFiles regular expression for names of filtered files.
 78  
      * @throws PatternSyntaxException if there is an error.
 79  
      */
 80  
     public SuppressElement(String aFiles)
 81  
         throws PatternSyntaxException
 82  16
     {
 83  16
         mFilePattern = aFiles;
 84  16
         mFileRegexp = Utils.getPattern(aFiles);
 85  16
     }
 86  
 
 87  
     /**
 88  
      * Set the check class pattern.
 89  
      * @param aChecks regular expression for filtered check classes.
 90  
      */
 91  
     public void setChecks(final String aChecks)
 92  
     {
 93  16
         mCheckPattern = aChecks;
 94  16
         mCheckRegexp = Utils.getPattern(aChecks);
 95  16
     }
 96  
 
 97  
     /**
 98  
      * Set the module id for filtering. Cannot be null.
 99  
      * @param aModuleId the id
 100  
      */
 101  
     public void setModuleId(final String aModuleId)
 102  
     {
 103  0
         mModuleId = aModuleId;
 104  0
     }
 105  
     /**
 106  
      * Sets the CSV values and ranges for line number filtering.
 107  
      * E.g. "1,7-15,18".
 108  
      * @param aLines CSV values and ranges for line number filtering.
 109  
      */
 110  
     public void setLines(String aLines)
 111  
     {
 112  9
         mLinesCSV = aLines;
 113  9
         if (aLines != null) {
 114  9
             mLineFilter = new CSVFilter(aLines);
 115  
         }
 116  
         else {
 117  0
             mLineFilter = null;
 118  
         }
 119  8
     }
 120  
 
 121  
     /**
 122  
      * Sets the CSV values and ranges for column number filtering.
 123  
      *  E.g. "1,7-15,18".
 124  
      * @param aColumns CSV values and ranges for column number filtering.
 125  
      */
 126  
     public void setColumns(String aColumns)
 127  
     {
 128  12
         mColumnsCSV = aColumns;
 129  12
         if (aColumns != null) {
 130  10
             mColumnFilter = new CSVFilter(aColumns);
 131  
         }
 132  
         else {
 133  2
             mColumnFilter = null;
 134  
         }
 135  12
     }
 136  
 
 137  
     /** {@inheritDoc} */
 138  
     public boolean accept(AuditEvent aEvent)
 139  
     {
 140  
         // file and check match?
 141  6
         if ((aEvent.getFileName() == null)
 142  
                 || !mFileRegexp.matcher(aEvent.getFileName()).find()
 143  
                 || (aEvent.getLocalizedMessage() == null)
 144  
                 || ((mModuleId != null) && !mModuleId.equals(aEvent
 145  
                         .getModuleId()))
 146  
                 || ((mCheckRegexp != null) && !mCheckRegexp.matcher(
 147  
                         aEvent.getSourceName()).find()))
 148  
         {
 149  1
             return true;
 150  
         }
 151  
 
 152  
         // reject if no line/column matching
 153  5
         if ((mLineFilter == null) && (mColumnFilter == null)) {
 154  1
             return false;
 155  
         }
 156  
 
 157  4
         if (mLineFilter != null && mLineFilter.accept(aEvent.getLine())) {
 158  1
             return false;
 159  
         }
 160  
 
 161  3
         if (mColumnFilter != null && mColumnFilter.accept(aEvent.getColumn())) {
 162  1
             return false;
 163  
         }
 164  2
         return true;
 165  
     }
 166  
 
 167  
     @Override
 168  
     public String toString()
 169  
     {
 170  0
         return "SuppressElement[files=" + mFilePattern + ",checks="
 171  
             + mCheckPattern + ",lines=" + mLinesCSV + ",columns="
 172  
             + mColumnsCSV + "]";
 173  
     }
 174  
 
 175  
     @Override
 176  
     public int hashCode()
 177  
     {
 178  12
         int result = HASH_MULT * mFilePattern.hashCode();
 179  12
         if (mCheckPattern != null) {
 180  12
             result = HASH_MULT * result + mCheckPattern.hashCode();
 181  
         }
 182  12
         if (mModuleId != null) {
 183  0
             result = HASH_MULT * result + mModuleId.hashCode();
 184  
         }
 185  12
         if (mLinesCSV != null) {
 186  6
             result = HASH_MULT * result + mLinesCSV.hashCode();
 187  
         }
 188  12
         if (mColumnsCSV != null) {
 189  6
             result = HASH_MULT * result + mColumnsCSV.hashCode();
 190  
         }
 191  12
         return result;
 192  
     }
 193  
 
 194  
     @Override
 195  
     public boolean equals(Object aObject)
 196  
     {
 197  13
         if (aObject instanceof SuppressElement) {
 198  13
             final SuppressElement other = (SuppressElement) aObject;
 199  
 
 200  
             // same file pattern?
 201  13
             if (!this.mFilePattern.equals(other.mFilePattern)) {
 202  0
                 return false;
 203  
             }
 204  
 
 205  
             // same check pattern?
 206  13
             if (mCheckPattern != null) {
 207  13
                 if (!mCheckPattern.equals(other.mCheckPattern)) {
 208  1
                     return false;
 209  
                 }
 210  
             }
 211  0
             else if (other.mCheckPattern != null) {
 212  0
                 return false;
 213  
             }
 214  
 
 215  
             // same module id?
 216  12
             if (mModuleId != null) {
 217  0
                 if (!mModuleId.equals(other.mModuleId)) {
 218  0
                     return false;
 219  
                 }
 220  
             }
 221  12
             else if (other.mModuleId != null) {
 222  0
                 return false;
 223  
             }
 224  
 
 225  
             // same line number filter?
 226  12
             if (mLineFilter != null) {
 227  6
                 if (!mLineFilter.equals(other.mLineFilter)) {
 228  1
                     return false;
 229  
                 }
 230  
             }
 231  6
             else if (other.mLineFilter != null) {
 232  0
                 return false;
 233  
             }
 234  
 
 235  
             // same column number filter?
 236  11
             if (mColumnFilter != null) {
 237  6
                 if (!mColumnFilter.equals(other.mColumnFilter)) {
 238  2
                     return false;
 239  
                 }
 240  
             }
 241  5
             else if (other.mColumnFilter != null) {
 242  1
                 return false;
 243  
             }
 244  
 
 245  
             // everything is the same
 246  8
             return true;
 247  
         }
 248  0
         return false;
 249  
     }
 250  
 }