Skip to content

Commit f6eebe4

Browse files
authored
Work around an upcoming JDK change. (#4017)
1 parent 3f13291 commit f6eebe4

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

firebase-messaging/src/test/java/com/google/firebase/iid/FirebaseInstanceIdWithFcmReceiverRoboTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import com.google.firebase.messaging.FcmBroadcastProcessor;
3636
import com.google.firebase.messaging.ServiceStarter;
3737
import java.lang.reflect.Field;
38+
import java.lang.reflect.InvocationTargetException;
39+
import java.lang.reflect.Method;
3840
import java.lang.reflect.Modifier;
3941
import java.util.concurrent.CountDownLatch;
4042
import java.util.concurrent.TimeUnit;
@@ -87,13 +89,38 @@ public void resetStaticState() {
8789
private void setFinalStatic(Field field, Object newValue) throws Exception {
8890
field.setAccessible(true);
8991

90-
Field modifiersField = Field.class.getDeclaredField("modifiers");
92+
Field modifiersField = getDeclaredField(Field.class, "modifiers");
9193
modifiersField.setAccessible(true);
9294
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
9395

9496
field.set(null, newValue);
9597
}
9698

99+
private static Field getDeclaredField(Class<?> clazz, String name) throws NoSuchFieldException {
100+
try {
101+
return clazz.getDeclaredField(name);
102+
} catch (NoSuchFieldException e1) {
103+
try {
104+
Method getDeclaredFields0 =
105+
Class.class.getDeclaredMethod("getDeclaredFields0", boolean.class);
106+
getDeclaredFields0.setAccessible(true);
107+
Field[] fields = (Field[]) getDeclaredFields0.invoke(clazz, false);
108+
for (Field f : fields) {
109+
if (f.getName().equals(name)) {
110+
return f;
111+
}
112+
}
113+
throw new NoSuchFieldException(name);
114+
} catch (NoSuchMethodException e2) {
115+
throw new NoSuchFieldException(name);
116+
} catch (IllegalAccessException e2) {
117+
throw new NoSuchFieldException(name);
118+
} catch (InvocationTargetException e2) {
119+
throw new NoSuchFieldException(name);
120+
}
121+
}
122+
}
123+
97124
@Test
98125
public void testNullIntent() throws Exception {
99126
receiver.onReceive(context, null);

0 commit comments

Comments
 (0)