KickJava   Java API By Example, From Geeks To Geeks.

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


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 class definitions.
26  *
27  * @author jrichard
28  */

29 public class ClassDefHandler 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 ClassDefHandler(IndentationCheck aIndentCheck,
40                            DetailAST aAst,
41                            ExpressionHandler aParent)
42     {
43         super(aIndentCheck,
44               (aAst.getType() == TokenTypes.CLASS_DEF)
45               ? "class def" : ((aAst.getType() == TokenTypes.ENUM_DEF)
46                                ? "enum def" : "interface def"),
47               aAst, aParent);
48     }
49
50     /**
51      * Get the left curly brace portion of the expression we are handling.
52      *
53      * @return the left curly brace expression
54      */

55     protected DetailAST getLCurly()
56     {
57         return getMainAst().findFirstToken(TokenTypes.OBJBLOCK)
58             .findFirstToken(TokenTypes.LCURLY);
59     }
60
61     /**
62      * Get the right curly brace portion of the expression we are handling.
63      *
64      * @return the right curly brace expression
65      */

66     protected DetailAST getRCurly()
67     {
68         return getMainAst().findFirstToken(TokenTypes.OBJBLOCK)
69             .findFirstToken(TokenTypes.RCURLY);
70     }
71
72     /**
73      * There is no top level expression for this handler.
74      *
75      * @return null
76      */

77     protected DetailAST getToplevelAST()
78     {
79         return null;
80         // note: ident checked by hand in check indentation;
81
}
82
83     /**
84      * Get the child element representing the list of statements.
85      *
86      * @return the statement list child
87      */

88     protected DetailAST getListChild()
89     {
90         return getMainAst().findFirstToken(TokenTypes.OBJBLOCK);
91     }
92
93     /**
94      * Check the indentation of the expression we are handling.
95      */

96     public void checkIndentation()
97     {
98         // TODO: still need to better deal with the modifiers and "class"
99
checkModifiers();
100
101         final LineSet lines = new LineSet();
102
103         // checks that line with class name starts at correct indentation,
104
// and following lines (in implements and extends clauses) are
105
// indented at least one level
106
final DetailAST ident = getMainAst().findFirstToken(TokenTypes.IDENT);
107         final int lineStart = getLineStart(ident);
108         if (!getLevel().accept(lineStart)) {
109             logError(ident, "ident", lineStart);
110         }
111
112         lines.addLineAndCol(new Integer JavaDoc(ident.getLineNo()), lineStart);
113
114         final DetailAST impl = getMainAst().findFirstToken(
115             TokenTypes.IMPLEMENTS_CLAUSE);
116         if ((impl != null) && (impl.getFirstChild() != null)) {
117             findSubtreeLines(lines, impl, false);
118         }
119
120         final DetailAST ext =
121             getMainAst().findFirstToken(TokenTypes.EXTENDS_CLAUSE);
122         if ((ext != null) && (ext.getFirstChild() != null)) {
123             findSubtreeLines(lines, ext, false);
124         }
125
126         checkLinesIndent(ident.getLineNo(), lines.lastLine(), getLevel());
127
128         super.checkIndentation();
129     }
130
131     /** {@inheritDoc} */
132     protected int[] getCheckedChildren()
133     {
134         return new int[] {
135             TokenTypes.EXPR,
136             TokenTypes.OBJBLOCK,
137             TokenTypes.LITERAL_BREAK,
138             TokenTypes.LITERAL_RETURN,
139             TokenTypes.LITERAL_THROW,
140             TokenTypes.LITERAL_CONTINUE,
141         };
142     }
143
144 }
145
Popular Tags