|
35 | 35 | import com.google.firebase.messaging.FcmBroadcastProcessor;
|
36 | 36 | import com.google.firebase.messaging.ServiceStarter;
|
37 | 37 | import java.lang.reflect.Field;
|
| 38 | +import java.lang.reflect.InvocationTargetException; |
| 39 | +import java.lang.reflect.Method; |
38 | 40 | import java.lang.reflect.Modifier;
|
39 | 41 | import java.util.concurrent.CountDownLatch;
|
40 | 42 | import java.util.concurrent.TimeUnit;
|
@@ -87,13 +89,38 @@ public void resetStaticState() {
|
87 | 89 | private void setFinalStatic(Field field, Object newValue) throws Exception {
|
88 | 90 | field.setAccessible(true);
|
89 | 91 |
|
90 |
| - Field modifiersField = Field.class.getDeclaredField("modifiers"); |
| 92 | + Field modifiersField = getDeclaredField(Field.class, "modifiers"); |
91 | 93 | modifiersField.setAccessible(true);
|
92 | 94 | modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
|
93 | 95 |
|
94 | 96 | field.set(null, newValue);
|
95 | 97 | }
|
96 | 98 |
|
| 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 | + |
97 | 124 | @Test
|
98 | 125 | public void testNullIntent() throws Exception {
|
99 | 126 | receiver.onReceive(context, null);
|
|
0 commit comments