View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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   * @author jrichard
29   */
30  public class IfHandler extends BlockParentHandler {
31  
32      /**
33       * Construct an instance of this handler with the given indentation check,
34       * abstract syntax tree, and parent handler.
35       *
36       * @param indentCheck   the indentation check
37       * @param ast           the abstract syntax tree
38       * @param parent        the parent handler
39       */
40      public IfHandler(IndentationCheck indentCheck,
41          DetailAST ast, AbstractExpressionHandler parent) {
42          super(indentCheck, "if", ast, parent);
43      }
44  
45      @Override
46      public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
47          final IndentLevel result;
48          if (child instanceof ElseHandler) {
49              result = getIndent();
50          }
51          else {
52              result = super.getSuggestedChildIndent(child);
53          }
54          return result;
55      }
56  
57      @Override
58      protected IndentLevel getIndentImpl() {
59          final IndentLevel result;
60          if (isIfAfterElse()) {
61              result = getParent().getIndent();
62          }
63          else {
64              result = super.getIndentImpl();
65          }
66          return result;
67      }
68  
69      /**
70       * Determines if this 'if' statement is part of an 'else' clause
71       * and on the same line.
72       *
73       * @return true if this 'if' is part of an 'else', false otherwise
74       */
75      private boolean isIfAfterElse() {
76          // check if there is an 'else' and an 'if' on the same line
77          final DetailAST parent = getMainAst().getParent();
78          return parent.getType() == TokenTypes.LITERAL_ELSE
79              && parent.getLineNo() == getMainAst().getLineNo();
80      }
81  
82      @Override
83      protected void checkTopLevelToken() {
84          if (!isIfAfterElse()) {
85              super.checkTopLevelToken();
86          }
87      }
88  
89      /**
90       * Check the indentation of the conditional expression.
91       */
92      private void checkCondExpr() {
93          final DetailAST condAst = getMainAst().findFirstToken(TokenTypes.LPAREN)
94              .getNextSibling();
95          final IndentLevel expected =
96              new IndentLevel(getIndent(), getBasicOffset());
97          checkExpressionSubtree(condAst, expected, false, false);
98      }
99  
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      * @param literalIfAst
110      *          literal-if ast node(TokenTypes.LITERAL_IF)
111      * @return right parenthesis of if statement.
112      */
113     private static DetailAST getIfStatementRightParen(DetailAST literalIfAst) {
114         return literalIfAst.findFirstToken(TokenTypes.RPAREN);
115     }
116 
117 }