001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 027 028/** 029 * <p> 030 * Checks that switch statement has a {@code default} clause. 031 * </p> 032 * <p> 033 * Rationale: It's usually a good idea to introduce a 034 * default case in every switch statement. Even if 035 * the developer is sure that all currently possible 036 * cases are covered, this should be expressed in the 037 * default branch, e.g. by using an assertion. This way 038 * the code is protected against later changes, e.g. 039 * introduction of new types in an enumeration type. 040 * </p> 041 * <p> 042 * This check does not validate any switch expressions. Rationale: 043 * The compiler requires switch expressions to be exhaustive. This means 044 * that all possible inputs must be covered. 045 * </p> 046 * <p> 047 * This check does not validate switch statements that use pattern or null 048 * labels. Rationale: Switch statements that use pattern or null labels are 049 * checked by the compiler for exhaustiveness. This means that all possible 050 * inputs must be covered. 051 * </p> 052 * <p> 053 * See the <a href="https://docs.oracle.com/javase/specs/jls/se17/html/jls-15.html#jls-15.28"> 054 * Java Language Specification</a> for more information about switch statements 055 * and expressions. 056 * </p> 057 * <p> 058 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 059 * </p> 060 * <p> 061 * Violation Message Keys: 062 * </p> 063 * <ul> 064 * <li> 065 * {@code missing.switch.default} 066 * </li> 067 * </ul> 068 * 069 * @since 3.1 070 */ 071@StatelessCheck 072public class MissingSwitchDefaultCheck extends AbstractCheck { 073 074 /** 075 * A key is pointing to the warning message text in "messages.properties" 076 * file. 077 */ 078 public static final String MSG_KEY = "missing.switch.default"; 079 080 @Override 081 public int[] getDefaultTokens() { 082 return getRequiredTokens(); 083 } 084 085 @Override 086 public int[] getAcceptableTokens() { 087 return getRequiredTokens(); 088 } 089 090 @Override 091 public int[] getRequiredTokens() { 092 return new int[] {TokenTypes.LITERAL_SWITCH}; 093 } 094 095 @Override 096 public void visitToken(DetailAST ast) { 097 if (!containsDefaultLabel(ast) 098 && !containsPatternCaseLabelElement(ast) 099 && !containsDefaultCaseLabelElement(ast) 100 && !containsNullCaseLabelElement(ast) 101 && !isSwitchExpression(ast)) { 102 log(ast, MSG_KEY); 103 } 104 } 105 106 /** 107 * Checks if the case group or its sibling contain the 'default' switch. 108 * 109 * @param detailAst first case group to check. 110 * @return true if 'default' switch found. 111 */ 112 private static boolean containsDefaultLabel(DetailAST detailAst) { 113 return TokenUtil.findFirstTokenByPredicate(detailAst, 114 ast -> ast.findFirstToken(TokenTypes.LITERAL_DEFAULT) != null 115 ).isPresent(); 116 } 117 118 /** 119 * Checks if a switch block contains a case label with a pattern variable definition. 120 * In this situation, the compiler enforces the given switch block to cover 121 * all possible inputs, and we do not need a default label. 122 * 123 * @param detailAst first case group to check. 124 * @return true if switch block contains a pattern case label element 125 */ 126 private static boolean containsPatternCaseLabelElement(DetailAST detailAst) { 127 return TokenUtil.findFirstTokenByPredicate(detailAst, ast -> { 128 return ast.getFirstChild() != null 129 && ast.getFirstChild().findFirstToken(TokenTypes.PATTERN_VARIABLE_DEF) != null; 130 }).isPresent(); 131 } 132 133 /** 134 * Checks if a switch block contains a default case label. 135 * 136 * @param detailAst first case group to check. 137 * @return true if switch block contains default case label 138 */ 139 private static boolean containsDefaultCaseLabelElement(DetailAST detailAst) { 140 return TokenUtil.findFirstTokenByPredicate(detailAst, ast -> { 141 return ast.getFirstChild() != null 142 && ast.getFirstChild().findFirstToken(TokenTypes.LITERAL_DEFAULT) != null; 143 }).isPresent(); 144 } 145 146 /** 147 * Checks if a switch block contains a null case label. 148 * 149 * @param detailAst first case group to check. 150 * @return true if switch block contains null case label 151 */ 152 private static boolean containsNullCaseLabelElement(DetailAST detailAst) { 153 return TokenUtil.findFirstTokenByPredicate(detailAst, ast -> { 154 return ast.getFirstChild() != null 155 && hasNullCaseLabel(ast.getFirstChild()); 156 }).isPresent(); 157 } 158 159 /** 160 * Checks if this LITERAL_SWITCH token is part of a switch expression. 161 * 162 * @param ast the switch statement we are checking 163 * @return true if part of a switch expression 164 */ 165 private static boolean isSwitchExpression(DetailAST ast) { 166 return ast.getParent().getType() == TokenTypes.EXPR 167 || ast.getParent().getParent().getType() == TokenTypes.EXPR; 168 } 169 170 /** 171 * Checks if the case contains null label. 172 * 173 * @param ast the switch statement we are checking 174 * @return returnValue the ast of null label 175 */ 176 private static boolean hasNullCaseLabel(DetailAST ast) { 177 final DetailAST firstChild = ast.getFirstChild(); 178 return firstChild != null 179 && TokenUtil.isOfType(firstChild.getFirstChild(), TokenTypes.LITERAL_NULL); 180 181 } 182}