KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > ArrayClassLoader


1 package net.sourceforge.groboutils.codecoverage.v2;
2
3 import java.util.Hashtable JavaDoc;
4
5 public class ArrayClassLoader extends ClassLoader JavaDoc
6 {
7     //----------------------------
8
// Public data
9

10
11
12     //----------------------------
13
// Private data
14

15     private Hashtable JavaDoc m_classList = new Hashtable JavaDoc();
16     private Hashtable JavaDoc m_classCache = new Hashtable JavaDoc();
17     
18     //----------------------------
19
// constructors
20

21     /**
22      * Default constructor
23      */

24     public ArrayClassLoader()
25     {
26         // do nothing
27
}
28     
29     
30     //----------------------------
31
// Public methods
32

33     
34     /**
35      * Add a new class to the internal list.
36      */

37     public void addClass( String JavaDoc name, byte[] bytecode )
38     {
39         if (name == null || bytecode == null)
40         {
41             throw new IllegalArgumentException JavaDoc("no null args");
42         }
43         this.m_classList.put( name, bytecode );
44     }
45     
46
47     // inherited from ClassLoader
48
/**
49      * @exception ClassNotFoundException thrown if the given class name
50      * could not be found, or if there was a problem loading the
51      * bytecode for the class.
52      */

53     public Class JavaDoc loadClass( String JavaDoc name, boolean resolve )
54             throws ClassNotFoundException JavaDoc
55     {
56         Class JavaDoc c;
57         
58         if (name == null)
59         {
60             throw new IllegalArgumentException JavaDoc("classname is null");
61         }
62         
63         c = (Class JavaDoc)this.m_classCache.get( name );
64         if (c == null)
65         {
66             byte bytecode[] = getBytecode( name );
67             if (bytecode == null)
68             {
69                 c = findSystemClass( name );
70             }
71             else
72             {
73                 try
74                 {
75                     c = defineClass( name, bytecode, 0, bytecode.length );
76                     this.m_classCache.put( name, c );
77                 }
78                 catch (Exception JavaDoc ex2)
79                 {
80                     // something wrong with the class format
81
throw new ClassNotFoundException JavaDoc(
82                         "Bad class format for class "+name );
83                 }
84             }
85         }
86         if (resolve)
87         {
88             resolveClass( c );
89         }
90         return c;
91     }
92     
93     //----------------------------
94
// Protected methods
95

96     
97     /**
98      * Retrieves the internal bytecode for the given class. If not known,
99      * then <tt>null</tt> is returned.
100      *
101      * @param className a non-null class name.
102      */

103     protected byte[] getBytecode( String JavaDoc className )
104     {
105         byte bytecode[] = (byte[])this.m_classList.get( className );
106         return bytecode;
107     }
108     
109     
110     //----------------------------
111
// Private methods
112
}
113
Popular Tags