1 package com.puppycrawl.tools.checkstyle.checks.indentation; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 23 24 29 public class IfHandler extends BlockParentHandler 30 { 31 39 public IfHandler(IndentationCheck aIndentCheck, 40 DetailAST aAst, ExpressionHandler aParent) 41 { 42 super(aIndentCheck, "if", aAst, aParent); 43 } 44 45 54 public IndentLevel suggestedChildLevel(ExpressionHandler aChild) 55 { 56 if (aChild instanceof ElseHandler) { 57 return getLevel(); 58 } 59 return super.suggestedChildLevel(aChild); 60 } 61 62 67 protected IndentLevel getLevelImpl() 68 { 69 if (isIfAfterElse()) { 70 return getParent().getLevel(); 71 } 72 return super.getLevelImpl(); 73 } 74 75 81 private boolean isIfAfterElse() 82 { 83 final DetailAST parent = getMainAst().getParent(); 85 return (parent.getType() == TokenTypes.LITERAL_ELSE) 86 && (parent.getLineNo() == getMainAst().getLineNo()); 87 } 88 89 92 protected void checkToplevelToken() 93 { 94 if (isIfAfterElse()) { 95 return; 96 } 97 98 super.checkToplevelToken(); 99 } 100 101 104 private void checkCondExpr() 105 { 106 final DetailAST condAst = (DetailAST) 107 getMainAst().findFirstToken(TokenTypes.LPAREN).getNextSibling(); 108 final IndentLevel expected = 109 new IndentLevel(getLevel(), getBasicOffset()); 110 checkExpressionSubtree(condAst, expected, false, false); 111 } 112 113 116 public void checkIndentation() 117 { 118 super.checkIndentation(); 119 checkCondExpr(); 120 } 121 } 122 | Popular Tags |