-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathReflectionUtils.java
More file actions
45 lines (40 loc) · 979 Bytes
/
ReflectionUtils.java
File metadata and controls
45 lines (40 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.reactnativenavigation.utils;
import androidx.annotation.Nullable;
import java.lang.reflect.Field;
public class ReflectionUtils {
public static void setField(Object obj, String name, Object value) {
try {
Field field = getField(obj.getClass(), name);
if (field == null) {
return;
}
field.setAccessible(true);
field.set(obj, value);
} catch (Exception e) {
e.printStackTrace();
}
}
@Nullable
public static Object getDeclaredField(Object obj, String fieldName) {
try {
Field f = getField(obj.getClass(), fieldName);
if (f == null) {
return null;
}
f.setAccessible(true);
return f.get(obj);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static Field getField(Class<?> clazz, String name) {
try {
return clazz.getDeclaredField(name);
} catch (NoSuchFieldException nsfe) {
return getField(clazz.getSuperclass(), name);
} catch (Exception e) {
return null;
}
}
}