Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/checks/usage/transmogrify/SymbolTable.java


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;
32  import java.util.Hashtable;
33  import java.util.Stack;
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 packages = new Hashtable();
56    private Stack scopes = new Stack();
57    private ScopeIndex index = new ScopeIndex();
58  
59    private File currentFile;
60    private int currentLine;
61  
62    private BaseScope baseScope;
63  
64    private SymTabAST root;
65  
66  //  private boolean outOfDate;
67  
68    /**
69     * constructor takes <code>SymTabAST</code>
70     * @param root root of the <code>SymTabAST</code> tree
71     */
72    public SymbolTable(SymTabAST root) {
73      scopes = new Stack();
74      this.root = root;
75  
76      baseScope = new BaseScope( this );
77      pushScope(baseScope);
78    }
79  
80    /**
81     * gets the root node
82     * @return <code>SymTabAST</code>
83     */
84    public SymTabAST getTree() {
85      return root;
86    }
87  
88  //  /**
89  //   * sets the <code>outOfDate</code> data member to <code>true</code>
90  //   * @return <code>void</code>
91  //   */
92  //  public void expire() {
93  //    outOfDate = true;
94  //  }
95  //
96  //  /**
97  //   * sets <code>outOfDate</code> member to <code>false</code>
98  //   * @param lastUpdated
99  //   * @return <code>void</code>
100 //   */
101 //  public void update(long lastUpdated) {
102 //    outOfDate = false;
103 //  }
104 
105   /**
106    * returns the "base" scope
107    *
108    * @return Scope the base scope
109    */
110   // REDTAG -- this should eventually be replaced by a call
111   //  to the lookup method that traverses scopes
112   public BaseScope getBaseScope() {
113     return baseScope;
114   }
115 
116   /**
117    * returns the current scope.  Scopes are nested in a stack (FIFO queue)
118    * and pushed/popped based on the structure of the AST
119    * @return <code>Scope</code>
120    */
121   public Scope getCurrentScope() {
122     return (Scope)scopes.peek();
123   }
124 
125   /**
126    * pushes a new scope onto the stack
127    *
128    * @param scope the <code>Scope</code> to push
129    * @return <code>void</code>
130    */
131   public void pushScope(Scope scope) {
132     scopes.push(scope);
133   }
134 
135   /**
136    * pops a scope from the stack.
137    *
138    * @return <code>Scope</code>
139    *
140    */
141   public Scope popScope() {
142     Scope scope = (Scope)(scopes.pop());
143     return scope;
144   }
145 
146   /**
147    * gets all packages stored in this symbol table
148    * @return <code>Hashtable</code>
149    */
150   public Hashtable getPackages() {
151     // REDTAG -- think about making this available as something simpler,
152     //           perhaps an enumeration
153     return packages;
154   }
155 
156   /**
157    * gets package by its name
158    * @param name
159    * @return <code>PackageDef</code>
160    */
161   public PackageDef getPackage( String name ) {
162     return (PackageDef)(packages.get( name ));
163   }
164 
165   /**
166    * adds <code>PackageDef</code> to its parent scope and stores the
167    * <code>PackageDef</code> in <code>packages</code>
168    * @param pkg
169    * @param parent
170    * @return <code>void</code>
171    */
172   public void definePackage( PackageDef pkg, Scope parent ) {
173     parent.addDefinition(pkg);
174     packages.put(pkg.getQualifiedName(), pkg);
175   }
176 
177   /**
178    * defines a class in the symbol table.
179    *
180    * @param def the class to define
181    * @return <code>void</code>
182    * @see #indexScope(Scope)
183    * @see #getCurrentScope()
184    */
185   public void defineClass(ClassDef def) {
186     indexScope(def);
187     getCurrentScope().addDefinition(def);
188   }
189 
190   /**
191    * defines a method in the symbol table
192    *
193    * @param method the method to be defined
194    * @return <code>void</code>
195    * @see #indexScope(Scope)
196    * @see #getCurrentScope()
197    */
198   public void defineMethod(MethodDef method) {
199     indexScope(method);
200     final Scope scope = getCurrentScope();
201     ((ClassDef)getCurrentScope()).addDefinition(method);
202   }
203 
204   /**
205    * defines a variable in the symbol table
206    *
207    * @param v the variable to define
208    * @return <code>void</code>
209    * @see #getCurrentScope()
210    */
211   public void defineVariable(VariableDef v) {
212     getCurrentScope().addDefinition(v);
213   }
214 
215   /**
216    * defines a block within the symbol table
217    *
218    * @param blockDef the block to define
219    * @return <code>void</code>
220    * @see #indexScope(Scope)
221    * @see #getCurrentScope()
222    */
223   public void defineBlock(BlockDef blockDef) {
224     indexScope(blockDef);
225     getCurrentScope().addDefinition(blockDef);
226   }
227 
228   /**
229    * defines a label within the symbol table
230    *
231    * @param labelDef the label to define
232    * @return <code>void</code>
233    * @see #getCurrentScope()
234    */
235   // REDTAG -- label does not define a new scope
236   public void defineLabel(LabelDef labelDef) {
237     getCurrentScope().addDefinition(labelDef);
238   }
239 
240   /**
241    * places a scope in the symbol table's index
242    *
243    * @param scope the scope to index
244    * @return <code>void</code>
245    */
246   public void indexScope(Scope scope) {
247     index.addScope(scope);
248   }
249 
250   /**
251    * gets the symbol table's scope index
252    *
253    * @return ScopeIndex
254    */
255   public ScopeIndex getScopeIndex() {
256     return index;
257   }
258 
259   /**
260    * sets the current file that the symbol table is processing
261    *
262    * @param file the <code>File</code> to use
263    * @return <code>void</code>
264    */
265   public void setCurrentFile(File file) {
266     currentFile = file;
267   }
268 
269   /**
270    * gets the file that the symbol table is currently processing
271    *
272    * @return <code>File</code>
273    */
274   public File getCurrentFile() {
275     return currentFile;
276   }
277 }