1 26 27 package net.sourceforge.groboutils.util.classes.v1.jdk0; 28 29 import java.util.Hashtable ; 30 import java.util.Enumeration ; 31 import java.util.Vector ; 32 33 34 35 42 public class ArrayClassLoader extends ClassLoader 43 { 44 47 48 49 52 55 private Hashtable m_classList = new Hashtable (); 56 private Hashtable m_classCache = new Hashtable (); 57 58 59 62 private BytecodeSource m_bytecodeSource = null; 63 64 67 70 public ArrayClassLoader() 71 { 72 } 74 75 76 79 80 85 public void setBytecodeSource( BytecodeSource bs ) 86 { 87 this.m_bytecodeSource = bs; 88 } 89 90 91 94 public void addClass( String name, byte[] bytecode ) 95 { 96 if (name == null || bytecode == null) 97 { 98 throw new IllegalArgumentException ("no null args"); 99 } 100 this.m_classList.put( name, bytecode ); 101 } 102 103 104 110 public Class loadClass( String name, boolean resolve ) 111 throws ClassNotFoundException 112 { 113 Class c; 114 115 if (name == null) 116 { 117 throw new IllegalArgumentException ("classname is null"); 118 } 119 120 c = (Class )this.m_classCache.get( name ); 121 if (c == null) 122 { 123 try 124 { 125 c = findSystemClass( name ); 126 } 127 catch (Exception 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 ( 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 ex2) 144 { 145 throw new ClassNotFoundException ( 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 162 163 169 protected byte[] getBytecode( String 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 } 190 191 | Popular Tags |