001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2016 the original author or authors. 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//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.coding; 021 022import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 023import com.puppycrawl.tools.checkstyle.api.DetailAST; 024import com.puppycrawl.tools.checkstyle.api.TokenTypes; 025 026/** 027 * <p> 028 * Check that the {@code default} is after all the {@code case}s 029 * in a {@code switch} statement. 030 * </p> 031 * <p> 032 * Rationale: Java allows {@code default} anywhere within the 033 * {@code switch} statement. But if it comes after the last 034 * {@code case} then it is more readable. 035 * </p> 036 * <p> 037 * An example of how to configure the check is: 038 * </p> 039 * <pre> 040 * <module name="DefaultComesLast"/> 041 * </pre> 042 * @author o_sukhodolsky 043 */ 044public class DefaultComesLastCheck extends AbstractCheck { 045 046 /** 047 * A key is pointing to the warning message text in "messages.properties" 048 * file. 049 */ 050 public static final String MSG_KEY = "default.comes.last"; 051 052 @Override 053 public int[] getAcceptableTokens() { 054 return new int[] { 055 TokenTypes.LITERAL_DEFAULT, 056 }; 057 } 058 059 @Override 060 public int[] getDefaultTokens() { 061 return getAcceptableTokens(); 062 } 063 064 @Override 065 public int[] getRequiredTokens() { 066 return getAcceptableTokens(); 067 } 068 069 @Override 070 public void visitToken(DetailAST ast) { 071 final DetailAST defaultGroupAST = ast.getParent(); 072 //default keywords used in annotations too - not what we're 073 //interested in 074 if (defaultGroupAST.getType() != TokenTypes.ANNOTATION_FIELD_DEF 075 && defaultGroupAST.getType() != TokenTypes.MODIFIERS) { 076 final DetailAST switchAST = defaultGroupAST.getParent(); 077 final DetailAST lastGroupAST = 078 switchAST.getLastChild().getPreviousSibling(); 079 080 if (defaultGroupAST.getLineNo() != lastGroupAST.getLineNo() 081 || defaultGroupAST.getColumnNo() 082 != lastGroupAST.getColumnNo()) { 083 log(ast, MSG_KEY); 084 } 085 } 086 } 087}