Transpiled version (12720L) is out of date.
1 | !include once #1019934 // BCEL |
2 | |
3 | // A = interface we are implementing |
4 | sclass ClassMaker<A> { |
5 | gettable S className; |
6 | gettable ClassGen cg; |
7 | JavaClass baked; |
8 | InMemoryClassLoader classLoader; |
9 | Class loadedClass; |
10 | settable bool printDisassembly; |
11 | settable S superClassName = "java.lang.Object"; |
12 | settable S fileName; |
13 | |
14 | *(S className) { |
15 | this(className, null); |
16 | } |
17 | |
18 | *(S *className, S *superClassName, S[] interfaces) { |
19 | setClassGen(new ClassGen(className, superClassName, fileName, |
20 | Const.ACC_PUBLIC, interfaces)); |
21 | } |
22 | |
23 | *(S *className, S[] interfaces) { |
24 | setClassGen(new ClassGen(className, superClassName, fileName, |
25 | Const.ACC_PUBLIC, interfaces)); |
26 | } |
27 | |
28 | *(Class<A> interfaceToImplement) { |
29 | className = randomClassName(); |
30 | setClassGen(new ClassGen(className, "java.lang.Object", null, |
31 | Const.ACC_PUBLIC, new S[] { main className(interfaceToImplement) })); |
32 | |
33 | addDefaultConstructor(); |
34 | } |
35 | |
36 | void addDefaultConstructor { |
37 | cg.addEmptyConstructor(Const.ACC_PUBLIC); |
38 | } |
39 | |
40 | void setClassGen(ClassGen cg) { |
41 | this.cg = cg; |
42 | |
43 | // up the byte code version so we can do ldc of class objects (e.g.) |
44 | cg.setMajor(50); // JDK 6 |
45 | //cg.setMajor(55); // JDK 11. can't use yet because StackMapTable became mandatory in JDK 8 and BCEL doesn't generate that |
46 | cg.setMinor(0); |
47 | } |
48 | |
49 | JavaClass bake() { |
50 | if (baked == null) { |
51 | baked = cg.getJavaClass(); |
52 | if (printDisassembly) |
53 | printClassWithMethods(); |
54 | } |
55 | ret baked; |
56 | } |
57 | |
58 | // tabs can look bad in JTextArea, so we're using tabToSingleSpace |
59 | void printClassWithMethods() { |
60 | // print class overview |
61 | print_tabToSingleSpace(bake()); |
62 | |
63 | // print the methods |
64 | for (method : baked.getMethods()) { |
65 | print_tabToSingleSpace("\n" + method); |
66 | print_tabToSingleSpace(method.getCode()); |
67 | } |
68 | } |
69 | |
70 | simplyCached byte[] toBytes aka getBytes() { |
71 | ret bake().getBytes(); |
72 | } |
73 | |
74 | Class<A> load() { |
75 | if (loadedClass == null) { |
76 | var bytes = toBytes(); |
77 | classLoader = new InMemoryClassLoader(myClassLoader()); |
78 | loadedClass = (Class) classLoader.defineAClass(className, bytes); |
79 | } |
80 | ret loadedClass; |
81 | } |
82 | |
83 | A newInstance() { |
84 | ret main newInstance(load()); |
85 | } |
86 | |
87 | void addField(FieldGen fg) { |
88 | cg.addField(fg.getField()); |
89 | } |
90 | |
91 | ConstantPoolGen getConstantPool() { |
92 | ret cg.getConstantPool(); |
93 | } |
94 | } |
Began life as a copy of #1034311
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034314 |
Snippet name: | ClassMaker [BCEL helper] |
Eternal ID of this version: | #1034314/27 |
Text MD5: | f685d5b204cbcd7d4e87378caf1198ac |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-12-16 04:43:17 |
Source code size: | 2482 bytes / 94 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 267 / 522 |
Version history: | 26 change(s) |
Referenced in: | [show references] |