1 29 30 package com.caucho.bytecode; 31 32 import java.lang.ref.SoftReference ; 33 import java.util.HashMap ; 34 import java.util.logging.Logger ; 35 36 39 abstract public class JClassLoader { 40 protected static final Logger log = 41 Logger.getLogger(JClassLoader.class.getName()); 42 43 private static JClassLoaderWrapper _staticClassLoader; 44 45 private static final HashMap <String ,JClass> _staticClassMap = 46 new HashMap <String ,JClass>(); 47 48 private final HashMap <String ,SoftReference <JClass>> _classMap = 49 new HashMap <String ,SoftReference <JClass>>(); 50 51 54 public JClass forName(String name) 55 { 56 SoftReference <JClass> jClassRef = _classMap.get(name); 57 58 JClass jClass = jClassRef != null ? jClassRef.get() : null; 59 60 if (jClass == null) { 61 jClass = _staticClassMap.get(name); 62 63 if (jClass == null) { 64 if (name.startsWith("[")) { 65 JClass subClass = descriptorToClass(name, 1); 66 jClass = new JClassArray(subClass); 67 } 68 else 69 jClass = loadClass(name); 70 } 71 72 _classMap.put(name, new SoftReference <JClass>(jClass)); 73 } 74 75 return jClass; 76 } 77 78 81 public void close() 82 { 83 _classMap.clear(); 84 } 85 86 89 public static JClass systemForName(String name) 90 { 91 return getSystemClassLoader().forName(name); 92 } 93 94 97 static JClassLoader getSystemClassLoader() 98 { 99 if (_staticClassLoader == null) 100 _staticClassLoader = JClassLoaderWrapper.create(ClassLoader.getSystemClassLoader()); 101 102 return _staticClassLoader; 103 } 104 105 108 public static JClass localForName(String name) 109 { 110 JClassLoaderWrapper jLoader = JClassLoaderWrapper.create(); 111 112 return jLoader.forName(name); 113 } 114 115 118 abstract protected JClass loadClass(String name); 119 120 123 public static JClassLoader getStaticClassLoader() 124 { 125 return getSystemClassLoader(); 126 } 127 128 public JClass descriptorToClass(String name, int i) 129 { 130 switch (name.charAt(i)) { 131 case 'V': return forName("void"); 132 case 'Z': return forName("boolean"); 133 case 'C': return forName("char"); 134 case 'B': return forName("byte"); 135 case 'S': return forName("short"); 136 case 'I': return forName("int"); 137 case 'J': return forName("long"); 138 case 'F': return forName("float"); 139 case 'D': return forName("double"); 140 case '[': 141 return forName(name.substring(i)); 142 143 case 'L': 144 { 145 int tail = name.indexOf(';', i); 146 147 if (tail < 0) 148 throw new IllegalStateException (name); 149 150 String className = name.substring(i + 1, tail).replace('/', '.'); 151 152 return forName(className); 153 } 154 155 default: 156 throw new UnsupportedOperationException (name.substring(i)); 157 } 158 } 159 160 public String toString() 161 { 162 return "JClassLoader[]"; 163 } 164 165 static { 166 _staticClassMap.put("void", JClass.VOID); 167 _staticClassMap.put("boolean", JClass.BOOLEAN); 168 _staticClassMap.put("byte", JClass.BYTE); 169 _staticClassMap.put("short", JClass.SHORT); 170 _staticClassMap.put("int", JClass.INT); 171 _staticClassMap.put("long", JClass.LONG); 172 _staticClassMap.put("float", JClass.FLOAT); 173 _staticClassMap.put("double", JClass.DOUBLE); 174 _staticClassMap.put("char", JClass.CHAR); 175 _staticClassMap.put("java.lang.String", JClass.STRING); 176 _staticClassMap.put("java.lang.Object", JClass.OBJECT); 177 } 178 } 179 | Popular Tags |