|
1 |
| package edu.rice.cs.dynamicjava.symbol; |
|
2 |
| |
|
3 |
| import koala.dynamicjava.interpreter.NodeProperties; |
|
4 |
| import koala.dynamicjava.tree.CompilationUnit; |
|
5 |
| import koala.dynamicjava.tree.Node; |
|
6 |
| import koala.dynamicjava.tree.PackageDeclaration; |
|
7 |
| import koala.dynamicjava.tree.TypeDeclaration; |
|
8 |
| |
|
9 |
| import edu.rice.cs.dynamicjava.Options; |
|
10 |
| import edu.rice.cs.dynamicjava.interpreter.TreeClassLoader; |
|
11 |
| import edu.rice.cs.plt.collect.IndexedInjectiveRelation; |
|
12 |
| import edu.rice.cs.plt.collect.InjectiveRelation; |
|
13 |
| import edu.rice.cs.plt.collect.TotalMap; |
|
14 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
15 |
| import edu.rice.cs.plt.lambda.Lambda; |
|
16 |
| |
|
17 |
| public class TreeLibrary implements Library { |
|
18 |
| |
|
19 |
| private final TotalMap<String, InjectiveRelation<String, DJClass>> _packages; |
|
20 |
| private final TreeClassLoader _loader; |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
0
| public TreeLibrary(Iterable<CompilationUnit> cus, ClassLoader parentLoader, Options opt) {
|
|
30 |
0
| _packages = new TotalMap<String, InjectiveRelation<String, DJClass>>(
|
|
31 |
| new Lambda<String, InjectiveRelation<String, DJClass>>() { |
|
32 |
0
| public InjectiveRelation<String, DJClass> value(String pkg) {
|
|
33 |
0
| return new IndexedInjectiveRelation<String, DJClass>();
|
|
34 |
| } |
|
35 |
| }, true); |
|
36 |
0
| _loader = new TreeClassLoader(parentLoader, opt);
|
|
37 |
0
| for (CompilationUnit cu : cus) {
|
|
38 |
0
| PackageDeclaration pd = cu.getPackage();
|
|
39 |
0
| String pkg = (pd == null) ? "" : pd.getName();
|
|
40 |
0
| InjectiveRelation<String, DJClass> classes = _packages.get(pkg);
|
|
41 |
0
| for (Node ast : cu.getDeclarations()) {
|
|
42 |
0
| if (ast instanceof TypeDeclaration) {
|
|
43 |
0
| String declaredName = ((TypeDeclaration) ast).getName();
|
|
44 |
0
| String fullName = pkg.equals("") ? declaredName : pkg + "." + declaredName;
|
|
45 |
0
| DJClass c = new TreeClass(fullName, null, null, ast, _loader, opt);
|
|
46 |
0
| NodeProperties.setDJClass(ast, c);
|
|
47 |
0
| classes.add(declaredName, c);
|
|
48 |
| } |
|
49 |
| } |
|
50 |
| } |
|
51 |
| } |
|
52 |
| |
|
53 |
0
| public Iterable<DJClass> declaredClasses(String fullName) {
|
|
54 |
0
| String packageName, className;
|
|
55 |
0
| int dot = fullName.lastIndexOf('.');
|
|
56 |
0
| if (dot == -1) { packageName = ""; className = fullName; }
|
|
57 |
0
| else { packageName = fullName.substring(0, dot); className = fullName.substring(dot + 1); }
|
|
58 |
0
| if (_packages.containsOverride(packageName)) {
|
|
59 |
0
| return _packages.get(packageName).matchFirst(className);
|
|
60 |
| } |
|
61 |
0
| else { return IterUtil.empty(); }
|
|
62 |
| } |
|
63 |
| |
|
64 |
0
| public ClassLoader classLoader() { return _loader; }
|
|
65 |
| } |