001: ////////////////////////////////////////////////////////////////////////////////
002: // checkstyle: Checks Java source code for adherence to a set of rules.
003: // Copyright (C) 2001-2007 Oliver Burn
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: package com.puppycrawl.tools.checkstyle.checks.indentation;
020:
021: import com.puppycrawl.tools.checkstyle.api.DetailAST;
022: import com.puppycrawl.tools.checkstyle.api.TokenTypes;
023:
024: /**
025: * Handler for inner classes.
026: *
027: * @author jrichard
028: */
029: public class ObjectBlockHandler extends BlockParentHandler {
030: /**
031: * Construct an instance of this handler with the given indentation check,
032: * abstract syntax tree, and parent handler.
033: *
034: * @param aIndentCheck the indentation check
035: * @param aAst the abstract syntax tree
036: * @param aParent the parent handler
037: */
038: public ObjectBlockHandler(IndentationCheck aIndentCheck,
039: DetailAST aAst, ExpressionHandler aParent) {
040: super (aIndentCheck, "object def", aAst, aParent);
041: }
042:
043: /**
044: * There is no top level expression for this handler.
045: *
046: * @return null
047: */
048: protected DetailAST getToplevelAST() {
049: return null;
050: }
051:
052: /**
053: * Get the left curly brace portion of the expression we are handling.
054: *
055: * @return the left curly brace expression
056: */
057: protected DetailAST getLCurly() {
058: return getMainAst().findFirstToken(TokenTypes.LCURLY);
059: }
060:
061: /**
062: * Get the right curly brace portion of the expression we are handling.
063: *
064: * @return the right curly brace expression
065: */
066: protected DetailAST getRCurly() {
067: return getMainAst().findFirstToken(TokenTypes.RCURLY);
068: }
069:
070: /**
071: * Get the child element representing the list of statements.
072: *
073: * @return the statement list child
074: */
075: protected DetailAST getListChild() {
076: return getMainAst();
077: }
078:
079: /**
080: * Compute the indentation amount for this handler.
081: *
082: * @return the expected indentation amount
083: */
084: protected IndentLevel getLevelImpl() {
085: final DetailAST parentAST = getMainAst().getParent();
086: IndentLevel indent = getParent().getLevel();
087: if (parentAST.getType() == TokenTypes.LITERAL_NEW) {
088: indent.addAcceptedIndent(super .getLevelImpl());
089: } else if (parentAST.getType() == TokenTypes.ENUM_CONSTANT_DEF) {
090: indent = super .getLevelImpl();
091: }
092: return indent;
093: }
094:
095: /**
096: * Check the indentation of the expression we are handling.
097: */
098: public void checkIndentation() {
099: // if we have a class or interface as a parent, don't do anything,
100: // as this is checked by class def; so
101: // only do this if we have a new for a parent (anonymous inner
102: // class)
103: final DetailAST parentAST = getMainAst().getParent();
104: if (parentAST.getType() != TokenTypes.LITERAL_NEW) {
105: return;
106: }
107:
108: super.checkIndentation();
109: }
110: }
|