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 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 /** 033 * Construct an instance of this handler with the given indentation check, 034 * abstract syntax tree, and parent handler. 035 * 036 * @param indentCheck the indentation check 037 * @param ast the abstract syntax tree 038 * @param parent the parent handler 039 */ 040 public ClassDefHandler(IndentationCheck indentCheck, 041 DetailAST ast, 042 AbstractExpressionHandler parent) { 043 super(indentCheck, getHandlerName(ast), ast, parent); 044 } 045 046 @Override 047 protected DetailAST getLeftCurly() { 048 return getMainAst().findFirstToken(TokenTypes.OBJBLOCK) 049 .findFirstToken(TokenTypes.LCURLY); 050 } 051 052 @Override 053 protected DetailAST getRightCurly() { 054 return getMainAst().findFirstToken(TokenTypes.OBJBLOCK) 055 .findFirstToken(TokenTypes.RCURLY); 056 } 057 058 @Override 059 protected DetailAST getTopLevelAst() { 060 return null; 061 // note: ident checked by hand in check indentation; 062 } 063 064 @Override 065 protected DetailAST getListChild() { 066 return getMainAst().findFirstToken(TokenTypes.OBJBLOCK); 067 } 068 069 @Override 070 public void checkIndentation() { 071 final DetailAST modifiers = getMainAst().findFirstToken(TokenTypes.MODIFIERS); 072 if (modifiers.getChildCount() == 0) { 073 if (getMainAst().getType() != TokenTypes.ANNOTATION_DEF) { 074 final DetailAST ident = getMainAst().findFirstToken(TokenTypes.IDENT); 075 final int lineStart = getLineStart(ident); 076 if (!getIndent().isAcceptable(lineStart)) { 077 logError(ident, "ident", lineStart); 078 } 079 } 080 } 081 else { 082 checkModifiers(); 083 } 084 if (getMainAst().getType() == TokenTypes.ANNOTATION_DEF) { 085 final DetailAST atAst = getMainAst().findFirstToken(TokenTypes.AT); 086 if (isOnStartOfLine(atAst)) { 087 checkWrappingIndentation(getMainAst(), getListChild(), 0, 088 getIndent().getFirstIndentLevel(), false); 089 } 090 } 091 else { 092 checkWrappingIndentation(getMainAst(), getListChild()); 093 } 094 super.checkIndentation(); 095 } 096 097 @Override 098 protected int[] getCheckedChildren() { 099 return new int[] { 100 TokenTypes.EXPR, 101 TokenTypes.OBJBLOCK, 102 TokenTypes.LITERAL_BREAK, 103 TokenTypes.LITERAL_RETURN, 104 TokenTypes.LITERAL_THROW, 105 TokenTypes.LITERAL_CONTINUE, 106 }; 107 } 108 109 /** 110 * Creates a handler name for this class according to ast type. 111 * 112 * @param ast the abstract syntax tree. 113 * @return handler name for this class. 114 */ 115 private static String getHandlerName(DetailAST ast) { 116 final String name; 117 118 if (ast.getType() == TokenTypes.CLASS_DEF) { 119 name = "class def"; 120 } 121 else if (ast.getType() == TokenTypes.ENUM_DEF) { 122 name = "enum def"; 123 } 124 else if (ast.getType() == TokenTypes.ANNOTATION_DEF) { 125 name = "annotation def"; 126 } 127 else { 128 name = "interface def"; 129 } 130 return name; 131 } 132 133}