001: // Transmogrify License
002: //
003: // Copyright (c) 2001, ThoughtWorks, Inc.
004: // All rights reserved.
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions
007: // are met:
008: // - Redistributions of source code must retain the above copyright notice,
009: // this list of conditions and the following disclaimer.
010: // - Redistributions in binary form must reproduce the above copyright
011: // notice, this list of conditions and the following disclaimer in the
012: // documentation and/or other materials provided with the distribution.
013: // Neither the name of the ThoughtWorks, Inc. nor the names of its
014: // contributors may be used to endorse or promote products derived from this
015: // software without specific prior written permission.
016: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
017: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
018: // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
019: // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
020: // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
021: // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
022: // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
023: // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
024: // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
025: // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
026: // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027:
028: package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
029:
030: import java.util.Enumeration;
031: import java.util.Hashtable;
032: import java.util.Iterator;
033: import java.util.SortedSet;
034: import java.util.TreeSet;
035: import java.util.Vector;
036:
037: /**
038: * Represents a scope of Java code.
039: *
040: * @author andrew mccormick, dave wood
041: * @version 1.0
042: * @since 1.0
043: * @see Definition
044: * @see Resolvable
045: */
046: public abstract class Scope extends Definition {
047:
048: // rename to references?
049: protected SortedSet referencesInScope = new TreeSet();
050:
051: protected Hashtable elements = new Hashtable();
052: protected Hashtable labels = new Hashtable();
053: protected Hashtable classes = new Hashtable();
054:
055: public Scope(String name, Scope parentScope, SymTabAST node) {
056: super (name, parentScope, node);
057: }
058:
059: public void addDefinition(VariableDef def) {
060: elements.put(def.getName(), def);
061: }
062:
063: public void addDefinition(BlockDef def) {
064: elements.put(def.getName(), def);
065: }
066:
067: public void addDefinition(ClassDef def) {
068: classes.put(def.getName(), def);
069: }
070:
071: public void addDefinition(LabelDef def) {
072: labels.put(def.getName(), def);
073: }
074:
075: public abstract void addDefinition(IPackage def);
076:
077: protected Enumeration getDefinitions() {
078: Vector allElements = new Vector();
079:
080: allElements.addAll(elements.values());
081: allElements.addAll(labels.values());
082: allElements.addAll(classes.values());
083:
084: return allElements.elements();
085: }
086:
087: protected Iterator getClasses() {
088: return classes.values().iterator();
089: }
090:
091: public abstract IMethod getMethodDefinition(String name,
092: ISignature signature);
093:
094: public abstract IVariable getVariableDefinition(String name);
095:
096: public abstract LabelDef getLabelDefinition(String name);
097:
098: public abstract IClass getClassDefinition(String name);
099:
100: public Iterator getReferencesIn() {
101: return referencesInScope.iterator();
102: }
103:
104: public Reference getSymbol(String name, Occurrence location) {
105: Reference result = null;
106:
107: for (Iterator it = getReferencesIn(); it.hasNext();) {
108: Reference reference = (Reference) it.next();
109: // if (name.equals(reference.getName())) {
110: if (reference.getLine() == location.getLine()
111: && reference.getColumn() == location.getColumn()) {
112: result = reference;
113: break;
114: }
115: // }
116: }
117: return result;
118: }
119:
120: public void addReferenceInScope(Reference reference) {
121: referencesInScope.add(reference);
122: }
123:
124: }
|