001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.indentation;
020
021import com.puppycrawl.tools.checkstyle.api.DetailAST;
022import com.puppycrawl.tools.checkstyle.api.TokenTypes;
023
024/**
025 * Handler for operator new.
026 * @author o_sukhodolsky
027 * @author Ilja Dubinin
028 */
029public class NewHandler extends ExpressionHandler
030{
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 NewHandler(IndentationCheck indentCheck,
040                      DetailAST ast,
041                      ExpressionHandler parent)
042    {
043        super(indentCheck, "operator new", ast, parent);
044    }
045
046    @Override
047    public void checkIndentation()
048    {
049        final DetailAST type = getMainAst().getFirstChild();
050        if (type != null) {
051            checkExpressionSubtree(type, getLevel(), false, false);
052        }
053
054        final DetailAST lparen = getMainAst().findFirstToken(TokenTypes.LPAREN);
055        final DetailAST rparen = getMainAst().findFirstToken(TokenTypes.RPAREN);
056        checkLParen(lparen);
057
058        if ((rparen == null) || (lparen == null)
059            || (rparen.getLineNo() == lparen.getLineNo()))
060        {
061            return;
062        }
063
064        if (getMainAst().getType() != TokenTypes.OBJBLOCK) {
065            return;
066        }
067
068        // if this method name is on the same line as a containing
069        // method, don't indent, this allows expressions like:
070        //    method("my str" + method2(
071        //        "my str2"));
072        // as well as
073        //    method("my str" +
074        //        method2(
075        //            "my str2"));
076        //
077
078        checkExpressionSubtree(
079            getMainAst().findFirstToken(TokenTypes.ELIST),
080            new IndentLevel(getLevel(), getBasicOffset()),
081            false, true);
082
083        checkRParen(lparen, rparen);
084    }
085
086    @Override
087    protected IndentLevel getLevelImpl()
088    {
089        // if our expression isn't first on the line, just use the start
090        // of the line
091        if (getLineStart(getMainAst()) != getMainAst().getColumnNo()) {
092            return new IndentLevel(getLineStart(getMainAst()));
093        }
094        return super.getLevelImpl();
095    }
096
097    @Override
098    protected boolean shouldIncreaseIndent()
099    {
100        return false;
101    }
102}