Coverage Report - com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
MissingSwitchDefaultCheck
100%
16/16
100%
6/6
1.6
 
 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.checks.coding;
 21  
 
 22  
 import com.puppycrawl.tools.checkstyle.StatelessCheck;
 23  
 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
 24  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 25  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 26  
 
 27  
 /**
 28  
  * <p>
 29  
  * Checks that switch statement has &quot;default&quot; clause.
 30  
  * </p>
 31  
  * <p>
 32  
  * Rationale: It's usually a good idea to introduce a
 33  
  * default case in every switch statement. Even if
 34  
  * the developer is sure that all currently possible
 35  
  * cases are covered, this should be expressed in the
 36  
  * default branch, e.g. by using an assertion. This way
 37  
  * the code is protected against later changes, e.g.
 38  
  * introduction of new types in an enumeration type.
 39  
  * </p>
 40  
  * <p>
 41  
  * An example of how to configure the check is:
 42  
  * </p>
 43  
  * <pre>
 44  
  * &lt;module name="MissingSwitchDefault"/&gt;
 45  
  * </pre>
 46  
  * @author o_sukhodolsky
 47  
  */
 48  
 @StatelessCheck
 49  11
 public class MissingSwitchDefaultCheck extends AbstractCheck {
 50  
 
 51  
     /**
 52  
      * A key is pointing to the warning message text in "messages.properties"
 53  
      * file.
 54  
      */
 55  
     public static final String MSG_KEY = "missing.switch.default";
 56  
 
 57  
     @Override
 58  
     public int[] getDefaultTokens() {
 59  14
         return getRequiredTokens();
 60  
     }
 61  
 
 62  
     @Override
 63  
     public int[] getAcceptableTokens() {
 64  6
         return getRequiredTokens();
 65  
     }
 66  
 
 67  
     @Override
 68  
     public int[] getRequiredTokens() {
 69  34
         return new int[] {TokenTypes.LITERAL_SWITCH};
 70  
     }
 71  
 
 72  
     @Override
 73  
     public void visitToken(DetailAST ast) {
 74  2
         final DetailAST firstCaseGroupAst = ast.findFirstToken(TokenTypes.CASE_GROUP);
 75  
 
 76  2
         if (!containsDefaultSwitch(firstCaseGroupAst)) {
 77  1
             log(ast.getLineNo(), MSG_KEY);
 78  
         }
 79  2
     }
 80  
 
 81  
     /**
 82  
      * Checks if the case group or its sibling contain the 'default' switch.
 83  
      * @param caseGroupAst first case group to check.
 84  
      * @return true if 'default' switch found.
 85  
      */
 86  
     private static boolean containsDefaultSwitch(DetailAST caseGroupAst) {
 87  2
         DetailAST nextAst = caseGroupAst;
 88  2
         boolean found = false;
 89  
 
 90  7
         while (nextAst != null) {
 91  6
             if (nextAst.findFirstToken(TokenTypes.LITERAL_DEFAULT) != null) {
 92  1
                 found = true;
 93  1
                 break;
 94  
             }
 95  
 
 96  5
             nextAst = nextAst.getNextSibling();
 97  
         }
 98  
 
 99  2
         return found;
 100  
     }
 101  
 }