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 com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024 025/** 026 * Handler for member definitions. 027 * 028 * @author o_sukhodolsky 029 */ 030public class MemberDefHandler extends AbstractExpressionHandler { 031 /** 032 * Construct an instance of this handler with the given indentation check, 033 * abstract syntax tree, and parent handler. 034 * 035 * @param indentCheck the indentation check 036 * @param ast the abstract syntax tree 037 * @param parent the parent handler 038 */ 039 public MemberDefHandler(IndentationCheck indentCheck, 040 DetailAST ast, AbstractExpressionHandler parent) { 041 super(indentCheck, "member def", ast, parent); 042 } 043 044 @Override 045 public void checkIndentation() { 046 final DetailAST modifiersNode = getMainAst().findFirstToken(TokenTypes.MODIFIERS); 047 if (modifiersNode.getChildCount() == 0) { 048 checkType(); 049 } 050 else { 051 checkModifiers(); 052 } 053 final DetailAST firstNode = getMainAst(); 054 final DetailAST lastNode = getVarDefStatementSemicolon(firstNode); 055 056 if (lastNode != null && !isArrayDeclaration(firstNode)) { 057 checkWrappingIndentation(firstNode, lastNode); 058 } 059 } 060 061 @Override 062 public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) { 063 return getIndent(); 064 } 065 066 @Override 067 protected void checkModifiers() { 068 final DetailAST modifier = getMainAst().findFirstToken(TokenTypes.MODIFIERS); 069 if (isOnStartOfLine(modifier) 070 && !getIndent().isAcceptable(expandedTabsColumnNo(modifier))) { 071 logError(modifier, "modifier", expandedTabsColumnNo(modifier)); 072 } 073 } 074 075 /** 076 * Check the indentation of the method type. 077 */ 078 private void checkType() { 079 final DetailAST type = getMainAst().findFirstToken(TokenTypes.TYPE); 080 final DetailAST ident = AbstractExpressionHandler.getFirstToken(type); 081 final int columnNo = expandedTabsColumnNo(ident); 082 if (isOnStartOfLine(ident) && !getIndent().isAcceptable(columnNo)) { 083 logError(ident, "type", columnNo); 084 } 085 } 086 087 /** 088 * Checks if variable_def node is array declaration. 089 * @param variableDef current variable_def. 090 * @return true if variable_def node is array declaration. 091 */ 092 private static boolean isArrayDeclaration(DetailAST variableDef) { 093 return variableDef.findFirstToken(TokenTypes.TYPE) 094 .findFirstToken(TokenTypes.ARRAY_DECLARATOR) != null; 095 } 096 097 /** 098 * Returns semicolon for variable definition statement. 099 * @param variableDef 100 * ast node of type TokenTypes.VARIABLE_DEF 101 * @return ast node of type TokenTypes.SEMI 102 */ 103 private static DetailAST getVarDefStatementSemicolon(DetailAST variableDef) { 104 DetailAST lastNode = variableDef.getLastChild(); 105 if (lastNode.getType() != TokenTypes.SEMI) { 106 lastNode = variableDef.getNextSibling(); 107 } 108 return lastNode; 109 } 110}