Skip to content

Commit c2b57bf

Browse files
committed
Improved ReflectionUtil by automatically casting returned objects.
Test: adb shell dumpsys activity service --user 10 com.afwsamples.testdpc is-foreground-user Bug: 186451999 Change-Id: I213413b8363d5bbfba0ce1844d2e56b4bda535cd
1 parent 00d3ae0 commit c2b57bf

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

app/src/main/java/com/afwsamples/testdpc/DevicePolicyManagerGatewayImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public boolean isHeadlessSystemUserMode() {
120120
// TODO(b/179160578): use proper method when available on SDK
121121
String method = "isHeadlessSystemUserMode";
122122
try {
123-
return (Boolean) ReflectionUtil.invoke(mUserManager, method);
123+
return ReflectionUtil.invoke(mUserManager, method);
124124
} catch (ReflectionIsTemporaryException e) {
125125
Log.wtf(TAG, "Error calling mUserManager." + method + "()", e);
126126
return false;
@@ -134,7 +134,7 @@ public boolean isUserForeground() {
134134
// TODO(b/179160578): use proper method when available on SDK
135135
String method = "isUserForeground";
136136
try {
137-
return (Boolean) ReflectionUtil.invoke(mUserManager, method);
137+
return ReflectionUtil.invoke(mUserManager, method);
138138
} catch (ReflectionIsTemporaryException e) {
139139
Log.wtf(TAG, "Error calling mUserManager." + method + "()", e);
140140
return false;
@@ -148,7 +148,7 @@ public List<UserHandle> listForegroundAffiliatedUsers() {
148148
// TODO(b/179160578): use proper method when available on SDK
149149
String method = "listForegroundAffiliatedUsers";
150150
try {
151-
return (List<UserHandle>) ReflectionUtil.invoke(mDevicePolicyManager, method);
151+
return ReflectionUtil.invoke(mDevicePolicyManager, method);
152152
} catch (ReflectionIsTemporaryException e) {
153153
Log.wtf(TAG, "Error calling mDevicePolicyManager." + method + "()", e);
154154
return Collections.emptyList();

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Common utility functions for reflection. These are intended to be used to test APIs before they
77
* are added to the SDK. There should be not uses of this class checked in to the repository.
88
*/
9-
public class ReflectionUtil {
9+
public final class ReflectionUtil {
1010
/**
1111
* Calls a method on an object with the given arguments. This can be used when the method is not
1212
* in the SDK. If any arguments are {@code null} or primitive types you must use
@@ -22,15 +22,15 @@ public class ReflectionUtil {
2222
* {@link #invoke(Object, String, Class[], Object[])}.
2323
* @return The result of the invocation. {@code null} if {@code void}.
2424
*/
25-
public static Object invoke(Object obj, String methodName, Object... args)
25+
public static <T> T invoke(Object obj, String methodName, Object... args)
2626
throws ReflectionIsTemporaryException {
2727
return invoke(obj.getClass(), obj, methodName, args);
2828
}
2929

3030
/**
3131
* Same as {@link #invoke(Object, String, Object...)} but for static methods.
3232
*/
33-
public static Object invoke(Class<?> clazz, String methodName, Object... args)
33+
public static <T> T invoke(Class<?> clazz, String methodName, Object... args)
3434
throws ReflectionIsTemporaryException {
3535
return invoke(clazz, null, methodName, args);
3636
}
@@ -49,21 +49,21 @@ public static Object invoke(Class<?> clazz, String methodName, Object... args)
4949
* @param args The arguments to pass to the method.
5050
* @return The result of the invocation. {@code null} if {@code void}.
5151
*/
52-
public static Object invoke(Object obj, String methodName, Class<?>[] parameterTypes,
52+
public static <T> T invoke(Object obj, String methodName, Class<?>[] parameterTypes,
5353
Object... args) throws ReflectionIsTemporaryException {
5454
return invoke(obj.getClass(), obj, methodName, parameterTypes, args);
5555
}
5656

5757
/**
5858
* Same as {@link #invoke(Object, String, Class[], Object...)} but for static methods.
5959
*/
60-
public static Object invoke(Class<?> clazz, String methodName, Class<?>[] parameterTypes,
60+
public static <T> T invoke(Class<?> clazz, String methodName, Class<?>[] parameterTypes,
6161
Object... args) throws ReflectionIsTemporaryException {
6262
return invoke(clazz, null, methodName, parameterTypes, args);
6363
}
6464

6565
/** Resolve the parameter types and invoke the method. */
66-
private static Object invoke(Class<?> clazz, Object obj, String methodName, Object... args)
66+
private static <T> T invoke(Class<?> clazz, Object obj, String methodName, Object... args)
6767
throws ReflectionIsTemporaryException {
6868
Class<?> parameterTypes[] = new Class<?>[args.length];
6969
for (int i = 0; i < args.length; ++i) {
@@ -73,11 +73,13 @@ private static Object invoke(Class<?> clazz, Object obj, String methodName, Obje
7373
}
7474

7575
/** Resolve the method and invoke it. */
76-
private static Object invoke(Class<?> clazz, Object obj, String methodName,
76+
private static <T> T invoke(Class<?> clazz, Object obj, String methodName,
7777
Class<?>[] parameterTypes, Object... args)
7878
throws ReflectionIsTemporaryException {
7979
try {
80-
return clazz.getMethod(methodName, parameterTypes).invoke(obj, args);
80+
@SuppressWarnings("unchecked")
81+
T result = (T) clazz.getMethod(methodName, parameterTypes).invoke(obj, args);
82+
return result;
8183
} catch (SecurityException | NoSuchMethodException | IllegalArgumentException
8284
| IllegalAccessException | InvocationTargetException e) {
8385
throw new ReflectionIsTemporaryException("Failed to invoke method", e);
@@ -136,4 +138,8 @@ public ReflectionIsTemporaryException(String message, Throwable cause) {
136138
super(message, cause);
137139
}
138140
}
141+
142+
private ReflectionUtil() {
143+
throw new UnsupportedOperationException("provides only static methods");
144+
}
139145
}

app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,7 +2517,7 @@ private void loadEnrollmentSpecificId() {
25172517
String esid = "";
25182518
try {
25192519
//TODO: Call directly when the S SDK is available.
2520-
esid = (String) ReflectionUtil.invoke(mDevicePolicyManager, "getEnrollmentSpecificId");
2520+
esid = ReflectionUtil.invoke(mDevicePolicyManager, "getEnrollmentSpecificId");
25212521
} catch (ReflectionIsTemporaryException e) {
25222522
Log.e(TAG, "Error invoking getEnterpriseSpecificId", e);
25232523
esid = "Error";
@@ -2562,9 +2562,7 @@ private void loadPasswordComplexity() {
25622562
@TargetApi(VERSION_CODES.S)
25632563
private int getRequiredComplexity(DevicePolicyManager dpm) {
25642564
try {
2565-
Integer complexity =
2566-
(Integer) ReflectionUtil.invoke(dpm, "getRequiredPasswordComplexity");
2567-
return complexity;
2565+
return ReflectionUtil.invoke(dpm, "getRequiredPasswordComplexity");
25682566
} catch (ReflectionIsTemporaryException e) {
25692567
Log.e(TAG, "Error invoking getRequiredPasswordComplexity", e);
25702568
}
@@ -2779,8 +2777,8 @@ private void reloadCommonCriteriaModeUi() {
27792777
@TargetApi(VERSION_CODES.S)
27802778
private void reloadEnableUsbDataSignalingUi() {
27812779
try {
2782-
boolean enabled = (boolean) ReflectionUtil
2783-
.invoke(mDevicePolicyManager, "isUsbDataSignalingEnabled");
2780+
boolean enabled = ReflectionUtil.invoke(mDevicePolicyManager,
2781+
"isUsbDataSignalingEnabled");
27842782
mEnableUsbDataSignalingPreference.setChecked(enabled);
27852783
} catch (ReflectionIsTemporaryException e) {
27862784
Log.e(TAG, "Error invoking isUsbDataSignalingEnabled", e);
@@ -4299,7 +4297,7 @@ private void showSetOrganizationIdDialog() {
42994297

43004298
private void setOrganizationId(String organizationId) {
43014299
try {
4302-
//TODO: Call directly when the S SDK is available.
4300+
//TODO(b/179160578): Call directly when the S SDK is available.
43034301
ReflectionUtil.invoke(mDevicePolicyManager, "setOrganizationId", organizationId);
43044302
} catch (ReflectionIsTemporaryException e) {
43054303
Log.e(TAG, "Error invoking setOrganizationId", e);

0 commit comments

Comments
 (0)