|
1 | 1 | package com.afwsamples.testdpc.common;
|
2 | 2 |
|
| 3 | +import android.util.Log; |
| 4 | + |
3 | 5 | import java.lang.reflect.InvocationTargetException;
|
| 6 | +import java.util.Arrays; |
4 | 7 |
|
5 | 8 | /**
|
6 | 9 | * Common utility functions for reflection. These are intended to be used to test APIs before they
|
7 | 10 | * are added to the SDK. There should be not uses of this class checked in to the repository.
|
8 | 11 | */
|
9 | 12 | public final class ReflectionUtil {
|
| 13 | + |
| 14 | + private static final String TAG = ReflectionUtil.class.getSimpleName(); |
| 15 | + |
10 | 16 | /**
|
11 | 17 | * Calls a method on an object with the given arguments. This can be used when the method is not
|
12 | 18 | * in the SDK. If any arguments are {@code null} or primitive types you must use
|
@@ -82,7 +88,8 @@ private static <T> T invoke(Class<?> clazz, Object obj, String methodName,
|
82 | 88 | return result;
|
83 | 89 | } catch (SecurityException | NoSuchMethodException | IllegalArgumentException
|
84 | 90 | | IllegalAccessException | InvocationTargetException e) {
|
85 |
| - throw new ReflectionIsTemporaryException("Failed to invoke method", e); |
| 91 | + ReflectionIsTemporaryException.rethrow(e, clazz, methodName, args); |
| 92 | + return null; |
86 | 93 | }
|
87 | 94 | }
|
88 | 95 |
|
@@ -133,10 +140,27 @@ public static String stringConstant(Class<?> clazz, String fieldName)
|
133 | 140 | *
|
134 | 141 | * To handle this, gracefully fail the operation in progress.
|
135 | 142 | */
|
136 |
| - public static class ReflectionIsTemporaryException extends Exception { |
137 |
| - public ReflectionIsTemporaryException(String message, Throwable cause) { |
| 143 | + public static final class ReflectionIsTemporaryException extends Exception { |
| 144 | + |
| 145 | + private static final long serialVersionUID = 1L; |
| 146 | + |
| 147 | + private ReflectionIsTemporaryException(String message, Throwable cause) { |
138 | 148 | super(message, cause);
|
139 | 149 | }
|
| 150 | + |
| 151 | + public static void rethrow(Exception e, Class<?> clazz, String methodName, Object... args) |
| 152 | + throws ReflectionIsTemporaryException { |
| 153 | + String method = clazz.getSimpleName() + "." + methodName + "(" |
| 154 | + + (args == null || args.length == 0 ? "" : Arrays.toString(args)) + ")"; |
| 155 | + Log.w(TAG, "Exception calling method " + method + ":", e); |
| 156 | + if (e instanceof InvocationTargetException) { |
| 157 | + Throwable cause = e.getCause(); |
| 158 | + if (cause instanceof RuntimeException) { |
| 159 | + throw (RuntimeException) cause; |
| 160 | + } |
| 161 | + } |
| 162 | + throw new ReflectionIsTemporaryException("Failed to invoke " + method, e); |
| 163 | + } |
140 | 164 | }
|
141 | 165 |
|
142 | 166 | private ReflectionUtil() {
|
|
0 commit comments