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