Source code: com/puppycrawl/tools/checkstyle/checks/indentation/SlistHandler.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2003 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 else {
74 return super.suggestedChildLevel(aChild);
75 }
76 }
77
78 /**
79 * Get the child element that is not a list of statements.
80 *
81 * @return the non-list child element
82 */
83 protected DetailAST getNonlistChild()
84 {
85 // blocks always have either block children or they are transparent
86 // and aren't checking children at all. In the later case, the
87 // superclass will want to check single children, so when it
88 // does tell it we have none.
89 return null;
90 }
91
92 /**
93 * Get the child element representing the list of statements.
94 *
95 * @return the statement list child
96 */
97 protected DetailAST getListChild()
98 {
99 return getMainAst();
100 }
101
102 /**
103 * Get the left curly brace portion of the expression we are handling.
104 *
105 * @return the left curly brace expression
106 */
107 protected DetailAST getLCurly()
108 {
109 return getMainAst();
110 }
111
112 /**
113 * Get the right curly brace portion of the expression we are handling.
114 *
115 * @return the right curly brace expression
116 */
117 protected DetailAST getRCurly()
118 {
119 return getMainAst().findFirstToken(TokenTypes.RCURLY);
120 }
121
122 /**
123 * There is no top level expression for this handler.
124 *
125 * @return null
126 */
127 protected DetailAST getToplevelAST()
128 {
129 return null;
130 }
131
132 /**
133 * Determine if the expression we are handling has a block parent.
134 *
135 * @return true if it does, false otherwise
136 */
137 private boolean hasBlockParent()
138 {
139 final int parentType = getMainAst().getParent().getType();
140 return (parentType == TokenTypes.LITERAL_IF)
141 || (parentType == TokenTypes.LITERAL_FOR)
142 || (parentType == TokenTypes.LITERAL_WHILE)
143 || (parentType == TokenTypes.LITERAL_DO)
144 || (parentType == TokenTypes.LITERAL_ELSE)
145 || (parentType == TokenTypes.LITERAL_TRY)
146 || (parentType == TokenTypes.LITERAL_CATCH)
147 || (parentType == TokenTypes.LITERAL_FINALLY)
148 || (parentType == TokenTypes.CTOR_DEF)
149 || (parentType == TokenTypes.METHOD_DEF)
150 || (parentType == TokenTypes.STATIC_INIT);
151 }
152
153 /**
154 * Check the indentation of the expression we are handling.
155 */
156 public void checkIndentation()
157 {
158 // only need to check this if parent is not
159 // an if, else, while, do, ctor, method
160 if (hasBlockParent()) {
161 return;
162 }
163 super.checkIndentation();
164 }
165 }