KickJava   Java API By Example, From Geeks To Geeks.

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


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 a list of statements.
26  *
27  * @author jrichard
28  */

29 public class SlistHandler 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 SlistHandler(IndentationCheck aIndentCheck,
40         DetailAST aAst, ExpressionHandler aParent)
41     {
42         super(aIndentCheck, "block", aAst, aParent);
43     }
44
45     /**
46      * Indentation level suggested for a child element. Children don't have
47      * to respect this, but most do.
48      *
49      * @param aChild child AST (so suggestion level can differ based on child
50      * type)
51      *
52      * @return suggested indentation for child
53      */

54     public IndentLevel suggestedChildLevel(ExpressionHandler aChild)
55     {
56         // this is:
57
// switch (var) {
58
// case 3: {
59
// break;
60
// }
61
// }
62
// ... the case SLIST is followed by a user-created SLIST and
63
// preceded by a switch
64

65         // if our parent is a block handler we want to be transparent
66
if (((getParent() instanceof BlockParentHandler)
67                 && !(getParent() instanceof SlistHandler))
68             || ((getParent() instanceof CaseHandler)
69                 && (aChild instanceof SlistHandler)))
70         {
71             return getParent().suggestedChildLevel(aChild);
72         }
73         return super.suggestedChildLevel(aChild);
74     }
75
76     /**
77      * Get the child element that is not a list of statements.
78      *
79      * @return the non-list child element
80      */

81     protected DetailAST getNonlistChild()
82     {
83         // blocks always have either block children or they are transparent
84
// and aren't checking children at all. In the later case, the
85
// superclass will want to check single children, so when it
86
// does tell it we have none.
87
return null;
88     }
89
90     /**
91      * Get the child element representing the list of statements.
92      *
93      * @return the statement list child
94      */

95     protected DetailAST getListChild()
96     {
97         return getMainAst();
98     }
99
100     /**
101      * Get the left curly brace portion of the expression we are handling.
102      *
103      * @return the left curly brace expression
104      */

105     protected DetailAST getLCurly()
106     {
107         return getMainAst();
108     }
109
110     /**
111      * Get the right curly brace portion of the expression we are handling.
112      *
113      * @return the right curly brace expression
114      */

115     protected DetailAST getRCurly()
116     {
117         return getMainAst().findFirstToken(TokenTypes.RCURLY);
118     }
119
120     /**
121      * There is no top level expression for this handler.
122      *
123      * @return null
124      */

125     protected DetailAST getToplevelAST()
126     {
127         return null;
128     }
129
130     /**
131      * Determine if the expression we are handling has a block parent.
132      *
133      * @return true if it does, false otherwise
134      */

135     private boolean hasBlockParent()
136     {
137         final int parentType = getMainAst().getParent().getType();
138         return (parentType == TokenTypes.LITERAL_IF)
139             || (parentType == TokenTypes.LITERAL_FOR)
140             || (parentType == TokenTypes.LITERAL_WHILE)
141             || (parentType == TokenTypes.LITERAL_DO)
142             || (parentType == TokenTypes.LITERAL_ELSE)
143             || (parentType == TokenTypes.LITERAL_TRY)
144             || (parentType == TokenTypes.LITERAL_CATCH)
145             || (parentType == TokenTypes.LITERAL_FINALLY)
146             || (parentType == TokenTypes.CTOR_DEF)
147             || (parentType == TokenTypes.METHOD_DEF)
148             || (parentType == TokenTypes.STATIC_INIT);
149     }
150
151     /**
152      * Check the indentation of the expression we are handling.
153      */

154     public void checkIndentation()
155     {
156         // only need to check this if parent is not
157
// an if, else, while, do, ctor, method
158
if (hasBlockParent()) {
159             return;
160         }
161         super.checkIndentation();
162     }
163 }
164
Popular Tags