1 10 package org.jgap.util; 11 12 import java.util.*; 13 14 31 public abstract class MultiClassLoader 32 extends ClassLoader { 33 34 private final static String CVS_REVISION = "$Revision: 1.1 $"; 35 36 private Hashtable classes = new Hashtable(); 38 39 private char classNameReplacementChar; 40 41 protected boolean monitorOn = false; 42 43 protected boolean sourceMonitorOn = true; 44 45 public MultiClassLoader() { 46 } 47 48 53 public Class loadClass(String className) 54 throws ClassNotFoundException { 55 return (loadClass(className, true)); 56 } 57 58 public synchronized Class loadClass(String className, 59 boolean resolveIt) 60 throws ClassNotFoundException { 61 Class result; 62 byte[] classBytes; 63 monitor(">> MultiClassLoader.loadClass(" + className + ", " + resolveIt + 64 ")"); 65 result = (Class ) classes.get(className); 67 if (result != null) { 68 monitor(">> returning cached result."); 69 return result; 70 } 71 try { 73 result = super.findSystemClass(className); 74 monitor(">> returning system class (in CLASSPATH)."); 75 return result; 76 } catch (ClassNotFoundException e) { 77 monitor(">> Not a system class."); 78 } 79 classBytes = loadClassBytes(className); 82 if (classBytes == null) { 83 throw new ClassNotFoundException (); 84 } 85 result = defineClass(className, classBytes, 0, classBytes.length); 87 if (result == null) { 88 throw new ClassFormatError (); 89 } 90 if (resolveIt) 92 resolveClass(result); 93 classes.put(className, result); 95 monitor(">> Returning newly loaded class."); 96 return result; 97 } 98 99 106 public void setClassNameReplacementChar(char replacement) { 107 classNameReplacementChar = replacement; 108 } 109 110 protected abstract byte[] loadClassBytes(String className); 111 112 protected String formatClassName(String className) { 113 if (classNameReplacementChar == '\u0000') { 114 return className.replace('.', '/') + ".class"; 116 } 117 else { 118 return className.replace('.', 120 classNameReplacementChar) + ".class"; 121 } 122 } 123 124 protected void monitor(String text) { 125 if (monitorOn) 126 print(text); 127 } 128 129 protected static void print(String text) { 130 System.out.println(text); 131 } 132 } 133 | Popular Tags |