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.indentation; 021 022import java.util.BitSet; 023 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 027 028/** 029 * Handler for a list of statements. 030 * 031 */ 032public class SlistHandler extends BlockParentHandler { 033 034 /** 035 * Parent token types. 036 */ 037 private static final BitSet PARENT_TOKEN_TYPES = TokenUtil.asBitSet( 038 TokenTypes.CTOR_DEF, 039 TokenTypes.METHOD_DEF, 040 TokenTypes.STATIC_INIT, 041 TokenTypes.LITERAL_SYNCHRONIZED, 042 TokenTypes.LITERAL_IF, 043 TokenTypes.LITERAL_WHILE, 044 TokenTypes.LITERAL_DO, 045 TokenTypes.LITERAL_FOR, 046 TokenTypes.LITERAL_ELSE, 047 TokenTypes.LITERAL_TRY, 048 TokenTypes.LITERAL_CATCH, 049 TokenTypes.LITERAL_FINALLY, 050 TokenTypes.COMPACT_CTOR_DEF 051 ); 052 053 /** 054 * Construct an instance of this handler with the given indentation check, 055 * abstract syntax tree, and parent handler. 056 * 057 * @param indentCheck the indentation check 058 * @param ast the abstract syntax tree 059 * @param parent the parent handler 060 */ 061 public SlistHandler(IndentationCheck indentCheck, 062 DetailAST ast, AbstractExpressionHandler parent) { 063 super(indentCheck, "block", ast, parent); 064 } 065 066 @Override 067 public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) { 068 // this is: 069 // switch (var) { 070 // case 3: { 071 // break; 072 // } 073 // } 074 // ... the case SLIST is followed by a user-created SLIST and 075 // preceded by a switch 076 077 final IndentLevel result; 078 // if our parent is a block handler we want to be transparent 079 if (getParent() instanceof BlockParentHandler 080 && !(getParent() instanceof SlistHandler) 081 || child instanceof SlistHandler 082 && getParent() instanceof CaseHandler) { 083 result = getParent().getSuggestedChildIndent(child); 084 } 085 else { 086 result = super.getSuggestedChildIndent(child); 087 } 088 return result; 089 } 090 091 @Override 092 protected DetailAST getListChild() { 093 return getMainAst(); 094 } 095 096 @Override 097 protected DetailAST getLeftCurly() { 098 return getMainAst(); 099 } 100 101 @Override 102 protected DetailAST getRightCurly() { 103 return getMainAst().findFirstToken(TokenTypes.RCURLY); 104 } 105 106 @Override 107 protected DetailAST getTopLevelAst() { 108 return null; 109 } 110 111 /** 112 * Determine if the expression we are handling has a block parent. 113 * 114 * @return true if it does, false otherwise 115 */ 116 private boolean hasBlockParent() { 117 final int parentType = getMainAst().getParent().getType(); 118 return PARENT_TOKEN_TYPES.get(parentType); 119 } 120 121 @Override 122 public void checkIndentation() { 123 // only need to check this if parent is not 124 // an if, else, while, do, ctor, method 125 if (!hasBlockParent() && !isSameLineCaseGroup()) { 126 super.checkIndentation(); 127 } 128 } 129 130 /** 131 * Checks if SLIST node is placed at the same line as CASE_GROUP node. 132 * 133 * @return true, if SLIST node is places at the same line as CASE_GROUP node. 134 */ 135 private boolean isSameLineCaseGroup() { 136 final DetailAST parentNode = getMainAst().getParent(); 137 return parentNode.getType() == TokenTypes.CASE_GROUP 138 && TokenUtil.areOnSameLine(getMainAst(), parentNode); 139 } 140 141}