|
6 | 6 | import java.lang.instrument.Instrumentation; |
7 | 7 | import java.lang.reflect.Field; |
8 | 8 | import java.lang.reflect.InvocationTargetException; |
9 | | -import java.lang.reflect.Method; |
10 | 9 | import java.util.Arrays; |
11 | 10 | import java.util.ServiceLoader; |
12 | 11 | import java.util.concurrent.Callable; |
13 | 12 | import java.util.concurrent.ConcurrentMap; |
14 | 13 |
|
| 14 | +import org.apache.commons.lang3.reflect.MethodUtils; |
15 | 15 | import org.junit.runner.Description; |
16 | 16 | import org.junit.runner.notification.RunListener; |
17 | 17 | import org.junit.runners.model.TestClass; |
@@ -258,27 +258,11 @@ static String getSubclassName(Object testObj) { |
258 | 258 | */ |
259 | 259 | @SuppressWarnings("unchecked") |
260 | 260 | static <T> T invoke(Object target, String methodName, Object... parameters) { |
261 | | - Class<?>[] parameterTypes = new Class<?>[parameters.length]; |
262 | | - for (int i = 0; i < parameters.length; i++) { |
263 | | - parameterTypes[i] = parameters[i].getClass(); |
264 | | - } |
265 | | - |
266 | | - Throwable thrown = null; |
267 | | - for (Class<?> current = target.getClass(); current != null; current = current.getSuperclass()) { |
268 | | - try { |
269 | | - Method method = current.getDeclaredMethod(methodName, parameterTypes); |
270 | | - method.setAccessible(true); |
271 | | - return (T) method.invoke(target, parameters); |
272 | | - } catch (NoSuchMethodException e) { |
273 | | - thrown = e; |
274 | | - } catch (SecurityException | IllegalAccessException | IllegalArgumentException |
275 | | - | InvocationTargetException e) { |
276 | | - thrown = e; |
277 | | - break; |
278 | | - } |
| 261 | + try { |
| 262 | + return (T) MethodUtils.invokeMethod(target, true, methodName, parameters); |
| 263 | + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
| 264 | + throw UncheckedThrow.throwUnchecked(e); |
279 | 265 | } |
280 | | - |
281 | | - throw UncheckedThrow.throwUnchecked(thrown); |
282 | 266 | } |
283 | 267 |
|
284 | 268 | /** |
|
0 commit comments