01: package org.acm.seguin.pmd.symboltable;
02:
03: import org.acm.seguin.pmd.util.Applier;
04:
05: import java.util.ArrayList;
06: import java.util.HashMap;
07: import java.util.Iterator;
08: import java.util.List;
09: import java.util.Map;
10:
11: /**
12: * Provides behavior common to all Scopes
13: */
14: public abstract class AbstractScope implements Scope {
15: private Scope parent;
16: protected Map variableNames = new HashMap();
17: protected Map methodNames = new HashMap();
18:
19: public ClassScope getEnclosingClassScope() {
20: return parent.getEnclosingClassScope();
21: }
22:
23: public void setParent(net.sourceforge.jrefactory.ast.Scope parent) {
24: this .parent = (Scope) parent;
25: }
26:
27: public net.sourceforge.jrefactory.ast.Scope getParent() {
28: return parent;
29: }
30:
31: public void addDeclaration(VariableNameDeclaration variableDecl) {
32: //System.err.println("AbstractScope.addDeclaration("+variableDecl+")");
33: if (variableNames.containsKey(variableDecl)) {
34: throw new RuntimeException("Variable " + variableDecl
35: + " is already in the symbol table");
36: }
37: variableNames.put(variableDecl, new ArrayList());
38: }
39:
40: public void addDeclaration(MethodNameDeclaration methodDecl) {
41: parent.addDeclaration(methodDecl);
42: }
43:
44: public boolean contains(NameOccurrence occurrence) {
45: return findVariableHere(occurrence) != null;
46: }
47:
48: public Map getVariableDeclarations(boolean lookingForUsed) {
49: VariableUsageFinderFunction f = new VariableUsageFinderFunction(
50: variableNames, lookingForUsed);
51: Applier.apply(f, variableNames.keySet().iterator());
52: return f.getUsed();
53: }
54:
55: public NameDeclaration addVariableNameOccurrence(
56: NameOccurrence occurrence) {
57: //System.err.println("AbstractScope.addVariableNameOccurrence("+occurrence+")");
58: NameDeclaration decl = findVariableHere(occurrence);
59: if (decl != null && !occurrence.isThisOrSuper()) {
60: List nameOccurrences = (List) variableNames.get(decl);
61: nameOccurrences.add(occurrence);
62: }
63: return decl;
64: }
65:
66: protected abstract NameDeclaration findVariableHere(
67: NameOccurrence occurrence);
68:
69: protected String glomNames() {
70: String result = "";
71: for (Iterator i = variableNames.keySet().iterator(); i
72: .hasNext();) {
73: result += i.next().toString() + ",";
74: }
75: return result;
76: }
77:
78: }
|