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 import java.util.Map;
20
21 import org.apache.commons.beanutils.BeanUtils;
22 import org.springframework.util.MethodInvoker;
23
24 public class ReflectionUtils extends org.springframework.util.ReflectionUtils {
25
26 @SuppressWarnings("unchecked")
27 public static Map<String, Object> describe(Object bean) {
28 try {
29 return BeanUtils.describe(bean);
30 } catch (IllegalAccessException e) {
31 throw new IllegalStateException(e);
32 } catch (InvocationTargetException e) {
33 throw new IllegalStateException(e);
34 } catch (NoSuchMethodException e) {
35 throw new IllegalStateException(e);
36 }
37 }
38
39 public static void copyProperty(Object bean, String name, Object value) {
40 try {
41 BeanUtils.copyProperty(bean, name, value);
42 } catch (IllegalAccessException e) {
43 throw new IllegalStateException(e);
44 } catch (InvocationTargetException e) {
45 throw new IllegalStateException(e);
46 }
47 }
48
49 public static Object invokeMethod(Class<?> targetClass, String targetMethod, Object... arguments) {
50 MethodInvoker invoker = new MethodInvoker();
51 invoker.setTargetClass(targetClass);
52 invoker.setTargetMethod(targetMethod);
53 invoker.setArguments(arguments);
54 return invoke(invoker);
55 }
56
57 public static Object invoke(MethodInvoker invoker) {
58 try {
59 invoker.prepare();
60 return invoker.invoke();
61 } catch (ClassNotFoundException e) {
62 throw new IllegalStateException(e);
63 } catch (NoSuchMethodException e) {
64 throw new IllegalStateException(e);
65 } catch (InvocationTargetException e) {
66 throw new IllegalStateException(e);
67 } catch (IllegalAccessException e) {
68 throw new IllegalStateException(e);
69 }
70 }
71
72 public static Class<?> getClass(String className) {
73 try {
74 return Class.forName(className);
75 } catch (ClassNotFoundException e) {
76 throw new IllegalArgumentException(e);
77 }
78 }
79
80 public static <T> T newInstance(String className) {
81 @SuppressWarnings("unchecked")
82 Class<T> clazz = (Class<T>) getClass(className);
83 return (T) newInstance(clazz);
84 }
85
86 public static <T> T newInstance(Class<T> instanceClass) {
87 try {
88 return (T) instanceClass.newInstance();
89 } catch (IllegalAccessException e) {
90 throw new IllegalArgumentException("Unexpected error", e);
91 } catch (InstantiationException e) {
92 throw new IllegalArgumentException("Unexpected error", e);
93 }
94 }
95
96 }