KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > types > TableResolver


1 package polyglot.types;
2
3 import polyglot.util.*;
4 import polyglot.main.Report;
5 import java.util.*;
6
7 /** A class resolver implemented as a map from names to types. */
8 public class TableResolver extends ClassResolver implements TopLevelResolver {
9     protected Map table;
10
11     /**
12      * Create a resolver.
13      */

14     public TableResolver() {
15     this.table = new HashMap();
16     }
17
18     /**
19      * Add a named type object to the table.
20      */

21     public void addNamed(Named type) {
22         addNamed(type.name(), type);
23     }
24
25     /**
26      * Add a named type object to the table.
27      */

28     public void addNamed(String JavaDoc name, Named type) {
29         if (name == null || type == null) {
30             throw new InternalCompilerError("Bad insertion into TableResolver");
31         }
32         if (Report.should_report(TOPICS, 3))
33         Report.report(3, "TableCR.addNamed(" + name + ", " + type + ")");
34     table.put(name, type);
35     }
36
37     public boolean packageExists(String JavaDoc name) {
38         /* Check if a package exists in the table. */
39         for (Iterator i = table.entrySet().iterator(); i.hasNext(); ) {
40             Map.Entry e = (Map.Entry) i.next();
41             Named type = (Named) e.getValue();
42             if (type instanceof Importable) {
43                 Importable im = (Importable) type;
44                 if (im.package_() != null &&
45                     im.package_().fullName().startsWith(name)) {
46                     return true;
47                 }
48             }
49         }
50       
51         return false;
52     }
53
54     /**
55      * Find a type by name.
56      */

57     public Named find(String JavaDoc name) throws SemanticException {
58         if (Report.should_report(TOPICS, 3))
59         Report.report(3, "TableCR.find(" + name + ")");
60
61     Named n = (Named) table.get(name);
62
63     if (n != null) {
64         return n;
65     }
66
67     throw new NoClassException(name);
68     }
69
70     public String JavaDoc toString() {
71         return "(table " + table + ")";
72     }
73     
74     private static final Collection TOPICS =
75                 CollectionUtil.list(Report.types, Report.resolver);
76 }
77
Popular Tags