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 ArrayInitHandler extends BlockParentHandler 30 { 31 39 public ArrayInitHandler(IndentationCheck aIndentCheck, 40 DetailAST aAst, ExpressionHandler aParent) 41 { 42 super(aIndentCheck, "array initialization", aAst, aParent); 43 } 44 45 50 protected IndentLevel getLevelImpl() 51 { 52 final DetailAST parentAST = getMainAst().getParent(); 53 final int type = parentAST.getType(); 54 if ((type == TokenTypes.LITERAL_NEW) || (type == TokenTypes.ASSIGN)) { 55 return new IndentLevel(getLineStart(parentAST)); 57 } 58 else if (getParent() instanceof ArrayInitHandler) { 59 return ((ArrayInitHandler) getParent()).getChildrenExpectedLevel(); 60 } 61 else { 62 return getParent().getLevel(); 63 } 64 } 65 66 71 protected DetailAST getToplevelAST() 72 { 73 return null; 74 } 75 76 81 protected DetailAST getLCurly() 82 { 83 return getMainAst(); 84 } 85 86 91 protected DetailAST getRCurly() 92 { 93 return getMainAst().findFirstToken(TokenTypes.RCURLY); 94 } 95 96 101 protected boolean rcurlyMustStart() 102 { 103 return false; 104 } 105 106 111 protected boolean childrenMayNest() 112 { 113 return true; 114 } 115 116 121 protected DetailAST getListChild() 122 { 123 return getMainAst(); 124 } 125 126 127 protected IndentLevel getChildrenExpectedLevel() 128 { 129 136 final IndentLevel expectedIndent = super.getChildrenExpectedLevel(); 137 138 final int firstLine = getFirstLine(Integer.MAX_VALUE, getListChild()); 139 if (hasCurlys() && (firstLine == getLCurly().getLineNo())) { 140 final int lcurlyPos = expandedTabsColumnNo(getLCurly()); 141 final int firstChildPos = 142 getNextFirstNonblankOnLineAfter(firstLine, lcurlyPos); 143 if (firstChildPos >= 0) { 144 expectedIndent.addAcceptedIndent(firstChildPos); 145 } 146 } 147 return expectedIndent; 148 } 149 150 158 private int getNextFirstNonblankOnLineAfter(int aLineNo, int aColumnNo) 159 { 160 int columnNo = aColumnNo + 1; 161 final String line = getIndentCheck().getLines()[aLineNo - 1]; 162 final int lineLength = line.length(); 163 while ((columnNo < lineLength) 164 && Character.isWhitespace(line.charAt(columnNo))) 165 { 166 columnNo++; 167 } 168 169 return (columnNo == lineLength) ? -1 : columnNo; 170 } 171 } 172 | Popular Tags |