KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > usage > transmogrify > SymbolTable


1
2 // Transmogrify License
3
//
4
// Copyright (c) 2001, ThoughtWorks, Inc.
5
// All rights reserved.
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions
8
// are met:
9
// - Redistributions of source code must retain the above copyright notice,
10
// this list of conditions and the following disclaimer.
11
// - Redistributions in binary form must reproduce the above copyright
12
// notice, this list of conditions and the following disclaimer in the
13
// documentation and/or other materials provided with the distribution.
14
// Neither the name of the ThoughtWorks, Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from this
16
// software without specific prior written permission.
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28

29 package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
30
31 import java.io.File JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Stack JavaDoc;
34
35
36
37
38 /**
39  * this class contains all of the definitions, references, and scopes
40  * created by the system.
41  *
42  * Other stuff this class does:
43  * <ul>
44  * <li> holds the "base" scope containing primitive definitions
45  * <li> holds the java.lang package
46  * <li> holds the definition of java.lang.Object, which is the base class
47  * of all class hierarchies
48  * <li> kicks off the resolve step
49  * <li> does some of the work of constructing object definitions
50  * </ul>
51  */

52
53 public class SymbolTable {
54
55   private Hashtable JavaDoc packages = new Hashtable JavaDoc();
56   private Stack JavaDoc scopes = new Stack JavaDoc();
57   private ScopeIndex index = new ScopeIndex();
58
59   private File JavaDoc currentFile;
60   private BaseScope baseScope;
61
62   private SymTabAST root;
63
64 // private boolean outOfDate;
65

66   /**
67    * constructor takes <code>SymTabAST</code>
68    * @param aRoot root of the <code>SymTabAST</code> tree
69    */

70   public SymbolTable(SymTabAST aRoot) {
71     scopes = new Stack JavaDoc();
72     this.root = aRoot;
73
74     baseScope = new BaseScope( this );
75     pushScope(baseScope);
76   }
77
78   /**
79    * gets the root node
80    * @return <code>SymTabAST</code>
81    */

82   public SymTabAST getTree() {
83     return root;
84   }
85
86 // /**
87
// * sets the <code>outOfDate</code> data member to <code>true</code>
88
// * @return <code>void</code>
89
// */
90
// public void expire() {
91
// outOfDate = true;
92
// }
93
//
94
// /**
95
// * sets <code>outOfDate</code> member to <code>false</code>
96
// * @param lastUpdated
97
// * @return <code>void</code>
98
// */
99
// public void update(long lastUpdated) {
100
// outOfDate = false;
101
// }
102

103   /**
104    * returns the "base" scope
105    *
106    * @return Scope the base scope
107    */

108   // REDTAG -- this should eventually be replaced by a call
109
// to the lookup method that traverses scopes
110
public BaseScope getBaseScope() {
111     return baseScope;
112   }
113
114   /**
115    * returns the current scope. Scopes are nested in a stack (FIFO queue)
116    * and pushed/popped based on the structure of the AST
117    * @return <code>Scope</code>
118    */

119   public Scope getCurrentScope() {
120     return (Scope)scopes.peek();
121   }
122
123   /**
124    * pushes a new scope onto the stack
125    *
126    * @param scope the <code>Scope</code> to push
127    * @return <code>void</code>
128    */

129   public void pushScope(Scope scope) {
130     scopes.push(scope);
131   }
132
133   /**
134    * pops a scope from the stack.
135    *
136    * @return <code>Scope</code>
137    *
138    */

139   public Scope popScope() {
140     Scope scope = (Scope)(scopes.pop());
141     return scope;
142   }
143
144   /**
145    * gets all packages stored in this symbol table
146    * @return <code>Hashtable</code>
147    */

148   public Hashtable JavaDoc getPackages() {
149     // REDTAG -- think about making this available as something simpler,
150
// perhaps an enumeration
151
return packages;
152   }
153
154   /**
155    * gets package by its name
156    * @param name
157    * @return <code>PackageDef</code>
158    */

159   public PackageDef getPackage( String JavaDoc name ) {
160     return (PackageDef)(packages.get( name ));
161   }
162
163   /**
164    * adds <code>PackageDef</code> to its parent scope and stores the
165    * <code>PackageDef</code> in <code>packages</code>
166    * @param pkg
167    * @param parent
168    * @return <code>void</code>
169    */

170   public void definePackage( PackageDef pkg, Scope parent ) {
171     parent.addDefinition(pkg);
172     packages.put(pkg.getQualifiedName(), pkg);
173   }
174
175   /**
176    * defines a class in the symbol table.
177    *
178    * @param def the class to define
179    * @return <code>void</code>
180    * @see #indexScope(Scope)
181    * @see #getCurrentScope()
182    */

183   public void defineClass(ClassDef def) {
184     indexScope(def);
185     getCurrentScope().addDefinition(def);
186   }
187
188   /**
189    * defines a method in the symbol table
190    *
191    * @param method the method to be defined
192    * @return <code>void</code>
193    * @see #indexScope(Scope)
194    * @see #getCurrentScope()
195    */

196   public void defineMethod(MethodDef method) {
197     indexScope(method);
198     ((ClassDef)getCurrentScope()).addDefinition(method);
199   }
200
201   /**
202    * defines a variable in the symbol table
203    *
204    * @param v the variable to define
205    * @return <code>void</code>
206    * @see #getCurrentScope()
207    */

208   public void defineVariable(VariableDef v) {
209     getCurrentScope().addDefinition(v);
210   }
211
212   /**
213    * defines a block within the symbol table
214    *
215    * @param blockDef the block to define
216    * @return <code>void</code>
217    * @see #indexScope(Scope)
218    * @see #getCurrentScope()
219    */

220   public void defineBlock(BlockDef blockDef) {
221     indexScope(blockDef);
222     getCurrentScope().addDefinition(blockDef);
223   }
224
225   /**
226    * defines a label within the symbol table
227    *
228    * @param labelDef the label to define
229    * @return <code>void</code>
230    * @see #getCurrentScope()
231    */

232   // REDTAG -- label does not define a new scope
233
public void defineLabel(LabelDef labelDef) {
234     getCurrentScope().addDefinition(labelDef);
235   }
236
237   /**
238    * places a scope in the symbol table's index
239    *
240    * @param scope the scope to index
241    * @return <code>void</code>
242    */

243   public void indexScope(Scope scope) {
244     index.addScope(scope);
245   }
246
247   /**
248    * gets the symbol table's scope index
249    *
250    * @return ScopeIndex
251    */

252   public ScopeIndex getScopeIndex() {
253     return index;
254   }
255
256   /**
257    * sets the current file that the symbol table is processing
258    *
259    * @param file the <code>File</code> to use
260    * @return <code>void</code>
261    */

262   public void setCurrentFile(File JavaDoc file) {
263     currentFile = file;
264   }
265
266   /**
267    * gets the file that the symbol table is currently processing
268    *
269    * @return <code>File</code>
270    */

271   public File JavaDoc getCurrentFile() {
272     return currentFile;
273   }
274 }
275
Popular Tags