|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ArrayInitHandler | Line # 29 | 94% |
0.94
|
18.07 | 28 | 18 | 18 | 0.64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (4) | |||
| Result | |||
|
0.94
|
com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheckTest.testValidArrayInitWithChecker
com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheckTest.testValidArrayInitWithChecker
|
1 PASS | |
|
0.86
|
com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheckTest.testInvalidArrayInitWithChecker
com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheckTest.testInvalidArrayInitWithChecker
|
1 PASS | |
|
0.36
|
com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheckTest.testValidMethodWithChecker
com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheckTest.testValidMethodWithChecker
|
1 PASS | |
|
0.36
|
com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheckTest.testInvalidMethodWithChecker
com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheckTest.testInvalidMethodWithChecker
|
1 PASS | |
| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2005 Oliver Burn | |
| 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 | 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 | /** | |
| 25 | * Handler for array initialization blocks. | |
| 26 | * | |
| 27 | * @author jrichard | |
| 28 | */ | |
| 29 | public class ArrayInitHandler 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 aIndentCheck the indentation check | |
| 36 | * @param aAst the abstract syntax tree | |
| 37 | * @param aParent the parent handler | |
| 38 | */ | |
| 39 | 60 |
public ArrayInitHandler(IndentationCheck aIndentCheck, |
| 40 | DetailAST aAst, ExpressionHandler aParent) | |
| 41 | { | |
| 42 | 60 | super(aIndentCheck, "array initialization", aAst, aParent); |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * Compute the indentation amount for this handler. | |
| 47 | * | |
| 48 | * @return the expected indentation amount | |
| 49 | */ | |
| 50 | 60 |
protected IndentLevel getLevelImpl() |
| 51 | { | |
| 52 | 60 | final DetailAST parentAST = getMainAst().getParent(); |
| 53 | 60 | final int type = parentAST.getType(); |
| 54 | 60 | if ((type == TokenTypes.LITERAL_NEW) || (type == TokenTypes.ASSIGN)) { |
| 55 | // note: assumes new or assignment is line to align with | |
| 56 | 58 | return new IndentLevel(getLineStart(parentAST)); |
| 57 | } | |
| 58 | 2 | else if (getParent() instanceof ArrayInitHandler) { |
| 59 | 2 | return ((ArrayInitHandler) getParent()).getChildrenExpectedLevel(); |
| 60 | } | |
| 61 | else { | |
| 62 | 0 | return getParent().getLevel(); |
| 63 | } | |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * There is no top level expression for this handler. | |
| 68 | * | |
| 69 | * @return null | |
| 70 | */ | |
| 71 | 60 |
protected DetailAST getToplevelAST() |
| 72 | { | |
| 73 | 60 | return null; |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * Get the left curly brace portion of the expression we are handling. | |
| 78 | * | |
| 79 | * @return the left curly brace expression | |
| 80 | */ | |
| 81 | 420 |
protected DetailAST getLCurly() |
| 82 | { | |
| 83 | 420 | return getMainAst(); |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Get the right curly brace portion of the expression we are handling. | |
| 88 | * | |
| 89 | * @return the right curly brace expression | |
| 90 | */ | |
| 91 | 280 |
protected DetailAST getRCurly() |
| 92 | { | |
| 93 | 280 | return getMainAst().findFirstToken(TokenTypes.RCURLY); |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * Determines if the right curly brace must be at the start of the line. | |
| 98 | * | |
| 99 | * @return false | |
| 100 | */ | |
| 101 | 42 |
protected boolean rcurlyMustStart() |
| 102 | { | |
| 103 | 42 | return false; |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Determines if child elements within the expression may be nested. | |
| 108 | * | |
| 109 | * @return true | |
| 110 | */ | |
| 111 | 30 |
protected boolean childrenMayNest() |
| 112 | { | |
| 113 | 30 | return true; |
| 114 | } | |
| 115 | ||
| 116 | /** | |
| 117 | * Get the child element representing the list of statements. | |
| 118 | * | |
| 119 | * @return the statement list child | |
| 120 | */ | |
| 121 | 100 |
protected DetailAST getListChild() |
| 122 | { | |
| 123 | 100 | return getMainAst(); |
| 124 | } | |
| 125 | ||
| 126 | /** {@inheritDoc} */ | |
| 127 | 40 |
protected IndentLevel getChildrenExpectedLevel() |
| 128 | { | |
| 129 | // now we accept | |
| 130 | // new int[] {1, | |
| 131 | // 2}; | |
| 132 | // and | |
| 133 | // new int[] {1, 2, | |
| 134 | // 3}; | |
| 135 | ||
| 136 | 40 | final IndentLevel expectedIndent = super.getChildrenExpectedLevel(); |
| 137 | ||
| 138 | 40 | final int firstLine = getFirstLine(Integer.MAX_VALUE, getListChild()); |
| 139 | 40 | if (hasCurlys() && (firstLine == getLCurly().getLineNo())) { |
| 140 | 40 | final int lcurlyPos = expandedTabsColumnNo(getLCurly()); |
| 141 | 40 | final int firstChildPos = |
| 142 | getNextFirstNonblankOnLineAfter(firstLine, lcurlyPos); | |
| 143 | 40 | if (firstChildPos >= 0) { |
| 144 | 3 | expectedIndent.addAcceptedIndent(firstChildPos); |
| 145 | } | |
| 146 | } | |
| 147 | 40 | return expectedIndent; |
| 148 | } | |
| 149 | ||
| 150 | /** | |
| 151 | * @param aLineNo number of line on which we search | |
| 152 | * @param aColumnNo number of column after which we search | |
| 153 | * | |
| 154 | * @return column number of first non-blank char after | |
| 155 | * specified column on specified line or -1 if | |
| 156 | * such char doesn't exist. | |
| 157 | */ | |