001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 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 * @author jrichard 031 */ 032public class SlistHandler extends BlockParentHandler { 033 034 /** 035 * Parent token types. 036 */ 037 private static final int[] PARENT_TOKEN_TYPES = { 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 }; 051 052 static { 053 // Array sorting for binary search 054 Arrays.sort(PARENT_TOKEN_TYPES); 055 } 056 057 /** 058 * Construct an instance of this handler with the given indentation check, 059 * abstract syntax tree, and parent handler. 060 * 061 * @param indentCheck the indentation check 062 * @param ast the abstract syntax tree 063 * @param parent the parent handler 064 */ 065 public SlistHandler(IndentationCheck indentCheck, 066 DetailAST ast, AbstractExpressionHandler parent) { 067 super(indentCheck, "block", ast, parent); 068 } 069 070 @Override 071 public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) { 072 // this is: 073 // switch (var) { 074 // case 3: { 075 // break; 076 // } 077 // } 078 // ... the case SLIST is followed by a user-created SLIST and 079 // preceded by a switch 080 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 return getParent().getSuggestedChildIndent(child); 087 } 088 return super.getSuggestedChildIndent(child); 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 Arrays.binarySearch(PARENT_TOKEN_TYPES, parentType) >= 0; 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 * @return true, if SLIST node is places at the same line as CASE_GROUP node. 133 */ 134 private boolean isSameLineCaseGroup() { 135 final DetailAST parentNode = getMainAst().getParent(); 136 return parentNode.getType() == TokenTypes.CASE_GROUP 137 && getMainAst().getLineNo() == parentNode.getLineNo(); 138 } 139}