1 package com.sun.org.apache.bcel.internal.util; 2 3 56 57 import java.util.Hashtable ; 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 87 public class ClassLoader extends java.lang.ClassLoader { 88 private Hashtable classes = new Hashtable (); private String [] ignored_packages = { 90 "java.", "javax.", "sun." 91 }; 92 93 public ClassLoader() { 94 } 95 96 99 public ClassLoader(String [] ignored_packages) { 100 String [] new_p = new String [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 loadClass(String class_name, boolean resolve) 110 throws ClassNotFoundException 111 { 112 Class cl = null; 113 114 116 if((cl=(Class )classes.get(class_name)) == null) { 117 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 132 if(class_name.indexOf("$$BCEL$$") >= 0) 133 clazz = createClass(class_name); 134 else 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 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 156 protected JavaClass modifyClass(JavaClass clazz) { 157 return clazz; 158 } 159 160 174 protected JavaClass createClass(String class_name) { 175 int index = class_name.indexOf("$$BCEL$$"); 176 String 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 e) { 185 e.printStackTrace(); 186 return null; 187 } 188 189 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 |