OSDN Git Service

srcディレクトリとdocディレクトリを作成
[xdf/git-repos.git] / src / xdf / src / main / java / jp / ac / aiit / xdf / utils / Reflections.java
1 package jp.ac.aiit.xdf.utils;
2
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6
7 import jp.ac.aiit.xdf.core.exceptions.UnexpectedBehaviorException;
8
9 /**
10  * @author Shunichi Takagi
11  */
12 public class Reflections {
13         
14         public static Object getInstance(String className) {
15                 try {
16                         return Reflections.getInstance(Class.forName(className));
17                 } catch (ClassNotFoundException e) {
18                         throw new UnexpectedBehaviorException("specified class is not exists : " + className);
19                 }
20         }
21         
22         public static <T> T getInstance(Class<T> clazz) {
23                 try {
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);
29                 }
30         }
31         
32         public static Object getInstance(String className, Object ...params) {
33                 try {
34                         return Reflections.getInstance(Class.forName(className), params);
35                 } catch (ClassNotFoundException e) {
36                         throw new UnexpectedBehaviorException("specified class is not exists : " + className);
37                 }
38         }
39         
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();
44                 }
45                 
46                 try {
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);
59                 }
60         }
61         
62         public static Object invokeMethod(Object obj, Method method, Object ...args) {
63                 try {
64                         Object result = null;
65                         
66                         if( method.isAccessible() ) {
67                                 result = method.invoke(obj, args);
68                         } else {
69                                 method.setAccessible(true);
70                                 result = method.invoke(obj, args);
71                                 method.setAccessible(false);
72                         }
73
74                         return result;
75                         
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);
82                 }
83         }
84         
85         public static Object invokeMethod(Object obj, String methodName, Object ...args) throws NoSuchMethodException, IllegalArgumentException {
86                 Class<?> clazz = obj.getClass();
87
88                 Class<?>[] types = new Class[args.length];
89                 for(int i=0; i<args.length; i++) {
90                         types[i] = args[i].getClass();
91                 }
92                 Method method = clazz.getMethod(methodName, types);
93                 
94                 return Reflections.invokeMethod(obj, method, args);
95                 
96         }
97
98         public static Object invokeMethod(Object obj, String methodName,  Class<?> types, Object ...args) throws NoSuchMethodException, IllegalArgumentException {
99                 Class<?> clazz = obj.getClass();
100
101                 Method method = clazz.getMethod(methodName, types);
102                 
103                 return Reflections.invokeMethod(obj, method, args);
104                 
105         }
106 }