| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DefaultComesLastCheck |
|
| 1.75;1.75 |
| 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.Check; | |
| 22 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 23 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 24 | ||
| 25 | /** | |
| 26 | * <p> | |
| 27 | * Check that the <code>default</code> is after all the <code>case</code>s | |
| 28 | * in a <code>switch</code> statement. | |
| 29 | * </p> | |
| 30 | * <p> | |
| 31 | * Rationale: Java allows <code>default</code> anywhere within the | |
| 32 | * <code>switch</code> statement. But if it comes after the last | |
| 33 | * <code>case</code> then it is more readable. | |
| 34 | * </p> | |
| 35 | * <p> | |
| 36 | * An example of how to configure the check is: | |
| 37 | * </p> | |
| 38 | * <pre> | |
| 39 | * <module name="DefaultComesLast"/> | |
| 40 | * </pre> | |
| 41 | * @author o_sukhodolsky | |
| 42 | */ | |
| 43 | public class DefaultComesLastCheck extends Check | |
| 44 | { | |
| 45 | /** Creates new instance of the check. */ | |
| 46 | public DefaultComesLastCheck() | |
| 47 | 1 | { |
| 48 | // do nothing | |
| 49 | 1 | } |
| 50 | ||
| 51 | @Override | |
| 52 | public int[] getDefaultTokens() | |
| 53 | { | |
| 54 | 1 | return new int[] { |
| 55 | TokenTypes.LITERAL_DEFAULT, | |
| 56 | }; | |
| 57 | } | |
| 58 | ||
| 59 | @Override | |
| 60 | public int[] getAcceptableTokens() | |
| 61 | { | |
| 62 | 0 | return getDefaultTokens(); |
| 63 | } | |
| 64 | ||
| 65 | @Override | |
| 66 | public void visitToken(DetailAST aAST) | |
| 67 | { | |
| 68 | 3 | final DetailAST defaultGroupAST = aAST.getParent(); |
| 69 | //default keywords used in annotations too - not what we're | |
| 70 | //interested in | |
| 71 | 3 | if (defaultGroupAST.getType() != TokenTypes.ANNOTATION_FIELD_DEF) { |
| 72 | 2 | final DetailAST switchAST = defaultGroupAST.getParent(); |
| 73 | 2 | final DetailAST lastGroupAST = |
| 74 | switchAST.getLastChild().getPreviousSibling(); | |
| 75 | ||
| 76 | 2 | if ((defaultGroupAST.getLineNo() != lastGroupAST.getLineNo()) |
| 77 | || (defaultGroupAST.getColumnNo() | |
| 78 | != lastGroupAST.getColumnNo())) | |
| 79 | { | |
| 80 | 1 | log(aAST, "default.comes.last"); |
| 81 | } | |
| 82 | } | |
| 83 | 3 | } |
| 84 | } |