KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > util > ClassLoader


1 package com.sun.org.apache.bcel.internal.util;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache BCEL" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache BCEL", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.util.Hashtable JavaDoc;
58 import java.io.*;
59 import java.util.zip.*;
60 import com.sun.org.apache.bcel.internal.*;
61 import com.sun.org.apache.bcel.internal.classfile.*;
62 import com.sun.org.apache.bcel.internal.generic.*;
63
64 /**
65  * <p>Drop in replacement for the standard class loader of the JVM. You can use it
66  * in conjunction with the JavaWrapper to dynamically modify/create classes
67  * as they're requested.</p>
68  *
69  * <p>This class loader recognizes special requests in a distinct
70  * format, i.e., when the name of the requested class contains with
71  * "$$BCEL$$" it calls the createClass() method with that name
72  * (everything bevor the $$BCEL$$ is considered to be the package
73  * name. You can subclass the class loader and override that
74  * method. "Normal" classes class can be modified by overriding the
75  * modifyClass() method which is called just before defineClass().</p>
76  *
77  * <p>There may be a number of packages where you have to use the default
78  * class loader (which may also be faster). You can define the set of packages
79  * where to use the system class loader in the constructor. The default value contains
80  * "java.", "sun.", "javax."</p>
81  *
82  * @version $Id: ClassLoader.java,v 1.1.1.1 2001/10/29 20:00:29 jvanzyl Exp $
83  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
84  * @see JavaWrapper
85  * @see ClassPath
86  */

87 public class ClassLoader extends java.lang.ClassLoader JavaDoc {
88   private Hashtable JavaDoc classes = new Hashtable JavaDoc(); // Hashtable is synchronized thus thread-safe
89
private String JavaDoc[] ignored_packages = {
90     "java.", "javax.", "sun."
91   };
92
93   public ClassLoader() {
94   }
95
96   /** @param ignored_packages classes contained in these packages will be loaded
97    * with the system class loader
98    */

99   public ClassLoader(String JavaDoc[] ignored_packages) {
100     String JavaDoc[] new_p = new String JavaDoc[ignored_packages.length + this.ignored_packages.length];
101
102     System.arraycopy(this.ignored_packages, 0, new_p, 0, this.ignored_packages.length);
103     System.arraycopy(ignored_packages, 0, new_p, this.ignored_packages.length,
104              ignored_packages.length);
105
106     this.ignored_packages = new_p;
107   }
108   
109   protected Class JavaDoc loadClass(String JavaDoc class_name, boolean resolve)
110     throws ClassNotFoundException JavaDoc
111   {
112     Class JavaDoc cl = null;
113
114     /* First try: lookup hash table.
115      */

116     if((cl=(Class JavaDoc)classes.get(class_name)) == null) {
117       /* Second try: Load system class using system class loader. You better
118        * don't mess around with them.
119        */

120       for(int i=0; i < ignored_packages.length; i++) {
121     if(class_name.startsWith(ignored_packages[i])) {
122       cl = Class.forName(class_name);
123       break;
124     }
125       }
126
127       if(cl == null) {
128     JavaClass clazz = null;
129
130     /* Third try: Special request?
131      */

132     if(class_name.indexOf("$$BCEL$$") >= 0)
133       clazz = createClass(class_name);
134     else // Fourth try: Load classes via repository
135
clazz = modifyClass(Repository.lookupClass(class_name));
136
137     if(clazz != null) {
138       byte[] bytes = clazz.getBytes();
139       cl = defineClass(class_name, bytes, 0, bytes.length);
140     } else // Fourth try: Use default class loader
141
cl = Class.forName(class_name);
142       }
143       
144       if(resolve)
145     resolveClass(cl);
146     }
147
148     classes.put(class_name, cl);
149
150     return cl;
151   }
152
153   /** Override this method if you want to alter a class before it gets actually
154    * loaded. Does nothing by default.
155    */

156   protected JavaClass modifyClass(JavaClass clazz) {
157     return clazz;
158   }
159   
160   /**
161    * Override this method to create you own classes on the fly. The
162    * name contains the special token $$BCEL$$. Everything before that
163    * token is consddered to be a package name. You can encode you own
164    * arguments into the subsequent string. You must regard however not
165    * to use any "illegal" characters, i.e., characters that may not
166    * appear in a Java class name too<br>
167    *
168    * The default implementation interprets the string as a encoded compressed
169    * Java class, unpacks and decodes it with the Utility.decode() method, and
170    * parses thee resulting byte array and returns the resulting JavaClass object.
171    *
172    * @param class_name compressed byte code with "$$BCEL$$" in it
173    */

174   protected JavaClass createClass(String JavaDoc class_name) {
175     int index = class_name.indexOf("$$BCEL$$");
176     String JavaDoc real_name = class_name.substring(index + 8);
177
178     JavaClass clazz = null;
179     try {
180       byte[] bytes = Utility.decode(real_name, true);
181       ClassParser parser = new ClassParser(new ByteArrayInputStream(bytes), "foo");
182
183       clazz = parser.parse();
184     } catch(Throwable JavaDoc e) {
185       e.printStackTrace();
186       return null;
187     }
188
189     // Adapt the class name to the passed value
190
ConstantPool cp = clazz.getConstantPool();
191
192     ConstantClass cl = (ConstantClass)cp.getConstant(clazz.getClassNameIndex(),
193                              Constants.CONSTANT_Class);
194     ConstantUtf8 name = (ConstantUtf8)cp.getConstant(cl.getNameIndex(),
195                              Constants.CONSTANT_Utf8);
196     name.setBytes(class_name.replace('.', '/'));
197
198     return clazz;
199   }
200 }
201
Popular Tags