This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
ProgramReflection.java
367 lines (299 loc) · 9.59 KB
/
ProgramReflection.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package j;
import com.jtransc.ds.FastIntMap;
import com.jtransc.ds.FastStringMap;
import com.jtransc.io.JTranscConsole;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
/**
* MetaReflectionPlugin set those methods
*/
@SuppressWarnings({"unused", "WeakerAccess", "ConstantConditions"})
public class ProgramReflection {
// Static initialization for classes.
// Necessary due to existing getClass() method in code will generate static constructor.
//static {
// _ensure();
//}
static public ClassInfo[] _classInfos;
static public String[] _classNames;
static public FastStringMap<ClassInfo> _classInfosByName;
private static final FastIntMap<FastIntMap<MemberInfo>> _directMethodsInfo = new FastIntMap<FastIntMap<MemberInfo>>();
private static boolean _ensureMethodsDone = false;
@SuppressWarnings("ConstantConditions")
static public void _ensure() {
if (_classInfos != null) return;
_classInfosByName = new FastStringMap<ClassInfo>();
_classInfos = ProgramReflection.getAllClasses();
if(_classInfos != null) {
_classNames = new String[_classInfos.length];
for (ClassInfo info : _classInfos) {
if (info == null) continue;
//if (info.name == null) JTranscConsole.error("ProgramReflection.ensure: info.name==null");
_classInfosByName.set(info.name, info);
_classNames[info.id] = info.name;
}
}
}
static public void _ensureMethods() {
if (_ensureMethodsDone) return;
_ensure();
for (ClassInfo info : _classInfos) {
if (info == null) continue;
FastIntMap<MemberInfo> map = new FastIntMap<MemberInfo>();
_directMethodsInfo.set(info.id, map);
MemberInfo[] minfo = getMethods(info.id);
if (minfo != null) {
for (MemberInfo i : minfo) {
map.set(i.id, i);
}
}
}
_ensureMethodsDone = true;
}
static public boolean hasClassWithName(String name) {
ProgramReflection._ensure();
return ProgramReflection._classInfosByName.has(name);
}
static public ClassInfo getClassInfoWithName(String name) {
ProgramReflection._ensure();
return hasClassWithName(name) ? ProgramReflection._classInfosByName.get(name) : null;
}
// Class
static public ClassInfo[] getAllClasses() {
return _classInfos != null ? _classInfos : AllClasses.getAllClasses();
}
public static class AllClasses {
public ClassInfo[] test;
static public ClassInfo[] getAllClasses() {
ClassInfo[][] classInfoParts = new ClassInfo[6][];
classInfoParts[0] = getAllClasses3000();
classInfoParts[1] = getAllClasses6000();
classInfoParts[2] = getAllClasses9000();
classInfoParts[3] = getAllClasses12000();
classInfoParts[4] = getAllClasses15000();
classInfoParts[5] = getAllClassesMax();
ClassInfo[] classInfo = new ClassInfo[getAllClassesCount()];
for (ClassInfo[] classInfoPart : classInfoParts) {
for (ClassInfo aClassInfo : classInfoPart) {
classInfo[aClassInfo.id] = aClassInfo;
}
}
return classInfo;
}
static public int getAllClassesCount() {
return 0;
}
static public ClassInfo[] getAllClasses3000() {
return new ClassInfo[0];
}
static public ClassInfo[] getAllClasses6000() {
return new ClassInfo[0];
}
static public ClassInfo[] getAllClasses9000() {
return new ClassInfo[0];
}
static public ClassInfo[] getAllClasses12000() {
return new ClassInfo[0];
}
static public ClassInfo[] getAllClasses15000() {
return new ClassInfo[0];
}
static public ClassInfo[] getAllClassesMax() {
return new ClassInfo[0];
}
}
// @NOTE: This will be replaced by MetaReflectionPlugin
static public MemberInfo[] getConstructors(int classId) {
return AllConstructors.getConstructors(classId);
}
public static class AllConstructors {
static public MemberInfo[] getConstructors(int classId) {
MemberInfo[] ret = AllConstructorsFirst.getConstructors(classId);
if (ret != null && ret.length > 0) {
return ret;
}
ret = AllConstructorsMiddle.getConstructors(classId);
if (ret != null && ret.length > 0) {
return ret;
}
return AllConstructorsLast.getConstructors(classId);
}
}
public static class AllConstructorsFirst {
static public MemberInfo[] getConstructors(int classId) {
return new MemberInfo[0];
}
}
public static class AllConstructorsMiddle {
static public MemberInfo[] getConstructors(int classId) {
return new MemberInfo[0];
}
}
public static class AllConstructorsLast {
static public MemberInfo[] getConstructors(int classId) {
return new MemberInfo[0];
}
}
// @NOTE: This will be replaced by MetaReflectionPlugin
static public MemberInfo[] getMethods(int classId) {
return AllMethods.getMethods(classId);
}
public static class AllMethods {
public AllMethods() {}
static public MemberInfo[] getMethods(int classId) {
MemberInfo[] ret = AllMethodsFirst.getMethods(classId);
if (ret != null && ret.length > 0) {
return ret;
}
ret = AllMethodsMiddle.getMethods(classId);
if (ret != null && ret.length > 0) {
return ret;
}
return AllMethodsLast.getMethods(classId);
}
}
public static class AllMethodsFirst {
public AllMethodsFirst() {}
static public MemberInfo[] getMethods(int classId) {
return new MemberInfo[0];
}
}
public static class AllMethodsMiddle {
public AllMethodsMiddle() {}
static public MemberInfo[] getMethods(int classId) {
return new MemberInfo[0];
}
}
public static class AllMethodsLast {
public AllMethodsLast() {}
static public MemberInfo[] getMethods(int classId) {
return new MemberInfo[0];
}
}
// @NOTE: This will be replaced by MetaReflectionPlugin
static public MemberInfo[] getFields(int classId) {
return AllFields.getFields(classId);
}
public static class AllFields {
public AllFields() {}
static public MemberInfo[] getFields(int classId) {
MemberInfo[] ret = AllFieldsFirst.getFields(classId);
if (ret != null && ret.length > 0) {
return ret;
}
ret = AllFieldsMiddle.getFields(classId);
if (ret != null && ret.length > 0) {
return ret;
}
return AllFieldsLast.getFields(classId);
}
}
public static class AllFieldsFirst {
public AllFieldsFirst() {}
static public MemberInfo[] getFields(int classId) {
return new MemberInfo[0];
}
}
public static class AllFieldsMiddle {
public AllFieldsMiddle() {}
static public MemberInfo[] getFields(int classId) {
return new MemberInfo[0];
}
}
public static class AllFieldsLast {
public AllFieldsLast() {}
static public MemberInfo[] getFields(int classId) {
return new MemberInfo[0];
}
}
// Constructor
static public Object dynamicNew(int classId, int constructorId, Object[] params) {
return DynamicNew.dynamicNew(classId, constructorId, params);
}
// Method
static public Object dynamicInvoke(int classId, int methodId, Object object, Object[] params) {
return DynamicInvoke.dynamicInvoke(classId, methodId, object, params);
}
// Field
static public Object dynamicGet(int classId, int fieldId, Object object) {
return DynamicGet.dynamicGet(classId, fieldId, object);
}
static public void dynamicSet(int classId, int fieldId, Object object, Object value) {
DynamicSet.dynamicSet(classId, fieldId, object, value);
}
public static class DynamicNew {
static public Object dynamicNew(int classId, int constructorId, Object[] params) {
return null;
}
}
public static class DynamicInvoke {
static public Object dynamicInvoke(int classId, int methodId, Object object, Object[] params) {
Object ret = DynamicInvokeFirst.dynamicInvoke(classId, methodId, object, params);
if (ret != null) {
return ret;
}
ret = DynamicInvokeMiddle.dynamicInvoke(classId, methodId, object, params);
if (ret != null) {
return ret;
}
return DynamicInvokeLast.dynamicInvoke(classId, methodId, object, params);
}
}
public static class DynamicInvokeFirst {
static public Object dynamicInvoke(int classId, int methodId, Object object, Object[] params) {
return null;
}
}
public static class DynamicInvokeMiddle {
static public Object dynamicInvoke(int classId, int methodId, Object object, Object[] params) {
return null;
}
}
public static class DynamicInvokeLast {
static public Object dynamicInvoke(int classId, int methodId, Object object, Object[] params) {
return null;
}
}
public static class DynamicGet {
static public Object dynamicGet(int classId, int fieldId, Object object) {
return null;
}
}
public static class DynamicSet {
static public void dynamicSet(int classId, int fieldId, Object object, Object value) {
}
}
static public Annotation[] getClassAnnotations(int classId) {
return new Annotation[0];
}
static public Annotation[] getFieldAnnotations(int classId, int fieldId) {
return new Annotation[0];
}
static public Annotation[] getMethodAnnotations(int classId, int methodId) {
return new Annotation[0];
}
static public Annotation[] getMethodArgumentAnnotations(int classId, int methodId, int argIndex) {
return new Annotation[0];
}
static public MemberInfo getMethodInfo(int classId, int methodId) {
_ensureMethods();
return _directMethodsInfo.get(classId).get(methodId);
}
//native static public Class<?> getClassByInfo(ClassInfo info);
native static public Method getMethodByInfo(Class<?> clazz, MemberInfo info);
static public Class<?> getClassById(int classId) {
ProgramReflection._ensure();
try {
return Class.forName(_classNames[classId]);
} catch (ClassNotFoundException e) {
JTranscConsole.syncPrintStackTrace(e);
return null;
}
}
static public Method getDirectMethod(int classId, int methodId) {
return getMethodByInfo(getClassById(classId), getMethodInfo(classId, methodId));
}
//static public long dynamicFieldPtr(int fieldId, Object object) {
// return 0L;
//}
}