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