1. package com.sun.org.apache.bcel.internal.util;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <http://www.apache.org/>.
  54. */
  55. import java.util.Hashtable;
  56. import java.io.*;
  57. import java.util.zip.*;
  58. import com.sun.org.apache.bcel.internal.*;
  59. import com.sun.org.apache.bcel.internal.classfile.*;
  60. import com.sun.org.apache.bcel.internal.generic.*;
  61. /**
  62. * <p>Drop in replacement for the standard class loader of the JVM. You can use it
  63. * in conjunction with the JavaWrapper to dynamically modify/create classes
  64. * as they're requested.</p>
  65. *
  66. * <p>This class loader recognizes special requests in a distinct
  67. * format, i.e., when the name of the requested class contains with
  68. * "$$BCEL$$" it calls the createClass() method with that name
  69. * (everything bevor the $$BCEL$$ is considered to be the package
  70. * name. You can subclass the class loader and override that
  71. * method. "Normal" classes class can be modified by overriding the
  72. * modifyClass() method which is called just before defineClass().</p>
  73. *
  74. * <p>There may be a number of packages where you have to use the default
  75. * class loader (which may also be faster). You can define the set of packages
  76. * where to use the system class loader in the constructor. The default value contains
  77. * "java.", "sun.", "javax."</p>
  78. *
  79. * @version $Id: ClassLoader.java,v 1.1.1.1 2001/10/29 20:00:29 jvanzyl Exp $
  80. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  81. * @see JavaWrapper
  82. * @see ClassPath
  83. */
  84. public class ClassLoader extends java.lang.ClassLoader {
  85. private Hashtable classes = new Hashtable(); // Hashtable is synchronized thus thread-safe
  86. private String[] ignored_packages = {
  87. "java.", "javax.", "sun."
  88. };
  89. public ClassLoader() {
  90. }
  91. /** @param ignored_packages classes contained in these packages will be loaded
  92. * with the system class loader
  93. */
  94. public ClassLoader(String[] ignored_packages) {
  95. String[] new_p = new String[ignored_packages.length + this.ignored_packages.length];
  96. System.arraycopy(this.ignored_packages, 0, new_p, 0, this.ignored_packages.length);
  97. System.arraycopy(ignored_packages, 0, new_p, this.ignored_packages.length,
  98. ignored_packages.length);
  99. this.ignored_packages = new_p;
  100. }
  101. protected Class loadClass(String class_name, boolean resolve)
  102. throws ClassNotFoundException
  103. {
  104. Class cl = null;
  105. /* First try: lookup hash table.
  106. */
  107. if((cl=(Class)classes.get(class_name)) == null) {
  108. /* Second try: Load system class using system class loader. You better
  109. * don't mess around with them.
  110. */
  111. for(int i=0; i < ignored_packages.length; i++) {
  112. if(class_name.startsWith(ignored_packages[i])) {
  113. cl = Class.forName(class_name);
  114. break;
  115. }
  116. }
  117. if(cl == null) {
  118. JavaClass clazz = null;
  119. /* Third try: Special request?
  120. */
  121. if(class_name.indexOf("$$BCEL$$") >= 0)
  122. clazz = createClass(class_name);
  123. else // Fourth try: Load classes via repository
  124. clazz = modifyClass(Repository.lookupClass(class_name));
  125. if(clazz != null) {
  126. byte[] bytes = clazz.getBytes();
  127. cl = defineClass(class_name, bytes, 0, bytes.length);
  128. } else // Fourth try: Use default class loader
  129. cl = Class.forName(class_name);
  130. }
  131. if(resolve)
  132. resolveClass(cl);
  133. }
  134. classes.put(class_name, cl);
  135. return cl;
  136. }
  137. /** Override this method if you want to alter a class before it gets actually
  138. * loaded. Does nothing by default.
  139. */
  140. protected JavaClass modifyClass(JavaClass clazz) {
  141. return clazz;
  142. }
  143. /**
  144. * Override this method to create you own classes on the fly. The
  145. * name contains the special token $$BCEL$$. Everything before that
  146. * token is consddered to be a package name. You can encode you own
  147. * arguments into the subsequent string. You must regard however not
  148. * to use any "illegal" characters, i.e., characters that may not
  149. * appear in a Java class name too<br>
  150. *
  151. * The default implementation interprets the string as a encoded compressed
  152. * Java class, unpacks and decodes it with the Utility.decode() method, and
  153. * parses thee resulting byte array and returns the resulting JavaClass object.
  154. *
  155. * @param class_name compressed byte code with "$$BCEL$$" in it
  156. */
  157. protected JavaClass createClass(String class_name) {
  158. int index = class_name.indexOf("$$BCEL$$");
  159. String real_name = class_name.substring(index + 8);
  160. JavaClass clazz = null;
  161. try {
  162. byte[] bytes = Utility.decode(real_name, true);
  163. ClassParser parser = new ClassParser(new ByteArrayInputStream(bytes), "foo");
  164. clazz = parser.parse();
  165. } catch(Throwable e) {
  166. e.printStackTrace();
  167. return null;
  168. }
  169. // Adapt the class name to the passed value
  170. ConstantPool cp = clazz.getConstantPool();
  171. ConstantClass cl = (ConstantClass)cp.getConstant(clazz.getClassNameIndex(),
  172. Constants.CONSTANT_Class);
  173. ConstantUtf8 name = (ConstantUtf8)cp.getConstant(cl.getNameIndex(),
  174. Constants.CONSTANT_Utf8);
  175. name.setBytes(class_name.replace('.', '/'));
  176. return clazz;
  177. }
  178. }