1 package org.sf.jlaunchpad.util;
2
3 import java.lang.reflect.Method;
4 import java.lang.reflect.Modifier;
5 import java.lang.reflect.Field;
6 import java.lang.reflect.InvocationTargetException;
7
8
9
10
11
12
13
14 public class ReflectionUtil {
15
16
17
18
19
20
21
22 public static Method getMainMethod(final Class clazz) {
23 return getMainMethod(clazz, new Class[]{java.lang.String[].class});
24 }
25
26
27
28
29
30
31
32
33 public static Method getMainMethodWithTwoParameters(final Class clazz, final Class secondParameterClass) {
34 return getMainMethod(clazz, new Class[]{java.lang.String[].class, secondParameterClass});
35 }
36
37
38
39
40
41
42
43
44 protected static Method getMainMethod(final Class clazz, final Class[] paramTypes) {
45 Method mainMethod = null;
46
47 final Method[] methods = clazz.getMethods();
48
49 for (int i = 0; i < methods.length && mainMethod == null; i++) {
50 final Method method = methods[i];
51
52 if(isCorrectMethod(methods[i], "main", paramTypes)) {
53 mainMethod = method;
54 }
55 }
56
57 return mainMethod;
58 }
59
60
61
62
63
64
65
66
67
68 private static boolean isCorrectMethod(Method method, String methodName, final Class[] paramTypes) {
69 boolean isCorrect = true;
70
71 if (method.getName().equals(methodName)) {
72 final int modifiers = method.getModifiers();
73
74 final boolean correctSignature =
75 Modifier.isStatic(modifiers) &&
76 Modifier.isPublic(modifiers) &&
77 (method.getReturnType() == Void.TYPE || method.getReturnType() == Object.class);
78 if (correctSignature) {
79 final Class[] currentParamTypes = method.getParameterTypes();
80
81 if (currentParamTypes.length == paramTypes.length) {
82 for (int j = 0; j < currentParamTypes.length && isCorrect; j ++) {
83 if (currentParamTypes[j] != paramTypes[j]) {
84 isCorrect = false;
85 }
86 }
87 }
88 else {
89 isCorrect = false;
90 }
91 }
92 }
93 else {
94 isCorrect = false;
95 }
96
97 return isCorrect;
98 }
99
100
101
102
103
104
105
106
107
108
109
110 public static Object launchClass(Class mainClass, String[] args, String illegalArgumentMessage)
111 throws IllegalAccessException, InvocationTargetException {
112 final Method mainMethod = getMainMethod(mainClass);
113
114 return launchClass(mainMethod, new Object[] { args }, illegalArgumentMessage);
115 }
116
117
118
119
120
121
122
123
124
125
126
127 public static Object launchClass(Method mainMethod, Object[] args, String illegalArgumentMessage)
128 throws IllegalAccessException, InvocationTargetException {
129 if (mainMethod == null) {
130 throw new IllegalArgumentException(illegalArgumentMessage);
131 }
132
133
134 return mainMethod.invoke(null, args);
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 public static Object getPrivateField(Object source, Class clazz, String name)
151 throws NoSuchFieldException, IllegalAccessException {
152 Field field = clazz.getDeclaredField(name);
153
154 field.setAccessible(true);
155
156 Object fieldObject = field.get(source);
157
158 field.setAccessible(false);
159
160 return fieldObject;
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176 public static void setPrivateField(Object source, Class clazz, String name, Object value)
177 throws NoSuchFieldException, IllegalAccessException {
178 Field field = clazz.getDeclaredField(name);
179
180 field.setAccessible(true);
181
182 field.set(source, value);
183
184 field.setAccessible(false);
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 public static Object invokePrivateMethod(Object source, Object[] parameters, Class clazz,
204 String name, Class[] parameterTypes)
205 throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
206 Method method = clazz.getDeclaredMethod(name, parameterTypes);
207
208 method.setAccessible(true);
209
210 Object methodResult = method.invoke(source, parameters);
211
212 method.setAccessible(false);
213
214 return methodResult;
215 }
216
217 }