Skip to content

Commit e1df254

Browse files
committed
Fixed ReflectionIsTemporaryException.
It was hiding real exceptions (such as SecurityException). Test: manual verification Fixes: 186459587 Change-Id: I8098df792542b466dfa937616dbd68bbeab993b1
1 parent c2b57bf commit e1df254

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

app/src/main/java/com/afwsamples/testdpc/common/ReflectionUtil.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package com.afwsamples.testdpc.common;
22

3+
import android.util.Log;
4+
35
import java.lang.reflect.InvocationTargetException;
6+
import java.util.Arrays;
47

58
/**
69
* Common utility functions for reflection. These are intended to be used to test APIs before they
710
* are added to the SDK. There should be not uses of this class checked in to the repository.
811
*/
912
public final class ReflectionUtil {
13+
14+
private static final String TAG = ReflectionUtil.class.getSimpleName();
15+
1016
/**
1117
* Calls a method on an object with the given arguments. This can be used when the method is not
1218
* 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,
8288
return result;
8389
} catch (SecurityException | NoSuchMethodException | IllegalArgumentException
8490
| IllegalAccessException | InvocationTargetException e) {
85-
throw new ReflectionIsTemporaryException("Failed to invoke method", e);
91+
ReflectionIsTemporaryException.rethrow(e, clazz, methodName, args);
92+
return null;
8693
}
8794
}
8895

@@ -133,10 +140,27 @@ public static String stringConstant(Class<?> clazz, String fieldName)
133140
*
134141
* To handle this, gracefully fail the operation in progress.
135142
*/
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) {
138148
super(message, cause);
139149
}
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+
}
140164
}
141165

142166
private ReflectionUtil() {

0 commit comments

Comments
 (0)