KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > DeclarationCollector


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;
20
21 import com.puppycrawl.tools.checkstyle.api.Check;
22 import com.puppycrawl.tools.checkstyle.api.DetailAST;
23 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28
29 /**
30  * Abstract class for chekcs which need to collect information about
31  * declared members/parameters/variables.
32  *
33  * @author o_sukhodolsky
34  */

35 public abstract class DeclarationCollector extends Check
36 {
37     /** Stack of variable declaration frames. */
38     private FrameStack mFrames;
39
40     /** {@inheritDoc} */
41     public void beginTree(DetailAST aRootAST)
42     {
43         mFrames = new FrameStack();
44     }
45
46     /** {@inheritDoc} */
47     public void visitToken(DetailAST aAST)
48     {
49         switch (aAST.getType()) {
50         case TokenTypes.PARAMETER_DEF :
51         case TokenTypes.VARIABLE_DEF : {
52             final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT);
53             this.mFrames.current().addName(nameAST.getText());
54             break;
55         }
56         case TokenTypes.CLASS_DEF :
57         case TokenTypes.INTERFACE_DEF :
58         case TokenTypes.ENUM_DEF :
59         case TokenTypes.ANNOTATION_DEF : {
60             final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT);
61             this.mFrames.current().addName(nameAST.getText());
62             this.mFrames.enter(new ClassFrame());
63             break;
64         }
65         case TokenTypes.SLIST :
66             this.mFrames.enter(new BlockFrame());
67             break;
68         case TokenTypes.METHOD_DEF :
69         case TokenTypes.CTOR_DEF :
70             this.mFrames.enter(new MethodFrame());
71             break;
72         default:
73             // do nothing
74
}
75     }
76
77
78     /** {@inheritDoc} */
79     public void leaveToken(DetailAST aAST)
80     {
81         switch (aAST.getType()) {
82         case TokenTypes.CLASS_DEF :
83         case TokenTypes.INTERFACE_DEF :
84         case TokenTypes.ENUM_DEF :
85         case TokenTypes.ANNOTATION_DEF :
86         case TokenTypes.SLIST :
87         case TokenTypes.METHOD_DEF :
88         case TokenTypes.CTOR_DEF :
89             this.mFrames.leave();
90             break;
91         default :
92             // do nothing
93
}
94     }
95
96     /**
97      * Check if given name is a name for declafred variable/parameter/member in
98      * current environment.
99      * @param aName a name to check
100      * @return true is the given name is declare one.
101      */

102     protected final boolean isDeclared(String JavaDoc aName)
103     {
104         return (null != mFrames.findFrame(aName));
105     }
106
107     /**
108      * Check if given name is a name for class field in current environment.
109      * @param aName a name to check
110      * @return true is the given name is name of method or member.
111      */

112     protected final boolean isClassField(String JavaDoc aName)
113     {
114         return (mFrames.findFrame(aName) instanceof ClassFrame);
115     }
116 }
117
118 /**
119  * A declaration frame.
120  * @author Stephen Bloch
121  * June 19, 2003
122  */

123 abstract class LexicalFrame
124 {
125     /** Set of name of variables declared in this frame. */
126     private HashSet JavaDoc mVarNames;
127
128     /** constructor -- invocable only via super() from subclasses */
129     protected LexicalFrame()
130     {
131         mVarNames = new HashSet JavaDoc();
132     }
133
134     /** add a name to the frame.
135      * @param aNameToAdd the name we're adding
136      */

137     void addName(String JavaDoc aNameToAdd)
138     {
139         this.mVarNames.add(aNameToAdd);
140     }
141
142     /** check whether the frame contains a given name.
143      * @param aNameToFind the name we're looking for
144      * @return whether it was found
145      */

146     boolean contains(String JavaDoc aNameToFind)
147     {
148         return this.mVarNames.contains(aNameToFind);
149     }
150 }
151
152 /**
153  * The global frame; should hold only class names.
154  * @author Stephen Bloch
155  */

156 class GlobalFrame extends LexicalFrame
157 {
158     /** Create new instance of hte frame. */
159     GlobalFrame()
160     {
161         super();
162     }
163 }
164
165 /**
166  * A frame initiated at method definition; holds parameter names.
167  * @author Stephen Bloch
168  */

169 class MethodFrame extends LexicalFrame
170 {
171     /** Create new instance of hte frame. */
172     MethodFrame()
173     {
174         super();
175     }
176 }
177
178 /**
179  * A frame initiated at class definition; holds instance variable
180  * names. For the present, I'm not worried about other class names,
181  * method names, etc.
182  * @author Stephen Bloch
183  */

184 class ClassFrame extends LexicalFrame
185 {
186     /** Create new instance of hte frame. */
187     ClassFrame()
188     {
189         super();
190     }
191 }
192
193 /**
194  * A frame initiated on entering a statement list; holds local variable
195  * names. For the present, I'm not worried about other class names,
196  * method names, etc.
197  * @author Stephen Bloch
198  */

199 class BlockFrame extends LexicalFrame
200 {
201     /** Create new instance of hte frame. */
202     BlockFrame()
203     {
204         super();
205     }
206 }
207
208 /**
209  * A stack of LexicalFrames. Standard issue....
210  * @author Stephen Bloch
211  */

212 class FrameStack
213 {
214     /** List of lexical frames. */
215     private LinkedList JavaDoc mFrameList;
216
217     /** Creates an empty FrameStack. */
218     FrameStack()
219     {
220         mFrameList = new LinkedList JavaDoc();
221         this.enter(new GlobalFrame());
222     }
223
224     /**
225      * Enter a scope, i.e. push a frame on the stack.
226      * @param aNewFrame the already-created frame to push
227      */

228     void enter(LexicalFrame aNewFrame)
229     {
230         mFrameList.addFirst(aNewFrame);
231     }
232
233     /** Leave a scope, i.e. pop a frame from the stack. */
234     void leave()
235     {
236         mFrameList.removeFirst();
237     }
238
239     /**
240      * Get current scope, i.e. top frame on the stack.
241      * @return the current frame
242      */

243     LexicalFrame current()
244     {
245         return (LexicalFrame) mFrameList.getFirst();
246     }
247
248     /**
249      * Search this and containing frames for a given name.
250      * @param aNameToFind the name we're looking for
251      * @return the frame in which it was found, or null if not found
252      */

253     LexicalFrame findFrame(String JavaDoc aNameToFind)
254     {
255         final Iterator JavaDoc it = mFrameList.iterator();
256         while (it.hasNext()) {
257             final LexicalFrame thisFrame = (LexicalFrame) it.next();
258             if (thisFrame.contains(aNameToFind)) {
259                 return thisFrame;
260             }
261         }
262         return null;
263     }
264 }
265
Popular Tags