1 7 package com.inversoft.util; 8 9 10 import java.lang.reflect.InvocationTargetException ; 11 import java.lang.reflect.Method ; 12 13 14 21 public class ReflectionTools { 22 23 30 public static Class findClass(String className) throws ReflectionException { 31 return findClass(className, null); 32 } 33 34 42 public static Class findClass(String className, String packageName) 43 throws ReflectionException { 44 45 assert (className != null) : "className == null"; 46 47 Class klass = null; 48 try { 49 if (StringTools.isTrimmedEmpty(packageName)) { 50 klass = Class.forName(className); 51 } else { 52 klass = Class.forName(packageName + "." + className); 53 } 54 } catch (ClassNotFoundException cnfe) { 55 throw new ReflectionException("Class not found: " + cnfe.getMessage(), 56 cnfe); 57 } 58 59 return klass; 60 } 61 62 79 public static Method getMethod(Class klass, String method, Class [] params) 80 throws ReflectionException { 81 try { 82 return klass.getMethod(method, params); 83 } catch (NoSuchMethodException nsme) { 84 throw new ReflectionException("No such method: " + nsme.getMessage(), 85 nsme); 86 } catch (SecurityException se) { 87 StringBuffer error = new StringBuffer (); 88 error.append("Security violation accessing method "); 89 error.append(klass.getName()); 90 error.append("#"); 91 error.append(method); 92 error.append("("); 93 for (int i = 0; params != null && i < params.length; i++) { 94 error.append(params[i].getName()); 95 } 96 error.append(")"); 97 throw new ReflectionException(error.toString(), se); 98 } 99 } 100 101 109 public static Method [] getMethods(Class klass) throws ReflectionException { 110 try { 111 return klass.getMethods(); 112 } catch (SecurityException se) { 113 StringBuffer error = new StringBuffer (); 114 error.append("Security violation getting methods of class: "); 115 error.append(klass.getName()); 116 throw new ReflectionException(error.toString(), se); 117 } 118 } 119 120 155 public static Method findMethod(Class klass, String method, Class [] params, 156 int index) { 157 Class [] localParams = new Class [params.length]; 158 System.arraycopy(params, 0, localParams, 0, params.length); 159 160 Class type = localParams[index]; 161 Method methodObj = null; 162 while (type != null) { 163 localParams[index] = type; 164 165 try { 166 methodObj = klass.getMethod(method, localParams); 167 break; 168 } catch (NoSuchMethodException nsme) { 169 type = localParams[index].getSuperclass(); 171 } catch (SecurityException se) { 172 type = localParams[index].getSuperclass(); 174 } 175 } 176 177 return methodObj; 178 } 179 180 204 public static Object invokeMethod(Method method, Object object, Object [] params) 205 throws ReflectionException, RuntimeException , Error { 206 try { 207 return method.invoke(object, params); 208 } catch (IllegalAccessException iae) { 209 StringBuffer error = new StringBuffer (); 210 error.append("Access error for method: "); 211 error.append(method.toString()); 212 throw new ReflectionException(error.toString(), iae); 213 } catch (IllegalArgumentException iare) { 214 StringBuffer error = new StringBuffer (); 215 error.append("Error while calling method: ").append(method.toString()); 216 throw new ReflectionException(error.toString(), iare); 217 } catch (InvocationTargetException ite) { 218 219 Throwable target = ite.getTargetException(); 221 222 if (target instanceof RuntimeException ) { 223 throw (RuntimeException ) target; 224 } 225 226 if (target instanceof Error ) { 227 throw (Error ) target; 228 } 229 230 StringBuffer error = new StringBuffer (); 231 error.append("Method "); 232 error.append(method.toString()); 233 error.append(" threw exception: "); 234 error.append(target.toString()); 235 throw new ReflectionException(error.toString(), ite, target); 236 } 237 } 238 239 251 public static Object instantiate(Class objectClass) throws ReflectionException { 252 try { 253 return objectClass.newInstance(); 254 } catch (InstantiationException ie) { 255 throw new ReflectionException("Error instantiating " + 256 objectClass.getName(), ie); 257 } catch (IllegalAccessException iae) { 258 throw new ReflectionException("Access violation instantiating " + 259 objectClass.getName(), iae); 260 } catch (SecurityException se) { 261 throw new ReflectionException("Security violation instantiating " + 262 objectClass.getName(), se); 263 } 264 } 265 266 277 public static Object instantiate(String className) throws ReflectionException { 278 try { 279 return instantiate(Class.forName(className)); 280 } catch (ClassNotFoundException cnfe) { 281 throw new ReflectionException("Class " + className + 282 " not found in class path", cnfe); 283 } 284 } 285 286 294 public static Class convertToWrapper(Class type) { 295 296 if (!type.isPrimitive()) { 297 return null; 298 } 299 300 Class klass = null; 301 if (type == Boolean.TYPE) { 302 klass = Boolean .class; 303 } else if (type == Byte.TYPE) { 304 klass = Byte .class; 305 } else if (type == Character.TYPE) { 306 klass = Character .class; 307 } else if (type == Short.TYPE) { 308 klass = Short .class; 309 } else if (type == Integer.TYPE) { 310 klass = Integer .class; 311 } else if (type == Long.TYPE) { 312 klass = Long .class; 313 } else if (type == Float.TYPE) { 314 klass = Float .class; 315 } else if (type == Double.TYPE) { 316 klass = Double .class; 317 } 318 319 return klass; 320 } 321 } 322 | Popular Tags |