1 18 package org.apache.activemq.util; 19 20 import java.lang.reflect.Array ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 29 public class ClassLoading { 30 31 44 public static Class loadClass(final String className, final ClassLoader classLoader) throws ClassNotFoundException { 45 if (className == null) { 46 throw new IllegalArgumentException ("className is null"); 47 } 48 49 try { 51 return load(className, classLoader); 52 } catch (ClassNotFoundException ignore) { 53 } 55 56 Class type = null; 57 58 type = getPrimitiveType(className); 60 if (type != null) 61 return type; 62 63 type = getVMPrimitiveType(className); 65 if (type != null) 66 return type; 67 68 if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';') { 70 String name = className.substring(1, className.length() - 1); 71 return load(name, classLoader); 72 } 73 74 if (className.charAt(0) == '[') { 76 int arrayDimension = className.lastIndexOf('[') + 1; 77 String componentClassName = className.substring(arrayDimension, className.length()); 78 type = loadClass(componentClassName, classLoader); 79 80 int dim[] = new int[arrayDimension]; 81 java.util.Arrays.fill(dim, 0); 82 return Array.newInstance(type, dim).getClass(); 83 } 84 85 if (className.endsWith("[]")) { 87 int arrayDimension = 0; 89 String componentClassName = className; 90 while (componentClassName.endsWith("[]")) { 91 componentClassName = componentClassName.substring(0, componentClassName.length() - 2); 92 arrayDimension++; 93 } 94 95 type = loadClass(componentClassName, classLoader); 97 98 int[] dim = new int[arrayDimension]; 100 java.util.Arrays.fill(dim, 0); 101 return Array.newInstance(type, dim).getClass(); 102 } 103 104 throw new ClassNotFoundException (className); 106 } 107 108 private static Class load(final String className, final ClassLoader classLoader) throws ClassNotFoundException { 109 if (classLoader == null) 110 return Class.forName(className); 111 else 112 return classLoader.loadClass(className); 113 } 114 115 public static String getClassName(Class clazz) { 116 StringBuffer rc = new StringBuffer (); 117 while (clazz.isArray()) { 118 rc.append('['); 119 clazz = clazz.getComponentType(); 120 } 121 if (!clazz.isPrimitive()) { 122 rc.append('L'); 123 rc.append(clazz.getName()); 124 rc.append(';'); 125 } else { 126 rc.append(VM_PRIMITIVES_REVERSE.get(clazz)); 127 } 128 return rc.toString(); 129 } 130 131 134 private static final Map PRIMITIVES = new HashMap (); 135 136 137 static { 138 PRIMITIVES.put("boolean", Boolean.TYPE); 139 PRIMITIVES.put("byte", Byte.TYPE); 140 PRIMITIVES.put("char", Character.TYPE); 141 PRIMITIVES.put("short", Short.TYPE); 142 PRIMITIVES.put("int", Integer.TYPE); 143 PRIMITIVES.put("long", Long.TYPE); 144 PRIMITIVES.put("float", Float.TYPE); 145 PRIMITIVES.put("double", Double.TYPE); 146 PRIMITIVES.put("void", Void.TYPE); 147 } 148 149 156 private static Class getPrimitiveType(final String name) { 157 return (Class ) PRIMITIVES.get(name); 158 } 159 160 163 private static final HashMap VM_PRIMITIVES = new HashMap (); 164 165 166 static { 167 VM_PRIMITIVES.put("B", byte.class); 168 VM_PRIMITIVES.put("C", char.class); 169 VM_PRIMITIVES.put("D", double.class); 170 VM_PRIMITIVES.put("F", float.class); 171 VM_PRIMITIVES.put("I", int.class); 172 VM_PRIMITIVES.put("J", long.class); 173 VM_PRIMITIVES.put("S", short.class); 174 VM_PRIMITIVES.put("Z", boolean.class); 175 VM_PRIMITIVES.put("V", void.class); 176 } 177 178 181 private static final HashMap VM_PRIMITIVES_REVERSE = new HashMap (); 182 183 184 static { 185 VM_PRIMITIVES_REVERSE.put(byte.class, "B"); 186 VM_PRIMITIVES_REVERSE.put(char.class, "C"); 187 VM_PRIMITIVES_REVERSE.put(double.class, "D"); 188 VM_PRIMITIVES_REVERSE.put(float.class, "F"); 189 VM_PRIMITIVES_REVERSE.put(int.class, "I"); 190 VM_PRIMITIVES_REVERSE.put(long.class, "J"); 191 VM_PRIMITIVES_REVERSE.put(short.class, "S"); 192 VM_PRIMITIVES_REVERSE.put(boolean.class, "Z"); 193 VM_PRIMITIVES_REVERSE.put(void.class, "V"); 194 } 195 196 219 private static Class getVMPrimitiveType(final String name) { 220 return (Class ) VM_PRIMITIVES.get(name); 221 } 222 223 226 private static final Map PRIMITIVE_WRAPPERS = new HashMap (); 227 228 229 static { 230 PRIMITIVE_WRAPPERS.put(Boolean.TYPE, Boolean .class); 231 PRIMITIVE_WRAPPERS.put(Byte.TYPE, Byte .class); 232 PRIMITIVE_WRAPPERS.put(Character.TYPE, Character .class); 233 PRIMITIVE_WRAPPERS.put(Double.TYPE, Double .class); 234 PRIMITIVE_WRAPPERS.put(Float.TYPE, Float .class); 235 PRIMITIVE_WRAPPERS.put(Integer.TYPE, Integer .class); 236 PRIMITIVE_WRAPPERS.put(Long.TYPE, Long .class); 237 PRIMITIVE_WRAPPERS.put(Short.TYPE, Short .class); 238 PRIMITIVE_WRAPPERS.put(Void.TYPE, Void .class); 239 } 240 } 241 | Popular Tags |