-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ReflectionHelper.java
166 lines (148 loc) · 5.88 KB
/
ReflectionHelper.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.denizenscript.denizencore.utilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
public class ReflectionHelper {
private static final Map<Class, Map<String, Field>> cachedFields = new HashMap<>();
private static final Map<Class, Map<String, MethodHandle>> cachedFieldSetters = new HashMap<>();
public static void setFieldValue(Class clazz, String fieldName, Object object, Object value) {
try {
getFields(clazz).get(fieldName).set(object, value);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
public static <T> T getFieldValue(Class clazz, String fieldName, Object object) {
Map<String, Field> cache = getFields(clazz);
try {
Field field = cache.get(fieldName);
if (field == null) {
return null;
}
cache.put(fieldName, field);
return (T) field.get(object);
}
catch (Exception ex) {
Debug.echoError(ex);
return null;
}
}
public static class CheckingFieldMap extends HashMap<String, Field> {
public Class<?> clazz;
public CheckingFieldMap(Class<?> clazz) {
this.clazz = clazz;
}
@Override
public Field get(Object name) {
Field f = super.get(name);
if (f == null) {
String err = "Reflection field missing - Tried to read field '" + name + "' of class '" + clazz.getCanonicalName() + "'.";
System.err.println("[Denizen] [ReflectionHelper]: " + err);
Debug.echoError(err);
}
return f;
}
public Field getNoCheck(String name) {
return super.get(name);
}
}
public static Map<String, Field> getFields(Class clazz) {
Map<String, Field> fields = cachedFields.get(clazz);
if (fields != null) {
return fields;
}
fields = new CheckingFieldMap(clazz);
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
fields.put(field.getName(), field);
}
cachedFields.put(clazz, fields);
return fields;
}
public static Method getMethod(Class<?> clazz, String method, Class<?>... params) {
Method f = null;
try {
f = clazz.getDeclaredMethod(method, params);
f.setAccessible(true);
}
catch (Exception ex) {
Debug.echoError(ex);
}
if (f == null) {
String err = "Reflection method missing - Tried to read method '" + method + "' of class '" + clazz.getCanonicalName() + "'.";
System.err.println("[Denizen] [ReflectionHelper]: " + err);
Debug.echoError(err);
}
return f;
}
public static MethodHandle getMethodHandle(Class<?> clazz, String method, Class<?>... params) {
try {
return LOOKUP.unreflect(getMethod(clazz, method, params));
}
catch (Exception ex) {
Debug.echoError(ex);
}
return null;
}
public static MethodHandle getFinalSetter(Class<?> clazz, String field) {
Map<String, MethodHandle> map = cachedFieldSetters.computeIfAbsent(clazz, k -> new HashMap<>());
MethodHandle result = map.get(field);
if (result != null) {
return result;
}
Field f = getFields(clazz).get(field);
if (f == null) {
return null;
}
int mod = f.getModifiers();
try {
if (MODIFIERS_FIELD == null) {
validateUnsafe();
boolean isStatic = Modifier.isStatic(mod);
long offset = (long) (isStatic ? UNSAFE_STATIC_FIELD_OFFSET.invoke(f) : UNSAFE_FIELD_OFFSET.invoke(f));
result = isStatic ? MethodHandles.insertArguments(UNSAFE_PUT_OBJECT, 0, clazz, offset)
: MethodHandles.insertArguments(UNSAFE_PUT_OBJECT, 1, offset);
}
else {
if (Modifier.isFinal(mod)) {
MODIFIERS_FIELD.setInt(f, mod & ~Modifier.FINAL);
}
result = LOOKUP.unreflectSetter(f);
}
}
catch (Throwable ex) {
Debug.echoError(ex);
return null;
}
if (result == null) {
return null;
}
cachedFieldSetters.get(clazz).put(field, result);
return result;
}
private static void validateUnsafe() {
if (UNSAFE == null) {
try {
UNSAFE = getFields(Class.forName("sun.misc.Unsafe")).get("theUnsafe");
}
catch (Exception ex) {
Debug.echoError(ex);
}
UNSAFE_STATIC_FIELD_OFFSET = getMethodHandle(UNSAFE.getClass(), "staticFieldOffset", Field.class).bindTo(UNSAFE);
UNSAFE_FIELD_OFFSET = getMethodHandle(UNSAFE.getClass(), "objectFieldOffset", Field.class).bindTo(UNSAFE);
UNSAFE_PUT_OBJECT = getMethodHandle(UNSAFE.getClass(), "putObject", Object.class, long.class, Object.class).bindTo(UNSAFE);
}
}
private static MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static Field MODIFIERS_FIELD = ((CheckingFieldMap) getFields(Field.class)).getNoCheck("modifiers");
private static Object UNSAFE;
private static MethodHandle UNSAFE_FIELD_OFFSET;
private static MethodHandle UNSAFE_PUT_OBJECT;
private static MethodHandle UNSAFE_STATIC_FIELD_OFFSET;
}