KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > indentation > ArrayInitHandler


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     public ArrayInitHandler(IndentationCheck aIndentCheck,
40         DetailAST aAst, ExpressionHandler aParent)
41     {
42         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     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             // note: assumes new or assignment is line to align with
56
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     /**
67      * There is no top level expression for this handler.
68      *
69      * @return null
70      */

71     protected DetailAST getToplevelAST()
72     {
73         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     protected DetailAST getLCurly()
82     {
83         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     protected DetailAST getRCurly()
92     {
93         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     protected boolean rcurlyMustStart()
102     {
103         return false;
104     }
105
106     /**
107      * Determines if child elements within the expression may be nested.
108      *
109      * @return true
110      */

111     protected boolean childrenMayNest()
112     {
113         return true;
114     }
115
116     /**
117      * Get the child element representing the list of statements.
118      *
119      * @return the statement list child
120      */

121     protected DetailAST getListChild()
122     {
123         return getMainAst();
124     }
125
126     /** {@inheritDoc} */
127     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         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     /**
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      */

158     private int getNextFirstNonblankOnLineAfter(int aLineNo, int aColumnNo)
159     {
160         int columnNo = aColumnNo + 1;
161         final String JavaDoc 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