1 package jp.ac.aiit.xdf.utils;
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
7 import jp.ac.aiit.xdf.core.exceptions.UnexpectedBehaviorException;
10 * @author Shunichi Takagi
12 public class Reflections {
14 public static Object getInstance(String className) {
16 return Reflections.getInstance(Class.forName(className));
17 } catch (ClassNotFoundException e) {
18 throw new UnexpectedBehaviorException("specified class is not exists : " + className);
22 public static <T> T getInstance(Class<T> clazz) {
24 return clazz.newInstance();
25 } catch (InstantiationException e) {
26 throw new UnexpectedBehaviorException("class instantiation failed : "+clazz.getCanonicalName(), e);
27 } catch (IllegalAccessException e) {
28 throw new UnexpectedBehaviorException("access illegal constructor : "+clazz.getCanonicalName()+"()", e);
32 public static Object getInstance(String className, Object ...params) {
34 return Reflections.getInstance(Class.forName(className), params);
35 } catch (ClassNotFoundException e) {
36 throw new UnexpectedBehaviorException("specified class is not exists : " + className);
40 public static <T> T getInstance(Class<T> clazz, Object ...params) {
41 Class<?>[] parameterTypes = new Class[params.length];
42 for(int i=0; i<params.length; i++) {
43 parameterTypes[i] = params[i].getClass();
47 Constructor<T> constructor = clazz.getConstructor(parameterTypes);
48 return constructor.newInstance(params);
49 } catch (SecurityException e) {
50 throw new UnexpectedBehaviorException("security exception", e);
51 } catch (NoSuchMethodException e) {
52 throw new UnexpectedBehaviorException("no such method exception", e);
53 } catch (InstantiationException e) {
54 throw new UnexpectedBehaviorException("class instantiation failed : "+clazz.getCanonicalName(), e);
55 } catch (IllegalAccessException e) {
56 throw new UnexpectedBehaviorException("access illegal constructor : "+clazz.getCanonicalName(), e);
57 } catch (InvocationTargetException e) {
58 throw new UnexpectedBehaviorException("invocation target exception", e);
62 public static Object invokeMethod(Object obj, Method method, Object ...args) {
66 if( method.isAccessible() ) {
67 result = method.invoke(obj, args);
69 method.setAccessible(true);
70 result = method.invoke(obj, args);
71 method.setAccessible(false);
76 } catch(SecurityException e) {
77 throw new UnexpectedBehaviorException(e);
78 } catch(IllegalAccessException e) {
79 throw new UnexpectedBehaviorException(e);
80 } catch(InvocationTargetException e) {
81 throw new UnexpectedBehaviorException(e);
85 public static Object invokeMethod(Object obj, String methodName, Object ...args) throws NoSuchMethodException, IllegalArgumentException {
86 Class<?> clazz = obj.getClass();
88 Class<?>[] types = new Class[args.length];
89 for(int i=0; i<args.length; i++) {
90 types[i] = args[i].getClass();
92 Method method = clazz.getMethod(methodName, types);
94 return Reflections.invokeMethod(obj, method, args);
98 public static Object invokeMethod(Object obj, String methodName, Class<?> types, Object ...args) throws NoSuchMethodException, IllegalArgumentException {
99 Class<?> clazz = obj.getClass();
101 Method method = clazz.getMethod(methodName, types);
103 return Reflections.invokeMethod(obj, method, args);