001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2015 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 class definitions. 027 * 028 * @author jrichard 029 */ 030public class ClassDefHandler extends BlockParentHandler { 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 ClassDefHandler(IndentationCheck indentCheck, 040 DetailAST ast, 041 AbstractExpressionHandler parent) { 042 super(indentCheck, getHandlerName(ast), ast, parent); 043 } 044 045 @Override 046 protected DetailAST getLCurly() { 047 return getMainAst().findFirstToken(TokenTypes.OBJBLOCK) 048 .findFirstToken(TokenTypes.LCURLY); 049 } 050 051 @Override 052 protected DetailAST getRCurly() { 053 return getMainAst().findFirstToken(TokenTypes.OBJBLOCK) 054 .findFirstToken(TokenTypes.RCURLY); 055 } 056 057 @Override 058 protected DetailAST getTopLevelAst() { 059 return null; 060 // note: ident checked by hand in check indentation; 061 } 062 063 @Override 064 protected DetailAST getListChild() { 065 return getMainAst().findFirstToken(TokenTypes.OBJBLOCK); 066 } 067 068 @Override 069 public void checkIndentation() { 070 final DetailAST modifiers = getMainAst().findFirstToken(TokenTypes.MODIFIERS); 071 if (modifiers.getChildCount() == 0) { 072 final DetailAST ident = getMainAst().findFirstToken(TokenTypes.IDENT); 073 final int lineStart = getLineStart(ident); 074 if (!getLevel().isAcceptable(lineStart)) { 075 logError(ident, "ident", lineStart); 076 } 077 078 } 079 else { 080 checkModifiers(); 081 } 082 083 final LineWrappingHandler lineWrap = 084 new LineWrappingHandler(getIndentCheck(), getMainAst(), getMainAst().getLastChild()); 085 lineWrap.checkIndentation(); 086 super.checkIndentation(); 087 } 088 089 @Override 090 protected int[] getCheckedChildren() { 091 return new int[] { 092 TokenTypes.EXPR, 093 TokenTypes.OBJBLOCK, 094 TokenTypes.LITERAL_BREAK, 095 TokenTypes.LITERAL_RETURN, 096 TokenTypes.LITERAL_THROW, 097 TokenTypes.LITERAL_CONTINUE, 098 }; 099 } 100 101 /** 102 * Creates a handler name for this class according to ast type. 103 * 104 * @param ast the abstract syntax tree. 105 * @return handler name for this class. 106 */ 107 private static String getHandlerName(DetailAST ast) { 108 final String name; 109 110 if (ast.getType() == TokenTypes.CLASS_DEF) { 111 name = "class def"; 112 } 113 else if (ast.getType() == TokenTypes.ENUM_DEF) { 114 name = "enum def"; 115 } 116 else { 117 name = "interface def"; 118 } 119 return name; 120 } 121}