1 16 package org.springframework.webflow.util; 17 18 import java.lang.reflect.InvocationTargetException ; 19 import java.lang.reflect.Method ; 20 21 28 public class ReflectionUtils { 29 30 39 public static Object invokeMethod(Method method, Object target) throws RuntimeException { 40 return invokeMethod(method, target, null); 41 } 42 43 52 public static Object invokeMethod(Method method, Object target, Object [] args) throws RuntimeException { 53 try { 54 return method.invoke(target, args); 55 } 56 catch (IllegalAccessException ex) { 57 handleReflectionException(ex); 58 throw new IllegalStateException ("Unexpected reflection exception - " + ex.getClass().getName() + ": " 59 + ex.getMessage()); 60 } 61 catch (InvocationTargetException ex) { 62 handleReflectionException(ex); 63 throw new IllegalStateException ("Unexpected reflection exception - " + ex.getClass().getName() + ": " 64 + ex.getMessage()); 65 } 66 } 67 68 78 private static void handleReflectionException(Exception ex) { 79 if (ex instanceof NoSuchMethodException ) { 80 throw new IllegalStateException ("Method not found: " + ex.getMessage()); 81 } 82 if (ex instanceof IllegalAccessException ) { 83 throw new IllegalStateException ("Could not access method: " + ex.getMessage()); 84 } 85 if (ex instanceof InvocationTargetException ) { 86 handleInvocationTargetException((InvocationTargetException ) ex); 87 } 88 throw new IllegalStateException ( 89 "Unexpected reflection exception - " + ex.getClass().getName() + ": " + ex.getMessage()); 90 } 91 92 101 private static void handleInvocationTargetException(InvocationTargetException ex) { 102 if (ex.getTargetException() instanceof RuntimeException ) { 103 throw (RuntimeException ) ex.getTargetException(); 104 } 105 if (ex.getTargetException() instanceof Error ) { 106 throw (Error ) ex.getTargetException(); 107 } 108 throw new IllegalStateException ( 109 "Unexpected exception thrown by method - " + ex.getTargetException().getClass().getName() + 110 ": " + ex.getTargetException().getMessage()); 111 } 112 } | Popular Tags |