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  
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  }