View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  	@SuppressWarnings("unchecked")
81  	public static <T> Class<? extends T> getTypedClass(String className) {
82  		try {
83  			return (Class<? extends T>) Class.forName(className);
84  		} catch (ClassNotFoundException e) {
85  			throw new IllegalArgumentException(e);
86  		}
87  	}
88  
89  	public static <T> T newInstance(String className) {
90  		@SuppressWarnings("unchecked")
91  		Class<T> clazz = (Class<T>) getClass(className);
92  		return (T) newInstance(clazz);
93  	}
94  
95  	public static <T> T newInstance(Class<T> instanceClass) {
96  		try {
97  			return (T) instanceClass.newInstance();
98  		} catch (IllegalAccessException e) {
99  			throw new IllegalArgumentException("Unexpected error", e);
100 		} catch (InstantiationException e) {
101 			throw new IllegalArgumentException("Unexpected error", e);
102 		}
103 	}
104 
105 }