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