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 MethodCallHandler extends ExpressionHandler 30 { 31 39 public MethodCallHandler(IndentationCheck aIndentCheck, 40 DetailAST aAST, ExpressionHandler aParent) 41 { 42 super(aIndentCheck, 43 aAST.getType() == TokenTypes.METHOD_CALL 44 ? "method call" : "ctor call", 45 aAST, 46 aParent); 47 } 48 49 54 protected IndentLevel getLevelImpl() 55 { 56 if (getParent() instanceof MethodCallHandler) { 59 final MethodCallHandler container = 60 ((MethodCallHandler) getParent()); 61 if (container != null) { 62 if (areOnSameLine(container.getMainAst(), getMainAst())) { 63 return container.getLevel(); 64 } 65 66 final DetailAST main = getMainAst(); 69 final DetailAST dot = (DetailAST) main.getFirstChild(); 70 final DetailAST target = (DetailAST) dot.getFirstChild(); 71 72 if ((dot.getType() == TokenTypes.DOT) 73 && (target.getType() == TokenTypes.METHOD_CALL)) 74 { 75 final DetailAST dot1 = (DetailAST) target.getFirstChild(); 76 final DetailAST target1 = (DetailAST) dot1.getFirstChild(); 77 78 if ((dot1.getType() == TokenTypes.DOT) 79 && (target1.getType() == TokenTypes.METHOD_CALL)) 80 { 81 return container.getLevel(); 82 } 83 } 84 return new IndentLevel(container.getLevel(), getBasicOffset()); 85 } 86 87 91 ExpressionHandler p = getParent(); 92 while (p instanceof MethodCallHandler) { 93 p = p.getParent(); 94 } 95 return p.suggestedChildLevel(this); 96 } 97 98 final LineSet lines = new LineSet(); 101 findSubtreeLines(lines, (DetailAST) getMainAst().getFirstChild(), true); 102 final int firstCol = lines.firstLineCol(); 103 final int lineStart = getLineStart(getFirstAst(getMainAst())); 104 if (lineStart != firstCol) { 105 return new IndentLevel(lineStart); 106 } 107 return super.getLevelImpl(); 108 } 109 110 118 private DetailAST getFirstAst(DetailAST aAst) 119 { 120 123 DetailAST ast = (DetailAST) aAst.getFirstChild(); 124 while ((ast != null) && (ast.getType() == TokenTypes.DOT)) { 125 ast = (DetailAST) ast.getFirstChild(); 126 } 127 128 if (ast == null) { 129 ast = aAst; 130 } 131 132 return ast; 133 } 134 135 144 public IndentLevel suggestedChildLevel(ExpressionHandler aChild) 145 { 146 152 final DetailAST first = (DetailAST) getMainAst().getFirstChild(); 153 int indentLevel = getLineStart(first); 154 if (!areOnSameLine((DetailAST) aChild.getMainAst().getFirstChild(), 155 (DetailAST) getMainAst().getFirstChild())) 156 { 157 indentLevel += getBasicOffset(); 158 } 159 return new IndentLevel(indentLevel); 160 } 161 162 165 public void checkIndentation() 166 { 167 final DetailAST methodName = (DetailAST) getMainAst().getFirstChild(); 168 checkExpressionSubtree(methodName, getLevel(), false, false); 169 170 final DetailAST lparen = getMainAst(); 171 final DetailAST rparen = getMainAst().findFirstToken(TokenTypes.RPAREN); 172 checkLParen(lparen); 173 174 if (rparen.getLineNo() == lparen.getLineNo()) { 175 return; 176 } 177 178 188 checkExpressionSubtree( 189 getMainAst().findFirstToken(TokenTypes.ELIST), 190 new IndentLevel(getLevel(), getBasicOffset()), 191 false, true); 192 193 checkRParen(lparen, rparen); 194 } 195 196 201 protected boolean shouldIncreaseIndent() 202 { 203 return false; 204 } 205 } 206 | Popular Tags |