001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 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//////////////////////////////////////////////////////////////////////////////// 019package com.puppycrawl.tools.checkstyle.checks.coding; 020 021import com.puppycrawl.tools.checkstyle.api.TokenTypes; 022import com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck; 023 024/** 025 * <p> 026 * Checks that switch statement has "default" clause. 027 * </p> 028 * <p> 029 * Rationale: It's usually a good idea to introduce a 030 * default case in every switch statement. Even if 031 * the developer is sure that all currently possible 032 * cases are covered, this should be expressed in the 033 * default branch, e.g. by using an assertion. This way 034 * the code is protected aginst later changes, e.g. 035 * introduction of new types in an enumeration type. 036 * </p> 037 * <p> 038 * An example of how to configure the check is: 039 * </p> 040 * <pre> 041 * <module name="MissingSwitchDefault"/> 042 * </pre> 043 * @author o_sukhodolsky 044 */ 045public class MissingSwitchDefaultCheck extends DescendantTokenCheck 046{ 047 /** Creates new instance of the check. */ 048 public MissingSwitchDefaultCheck() 049 { 050 setLimitedTokens(new String[] { 051 TokenTypes.getTokenName(TokenTypes.LITERAL_DEFAULT), 052 }); 053 setMinimumNumber(1); 054 setMaximumDepth(2); 055 setMinimumMessage("missing.switch.default"); 056 } 057 058 @Override 059 public int[] getDefaultTokens() 060 { 061 return new int[]{TokenTypes.LITERAL_SWITCH}; 062 } 063 064 @Override 065 public int[] getAcceptableTokens() 066 { 067 return getDefaultTokens(); 068 } 069}