Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

79
LINES

< > BotCompany Repo | #1034300 // Test making class from scratch with BCEL [OK!]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 1311K of libraries. Click here for Pure Java version (5063L/24K).

1  
!7
2  
3  
set flag AllPublic.
4  
5  
!include once #1019934 // BCEL
6  
7  
import org.apache.bcel.Const;
8  
import org.apache.bcel.generic.*;
9  
10  
replace BType with org.apache.bcel.generic.Type.
11  
12  
p {
13  
  ClassGen cg = new("User123", "java.lang.Object", null,
14  
    Const.ACC_PUBLIC, new S[] { className(IF0) });
15  
    
16  
  cg.addEmptyConstructor(Const.ACC_PUBLIC);
17  
  
18  
  // make code for get() method
19  
  
20  
  new InstructionList il;
21  
  ConstantPoolGen cp = cg.getConstantPool();
22  
  var factory = new InstructionFactory(cg);
23  
  
24  
  var mg = new MethodGen(Const.ACC_PUBLIC,
25  
    new ObjectType(className(/*S*/O)), // String doesn't work
26  
    new BType[] {}, null, "get", cg.getClassName(), il, cp);
27  
  
28  
  // create empty StringBuffer, store in local variable
29  
  int iBuffer = 1; // local variable index
30  
  
31  
  il.append(factory.createNew("java.lang.StringBuffer"));
32  
  il.append(InstructionConst.DUP);
33  
  il.append(factory.createInvoke("java.lang.StringBuffer", "<init>", BType.VOID, new BType[] {}, Const.INVOKESPECIAL));
34  
  il.append(new ASTORE(iBuffer));
35  
  
36  
  // Append twice
37  
  
38  
  for (string : ll("Hello ", "world")) {
39  
    il.append(new ALOAD(iBuffer));
40  
    il.append(new PUSH(cp, string));
41  
    il.append(factory.createAppend(org.apache.bcel.generic.Type.OBJECT));
42  
  }
43  
  
44  
  // call StringBuffer.toString() and return
45  
  
46  
  il.append(new ALOAD(iBuffer));
47  
  il.append(factory.createInvoke("java.lang.StringBuffer", "toString",
48  
    new ObjectType("java.lang.String"), new BType[] {}, Const.INVOKEVIRTUAL));
49  
  il.append(InstructionConst.ARETURN);
50  
51  
  mg.stripAttributes(true);
52  
  mg.setMaxStack();
53  
  mg.setMaxLocals();
54  
  cg.addMethod(mg.getMethod());
55  
  
56  
  // bake class
57  
  JavaClass baked = cg.getJavaClass();
58  
  
59  
  // print class overview
60  
  print(baked);
61  
  
62  
  // print the methods
63  
  for (method : baked.getMethods()) {
64  
    print("\n" + method);
65  
    print(method.getCode());
66  
  }
67  
  
68  
  byte[] bytes = baked.getBytes();
69  
  var cl = new InMemoryClassLoader(myClassLoader());
70  
  
71  
  time "Defining class" {
72  
    Class<? extends IF0> c = cast cl.defineAClass("User123", bytes);
73  
  }
74  
  print(c);
75  
  
76  
  IF0 f = newInstance(c);
77  
  print(f);
78  
  assertEqualsVerbose("Hello world", f!);
79  
}

Author comment

Began life as a copy of #1034298

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034300
Snippet name: Test making class from scratch with BCEL [OK!]
Eternal ID of this version: #1034300/24
Text MD5: b3eb76e3a78f037a3de32c38ed6b8e6a
Transpilation MD5: 0ce1e45387601a9788975b4b1830706c
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-01 16:15:42
Source code size: 2190 bytes / 79 lines
Pitched / IR pitched: No / No
Views / Downloads: 294 / 1072
Version history: 23 change(s)
Referenced in: [show references]