1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util;
17
18 import java.lang.reflect.InvocationTargetException;
19
20 import org.springframework.util.MethodInvoker;
21
22 public class ReflectionUtils extends org.springframework.util.ReflectionUtils {
23
24 public static Object invokeMethod(Class<?> targetClass, String targetMethod, Object... arguments) {
25 MethodInvoker invoker = new MethodInvoker();
26 invoker.setTargetClass(targetClass);
27 invoker.setTargetMethod(targetMethod);
28 invoker.setArguments(arguments);
29 return invoke(invoker);
30 }
31
32 public static Object invoke(MethodInvoker invoker) {
33 try {
34 invoker.prepare();
35 return invoker.invoke();
36 } catch (ClassNotFoundException e) {
37 throw new IllegalStateException(e);
38 } catch (NoSuchMethodException e) {
39 throw new IllegalStateException(e);
40 } catch (InvocationTargetException e) {
41 throw new IllegalStateException(e);
42 } catch (IllegalAccessException e) {
43 throw new IllegalStateException(e);
44 }
45 }
46
47 public static Class<?> getClass(String className) {
48 try {
49 return Class.forName(className);
50 } catch (ClassNotFoundException e) {
51 throw new IllegalArgumentException(e);
52 }
53 }
54
55 public static <T> T newInstance(String className) {
56 @SuppressWarnings("unchecked")
57 Class<T> clazz = (Class<T>) getClass(className);
58 return (T) newInstance(clazz);
59 }
60
61 public static <T> T newInstance(Class<T> instanceClass) {
62 try {
63 return (T) instanceClass.newInstance();
64 } catch (IllegalAccessException e) {
65 throw new IllegalArgumentException("Unexpected error", e);
66 } catch (InstantiationException e) {
67 throw new IllegalArgumentException("Unexpected error", e);
68 }
69 }
70
71 }