KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > classes > v1 > jdk0 > ArrayClassLoader


1 /*
2  * @(#)ArrayClassLoader.java 1.0.0 11/17/2000 - 12:01:17
3  *
4  * Copyright (C) 2000,2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.util.classes.v1.jdk0;
28
29 import java.util.Hashtable JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Vector JavaDoc;
32
33
34
35 /**
36  * Load classes by byte Arrays. JDK 1.0+ compatible.
37  *
38  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
39  * @version $Date: 2003/02/10 22:52:36 $
40  * @since November 17, 2000 (GroboUtils Alpha 0.9.0)
41  */

42 public class ArrayClassLoader extends ClassLoader JavaDoc
43 {
44     //----------------------------
45
// Public data
46

47
48
49     //----------------------------
50
// Private data
51

52     /**
53      * list of all classes
54      */

55     private Hashtable JavaDoc m_classList = new Hashtable JavaDoc();
56     private Hashtable JavaDoc m_classCache = new Hashtable JavaDoc();
57     
58     
59     /**
60      * The original bytecode source, for linking.
61      */

62     private BytecodeSource m_bytecodeSource = null;
63     
64     //----------------------------
65
// constructors
66

67     /**
68     * Default constructor
69     */

70     public ArrayClassLoader()
71     {
72         // do nothing
73
}
74     
75     
76     //----------------------------
77
// Public methods
78

79     
80     /**
81      * Sets the reference to the original bytecode loader. By default,
82      * the value is <tt>null</tt>. If the value is <tt>null</tt>, then
83      * there is the possibility for a link error.
84      */

85     public void setBytecodeSource( BytecodeSource bs )
86     {
87         this.m_bytecodeSource = bs;
88     }
89     
90     
91     /**
92      * Add a new class to the internal list.
93      */

94     public void addClass( String JavaDoc name, byte[] bytecode )
95     {
96         if (name == null || bytecode == null)
97         {
98             throw new IllegalArgumentException JavaDoc("no null args");
99         }
100         this.m_classList.put( name, bytecode );
101     }
102     
103
104     // inherited from ClassLoader
105
/**
106      * @exception ClassNotFoundException thrown if the given class name
107      * could not be found, or if there was a problem loading the
108      * bytecode for the class.
109      */

110     public Class JavaDoc loadClass( String JavaDoc name, boolean resolve )
111             throws ClassNotFoundException JavaDoc
112     {
113         Class JavaDoc c;
114         
115         if (name == null)
116         {
117             throw new IllegalArgumentException JavaDoc("classname is null");
118         }
119         
120         c = (Class JavaDoc)this.m_classCache.get( name );
121         if (c == null)
122         {
123             try
124             {
125                 c = findSystemClass( name );
126             }
127             catch (Exception JavaDoc ex)
128             {
129                 byte bytecode[] = getBytecode( name );
130                 if (bytecode == null)
131                 {
132 System.out.println(this.getClass().getName()+"::loadClass( '"+name+
133 "' ): bytecode for class was never defined.");
134                     throw new ClassNotFoundException JavaDoc( name );
135                 }
136                 else
137                 {
138                     try
139                     {
140                         c = defineClass( name, bytecode, 0, bytecode.length );
141                         this.m_classCache.put( name, c );
142                     }
143                     catch (Exception JavaDoc ex2)
144                     {
145                         // something wrong with the class format
146
throw new ClassNotFoundException JavaDoc(
147                             "Bad class format for class "+name );
148                     }
149                 }
150             }
151         }
152         if (resolve)
153         {
154             resolveClass( c );
155         }
156         return c;
157     }
158     
159     //----------------------------
160
// Protected methods
161

162     
163     /**
164      * Retrieves the internal bytecode for the given class. If not known,
165      * then will attempt to pass it to the bytecode source.
166      *
167      * @param className a non-null class name.
168      */

169     protected byte[] getBytecode( String JavaDoc className )
170     {
171         byte bytecode[] = (byte[])this.m_classList.get( className );
172         if (bytecode == null)
173         {
174             if (this.m_bytecodeSource != null)
175             {
176                 bytecode = this.m_bytecodeSource.getBytecode( className );
177                 if (bytecode != null)
178                 {
179                     addClass( className, bytecode );
180                 }
181             }
182         }
183         return bytecode;
184     }
185     
186     
187     //----------------------------
188
// Private methods
189
}
190
191
Popular Tags