-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
BeansAccessBuilder.java
484 lines (449 loc) · 15.5 KB
/
BeansAccessBuilder.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package net.minidev.asm;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ACONST_NULL;
import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.ARETURN;
import static org.objectweb.asm.Opcodes.ASTORE;
import static org.objectweb.asm.Opcodes.ATHROW;
import static org.objectweb.asm.Opcodes.BIPUSH;
import static org.objectweb.asm.Opcodes.CHECKCAST;
import static org.objectweb.asm.Opcodes.DUP;
import static org.objectweb.asm.Opcodes.F_SAME;
import static org.objectweb.asm.Opcodes.GETFIELD;
import static org.objectweb.asm.Opcodes.ICONST_1;
import static org.objectweb.asm.Opcodes.ICONST_2;
import static org.objectweb.asm.Opcodes.ICONST_3;
import static org.objectweb.asm.Opcodes.ICONST_4;
import static org.objectweb.asm.Opcodes.ICONST_5;
import static org.objectweb.asm.Opcodes.IFEQ;
import static org.objectweb.asm.Opcodes.IFNE;
import static org.objectweb.asm.Opcodes.IFNULL;
import static org.objectweb.asm.Opcodes.IF_ICMPNE;
import static org.objectweb.asm.Opcodes.ILOAD;
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.NEW;
import static org.objectweb.asm.Opcodes.PUTFIELD;
import static org.objectweb.asm.Opcodes.RETURN;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
/**
* A builder class for dynamically creating {@link BeansAccess} classes for accessing bean properties efficiently.
* This class utilizes the ASM library to generate bytecode at runtime, thereby bypassing the need for Java reflection.
*/
public class BeansAccessBuilder {
static private String METHOD_ACCESS_NAME = Type.getInternalName(BeansAccess.class);
final Class<?> type;
final Accessor[] accs;
final DynamicClassLoader loader;
final String className;
final String accessClassName;
final String accessClassNameInternal;
final String classNameInternal;
final HashMap<Class<?>, Method> convMtds = new HashMap<Class<?>, Method>();
// Class<? extends Exception> exceptionClass = net.minidev.asm.ex.NoSuchFieldException.class;
Class<? extends Exception> exceptionClass = NoSuchFieldException.class;
/**
* Initializes a new builder instance for a given bean class.
*
* @param type
* type to be access
* @param accs
* used accessor
* @param loader
* Loader used to store the generated class
*/
public BeansAccessBuilder(Class<?> type, Accessor[] accs, DynamicClassLoader loader) {
this.type = type;
this.accs = accs;
this.loader = loader;
this.className = type.getName();
if (className.startsWith("java."))
this.accessClassName = "net.minidev.asm." + className + "AccAccess";
else
this.accessClassName = className.concat("AccAccess");
this.accessClassNameInternal = accessClassName.replace('.', '/');
this.classNameInternal = className.replace('.', '/');
}
public void addConversion(Iterable<Class<?>> conv) {
if (conv == null)
return;
for (Class<?> c : conv)
addConversion(c);
}
public void addConversion(Class<?> conv) {
if (conv == null)
return;
for (Method mtd : conv.getMethods()) {
if ((mtd.getModifiers() & Modifier.STATIC) == 0)
continue;
Class<?>[] param = mtd.getParameterTypes();
if (param.length != 1)
continue;
if (!param[0].equals(Object.class))
continue;
Class<?> rType = mtd.getReturnType();
if (rType.equals(Void.TYPE))
continue;
convMtds.put(rType, mtd);
}
}
public Class<?> bulid() {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
MethodVisitor mv;
boolean USE_HASH = accs.length > 10;
int HASH_LIMIT = 14;
String signature = "Lnet/minidev/asm/BeansAccess<L" + classNameInternal + ";>;";
cw.visit(Opcodes.V1_6, ACC_PUBLIC + Opcodes.ACC_SUPER, accessClassNameInternal, signature, METHOD_ACCESS_NAME, null);
// init
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, METHOD_ACCESS_NAME, "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
////////////
// Build SETter using filed index
//
// public void set(Object object, int fieldId, Object value)
mv = cw.visitMethod(ACC_PUBLIC, "set", "(Ljava/lang/Object;ILjava/lang/Object;)V", null, null);
mv.visitCode();
// if no Field simply return
if (accs.length == 0) {
//
// mv.visitInsn(RETURN);
} else if (accs.length > HASH_LIMIT) {
// lots of field Use Switch Statement
mv.visitVarInsn(ILOAD, 2);
Label[] labels = ASMUtil.newLabels(accs.length);
Label defaultLabel = new Label();
mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);
int i = 0;
for (Accessor acc : accs) {
mv.visitLabel(labels[i++]);
if (!acc.isWritable()) {
mv.visitInsn(RETURN);
continue;
}
internalSetFiled(mv, acc);
}
mv.visitLabel(defaultLabel);
} else {
Label[] labels = ASMUtil.newLabels(accs.length);
int i = 0;
for (Accessor acc : accs) {
ifNotEqJmp(mv, 2, i, labels[i]);
internalSetFiled(mv, acc);
mv.visitLabel(labels[i]);
mv.visitFrame(F_SAME, 0, null, 0, null);
i++;
}
}
if (exceptionClass != null)
throwExIntParam(mv, exceptionClass);
else
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
////////////
// Build GETter using filed index
//
// public Object get(Object object, int fieldId)
mv = cw.visitMethod(ACC_PUBLIC, "get", "(Ljava/lang/Object;I)Ljava/lang/Object;", null, null);
mv.visitCode();
if (accs.length == 0) {
mv.visitFrame(F_SAME, 0, null, 0, null);
} else if (accs.length > HASH_LIMIT) {
mv.visitVarInsn(ILOAD, 2);
Label[] labels = ASMUtil.newLabels(accs.length);
Label defaultLabel = new Label();
mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);
int i = 0;
for (Accessor acc : accs) {
mv.visitLabel(labels[i++]);
mv.visitFrame(F_SAME, 0, null, 0, null);
if (!acc.isReadable()) {
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
continue;
}
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, classNameInternal);
Type fieldType = Type.getType(acc.getType());
if (acc.isPublic() || acc.getter == null) {
mv.visitFieldInsn(GETFIELD, classNameInternal, acc.getName(), fieldType.getDescriptor());
} else {
String sig = Type.getMethodDescriptor(acc.getter);
mv.visitMethodInsn(INVOKEVIRTUAL, classNameInternal, acc.getter.getName(), sig, false);
}
ASMUtil.autoBoxing(mv, fieldType);
mv.visitInsn(ARETURN);
}
mv.visitLabel(defaultLabel);
mv.visitFrame(F_SAME, 0, null, 0, null);
} else {
Label[] labels = ASMUtil.newLabels(accs.length);
int i = 0;
for (Accessor acc : accs) {
ifNotEqJmp(mv, 2, i, labels[i]);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, classNameInternal);
Type fieldType = Type.getType(acc.getType());
if (acc.isPublic() || acc.getter == null) {
mv.visitFieldInsn(GETFIELD, classNameInternal, acc.getName(), fieldType.getDescriptor());
} else {
if (acc.getter == null)
throw new RuntimeException("no Getter for field " + acc.getName() + " in class " + this.className);
String sig = Type.getMethodDescriptor(acc.getter);
mv.visitMethodInsn(INVOKEVIRTUAL, classNameInternal, acc.getter.getName(), sig, false);
}
ASMUtil.autoBoxing(mv, fieldType);
mv.visitInsn(ARETURN);
mv.visitLabel(labels[i]);
mv.visitFrame(F_SAME, 0, null, 0, null);
i++;
}
}
if (exceptionClass != null)
throwExIntParam(mv, exceptionClass);
else {
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
}
mv.visitMaxs(0, 0);
mv.visitEnd();
////////////
// Build SETter using filed Name
//
// public Object set(Object object, String methodName, Object value)
if (!USE_HASH) {
mv = cw.visitMethod(ACC_PUBLIC, "set", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V", null, null);
mv.visitCode();
Label[] labels = ASMUtil.newLabels(accs.length);
int i = 0;
for (Accessor acc : accs) {
mv.visitVarInsn(ALOAD, 2);
mv.visitLdcInsn(acc.fieldName);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
mv.visitJumpInsn(IFEQ, labels[i]);
internalSetFiled(mv, acc);
mv.visitLabel(labels[i]);
mv.visitFrame(F_SAME, 0, null, 0, null);
i++;
}
if (exceptionClass != null)
throwExStrParam(mv, exceptionClass);
else
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0); // 2,4
mv.visitEnd();
}
////////////
// Build GETter using filed Name
//
// public Object get(Object object, String methodName)
if (!USE_HASH) {
mv = cw.visitMethod(ACC_PUBLIC, "get", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;", null, null);
mv.visitCode();
Label[] labels = ASMUtil.newLabels(accs.length);
int i = 0;
for (Accessor acc : accs) {
mv.visitVarInsn(ALOAD, 2); // methodName
mv.visitLdcInsn(acc.fieldName);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
mv.visitJumpInsn(IFEQ, labels[i]);
mv.visitVarInsn(ALOAD, 1); // object
mv.visitTypeInsn(CHECKCAST, classNameInternal);
Type fieldType = Type.getType(acc.getType());
if (acc.isPublic() || acc.getter == null) {
mv.visitFieldInsn(GETFIELD, classNameInternal, acc.getName(), fieldType.getDescriptor());
} else {
String sig = Type.getMethodDescriptor(acc.getter);
mv.visitMethodInsn(INVOKEVIRTUAL, classNameInternal, acc.getter.getName(), sig, false);
}
ASMUtil.autoBoxing(mv, fieldType);
mv.visitInsn(ARETURN);
mv.visitLabel(labels[i]);
mv.visitFrame(F_SAME, 0, null, 0, null);
i++;
}
if (exceptionClass != null)
throwExStrParam(mv, exceptionClass);
else {
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
}
mv.visitMaxs(0, 0);
mv.visitEnd();
}
////////////
// Build constructor
//
{
mv = cw.visitMethod(ACC_PUBLIC, "newInstance", "()Ljava/lang/Object;", null, null);
mv.visitCode();
mv.visitTypeInsn(NEW, classNameInternal);
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, classNameInternal, "<init>", "()V", false);
mv.visitInsn(ARETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
cw.visitEnd();
byte[] data = cw.toByteArray();
// dumpDebug(data, "/tmp/debug-" + accessClassName + ".txt");
return loader.defineClass(accessClassName, data);
}
/**
* Dump Generate Code
*/
@SuppressWarnings("unused")
private void dumpDebug(byte[] data, String destFile) {
// try {
// File debug = new File(destFile);
// int flags = ClassReader.SKIP_DEBUG;
// ClassReader cr = new ClassReader(new ByteArrayInputStream(data));
// cr.accept(new ASMifierClassVisitor(new PrintWriter(debug)),
// ASMifierClassVisitor.getDefaultAttributes(),
// flags);
// } catch (Exception e) {
// }
}
/**
* Dump Set Field Code
*
* @param mv
* @param acc
*/
private void internalSetFiled(MethodVisitor mv, Accessor acc) {
/**
* FNC params
*
* 1 -> object to alter
*
* 2 -> id of field
*
* 3 -> new value
*/
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, classNameInternal);
// get VALUE
mv.visitVarInsn(ALOAD, 3);
Type fieldType = Type.getType(acc.getType());
Class<?> type = acc.getType();
String destClsName = Type.getInternalName(type);
Method conMtd = convMtds.get(type);
if (conMtd != null) {
// external conversion
String clsSig = Type.getInternalName(conMtd.getDeclaringClass());
String mtdName = conMtd.getName();
String mtdSig = Type.getMethodDescriptor(conMtd);
mv.visitMethodInsn(INVOKESTATIC, clsSig, mtdName, mtdSig, false);
} else if (acc.isEnum()) {
// builtIn Enum Conversion
Label isNull = new Label();
mv.visitJumpInsn(IFNULL, isNull);
mv.visitVarInsn(ALOAD, 3);
// mv.visitTypeInsn(CHECKCAST, "java/lang/String");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKESTATIC, destClsName, "valueOf", "(Ljava/lang/String;)L" + destClsName + ";", false);
mv.visitVarInsn(ASTORE, 3);
mv.visitLabel(isNull);
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, this.classNameInternal); // "net/minidev/asm/bean/BEnumPriv"
mv.visitVarInsn(ALOAD, 3);
mv.visitTypeInsn(CHECKCAST, destClsName);
} else if (type.equals(String.class)) {
// built In String Conversion
Label isNull = new Label();
mv.visitJumpInsn(IFNULL, isNull);
mv.visitVarInsn(ALOAD, 3);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;", false);
mv.visitVarInsn(ASTORE, 3);
mv.visitLabel(isNull);
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, this.classNameInternal);
mv.visitVarInsn(ALOAD, 3);
mv.visitTypeInsn(CHECKCAST, destClsName);
} else {
// just check Cast
mv.visitTypeInsn(CHECKCAST, destClsName);
}
if (acc.isPublic() || acc.setter == null) {
mv.visitFieldInsn(PUTFIELD, classNameInternal, acc.getName(), fieldType.getDescriptor());
} else {
String sig = Type.getMethodDescriptor(acc.setter);
mv.visitMethodInsn(INVOKEVIRTUAL, classNameInternal, acc.setter.getName(), sig, false);
}
mv.visitInsn(RETURN);
}
/**
* add Throws statement with int param 2
*/
private void throwExIntParam(MethodVisitor mv, Class<?> exCls) {
String exSig = Type.getInternalName(exCls);
mv.visitTypeInsn(NEW, exSig);
mv.visitInsn(DUP);
mv.visitLdcInsn("mapping " + this.className + " failed to map field:");
mv.visitVarInsn(ILOAD, 2);
mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "toString", "(I)Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "concat", "(Ljava/lang/String;)Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKESPECIAL, exSig, "<init>", "(Ljava/lang/String;)V", false);
mv.visitInsn(ATHROW);
}
/**
* add Throws statement with String param 2
*/
private void throwExStrParam(MethodVisitor mv, Class<?> exCls) {
String exSig = Type.getInternalName(exCls);
mv.visitTypeInsn(NEW, exSig);
mv.visitInsn(DUP);
mv.visitLdcInsn("mapping " + this.className + " failed to map field:");
mv.visitVarInsn(ALOAD, 2);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "concat", "(Ljava/lang/String;)Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKESPECIAL, exSig, "<init>", "(Ljava/lang/String;)V", false);
mv.visitInsn(ATHROW);
}
/**
* dump a Jump if not EQ
*/
private void ifNotEqJmp(MethodVisitor mv, int param, int value, Label label) {
mv.visitVarInsn(ILOAD, param);
if (value == 0) {
/* notest for value 0 */
mv.visitJumpInsn(IFNE, label);
} else if (value == 1) {
mv.visitInsn(ICONST_1);
mv.visitJumpInsn(IF_ICMPNE, label);
} else if (value == 2) {
mv.visitInsn(ICONST_2);
mv.visitJumpInsn(IF_ICMPNE, label);
} else if (value == 3) {
mv.visitInsn(ICONST_3);
mv.visitJumpInsn(IF_ICMPNE, label);
} else if (value == 4) {
mv.visitInsn(ICONST_4);
mv.visitJumpInsn(IF_ICMPNE, label);
} else if (value == 5) {
mv.visitInsn(ICONST_5);
mv.visitJumpInsn(IF_ICMPNE, label);
} else if (value >= 6) {
mv.visitIntInsn(BIPUSH, value);
mv.visitJumpInsn(IF_ICMPNE, label);
} else {
throw new RuntimeException("non supported negative values");
}
}
}