* tests/regression/bugzilla/PR119.java: Compile fix.
[cacao.git] / tests / regression / resolving / TestLoader.java
1 import java.util.Hashtable;
2 import java.io.*;
3
4 public class TestLoader extends ClassLoader {
5
6     Hashtable registry_;
7     String name_;
8     TestController controller_;
9
10     class Entry {
11     }
12
13     class ClassfileEntry extends Entry {
14         public String filename_;
15         public ClassfileEntry(String filename) { filename_ = filename; }
16     }
17
18     class DelegationEntry extends Entry {
19         public ClassLoader loader_;
20         public DelegationEntry(ClassLoader loader) { loader_ = loader; }
21     }
22
23     class SuperDelegationEntry extends Entry {
24     }
25
26     public TestLoader(ClassLoader parent, String name, TestController controller) {
27         super(parent);
28         name_ = name;
29         controller_ = controller;
30         registry_ = new Hashtable();
31     }
32
33     public void addClassfile(String classname, String filename) {
34         registry_.put(classname, new ClassfileEntry(filename));
35     }
36
37     public void addDelegation(String classname, ClassLoader loader) {
38         registry_.put(classname, new DelegationEntry(loader));
39     }
40
41     public void addParentDelegation(String classname) {
42         registry_.put(classname, new DelegationEntry(getParent()));
43     }
44
45     public void addSuperDelegation(String classname) {
46         registry_.put(classname, new SuperDelegationEntry());
47     }
48
49     public String toString() {
50         return "TestLoader<" + name_ + ">";
51     }
52
53     public Class loadClass(String classname) throws ClassNotFoundException {
54         controller_.reportRequest(this, classname);
55
56         Entry entry = (Entry) registry_.get(classname);
57
58         if (entry == null) {
59             controller_.reportUnexpectedClassRequested(this, classname);
60             throw new ClassNotFoundException(this + " does not know how to load class " + classname);
61         }
62
63         if (entry instanceof ClassfileEntry) {
64             Class cls = findLoadedClass(classname);
65
66             if (cls != null) {
67                 controller_.reportFoundLoaded(this, cls);
68                 return cls;
69             }
70
71             String filename = ((ClassfileEntry)entry).filename_;
72
73             try {
74                 byte[] bytes = slurpFile(filename);
75
76                 cls = defineClass(classname, bytes, 0, bytes.length);
77
78                 controller_.reportDefinition(this, cls);
79
80                 return cls;
81             }
82             catch (Exception e) {
83                 throw new ClassNotFoundException(e.toString());
84             }
85         }
86         else if (entry instanceof DelegationEntry) {
87             ClassLoader delegate = ((DelegationEntry)entry).loader_;
88
89             controller_.reportDelegation(this, delegate, classname);
90
91             Class cls = delegate.loadClass(classname);
92
93             controller_.reportLoaded(this, cls);
94
95             return cls;
96         }
97
98         throw new ClassNotFoundException("unknown TestLoader entry: " + entry);
99     }
100
101     byte[] slurpFile(String filename) throws IOException {
102         File file = new File(filename);
103         InputStream is = new FileInputStream(file);
104         long len = file.length();
105         if (len > Integer.MAX_VALUE)
106             throw new IOException("file " + file.getName() + " is too large");
107         byte[] bytes = new byte[(int) len];
108
109         int ofs = 0;
110         int read = 0;
111         while ((ofs < len) && (read = is.read(bytes, ofs, bytes.length - ofs)) >= 0)
112             ofs += read;
113
114         if (ofs < len)
115             throw new IOException("error reading file " + file.getName());
116
117         is.close();
118         return bytes;
119     }
120 }
121
122 // vim: et sw=4
123