001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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.indentation; 021 022import com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 025 026/** 027 * Handler for switch statements. 028 * 029 */ 030public class SwitchHandler extends BlockParentHandler { 031 032 /** 033 * Token types that, when appearing as a parent or grandparent of the 034 * current switch expression, indicate that the expression is likely 035 * line-wrapped and should be indented. 036 */ 037 private static final int[] LINE_WRAPPING_INDENT_TRIGGERS = { 038 TokenTypes.ASSIGN, 039 TokenTypes.SWITCH_RULE, 040 TokenTypes.LAMBDA, 041 }; 042 043 /** 044 * Construct an instance of this handler with the given indentation check, 045 * abstract syntax tree, and parent handler. 046 * 047 * @param indentCheck the indentation check 048 * @param ast the abstract syntax tree 049 * @param parent the parent handler 050 */ 051 public SwitchHandler(IndentationCheck indentCheck, 052 DetailAST ast, AbstractExpressionHandler parent) { 053 super(indentCheck, "switch", ast, parent); 054 } 055 056 @Override 057 protected DetailAST getLeftCurly() { 058 return getMainAst().findFirstToken(TokenTypes.LCURLY); 059 } 060 061 @Override 062 protected DetailAST getRightCurly() { 063 return getMainAst().findFirstToken(TokenTypes.RCURLY); 064 } 065 066 @Override 067 protected DetailAST getListChild() { 068 // all children should be taken care of by case handler (plus 069 // there is no parent of just the cases, if checking is needed 070 // here in the future, an additional way beyond checkChildren() 071 // will have to be devised to get children) 072 return null; 073 } 074 075 @Override 076 protected DetailAST getNonListChild() { 077 return null; 078 } 079 080 /** 081 * Check the indentation of the switch expression. 082 */ 083 private void checkSwitchExpr() { 084 checkExpressionSubtree( 085 getMainAst().findFirstToken(TokenTypes.LPAREN).getNextSibling(), 086 getIndent(), 087 false, 088 false); 089 } 090 091 @Override 092 protected IndentLevel getIndentImpl() { 093 IndentLevel indentLevel = super.getIndentImpl(); 094 // if switch is starting the line 095 if (isOnStartOfLine(getMainAst())) { 096 final DetailAST parent = getMainAst().getParent(); 097 final DetailAST grandParent = parent.getParent(); 098 099 if (shouldIndentDueToWrapping(parent, grandParent)) { 100 indentLevel = new IndentLevel(indentLevel, 101 getIndentCheck().getLineWrappingIndentation()); 102 } 103 } 104 return indentLevel; 105 } 106 107 /** 108 * Determines if indentation is needed due to line wrapping caused by intermediate nodes 109 * between the current AST node and its enclosing handler node. 110 * 111 * @param directParent The immediate parent node of the current AST node 112 * @param grandParent The grandparent node of the current AST node 113 * @return true if either the direct parent or grandparent requires additional indentation, 114 * but only when they are not the enclosing handler node itself 115 */ 116 private boolean shouldIndentDueToWrapping(DetailAST directParent, DetailAST grandParent) { 117 // The enclosing handler node that already determines base indentation 118 // (e.g., method declaration containing our current node) 119 final DetailAST enclosingHandlerNode = getParent().getMainAst(); 120 final boolean isDirectParentTheHandler = directParent.equals(enclosingHandlerNode); 121 122 final boolean shouldIndentForDirectParent = !isDirectParentTheHandler 123 && isWrappingTrigger(directParent); 124 125 // Check if grandparent requires extra indentation (when 126 // neither it nor direct parent is the handler) 127 final boolean shouldIndentForGrandParent = !isDirectParentTheHandler 128 && !grandParent.equals(enclosingHandlerNode) 129 && isWrappingTrigger(grandParent); 130 131 return shouldIndentForDirectParent || shouldIndentForGrandParent; 132 } 133 134 /** 135 * Checks if the given AST node represents a construct that typically causes line wrapping 136 * and therefore requires additional indentation level. 137 * 138 * @param astNode The AST node to check 139 * @return true if the node type matches one of the line-wrapping triggers 140 * (e.g., assignments, switch rules, lambdas) 141 */ 142 private static boolean isWrappingTrigger(DetailAST astNode) { 143 return TokenUtil.isOfType(astNode, LINE_WRAPPING_INDENT_TRIGGERS); 144 } 145 146 @Override 147 public void checkIndentation() { 148 checkSwitchExpr(); 149 super.checkIndentation(); 150 } 151 152}