1 17 package org.apache.bcel.util; 18 19 import java.io.ByteArrayInputStream ; 20 import java.util.Hashtable ; 21 import org.apache.bcel.Constants; 22 import org.apache.bcel.classfile.ClassParser; 23 import org.apache.bcel.classfile.ConstantClass; 24 import org.apache.bcel.classfile.ConstantPool; 25 import org.apache.bcel.classfile.ConstantUtf8; 26 import org.apache.bcel.classfile.JavaClass; 27 import org.apache.bcel.classfile.Utility; 28 29 53 public class ClassLoader extends java.lang.ClassLoader { 54 55 public static final String [] DEFAULT_IGNORED_PACKAGES = { 56 "java.", "javax.", "sun." 57 }; 58 private Hashtable classes = new Hashtable (); private String [] ignored_packages; 60 private Repository repository = SyntheticRepository.getInstance(); 61 62 63 66 public ClassLoader() { 67 this(DEFAULT_IGNORED_PACKAGES); 68 } 69 70 71 73 public ClassLoader(java.lang.ClassLoader deferTo) { 74 super(deferTo); 75 this.ignored_packages = DEFAULT_IGNORED_PACKAGES; 76 this.repository = new ClassLoaderRepository(deferTo); 77 } 78 79 80 83 public ClassLoader(String [] ignored_packages) { 84 this.ignored_packages = ignored_packages; 85 } 86 87 88 92 public ClassLoader(java.lang.ClassLoader deferTo, String [] ignored_packages) { 93 this(ignored_packages); 94 this.repository = new ClassLoaderRepository(deferTo); 95 } 96 97 98 protected Class loadClass( String class_name, boolean resolve ) throws ClassNotFoundException { 99 Class cl = null; 100 102 if ((cl = (Class ) classes.get(class_name)) == null) { 103 106 for (int i = 0; i < ignored_packages.length; i++) { 107 if (class_name.startsWith(ignored_packages[i])) { 108 cl = getParent().loadClass(class_name); 109 break; 110 } 111 } 112 if (cl == null) { 113 JavaClass clazz = null; 114 116 if (class_name.indexOf("$$BCEL$$") >= 0) { 117 clazz = createClass(class_name); 118 } else { if ((clazz = repository.loadClass(class_name)) != null) { 120 clazz = modifyClass(clazz); 121 } else { 122 throw new ClassNotFoundException (class_name); 123 } 124 } 125 if (clazz != null) { 126 byte[] bytes = clazz.getBytes(); 127 cl = defineClass(class_name, bytes, 0, bytes.length); 128 } else { 129 cl = Class.forName(class_name); 130 } 131 } 132 if (resolve) { 133 resolveClass(cl); 134 } 135 } 136 classes.put(class_name, cl); 137 return cl; 138 } 139 140 141 144 protected JavaClass modifyClass( JavaClass clazz ) { 145 return clazz; 146 } 147 148 149 163 protected JavaClass createClass( String class_name ) { 164 int index = class_name.indexOf("$$BCEL$$"); 165 String real_name = class_name.substring(index + 8); 166 JavaClass clazz = null; 167 try { 168 byte[] bytes = Utility.decode(real_name, true); 169 ClassParser parser = new ClassParser(new ByteArrayInputStream (bytes), "foo"); 170 clazz = parser.parse(); 171 } catch (Throwable e) { 172 e.printStackTrace(); 173 return null; 174 } 175 ConstantPool cp = clazz.getConstantPool(); 177 ConstantClass cl = (ConstantClass) cp.getConstant(clazz.getClassNameIndex(), 178 Constants.CONSTANT_Class); 179 ConstantUtf8 name = (ConstantUtf8) cp.getConstant(cl.getNameIndex(), 180 Constants.CONSTANT_Utf8); 181 name.setBytes(class_name.replace('.', '/')); 182 return clazz; 183 } 184 } 185 | Popular Tags |