1 27 28 package org.objectweb.kilim.model.services; 29 30 import java.lang.reflect.Array ; 31 import java.util.HashMap ; 32 33 import org.objectweb.kilim.KilimException; 34 import org.objectweb.kilim.description.Type; 35 36 39 public class DefaultRuntimeClassLoader implements RuntimeClassLoader { 40 private HashMap caches; 41 42 43 public static RuntimeClassLoader instance = new DefaultRuntimeClassLoader(); 44 45 48 public DefaultRuntimeClassLoader() { 49 caches = new HashMap (); 50 } 51 52 55 public Class getClass(Type type) throws KilimException { 56 return getClass(type.getName()); 57 } 58 59 62 public Class getClass(String class_name) throws KilimException { 63 Class result = null; 64 ClassLoader class_loader = Thread.currentThread().getContextClassLoader(); 65 Object cl_cache = caches.get(class_loader); 66 HashMap class_loader_cache = null; 67 if (cl_cache == null) { 68 class_loader_cache = new HashMap (); 69 caches.put(class_loader, class_loader_cache); 70 } else { 71 class_loader_cache = (HashMap ) cl_cache; 72 result = (Class ) class_loader_cache.get(class_name); 73 } 74 75 if (result != null) { 76 return result; 77 } 78 79 int size = 0; 80 int index = 0; 81 while (true) { 82 index = class_name.indexOf('[', index); 83 if (index < 0) { 84 break; 85 } else { 86 size++; 87 index += 2; 89 } 90 } 91 if (size > 0) { 92 String element_class_name = class_name.substring(0, class_name.indexOf('[')); 94 Class element_class = getClass(element_class_name); 95 int[] sizes = new int[size]; 97 for (int i = 0; i < size; i++) { 98 sizes[i] = 0; 99 } 100 Object array = Array.newInstance(element_class, sizes); 101 result = array.getClass(); 103 } else { 104 result = primitiveClass(class_name); 105 106 if (result == null) { 107 try { 108 result = class_loader.loadClass(class_name); 109 } catch (ClassNotFoundException ignored) { } 110 if (result == null) { 111 try { 112 result = Class.forName(class_name); 113 } catch (ClassNotFoundException e) { 114 throw new KilimException(e); 115 } 116 } 117 } 118 } 119 class_loader_cache.put(class_name, result); 120 121 return result; 122 } 123 124 static Class primitiveClass(String name) { 125 switch (name.charAt(0)) { 126 case 'b': 127 switch (name.charAt(1)) { 128 case 'o': 129 return "boolean".equals(name) ? boolean.class : null; 130 default: 131 return "byte".equals(name) ? byte.class : null; 132 } 133 case 'c': 134 return "char".equals(name) ? char.class : null; 135 case 'd': 136 return "double".equals(name) ? double.class : null; 137 case 'f': 138 return "float".equals(name) ? float.class : null; 139 case 'i': 140 return "int".equals(name) ? int.class : null; 141 case 'l': 142 return "long".equals(name) ? long.class : null; 143 case 's': 144 return "short".equals(name) ? short.class : null; 145 default: 146 return "void".equals(name) ? void.class : null; 147 } 148 } 149 } 150 | Popular Tags |