001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2021 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;
024import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
025
026/**
027 * Handler for if statements.
028 *
029 */
030public class IfHandler 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 IfHandler(IndentationCheck indentCheck,
041        DetailAST ast, AbstractExpressionHandler parent) {
042        super(indentCheck, "if", ast, parent);
043    }
044
045    @Override
046    public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
047        final IndentLevel result;
048        if (child instanceof ElseHandler) {
049            result = getIndent();
050        }
051        else {
052            result = super.getSuggestedChildIndent(child);
053        }
054        return result;
055    }
056
057    @Override
058    protected IndentLevel getIndentImpl() {
059        final IndentLevel result;
060        if (isIfAfterElse()) {
061            result = getParent().getIndent();
062        }
063        else {
064            result = super.getIndentImpl();
065        }
066        return result;
067    }
068
069    /**
070     * Determines if this 'if' statement is part of an 'else' clause
071     * and on the same line.
072     *
073     * @return true if this 'if' is part of an 'else', false otherwise
074     */
075    private boolean isIfAfterElse() {
076        // check if there is an 'else' and an 'if' on the same line
077        final DetailAST parent = getMainAst().getParent();
078        return parent.getType() == TokenTypes.LITERAL_ELSE
079            && TokenUtil.areOnSameLine(parent, getMainAst());
080    }
081
082    @Override
083    protected void checkTopLevelToken() {
084        if (!isIfAfterElse()) {
085            super.checkTopLevelToken();
086        }
087    }
088
089    /**
090     * Check the indentation of the conditional expression.
091     */
092    private void checkCondExpr() {
093        final DetailAST condAst = getMainAst().findFirstToken(TokenTypes.LPAREN)
094            .getNextSibling();
095        final IndentLevel expected =
096            new IndentLevel(getIndent(), getBasicOffset());
097        checkExpressionSubtree(condAst, expected, false, false);
098    }
099
100    @Override
101    public void checkIndentation() {
102        super.checkIndentation();
103        checkCondExpr();
104        checkWrappingIndentation(getMainAst(), getIfStatementRightParen(getMainAst()));
105    }
106
107    /**
108     * Returns right parenthesis of if statement.
109     *
110     * @param literalIfAst
111     *          literal-if ast node(TokenTypes.LITERAL_IF)
112     * @return right parenthesis of if statement.
113     */
114    private static DetailAST getIfStatementRightParen(DetailAST literalIfAst) {
115        return literalIfAst.findFirstToken(TokenTypes.RPAREN);
116    }
117
118}