Coverage Report - com.puppycrawl.tools.checkstyle.checks.coding.IllegalThrowsCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
IllegalThrowsCheck
95%
20/21
100%
10/10
1.833
 
 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.checks.coding;
 20  
 
 21  
 import com.google.common.collect.Sets;
 22  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 23  
 import com.puppycrawl.tools.checkstyle.api.FullIdent;
 24  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 25  
 import java.util.Set;
 26  
 
 27  
 /**
 28  
  * Throwing java.lang.Error or java.lang.RuntimeException
 29  
  * is almost never acceptable.
 30  
  * @author Oliver Burn
 31  
  */
 32  
 public final class IllegalThrowsCheck extends AbstractIllegalCheck
 33  
 {
 34  
 
 35  
     /** Default ignored method names. */
 36  1
     private static final String[] DEFAULT_IGNORED_METHOD_NAMES = {
 37  
         "finalize",
 38  
     };
 39  
 
 40  
     /** methods which should be ignored. */
 41  4
     private final Set<String> mIgnoredMethodNames = Sets.newHashSet();
 42  
 
 43  
     /** Creates new instance of the check. */
 44  
     public IllegalThrowsCheck()
 45  
     {
 46  4
         super(new String[] {"Error",
 47  
                             "RuntimeException", "Throwable",
 48  
                             "java.lang.Error",
 49  
                             "java.lang.RuntimeException",
 50  
                             "java.lang.Throwable",
 51  
         });
 52  4
         setIgnoredMethodNames(DEFAULT_IGNORED_METHOD_NAMES);
 53  4
     }
 54  
 
 55  
     @Override
 56  
     public int[] getDefaultTokens()
 57  
     {
 58  4
         return new int[] {TokenTypes.LITERAL_THROWS};
 59  
     }
 60  
 
 61  
     @Override
 62  
     public int[] getRequiredTokens()
 63  
     {
 64  0
         return getDefaultTokens();
 65  
     }
 66  
 
 67  
     @Override
 68  
     public void visitToken(DetailAST aDetailAST)
 69  
     {
 70  16
         DetailAST token = aDetailAST.getFirstChild();
 71  
         // Check if the method with the given name should be ignored.
 72  16
         if (!(shouldIgnoreMethod(aDetailAST.getParent().findFirstToken(
 73  
                                      TokenTypes.IDENT).getText())))
 74  
         {
 75  28
             while (token != null) {
 76  16
                 if (token.getType() != TokenTypes.COMMA) {
 77  14
                     final FullIdent ident = FullIdent.createFullIdent(token);
 78  14
                     if (isIllegalClassName(ident.getText())) {
 79  9
                         log(token, "illegal.throw", ident.getText());
 80  
                     }
 81  
                 }
 82  16
                 token = token.getNextSibling();
 83  
             }
 84  
         }
 85  16
     }
 86  
 
 87  
     /**
 88  
      * Check if the method is specified in the ignore method list
 89  
      * @param aName the name to check
 90  
      * @return whether the method with the passed name should be ignored
 91  
      */
 92  
     private boolean shouldIgnoreMethod(String aName)
 93  
     {
 94  16
         return mIgnoredMethodNames.contains(aName);
 95  
     }
 96  
 
 97  
     /**
 98  
      * Set the list of ignore method names.
 99  
      * @param aMethodNames array of ignored method names
 100  
      */
 101  
     public void setIgnoredMethodNames(String[] aMethodNames)
 102  
     {
 103  6
         mIgnoredMethodNames.clear();
 104  12
         for (String element : aMethodNames) {
 105  6
             mIgnoredMethodNames.add(element);
 106  
         }
 107  6
     }
 108  
 }