1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2019 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.indentation;
21
22 import com.puppycrawl.tools.checkstyle.api.DetailAST;
23 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24
25 /**
26 * Handler for if statements.
27 *
28 */
29 public class IfHandler extends BlockParentHandler {
30
31 /**
32 * Construct an instance of this handler with the given indentation check,
33 * abstract syntax tree, and parent handler.
34 *
35 * @param indentCheck the indentation check
36 * @param ast the abstract syntax tree
37 * @param parent the parent handler
38 */
39 public IfHandler(IndentationCheck indentCheck,
40 DetailAST ast, AbstractExpressionHandler parent) {
41 super(indentCheck, "if", ast, parent);
42 }
43
44 @Override
45 public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
46 final IndentLevel result;
47 if (child instanceof ElseHandler) {
48 result = getIndent();
49 }
50 else {
51 result = super.getSuggestedChildIndent(child);
52 }
53 return result;
54 }
55
56 @Override
57 protected IndentLevel getIndentImpl() {
58 final IndentLevel result;
59 if (isIfAfterElse()) {
60 result = getParent().getIndent();
61 }
62 else {
63 result = super.getIndentImpl();
64 }
65 return result;
66 }
67
68 /**
69 * Determines if this 'if' statement is part of an 'else' clause
70 * and on the same line.
71 *
72 * @return true if this 'if' is part of an 'else', false otherwise
73 */
74 private boolean isIfAfterElse() {
75 // check if there is an 'else' and an 'if' on the same line
76 final DetailAST parent = getMainAst().getParent();
77 return parent.getType() == TokenTypes.LITERAL_ELSE
78 && parent.getLineNo() == getMainAst().getLineNo();
79 }
80
81 @Override
82 protected void checkTopLevelToken() {
83 if (!isIfAfterElse()) {
84 super.checkTopLevelToken();
85 }
86 }
87
88 /**
89 * Check the indentation of the conditional expression.
90 */
91 private void checkCondExpr() {
92 final DetailAST condAst = getMainAst().findFirstToken(TokenTypes.LPAREN)
93 .getNextSibling();
94 final IndentLevel expected =
95 new IndentLevel(getIndent(), getBasicOffset());
96 checkExpressionSubtree(condAst, expected, false, false);
97 }
98
99 @Override
100 public void checkIndentation() {
101 super.checkIndentation();
102 checkCondExpr();
103 checkWrappingIndentation(getMainAst(), getIfStatementRightParen(getMainAst()));
104 }
105
106 /**
107 * Returns right parenthesis of if statement.
108 * @param literalIfAst
109 * literal-if ast node(TokenTypes.LITERAL_IF)
110 * @return right parenthesis of if statement.
111 */
112 private static DetailAST getIfStatementRightParen(DetailAST literalIfAst) {
113 return literalIfAst.findFirstToken(TokenTypes.RPAREN);
114 }
115
116 }