| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MissingSwitchDefaultCheck |
|
| 1.0;1 |
| 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.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 22 | import com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck; | |
| 23 | ||
| 24 | /** | |
| 25 | * <p> | |
| 26 | * Checks that switch statement has "default" clause. | |
| 27 | * </p> | |
| 28 | * <p> | |
| 29 | * Rationale: It's usually a good idea to introduce a | |
| 30 | * default case in every switch statement. Even if | |
| 31 | * the developer is sure that all currently possible | |
| 32 | * cases are covered, this should be expressed in the | |
| 33 | * default branch, e.g. by using an assertion. This way | |
| 34 | * the code is protected aginst later changes, e.g. | |
| 35 | * introduction of new types in an enumeration type. | |
| 36 | * </p> | |
| 37 | * <p> | |
| 38 | * An example of how to configure the check is: | |
| 39 | * </p> | |
| 40 | * <pre> | |
| 41 | * <module name="MissingSwitchDefault"/> | |
| 42 | * </pre> | |
| 43 | * @author o_sukhodolsky | |
| 44 | */ | |
| 45 | public class MissingSwitchDefaultCheck extends DescendantTokenCheck | |
| 46 | { | |
| 47 | /** Creates new instance of the check. */ | |
| 48 | public MissingSwitchDefaultCheck() | |
| 49 | 1 | { |
| 50 | 1 | setLimitedTokens(new String[] { |
| 51 | TokenTypes.getTokenName(TokenTypes.LITERAL_DEFAULT), | |
| 52 | }); | |
| 53 | 1 | setMinimumNumber(1); |
| 54 | 1 | setMaximumDepth(2); |
| 55 | 1 | setMinimumMessage("missing.switch.default"); |
| 56 | 1 | } |
| 57 | ||
| 58 | @Override | |
| 59 | public int[] getDefaultTokens() | |
| 60 | { | |
| 61 | 1 | return new int[]{TokenTypes.LITERAL_SWITCH}; |
| 62 | } | |
| 63 | ||
| 64 | @Override | |
| 65 | public int[] getAcceptableTokens() | |
| 66 | { | |
| 67 | 0 | return getDefaultTokens(); |
| 68 | } | |
| 69 | } |