forked from EB-wilson/JavaDynamilizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicMaker.java
More file actions
1198 lines (1037 loc) · 46.6 KB
/
Copy pathDynamicMaker.java
File metadata and controls
1198 lines (1037 loc) · 46.6 KB
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package dynamilize;
import dynamilize.classmaker.*;
import dynamilize.classmaker.code.*;
import dynamilize.classmaker.code.annotation.AnnotationType;
import org.objectweb.asm.Opcodes;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import static dynamilize.classmaker.ClassInfo.*;
/**动态类型运作的核心工厂类型,用于将传入的动态类型与委托基类等构造出动态委托类型以及其实例。
* <p>定义了基于{@link ASMGenerator}的默认生成器实现,通过{@link DynamicMaker#getDefault()}获取该实例以使用
* <p>若需要特殊的实现,则需要重写/实现此类的方法,主要的方法:
* <ul>
* <li><strong>{@link DynamicMaker#makeClassInfo(Class, Class[])}</strong>:决定此工厂如何构造委托类的描述信息
* <li><strong>{@link DynamicMaker#generateClass(Class, Class[])}</strong>:决定如何解析描述信息生成字节码以及如何加载字节码为java类
* </ul>
* 实施时必须按照方法的描述给出基本的实现,不应该改变原定的最低上下文请求。
* <pre>
* 使用方法可参阅:
* {@link DynamicClass}
* DynamicMaker的方法:
* {@link DynamicMaker#newInstance(DynamicClass)}
* {@link DynamicMaker#newInstance(Class[], DynamicClass)}
* {@link DynamicMaker#newInstance(Class, DynamicClass, Object...)}
* {@link DynamicMaker#newInstance(Class, Class[], DynamicClass, Object...)}
* </pre>
*
* @see DynamicClass
* @see DynamicObject
* @see DataPool
*
* @author EBwilson */
@SuppressWarnings({"rawtypes", "DuplicatedCode"})
public abstract class DynamicMaker{
public static final String CALLSUPER = "$super";
private static final HashSet<String> INTERNAL_FIELD = new HashSet<>(Arrays.asList(
"$dynamic_type$",
"$datapool$",
"$varValuePool$",
"$superbasepointer$"
));
public static final ClassInfo<DynamicClass> DYNAMIC_CLASS_TYPE = asType(DynamicClass.class);
public static final ClassInfo<DynamicObject> DYNAMIC_OBJECT_TYPE = asType(DynamicObject.class);
public static final ClassInfo<DataPool> DATA_POOL_TYPE = asType(DataPool.class);
public static final ClassInfo<FunctionType> FUNCTION_TYPE_TYPE = asType(FunctionType.class);
public static final MethodInfo<FunctionType, FunctionType> TYPE_INST = FUNCTION_TYPE_TYPE.getMethod(
FUNCTION_TYPE_TYPE,
"inst",
CLASS_TYPE.asArray());
public static final ClassInfo<Function> FUNCTION_TYPE = asType(Function.class);
public static final ClassInfo<DataPool.ReadOnlyPool> READONLY_POOL_TYPE = asType(DataPool.ReadOnlyPool.class);
public static final ClassInfo<Integer> INTEGER_CLASS_TYPE = asType(Integer.class);
public static final ClassInfo<HashMap> HASH_MAP_TYPE = asType(HashMap.class);
public static final ClassInfo<IVariable> VAR_TYPE = ClassInfo.asType(IVariable.class);
public static final ClassInfo<IllegalStateException> STATE_EXCEPTION_TYPE = asType(IllegalStateException.class);
public static final ClassInfo<ArgumentList> ARG_LIST_TYPE = asType(ArgumentList.class);
public static final ClassInfo<Function.SuperGetFunction> SUPER_GET_FUNC_TYPE = ClassInfo.asType(Function.SuperGetFunction.class);
public static final ClassInfo<IFunctionEntry> FUNC_ENTRY_TYPE = ClassInfo.asType(IFunctionEntry.class);
public static final IMethod<DataPool, DataPool.ReadOnlyPool> GET_READER = DATA_POOL_TYPE.getMethod(READONLY_POOL_TYPE, "getReader", DYNAMIC_OBJECT_TYPE);
public static final IMethod<HashMap, Object> MAP_GET = HASH_MAP_TYPE.getMethod(OBJECT_TYPE, "get", OBJECT_TYPE);
public static final IMethod<Integer, Integer> VALUE_OF = INTEGER_CLASS_TYPE.getMethod(INTEGER_CLASS_TYPE, "valueOf", INT_TYPE);
public static final IMethod<HashMap, Object> MAP_PUT = HASH_MAP_TYPE.getMethod(OBJECT_TYPE, "put", OBJECT_TYPE, OBJECT_TYPE);
public static final IMethod<DataPool, IVariable> GET_VAR = DATA_POOL_TYPE.getMethod(VAR_TYPE, "getVariable", STRING_TYPE);
public static final IMethod<DataPool, Void> SET_VAR = DATA_POOL_TYPE.getMethod(ClassInfo.VOID_TYPE, "setVariable", VAR_TYPE);
public static final IMethod<DataPool, Void> SETFUNC = DATA_POOL_TYPE.getMethod(ClassInfo.VOID_TYPE, "setFunction", STRING_TYPE, FUNCTION_TYPE, ClassInfo.CLASS_TYPE.asArray());
public static final IMethod<DataPool, Void> SETFUNC2 = DATA_POOL_TYPE.getMethod(ClassInfo.VOID_TYPE, "setFunction", STRING_TYPE, SUPER_GET_FUNC_TYPE, ClassInfo.CLASS_TYPE.asArray());
public static final IMethod<DataPool, IFunctionEntry> SELECT = DATA_POOL_TYPE.getMethod(FUNC_ENTRY_TYPE, "select", STRING_TYPE, FUNCTION_TYPE_TYPE);
public static final IMethod<DataPool, Void> INIT = DATA_POOL_TYPE.getMethod(VOID_TYPE, "init", DYNAMIC_OBJECT_TYPE, OBJECT_TYPE.asArray());
public static final IMethod<DynamicObject, Object> INVOKE = DYNAMIC_OBJECT_TYPE.getMethod(OBJECT_TYPE, "invokeFunc", FUNCTION_TYPE_TYPE, STRING_TYPE, OBJECT_TYPE.asArray());
public static final IMethod<ArgumentList, Object[]> GET_LIST = ARG_LIST_TYPE.getMethod(OBJECT_TYPE.asArray(), "getList", INT_TYPE);
public static final IMethod<ArgumentList, Void> RECYCLE_LIST = ARG_LIST_TYPE.getMethod(VOID_TYPE, "recycleList", OBJECT_TYPE.asArray());
private static final MethodHandles.Lookup LOOKUP_INST = MethodHandles.lookup();
private static final Map<String, Set<FunctionType>> OVERRIDES = new HashMap<>();
private static final Set<Class<?>> INTERFACE_TEMP = new HashSet<>();
private static final Class[] EMPTY_CLASSES = new Class[0];
public static final ILocal[] LOCALS_EMP = new ILocal[0];
public static final HashSet<FunctionType> EMP_MAP = new HashSet<>();
private final JavaHandleHelper helper;
private final HashMap<ClassImplements<?>, Class<?>> classPool = new HashMap<>();
private final HashMap<Class<?>, DataPool> classPoolsMap = new HashMap<>();
private final HashMap<Class<?>, HashMap<FunctionType, MethodHandle>> constructors = new HashMap<>();
/**创建一个实例,并传入其要使用的{@linkplain JavaHandleHelper java行为支持器},子类引用此构造器可能直接设置默认的行为支持器而无需外部传入*/
protected DynamicMaker(JavaHandleHelper helper){
this.helper = helper;
}
/**获取默认的动态类型工厂,工厂具备基于{@link ASMGenerator}与适用于<i>HotSpot JVM</i>运行时的{@link JavaHandleHelper}进行的实现。
* 适用于:
* <ul>
* <li>java运行时版本1.8的所有jvm
* <li>java运行时版本大于等于1.9的<i>甲骨文HotSpot JVM</i>
* <li><strong><i>IBM OpenJ9</i>运行时尚未支持</strong>
* </ul>
* 若有范围外的需求,可按需要进行实现*/
public static DynamicMaker getDefault(){
BaseClassLoader loader = new BaseClassLoader(DynamicMaker.class.getClassLoader());
ASMGenerator generator = new ASMGenerator(loader, Opcodes.V1_8);
return new DynamicMaker(acc -> acc.setAccessible(true)){
@Override
protected <T> Class<? extends T> generateClass(Class<T> baseClass, Class<?>[] interfaces){
return makeClassInfo(baseClass, interfaces).generate(generator);
}
};
}
/**使用默认构造函数构造没有实现额外接口的动态类的实例,实例的java类型委托类为{@link Object}
*
* @param dynamicClass 用于实例化的动态类型
* @return 构造出的动态实例*/
public DynamicObject newInstance(DynamicClass dynamicClass){
return newInstance(EMPTY_CLASSES, dynamicClass);
}
/**使用默认构造函数构造动态类的实例,实例实现了给出的接口列表,java类型委托为{@link Object}
*
* @param interfaces 实例实现的接口列表
* @param dynamicClass 用于实例化的动态类型
* @return 构造出的动态实例*/
@SuppressWarnings("rawtypes")
public DynamicObject newInstance(Class<?>[] interfaces, DynamicClass dynamicClass){
return newInstance(Object.class, interfaces, dynamicClass);
}
/**用给出的构造函数参数构造动态类的实例,参数表必须可以在委托的java类型中存在匹配的可用构造器。
* <p>实例无额外接口,类型委托由参数确定
*
* @param base 执行委托的java类型,这将决定此实例可分配到的类型
* @param dynamicClass 用于实例化的动态类型
* @param args 构造函数实参
* @return 构造出的动态实例
*
* @throws RuntimeException 若构造函数实参无法匹配到相应的构造器或者存在其他异常*/
public <T> DynamicObject<T> newInstance(Class<T> base, DynamicClass dynamicClass, Object... args){
return newInstance(base, EMPTY_CLASSES, dynamicClass, args);
}
/**用给出的构造函数参数构造动态类的实例,参数表必须可以在委托的java类型中存在匹配的可用构造器。
* <p>实例实现了给出的接口列表,类型委托由参数确定
*
* @param base 执行委托的java类型,这将决定此实例可分配到的类型
* @param interfaces 实例实现的接口列表
* @param dynamicClass 用于实例化的动态类型
* @param args 构造函数实参
* @return 构造出的动态实例
*
* @throws RuntimeException 若构造函数实参无法匹配到相应的构造器或者存在其他异常*/
@SuppressWarnings("unchecked")
public <T> DynamicObject<T> newInstance(Class<T> base, Class<?>[] interfaces, DynamicClass dynamicClass, Object... args){
checkBase(base);
Class<? extends T> clazz = getDynamicBase(base, interfaces);
try{
List<Object> argsLis = new ArrayList<>(Arrays.asList(
dynamicClass,
genPool(clazz, dynamicClass),
classPoolsMap.get(clazz)
));
argsLis.addAll(Arrays.asList(args));
Constructor<?> cstr = null;
for(Constructor<?> constructor: clazz.getDeclaredConstructors()){
FunctionType t;
if((t = FunctionType.from(constructor)).match(argsLis.toArray())){
cstr = constructor;
break;
}
t.recycle();
}
if(cstr == null)
throw new NoSuchMethodError("no matched constructor found with parameter " + Arrays.toString(args));
FunctionType type = FunctionType.inst(cstr.getParameterTypes());
Constructor<?> c = cstr;
DynamicObject<T> inst = (DynamicObject<T>) constructors.computeIfAbsent(clazz, e -> new HashMap<>())
.computeIfAbsent(type, t -> {
try{
return LOOKUP_INST.unreflectConstructor(c);
}catch(IllegalAccessException e){
throw new RuntimeException(e);
}
}).invokeWithArguments(argsLis.toArray());
type.recycle();
return inst;
}catch(Throwable e){
throw new RuntimeException(e);
}
}
public JavaHandleHelper getHelper(){
return helper;
}
/**检查委托基类的可用性,基类若为final或者基类为接口则抛出异常*/
protected <T> void checkBase(Class<T> base){
if(base.isInterface())
throw new IllegalHandleException("interface cannot use to derive a dynamic class, please write it in parameter \"interfaces\" to implement this interface");
if(Modifier.isFinal(base.getModifiers()))
throw new IllegalHandleException("cannot derive a dynamic class with a final class");
}
/**生成动态类型相应的数据池,数据池完成动态类型向上对超类的迭代逐级分配数据池信息,同时对委托产生的动态类型生成所有方法和字段的函数/变量入口并放入数据池
*
* @param base 动态委托类
* @param dynamicClass 描述行为的动态类型
* @return 生成的动态类型数据池*/
protected <T> DataPool genPool(Class<? extends T> base, DynamicClass dynamicClass){
DataPool basePool = classPoolsMap.computeIfAbsent(base, clazz -> {
AtomicBoolean immutable = new AtomicBoolean();
DataPool res = new DataPool(null){
@Override
public void setFunction(String name, Function<?, ?> function, Class<?>... argsType){
if(immutable.get())
throw new IllegalHandleException("immutable pool");
super.setFunction(name, function, argsType);
}
@Override
public void setVariable(IVariable var){
if(immutable.get())
throw new IllegalHandleException("immutable pool");
super.setVariable(var);
}
};
Class<?> curr = clazz;
while(curr != null){
if(curr.getAnnotation(DynamicType.class) != null){
for(Method method: curr.getDeclaredMethods()){
CallSuperMethod callSuper = method.getAnnotation(CallSuperMethod.class);
if(callSuper != null){
String name = callSuper.srcMethod();
String signature = FunctionType.signature(name, method.getParameterTypes());
res.setFunction(
name,
(self, args) -> ((SuperInvoker) self).invokeSuper(signature, args.args()),
method.getParameterTypes()
);
}
}
curr = curr.getSuperclass();
}
for(Field field: curr.getDeclaredFields()){
if(Modifier.isStatic(field.getModifiers()) || isInternalField(field.getName())) continue;
helper.setAccess(field);
res.setVariable(new JavaVariable(field));
}
curr = curr.getSuperclass();
}
immutable.set(true);
return res;
});
return dynamicClass.genPool(basePool);
}
private static boolean isInternalField(String name){
return INTERNAL_FIELD.contains(name);
}
/**根据委托基类和实现的接口获取生成的动态类型实例的类型,类型会生成并放入池,下一次获取会直接从池中取出该类型
*
* @param base 委托的基类
* @param interfaces 需要实现的接口列表*/
@SuppressWarnings("unchecked")
protected <T> Class<? extends T> getDynamicBase(Class<T> base, Class<?>[] interfaces){
return (Class<? extends T>) classPool.computeIfAbsent(new ClassImplements<>(base, interfaces), e -> generateClass(base, interfaces));
}
/**由基类与接口列表建立动态类的打包名称,打包名称具有唯一性(或者足够高的离散性,不应出现频繁的碰撞)和不变性
*
* @param baseClass 基类
* @param interfaces 接口列表
* @return 由基类名称与全部接口名称的哈希值构成的打包名称*/
public static <T> String getDynamicName(Class<T> baseClass, Class<?>... interfaces){
return ensurePackage(baseClass.getName()) + "$dynamic$" + FunctionType.typeNameHash(interfaces);
}
private static String ensurePackage(String name){
if(name.startsWith("java.")){
return name.replaceFirst("java\\.", "lava.");
}
return name;
}
/**对动态委托产生的类型进行继续委托创建代码的方法,应当将首要处理过程向上传递给超类。
* 与{@link DynamicMaker#makeClassInfo(Class, Class[])}的行为相似,但需要将部分行为适应到已经具备委托行为的超类*/
@SuppressWarnings({"unchecked"})
protected <T> ClassInfo<? extends T> makeClassInfoOnDynmaic(Class<T> baseClass, Class<?>[] interfaces){
ArrayList<ClassInfo<?>> inter = new ArrayList<>(interfaces.length);
for(Class<?> i: interfaces){
inter.add(asType(i));
}
ClassInfo<? extends T> classInfo = new ClassInfo<>(
Modifier.PUBLIC,
ensurePackage(baseClass.getName()) + "$" + FunctionType.typeNameHash(interfaces),
asType(baseClass),
inter.toArray(new ClassInfo[0])
);
FieldInfo<HashMap> methodIndex = classInfo.declareField(
Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL,
"$methodIndex$",
HASH_MAP_TYPE,
null
);
CodeBlock<Void> clinit = classInfo.getClinitBlock();
ILocal<HashMap> caseIndex = clinit.local(HASH_MAP_TYPE);
clinit.newInstance(
asType(HashMap.class).getConstructor(),
caseIndex
);
clinit.assign(null, caseIndex, methodIndex);
ILocal<Integer> tempInt = clinit.local(INT_TYPE);
ILocal<Integer> tempIndexWrap = clinit.local(INTEGER_CLASS_TYPE);
ILocal<String> tempSign = clinit.local(STRING_TYPE);
ILocal<FunctionType> tempType = clinit.local(FUNCTION_TYPE_TYPE);
ILocal<Class> tempClass = clinit.local(CLASS_TYPE);
ILocal<Class[]> tempClasses = clinit.local(CLASS_TYPE.asArray());
HashMap<Method, Integer> callSuperCaseMap = new HashMap<>();
// public <init>(*parameters*){
// super(*parameters*);
// }
for(Constructor<?> cstr: baseClass.getDeclaredConstructors()){
if((cstr.getModifiers() & (Modifier.PUBLIC | Modifier.PROTECTED)) == 0) continue;
if(Modifier.isFinal(cstr.getModifiers())) continue;
ArrayList<IClass<?>> args = new ArrayList<>();
for(Class<?> type: cstr.getParameterTypes()){
args.add(ClassInfo.asType(type));
}
IClass<?>[] argArr = args.toArray(new IClass[0]);
IMethod<?, Void> constructor = classInfo.superClass().getConstructor(argArr);
CodeBlock<Void> code = classInfo.declareConstructor(Modifier.PUBLIC, Parameter.trans(argArr));
code.invokeSuper(code.getThis(), constructor, null, code.getParamList().toArray(new ILocal<?>[0]));
}
{//interfaces
Stack<Class<?>> interfaceStack = new Stack<>();
Class<?> curr = baseClass;
INTERFACE_TEMP.clear();
while(curr != null || !interfaceStack.empty()){
if(curr != null){
for(Class<?> i: curr.getInterfaces()){
if(INTERFACE_TEMP.add(i)) interfaceStack.push(i);
}
}else curr = interfaceStack.pop();
if(!curr.isInterface()){
curr = curr.getSuperclass();
}
else curr = null;
}
interfaceStack.addAll(Arrays.asList(interfaces));
while(!interfaceStack.empty()){
Class<?> interfaceCurr = interfaceStack.pop();
for(Class<?> i: interfaceCurr.getInterfaces()){
if(INTERFACE_TEMP.add(i)) interfaceStack.push(i);
}
for(Method method: interfaceCurr.getMethods()){
if(Modifier.isStatic(method.getModifiers())) continue;
ClassInfo<?> typeClass = ClassInfo.asType(interfaceCurr);
MethodInfo<?, ?> superMethod;
String methodName = method.getName();
ClassInfo<?> returnType = asType(method.getReturnType());
if(OVERRIDES.computeIfAbsent(methodName, e -> new HashSet<>()).add(FunctionType.from(method))){
superMethod = !Modifier.isAbstract(method.getModifiers()) && method.isDefault()? typeClass.getMethod(
returnType,
methodName,
Arrays.stream(method.getParameterTypes()).map(ClassInfo::asType).toArray(ClassInfo[]::new)
): null;
callSuperCaseMap.put(method, callSuperCaseMap.size());
String typeF = methodName + "$" + FunctionType.typeNameHash(method.getParameterTypes());
FieldInfo<FunctionType> funType = classInfo.declareField(
Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL,
typeF,
FUNCTION_TYPE_TYPE,
null
);
// private static final FunctionType FUNCTION_TYPE$*name*;
// static {
// ...
// FUNCTION_TYPE$*signature* = FunctionType.as(*paramTypes*);
// methodIndex.put(*signature*, *index*);
// ...
// }
{
String signature = FunctionType.signature(method);
clinit.loadConstant(tempInt, method.getParameterCount());
clinit.newArray(
CLASS_TYPE,
tempClasses,
tempInt
);
Class<?>[] paramTypes = method.getParameterTypes();
for(int i=0; i<paramTypes.length; i++){
clinit.loadConstant(tempClass, paramTypes[i]);
clinit.loadConstant(tempInt, i);
clinit.arrayPut(tempClasses, tempInt, tempClass);
}
clinit.invoke(
null,
TYPE_INST,
tempType,
tempClasses
);
clinit.assign(null, tempType, funType);
clinit.loadConstant(tempSign, signature);
clinit.loadConstant(tempInt, callSuperCaseMap.get(method));
clinit.invoke(
null,
VALUE_OF,
tempIndexWrap,
tempInt
);
clinit.invoke(
caseIndex,
MAP_PUT,
null,
tempSign,
tempIndexWrap
);
}
// public *returnType* *name*(*parameters*){
// *[return]* this.invokeFunc(FUNCTION_TYPE$*signature* ,"*name*", parameters);
// }
{
CodeBlock<?> code = classInfo.declareMethod(
Modifier.PUBLIC,
methodName,
returnType,
Parameter.asParameter(method.getParameters())
);
ILocal<FunctionType> type = code.local(FUNCTION_TYPE_TYPE);
ILocal<String> met = code.local(STRING_TYPE);
code.assign(null, funType, type);
code.loadConstant(met, method.getName());
ILocal<Object[]> argList = code.local(OBJECT_TYPE.asArray());
ILocal<Integer> length = code.local(INT_TYPE);
code.loadConstant(length, method.getParameterCount());
code.invoke(null, GET_LIST, argList, length);
if(method.getParameterCount() > 0){
ILocal<Integer> index = code.local(INT_TYPE);
for(int i = 0; i < code.getParamList().size(); i++){
code.loadConstant(index, i);
code.arrayPut(argList, index, code.getRealParam(i));
}
}
if(returnType != VOID_TYPE){
ILocal res = code.local(returnType);
code.invoke(code.getThis(), INVOKE, res, type, met, argList);
code.invoke(null, RECYCLE_LIST, null, argList);
code.returnValue(res);
}
else{
code.invoke(code.getThis(), INVOKE, null, type, met, argList);
code.invoke(null, RECYCLE_LIST, null, argList);
}
}
// private final *returnType* *name*$super(*parameters*){
// *[return]* super.*name*(*parameters*);
// }
if(superMethod != null){
CodeBlock<?> code = classInfo.declareMethod(
Modifier.PRIVATE | Modifier.FINAL,
methodName + CALLSUPER,
returnType,
Parameter.asParameter(method.getParameters())
);
if(returnType != VOID_TYPE){
ILocal res = code.local(returnType);
code.invokeSuper(code.getThis(), superMethod, res, code.getParamList().toArray(LOCALS_EMP));
code.returnValue(res);
}
else code.invokeSuper(code.getThis(), superMethod, null, code.getParamList().toArray(LOCALS_EMP));
AnnotationType<CallSuperMethod> callSuper = AnnotationType.asAnnotationType(CallSuperMethod.class);
HashMap<String, Object> map = new HashMap<>();
map.put("srcMethod", methodName);
callSuper.annotateTo(code.owner(), map);
}
}
}
}
}
//switch super
// public Object invokeSuper(String signature, Object... args);{
// Integer ind = methodIndex.get(signature);
// if(ind == null) return super.invokeSuper(signature, args)
// switch(ind){
// ...
// case *index*: *method*$super(args[0], args[1],...); break;
// ...
// }
// }
{
CodeBlock<Object> code = classInfo.declareMethod(
Modifier.PUBLIC,
"invokeSuper",
OBJECT_TYPE,
Parameter.trans(
STRING_TYPE,
OBJECT_TYPE.asArray()
)
);
IMethod<T, Object> superCaller = (IMethod<T, Object>) classInfo.superClass().getMethod(code.owner().returnType(), code.owner().name(),
code.owner().parameters().stream().map(Parameter::getType).toArray(IClass[]::new));
ILocal<Integer> index = code.local(INT_TYPE);
ILocal<Integer> indexWrap = code.local(INTEGER_CLASS_TYPE);
ILocal<Object> obj = code.local(OBJECT_TYPE);
ILocal<Object> emp = code.local(OBJECT_TYPE);
ILocal<HashMap> caseMap = code.local(HASH_MAP_TYPE);
code.assign(null, methodIndex, caseMap);
code.invoke(
caseMap,
MAP_GET,
obj,
code.getRealParam(0)
);
Label j = code.label();
code.loadConstant(emp, null);
code.compare(obj, ICompare.Comparison.UNEQUAL, emp, j);
code.invokeSuper(code.getThis(), superCaller, obj, code.getParamList().toArray(new ILocal<?>[0]));
code.returnValue(obj);
code.markLabel(j);
code.cast(obj, indexWrap);
code.invoke(
indexWrap,
INTEGER_CLASS_TYPE.getMethod(INT_TYPE, "intValue"),
index
);
Label end = code.label();
ISwitch<Integer> iSwitch = code.switchDef(index, end);
ILocal<Object[]> args = code.getRealParam(1);
makeSwitch(callSuperCaseMap, code, obj, end, iSwitch, args);
}
return classInfo;
}
/**创建动态实例类型的类型标识,这应当覆盖所有委托目标类的方法和实现的接口中的方法,若超类的某一成员方法不是抽象的,需保留对超类方法的入口,
* 再重写本方法,对超类方法的入口需要有一定的标识以供生成基类数据池的引用函数时使用。
* <p>对于给定的基类和接口列表,生成的动态实例基类的名称是唯一的(或者足够的离散以至于几乎不可能碰撞)。
* <p>关于此方法实现需要完成的工作,请参阅{@link DynamicObject}中的描述
*
* @param baseClass 委托基类
* @param interfaces 实现的接口列表
* @return 完成了所有必要描述的类型标识*/
@SuppressWarnings({"unchecked", "rawtypes"})
protected <T> ClassInfo<? extends T> makeClassInfo(Class<T> baseClass, Class<?>[] interfaces){
if(baseClass.getAnnotation(DynamicType.class) != null)
return makeClassInfoOnDynmaic(baseClass, interfaces);
ArrayList<ClassInfo<?>> inter = new ArrayList<>(interfaces.length + 1);
inter.add(asType(DynamicObject.class));
inter.add(asType(SuperInvoker.class));
for(Class<?> i: interfaces){
inter.add(asType(i));
}
ClassInfo<? extends T> classInfo = new ClassInfo<>(
Modifier.PUBLIC,
getDynamicName(baseClass, interfaces),
asType(baseClass),
inter.toArray(new ClassInfo[0])
);
FieldInfo<DynamicClass> dyType = classInfo.declareField(
Modifier.PRIVATE | Modifier.FINAL,
"$dynamic_type$",
DYNAMIC_CLASS_TYPE,
null
);
FieldInfo<DataPool> dataPool = classInfo.declareField(
Modifier.PRIVATE | Modifier.FINAL,
"$datapool$",
DATA_POOL_TYPE,
null
);
FieldInfo<HashMap<String, Object>> varPool = (FieldInfo) classInfo.declareField(
Modifier.PRIVATE | Modifier.FINAL,
"$varValuePool$",
HASH_MAP_TYPE,
null
);
FieldInfo<DataPool.ReadOnlyPool> basePoolPointer = classInfo.declareField(
Modifier.PRIVATE | Modifier.FINAL,
"$superbasepointer$",
READONLY_POOL_TYPE,
null
);
FieldInfo<HashMap> methodIndex = classInfo.declareField(
Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL,
"$methodIndex$",
HASH_MAP_TYPE,
null
);
CodeBlock<Void> clinit = classInfo.getClinitBlock();
ILocal<HashMap> caseIndex = clinit.local(HASH_MAP_TYPE);
clinit.newInstance(
asType(HashMap.class).getConstructor(),
caseIndex
);
clinit.assign(null, caseIndex, methodIndex);
ILocal<Integer> tempInt = clinit.local(INT_TYPE);
ILocal<Integer> tempIndexWrap = clinit.local(INTEGER_CLASS_TYPE);
ILocal<String> tempSign = clinit.local(STRING_TYPE);
ILocal<FunctionType> tempType = clinit.local(FUNCTION_TYPE_TYPE);
ILocal<Class> tempClass = clinit.local(CLASS_TYPE);
ILocal<Class[]> tempClasses = clinit.local(CLASS_TYPE.asArray());
HashMap<Method, Integer> callSuperCaseMap = new HashMap<>();
// public <init>(DynamicClass $dyC$, DataPool $datP$, DataPool.ReadOnlyPool $basePool$, *parameters*){
// this.$dynamic_type$ = $dyC$;
// this.$datapool$ = $datP$;
// this.$varValuePool$ = new HashMap<>();
// super(*parameters*);
// this.$superbasepointer$ = $datapool$.getReader(this);
//
// this.$datapool$.init(*parameters*);
// }
for(Constructor<?> cstr: baseClass.getDeclaredConstructors()){
if((cstr.getModifiers() & (Modifier.PUBLIC | Modifier.PROTECTED)) == 0) continue;
if(Modifier.isFinal(cstr.getModifiers())) continue;
List<Parameter<?>> params = new ArrayList<>(Arrays.asList(Parameter.as(
0, DynamicClass.class, "$dyc$",
0, DataPool.class, "$datP$",
0, DataPool.class, "$basePool$"
)));
List<Parameter<?>> superParams = Arrays.asList(Parameter.asParameter(cstr.getParameters()));
params.addAll(superParams);
IMethod<?, Void> constructor =classInfo.superClass().getConstructor(superParams.stream().map(Parameter::getType).toArray(IClass[]::new));
CodeBlock<Void> code = classInfo.declareConstructor(Modifier.PUBLIC, params.toArray(new Parameter[0]));
List<ILocal<?>> l = code.getParamList();
ILocal<?> self = code.getThis();
ILocal<DynamicClass> dyC = code.getParam(1);
ILocal<DataPool> datP = code.getParam(2);
code.assign(self, dyC, dyType);
code.assign(self, datP, dataPool);
ILocal<HashMap> map = code.local(HASH_MAP_TYPE);
code.newInstance(HASH_MAP_TYPE.getConstructor(), map);
code.assign(self, map, varPool);
code.invokeSuper(self, constructor, null, l.subList(3, l.size()).toArray(LOCALS_EMP));
ILocal<DataPool.ReadOnlyPool> base = code.local(READONLY_POOL_TYPE);
code.invoke(code.getParam(3), GET_READER, base, self);
code.assign(self, base, basePoolPointer);
ILocal<Object[]> argList = code.local(OBJECT_TYPE.asArray());
ILocal<Integer> length = code.local(INT_TYPE);
code.loadConstant(length, cstr.getParameterCount());
code.invoke(null, GET_LIST, argList, length);
if(cstr.getParameterCount() > 0){
ILocal<Integer> index = code.local(INT_TYPE);
for(int i = 3; i < code.getParamList().size(); i++){
code.loadConstant(index, i - 3);
code.arrayPut(argList, index, code.getRealParam(i));
}
}
code.invoke(datP, INIT, null, self, argList);
code.invoke(null, RECYCLE_LIST, null, argList);
}
OVERRIDES.clear();
INTERFACE_TEMP.clear();
Stack<Class<?>> interfaceStack = new Stack<>();
HashMap<String, HashSet<FunctionType>> overrideMethods = new HashMap<>();
HashMap<String, HashSet<FunctionType>> finalMethods = new HashMap<>();
Class<?> curr = baseClass;
ClassInfo<?> typeClass;
MethodInfo<?, ?> superMethod;
while(curr != null || !interfaceStack.empty()){
if(curr != null){
for(Class<?> i: curr.getInterfaces()){
if(INTERFACE_TEMP.add(i)) interfaceStack.push(i);
}
}
else curr = interfaceStack.pop();
typeClass = asType(curr);
for(Method method: curr.getDeclaredMethods()){
// 如果方法是静态的,或者方法不对子类可见则不重写此方法
if(Modifier.isStatic(method.getModifiers())) continue;
if((method.getModifiers() & (Modifier.PUBLIC | Modifier.PROTECTED)) == 0) continue;
if(Modifier.isFinal(method.getModifiers())){
finalMethods.computeIfAbsent(method.getName(), e -> new HashSet<>()).add(FunctionType.from(method));
continue;
}
if(!overrideMethods.computeIfAbsent(method.getName(), e -> new HashSet<>()).add(FunctionType.from(method))
|| finalMethods.getOrDefault(method.getName(), EMP_MAP).contains(FunctionType.from(method))) continue;
String methodName = method.getName();
ClassInfo<?> returnType = asType(method.getReturnType());
if(OVERRIDES.computeIfAbsent(methodName, e -> new HashSet<>()).add(FunctionType.from(method))){
superMethod = !Modifier.isAbstract(method.getModifiers()) && !curr.isInterface() || method.isDefault()? typeClass.getMethod(
returnType,
methodName,
Arrays.stream(method.getParameterTypes()).map(ClassInfo::asType).toArray(ClassInfo[]::new)
): null;
callSuperCaseMap.put(method, callSuperCaseMap.size());
String typeF = methodName + "$" + FunctionType.typeNameHash(method.getParameterTypes());
FieldInfo<FunctionType> funType = classInfo.declareField(
Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL,
typeF,
FUNCTION_TYPE_TYPE,
null
);
// private static final FunctionType FUNCTION_TYPE$*name*;
// static {
// ...
// FUNCTION_TYPE$*signature* = FunctionType.as(*paramTypes*);
// methodIndex.put(*signature*, *index*);
// ...
// }
{
String signature = FunctionType.signature(method);
clinit.loadConstant(tempInt, method.getParameterCount());
clinit.newArray(
CLASS_TYPE,
tempClasses,
tempInt
);
Class<?>[] paramTypes = method.getParameterTypes();
for(int i=0; i<paramTypes.length; i++){
clinit.loadConstant(tempClass, paramTypes[i]);
clinit.loadConstant(tempInt, i);
clinit.arrayPut(tempClasses, tempInt, tempClass);
}
clinit.invoke(
null,
TYPE_INST,
tempType,
tempClasses
);
clinit.assign(null, tempType, funType);
clinit.loadConstant(tempSign, signature);
clinit.loadConstant(tempInt, callSuperCaseMap.get(method));
clinit.invoke(
null,
VALUE_OF,
tempIndexWrap,
tempInt
);
clinit.invoke(
caseIndex,
MAP_PUT,
null,
tempSign,
tempIndexWrap
);
}
// public *returnType* *name*(*parameters*){
// *[return]* this.invokeFunc(FUNCTION_TYPE$*signature* ,"*name*", parameters);
// }
{
CodeBlock<?> code = classInfo.declareMethod(
Modifier.PUBLIC,
methodName,
returnType,
Parameter.asParameter(method.getParameters())
);
ILocal<FunctionType> type = code.local(FUNCTION_TYPE_TYPE);
ILocal<String> met = code.local(STRING_TYPE);
code.assign(null, funType, type);
code.loadConstant(met, method.getName());
ILocal<Object[]> argList = code.local(OBJECT_TYPE.asArray());
ILocal<Integer> length = code.local(INT_TYPE);
code.loadConstant(length, method.getParameterCount());
code.invoke(null, GET_LIST, argList, length);
if(method.getParameterCount() > 0){
ILocal<Integer> index = code.local(INT_TYPE);
for(int i = 0; i < code.getParamList().size(); i++){
code.loadConstant(index, i);
code.arrayPut(argList, index, code.getRealParam(i));
}
}
if(returnType != VOID_TYPE){
ILocal res = code.local(returnType);
code.invoke(code.getThis(), INVOKE, res, type, met, argList);
code.invoke(null, RECYCLE_LIST, null, argList);
code.returnValue(res);
}
else{
code.invoke(code.getThis(), INVOKE, null, type, met, argList);
code.invoke(null, RECYCLE_LIST, null, argList);
}
}
// private final *returnType* *name*$super(*parameters*){
// *[return]* super.*name*(*parameters*);
// }
if(superMethod != null){
CodeBlock<?> code = classInfo.declareMethod(
Modifier.PRIVATE | Modifier.FINAL,
methodName + CALLSUPER,
returnType,
Parameter.asParameter(method.getParameters())
);
if(returnType != VOID_TYPE){
ILocal res = code.local(returnType);
code.invokeSuper(code.getThis(), superMethod, res, code.getParamList().toArray(LOCALS_EMP));
code.returnValue(res);
}
else code.invokeSuper(code.getThis(), superMethod, null, code.getParamList().toArray(LOCALS_EMP));
AnnotationType<CallSuperMethod> callSuper = AnnotationType.asAnnotationType(CallSuperMethod.class);
HashMap<String, Object> map = new HashMap<>();
map.put("srcMethod", methodName);
callSuper.annotateTo(code.owner(), map);
}
}
}
if(!curr.isInterface()){
curr = curr.getSuperclass();
}
else curr = null;
}
// public Object invokeSuper(String signature, Object... args);{
// switch(methodIndex.get(signature)){
// ...
// case *index*: *method*$super(args[0], args[1],...); break;
// ...
// }
// }
{
CodeBlock<Object> code = classInfo.declareMethod(
Modifier.PUBLIC,
"invokeSuper",
OBJECT_TYPE,
Parameter.trans(
STRING_TYPE,
OBJECT_TYPE.asArray()
)
);
ILocal<Integer> index = code.local(INT_TYPE);
ILocal<Integer> indexWrap = code.local(INTEGER_CLASS_TYPE);
ILocal<Object> obj = code.local(OBJECT_TYPE);
ILocal<HashMap> caseMap = code.local(HASH_MAP_TYPE);
code.assign(null, methodIndex, caseMap);
code.invoke(
caseMap,
MAP_GET,
obj,
code.getRealParam(0)
);
code.cast(obj, indexWrap);
code.invoke(
indexWrap,
INTEGER_CLASS_TYPE.getMethod(INT_TYPE, "intValue"),
index
);
Label end = code.label();
ISwitch<Integer> iSwitch = code.switchDef(index, end);
ILocal<Object[]> args = code.getRealParam(1);
ILocal<Object> tmpObj = code.local(OBJECT_TYPE);
makeSwitch(callSuperCaseMap, code, tmpObj, end, iSwitch, args);
}
// public DataPool.ReadOnlyPool baseSuperPool(){
// return this.$superbasepointer$;
// }
{
CodeBlock<DataPool.ReadOnlyPool> code = classInfo.declareMethod(
Modifier.PUBLIC,
"baseSuperPointer",
READONLY_POOL_TYPE
);
ILocal<DataPool.ReadOnlyPool> res = code.local(READONLY_POOL_TYPE);
code.assign(code.getThis(), basePoolPointer, res);
code.returnValue(res);
}
// public DynamicClass<Self> getDyClass(){
// return this.$dynamic_type$;
// }
{
CodeBlock<DynamicClass> code = classInfo.declareMethod(
Modifier.PUBLIC,
"getDyClass",
DYNAMIC_CLASS_TYPE
);
ILocal<DynamicClass> res = code.local(DYNAMIC_CLASS_TYPE);
code.assign(code.getThis(), dyType, res);
code.returnValue(res);
}
// public <T> T varValueGet(String name){
// return this.$varValuePool$.get(name);
// }
{
CodeBlock<Object> code = classInfo.declareMethod(
Modifier.PUBLIC,
"varValueGet",
OBJECT_TYPE,
Parameter.trans(STRING_TYPE)
);