001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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 java.util.ArrayList; 023import java.util.Collections; 024import java.util.List; 025 026import com.puppycrawl.tools.checkstyle.StatelessCheck; 027import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 028import com.puppycrawl.tools.checkstyle.api.DetailAST; 029import com.puppycrawl.tools.checkstyle.api.TokenTypes; 030import com.puppycrawl.tools.checkstyle.utils.CheckUtil; 031 032/** 033 * <div> 034 * Ensures that the enhanced switch (using {@code ->} for case labels) is used 035 * instead of the traditional switch (using {@code :} for case labels) where possible. 036 * </div> 037 * 038 * <p> 039 * Rationale: Java 14 has introduced enhancements for switch statements and expressions 040 * that disallow fall-through behavior. The enhanced switch syntax using {@code ->} 041 * for case labels typically leads to more concise and readable code, reducing the likelihood 042 * of errors associated with fall-through cases. 043 * </p> 044 * 045 * <p> 046 * See the <a href="https://docs.oracle.com/javase/specs/jls/se22/html/jls-14.html#jls-14.11.1-200"> 047 * Java Language Specification</a> for more information about {@code ->} case labels, also known as 048 * "switch rules". 049 * </p> 050 * 051 * @since 13.3.0 052 */ 053@StatelessCheck 054public class UseEnhancedSwitchCheck extends AbstractCheck { 055 056 /** 057 * A key is pointing to the warning message text in "messages.properties" 058 * file. 059 */ 060 public static final String MSG_KEY = "use.enhanced.switch"; 061 062 @Override 063 public int[] getDefaultTokens() { 064 return getRequiredTokens(); 065 } 066 067 @Override 068 public int[] getAcceptableTokens() { 069 return getRequiredTokens(); 070 } 071 072 @Override 073 public int[] getRequiredTokens() { 074 return new int[] {TokenTypes.LITERAL_SWITCH}; 075 } 076 077 @Override 078 public void visitToken(DetailAST ast) { 079 final List<DetailAST> caseGroups = getCaseGroups(ast); 080 if (!caseGroups.isEmpty() && allCaseGroupsTerminate(caseGroups)) { 081 log(ast, MSG_KEY); 082 } 083 } 084 085 /** 086 * Check if all case groups terminate (i.e. do not fall through), except the last one which 087 * terminates regardless of its content. 088 * 089 * @param caseGroups the list of case groups to check 090 * @return {@code true} if all case groups terminate, {@code false} otherwise 091 */ 092 private static boolean allCaseGroupsTerminate(List<DetailAST> caseGroups) { 093 boolean allCaseGroupsTerminate = true; 094 for (int index = 0; index < caseGroups.size() - 1; index++) { 095 final DetailAST statementList = caseGroups.get(index).findFirstToken(TokenTypes.SLIST); 096 097 if (!CheckUtil.isTerminated(statementList)) { 098 allCaseGroupsTerminate = false; 099 break; 100 } 101 } 102 return allCaseGroupsTerminate; 103 } 104 105 /** 106 * Get all case groups from the switch. 107 * 108 * @param switchAst the AST node representing a {@code LITERAL_SWITCH} 109 * @return all {@code CASE_GROUP} nodes within the switch 110 */ 111 private static List<DetailAST> getCaseGroups(DetailAST switchAst) { 112 final List<DetailAST> caseGroups = new ArrayList<>(); 113 DetailAST ast = switchAst.getFirstChild(); 114 while (ast != null) { 115 if (ast.getType() == TokenTypes.CASE_GROUP) { 116 caseGroups.add(ast); 117 } 118 ast = ast.getNextSibling(); 119 } 120 return Collections.unmodifiableList(caseGroups); 121 } 122 123}