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 /** 063 * Creates a new {@code UseEnhancedSwitchCheck} instance. 064 */ 065 public UseEnhancedSwitchCheck() { 066 // no code by default 067 } 068 069 @Override 070 public int[] getDefaultTokens() { 071 return getRequiredTokens(); 072 } 073 074 @Override 075 public int[] getAcceptableTokens() { 076 return getRequiredTokens(); 077 } 078 079 @Override 080 public int[] getRequiredTokens() { 081 return new int[] {TokenTypes.LITERAL_SWITCH}; 082 } 083 084 @Override 085 public void visitToken(DetailAST ast) { 086 final List<DetailAST> caseGroups = getCaseGroups(ast); 087 if (!caseGroups.isEmpty() && allCaseGroupsTerminate(caseGroups)) { 088 log(ast, MSG_KEY); 089 } 090 } 091 092 /** 093 * Check if all case groups terminate (i.e. do not fall through), except the last one which 094 * terminates regardless of its content. 095 * 096 * @param caseGroups the list of case groups to check 097 * @return {@code true} if all case groups terminate, {@code false} otherwise 098 */ 099 private static boolean allCaseGroupsTerminate(List<DetailAST> caseGroups) { 100 boolean allCaseGroupsTerminate = true; 101 for (int index = 0; index < caseGroups.size() - 1; index++) { 102 final DetailAST statementList = caseGroups.get(index).findFirstToken(TokenTypes.SLIST); 103 104 if (!CheckUtil.isTerminated(statementList)) { 105 allCaseGroupsTerminate = false; 106 break; 107 } 108 } 109 return allCaseGroupsTerminate; 110 } 111 112 /** 113 * Get all case groups from the switch. 114 * 115 * @param switchAst the AST node representing a {@code LITERAL_SWITCH} 116 * @return all {@code CASE_GROUP} nodes within the switch 117 */ 118 private static List<DetailAST> getCaseGroups(DetailAST switchAst) { 119 final List<DetailAST> caseGroups = new ArrayList<>(); 120 DetailAST ast = switchAst.getFirstChild(); 121 while (ast != null) { 122 if (ast.getType() == TokenTypes.CASE_GROUP) { 123 caseGroups.add(ast); 124 } 125 ast = ast.getNextSibling(); 126 } 127 return Collections.unmodifiableList(caseGroups); 128 } 129 130}