01: package net.sourceforge.groboutils.codecoverage.v2;
02:
03: import java.util.Hashtable;
04:
05: public class ArrayClassLoader extends ClassLoader {
06: //----------------------------
07: // Public data
08:
09: //----------------------------
10: // Private data
11:
12: private Hashtable m_classList = new Hashtable();
13: private Hashtable m_classCache = new Hashtable();
14:
15: //----------------------------
16: // constructors
17:
18: /**
19: * Default constructor
20: */
21: public ArrayClassLoader() {
22: // do nothing
23: }
24:
25: //----------------------------
26: // Public methods
27:
28: /**
29: * Add a new class to the internal list.
30: */
31: public void addClass(String name, byte[] bytecode) {
32: if (name == null || bytecode == null) {
33: throw new IllegalArgumentException("no null args");
34: }
35: this .m_classList.put(name, bytecode);
36: }
37:
38: // inherited from ClassLoader
39: /**
40: * @exception ClassNotFoundException thrown if the given class name
41: * could not be found, or if there was a problem loading the
42: * bytecode for the class.
43: */
44: public Class loadClass(String name, boolean resolve)
45: throws ClassNotFoundException {
46: Class c;
47:
48: if (name == null) {
49: throw new IllegalArgumentException("classname is null");
50: }
51:
52: c = (Class) this .m_classCache.get(name);
53: if (c == null) {
54: byte bytecode[] = getBytecode(name);
55: if (bytecode == null) {
56: c = findSystemClass(name);
57: } else {
58: try {
59: c = defineClass(name, bytecode, 0, bytecode.length);
60: this .m_classCache.put(name, c);
61: } catch (Exception ex2) {
62: // something wrong with the class format
63: throw new ClassNotFoundException(
64: "Bad class format for class " + name);
65: }
66: }
67: }
68: if (resolve) {
69: resolveClass(c);
70: }
71: return c;
72: }
73:
74: //----------------------------
75: // Protected methods
76:
77: /**
78: * Retrieves the internal bytecode for the given class. If not known,
79: * then <tt>null</tt> is returned.
80: *
81: * @param className a non-null class name.
82: */
83: protected byte[] getBytecode(String className) {
84: byte bytecode[] = (byte[]) this .m_classList.get(className);
85: return bytecode;
86: }
87:
88: //----------------------------
89: // Private methods
90: }
|