1 17 18 package org.apache.geronimo.kernel; 19 20 import java.lang.reflect.Array ; 21 import java.util.HashMap ; 22 import java.util.Set ; 23 import java.util.LinkedHashSet ; 24 import java.util.LinkedList ; 25 import java.util.Arrays ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 29 import org.apache.geronimo.kernel.config.MultiParentClassLoader; 30 31 47 public class ClassLoading { 48 49 53 private static final HashMap PRIMITIVE_CLASS_MAP = new HashMap (); 54 55 60 private static final HashMap CLASS_TO_SIGNATURE_MAP = new HashMap (); 61 62 63 67 static { 68 PRIMITIVE_CLASS_MAP.put("boolean", boolean.class); 69 PRIMITIVE_CLASS_MAP.put("Z", boolean.class); 70 PRIMITIVE_CLASS_MAP.put("byte", byte.class); 71 PRIMITIVE_CLASS_MAP.put("B", byte.class); 72 PRIMITIVE_CLASS_MAP.put("char", char.class); 73 PRIMITIVE_CLASS_MAP.put("C", char.class); 74 PRIMITIVE_CLASS_MAP.put("short", short.class); 75 PRIMITIVE_CLASS_MAP.put("S", short.class); 76 PRIMITIVE_CLASS_MAP.put("int", int.class); 77 PRIMITIVE_CLASS_MAP.put("I", int.class); 78 PRIMITIVE_CLASS_MAP.put("long", long.class); 79 PRIMITIVE_CLASS_MAP.put("J", long.class); 80 PRIMITIVE_CLASS_MAP.put("float", float.class); 81 PRIMITIVE_CLASS_MAP.put("F", float.class); 82 PRIMITIVE_CLASS_MAP.put("double", double.class); 83 PRIMITIVE_CLASS_MAP.put("D", double.class); 84 PRIMITIVE_CLASS_MAP.put("void", void.class); 85 PRIMITIVE_CLASS_MAP.put("V", void.class); 86 87 91 CLASS_TO_SIGNATURE_MAP.put(boolean.class, "Z"); 92 CLASS_TO_SIGNATURE_MAP.put(byte.class, "B"); 93 CLASS_TO_SIGNATURE_MAP.put(char.class, "C"); 94 CLASS_TO_SIGNATURE_MAP.put(short.class, "S"); 95 CLASS_TO_SIGNATURE_MAP.put(int.class, "I"); 96 CLASS_TO_SIGNATURE_MAP.put(long.class, "J"); 97 CLASS_TO_SIGNATURE_MAP.put(float.class, "F"); 98 CLASS_TO_SIGNATURE_MAP.put(double.class, "D"); 99 CLASS_TO_SIGNATURE_MAP.put(void.class, "V"); 100 } 101 102 103 116 public static Class loadClass(String className, ClassLoader classLoader) throws ClassNotFoundException { 117 118 if (className == null) { 120 throw new IllegalArgumentException ("className is null"); 121 } 122 123 if (classLoader == null) { 124 throw new IllegalArgumentException ("classLoader is null"); 125 } 126 try { 130 return classLoader.loadClass(className); 131 } catch (ClassNotFoundException ignore) { 132 } 134 135 136 Class resolvedClass = (Class ) PRIMITIVE_CLASS_MAP.get(className); 139 if (resolvedClass != null) { 140 return resolvedClass; 141 } 142 143 if (className.endsWith(";") && className.startsWith("L")) { 149 String typeName = className.substring(1, className.length() - 1); 151 return classLoader.loadClass(typeName); 153 } 154 155 160 if (className.charAt(0) == '[') { 162 int count = 0; 165 int nameLen = className.length(); 166 167 while (count < nameLen && className.charAt(count) == '[') { 168 count++; 169 } 170 171 String arrayTypeName = className.substring(count, className.length()); 173 Class arrayType = loadClass(arrayTypeName, classLoader); 176 177 return getArrayClass(arrayType, count); 181 } 182 183 184 if (className.endsWith("[]")) { 188 int count = 0; 190 int position = className.length(); 191 192 while (position > 1 && className.substring(position - 2, position).equals("[]")) { 193 count++; 195 position -= 2; 197 } 198 199 202 String typeName = className.substring(0, position); 203 204 Class arrayType = loadClass(typeName, classLoader); 206 return getArrayClass(arrayType, count); 208 } 209 210 if (classLoader instanceof MultiParentClassLoader) { 212 MultiParentClassLoader cl = (MultiParentClassLoader) classLoader; 213 throw new ClassNotFoundException ("Could not load class " + className + " from classloader: " + cl.getId() + ", destroyed state: " + cl.isDestroyed()); 214 } 215 throw new ClassNotFoundException ("Could not load class " + className + " from unknown classloader; " + classLoader); 216 } 217 218 219 232 public static String getClassName(Class type) { 233 StringBuffer name = new StringBuffer (); 234 235 239 while (type.isArray()) { 244 name.append('['); 247 type = type.getComponentType(); 248 } 249 250 if (type.isPrimitive()) { 253 name.append((String ) CLASS_TO_SIGNATURE_MAP.get(type)); 254 } 255 else { 257 name.append('L'); 258 name.append(type.getName()); 259 name.append(';'); 260 } 261 return name.toString(); 262 } 263 264 private static Class getArrayClass(Class type, int dimension) { 265 int dimensions[] = new int[dimension]; 269 return Array.newInstance(type, dimensions).getClass(); 271 } 272 273 public static Set getAllTypes(Class type) { 274 Set allTypes = new LinkedHashSet (); 275 allTypes.add(type); 276 allTypes.addAll(getAllSuperClasses(type)); 277 allTypes.addAll(getAllInterfaces(type)); 278 return allTypes; 279 } 280 281 private static Set getAllSuperClasses(Class clazz) { 282 Set allSuperClasses = new LinkedHashSet (); 283 for (Class superClass = clazz.getSuperclass(); superClass != null; superClass = superClass.getSuperclass()) { 284 allSuperClasses.add(superClass); 285 } 286 return allSuperClasses; 287 } 288 289 private static Set getAllInterfaces(Class clazz) { 290 Set allInterfaces = new LinkedHashSet (); 291 LinkedList stack = new LinkedList (); 292 stack.addAll(Arrays.asList(clazz.getInterfaces())); 293 while (!stack.isEmpty()) { 294 Class intf = (Class ) stack.removeFirst(); 295 if (!allInterfaces.contains(intf)) { 296 allInterfaces.add(intf); 297 stack.addAll(Arrays.asList(intf.getInterfaces())); 298 } 299 } 300 return allInterfaces; 301 } 302 303 public static Set reduceInterfaces(Set source) { 304 Class [] classes = (Class []) source.toArray(new Class [source.size()]); 305 classes = reduceInterfaces(classes); 306 return new LinkedHashSet (Arrays.asList(classes)); 307 } 308 309 320 public static Class [] reduceInterfaces(Class [] source) { 321 source = (Class []) source.clone(); 323 324 for (int leftIndex = 0; leftIndex < source.length-1; leftIndex++) { 325 Class left = source[leftIndex]; 326 if(left == null) { 327 continue; 328 } 329 330 for (int rightIndex = leftIndex +1; rightIndex < source.length; rightIndex++) { 331 Class right = source[rightIndex]; 332 if(right == null) { 333 continue; 334 } 335 336 if(left == right || right.isAssignableFrom(left)) { 337 source[rightIndex] = null; 339 } else if(left.isAssignableFrom(right)) { 340 source[leftIndex] = null; 342 343 break; 345 } 346 } 347 } 348 349 Class clazz = null; 350 for (int i = 0; i < source.length; i++) { 351 if (source[i] != null && !source[i].isInterface()) { 352 if (clazz != null) { 353 throw new IllegalArgumentException ("Source contains two classes which are not subclasses of each other: " + clazz.getName() + ", " + source[i].getName()); 354 } 355 clazz = source[i]; 356 source[i] = null; 357 } 358 } 359 360 List list = new ArrayList (source.length); 361 if (clazz != null) list.add(clazz); 362 for (int i = 0; i < source.length; i++) { 363 if(source[i] != null) { 364 list.add(source[i]); 365 } 366 } 367 return (Class []) list.toArray(new Class [list.size()]); 368 } 369 } 370 371 | Popular Tags |