1 package dynaop.util; 2 3 import java.util.*; 4 import java.lang.reflect.*; 5 6 11 public class Classes { 12 13 public static Method EQUALS_METHOD; 14 public static Method HASHCODE_METHOD; 15 public static Method TOSTRING_METHOD; 16 17 static { 18 try { 19 EQUALS_METHOD = 20 Object .class.getMethod("equals", new Class [] { Object .class }); 21 HASHCODE_METHOD = 22 Object .class.getMethod("hashCode", null); 23 TOSTRING_METHOD = 24 Object .class.getMethod("toString", null); 25 } 26 catch (Exception e) { 27 throw NestedException.wrap(e); 28 } 29 } 30 31 public static Method[] OBJECT_METHODS = new Method[] { EQUALS_METHOD, 32 HASHCODE_METHOD, TOSTRING_METHOD }; 33 public static Set OBJECT_METHODS_SET = 34 new HashSet(Arrays.asList(OBJECT_METHODS)); 35 36 private Classes() {} 37 38 42 public static Object newInstance(Class clazz) { 43 try { 44 return clazz.newInstance(); 45 } 46 catch (Exception e) { 47 throw NestedException.wrap(e); 48 } 49 } 50 51 54 public static Object newInstance(Constructor constructor, 55 Object [] arguments) { 56 try { 57 return constructor.newInstance(arguments); 58 } 59 catch (Exception e) { 60 throw NestedException.wrap(e); 61 } 62 } 63 64 67 public static boolean implementsInterfaces(Class clazz, 68 Class [] interfaces) { 69 for (int i = 0; i < interfaces.length; i++) 70 if (!interfaces[i].isAssignableFrom(clazz)) 71 return false; 72 return true; 73 } 74 75 79 public static Object invoke(Object target, Method method, 80 Object [] args) throws Throwable { 81 if (target instanceof Proxy) { 82 return Proxy.getInvocationHandler(target). 83 invoke(target, method, args); 84 } 85 else { 86 try { 87 return method.invoke(target, args); 88 } 89 catch (IllegalAccessException e) { 90 throw NestedException.wrap(e); 91 } 92 catch (InvocationTargetException e) { 93 throw NestedException.wrap(e.getTargetException()); 94 } 95 } 96 } 97 98 102 public static ClassLoader getClassLoader(Class clazz) { 103 ClassLoader loader = clazz.getClassLoader(); 104 return (loader == null) ? ClassLoader.getSystemClassLoader() : 105 loader; 106 } 107 108 112 public static ClassLoader getClassLoader() { 113 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 114 return (loader == null) ? ClassLoader.getSystemClassLoader() : 115 loader; 116 } 117 118 121 public static ClassLoader commonLoader(Class [] classes) { 122 if (classes.length == 0) throw new IllegalArgumentException ( 123 "No classes provided."); 124 if (classes.length == 1) return getClassLoader(classes[0]); 125 126 for (int i = 1; i < classes.length; i++) { 128 try { 129 ClassLoader loader = getClassLoader(classes[i]); 130 for (int i2 = 0; i2 < classes.length; i2++) { 131 if (i == i2) 132 continue; 133 loader.loadClass(classes[i2].getName()); 134 } 135 return loader; 136 } 137 catch (ClassNotFoundException e) {} 138 } 139 140 throw new RuntimeException ("No common class loader: " + 141 Arrays.asList(classes)); 142 } 143 144 147 public static ClassLoader commonLoader(Collection classes) { 148 return commonLoader( 149 (Class []) classes.toArray(new Class [classes.size()])); 150 } 151 152 155 public static List getAllInterfacesAsList(Class clazz) { 156 List interfaces = null; 157 if (clazz.isInterface()) 158 return Collections.singletonList(clazz); 159 else { 160 interfaces = new ArrayList(); 161 addAllInterfaces(clazz, interfaces); 162 Collections.reverse(interfaces); 163 return interfaces; 164 } 165 } 166 167 170 public static Class [] getAllInterfaces(Class clazz) { 171 return (Class []) getAllInterfacesAsList(clazz). 172 toArray(new Class [0]); 173 } 174 175 179 static void addAllInterfaces(Class clazz, List list) { 180 while (clazz != null && clazz != Object .class) { 181 Class [] interfaces = clazz.getInterfaces(); 182 183 for (int i = interfaces.length - 1; i >= 0; i--) 185 if (!list.contains(interfaces[i])) 186 list.add(interfaces[i]); 187 clazz = clazz.getSuperclass(); 188 } 189 } 190 191 static final Map primitiveMap = new HashMap(); 192 193 static { 194 primitiveMap.put("boolean", boolean.class); 196 primitiveMap.put("byte", byte.class); 197 primitiveMap.put("char", char.class); 198 primitiveMap.put("short", short.class); 199 primitiveMap.put("int", int.class); 200 primitiveMap.put("long", long.class); 201 primitiveMap.put("float", float.class); 202 primitiveMap.put("double", double.class); 203 primitiveMap.put("void", void.class); 204 } 205 206 210 public static Class forName(String name) throws ClassNotFoundException { 211 Class clazz = (Class ) primitiveMap.get(name); 212 if (clazz != null) 213 return clazz; 214 return getClassLoader().loadClass(name); 215 } 216 } 217 | Popular Tags |