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).

!7

set flag AllPublic.

!include once #1019934 // BCEL

import org.apache.bcel.Const;
import org.apache.bcel.generic.*;

replace BType with org.apache.bcel.generic.Type.

p {
  ClassGen cg = new("User123", "java.lang.Object", null,
    Const.ACC_PUBLIC, new S[] { className(IF0) });
    
  cg.addEmptyConstructor(Const.ACC_PUBLIC);
  
  // make code for get() method
  
  new InstructionList il;
  ConstantPoolGen cp = cg.getConstantPool();
  var factory = new InstructionFactory(cg);
  
  var mg = new MethodGen(Const.ACC_PUBLIC,
    new ObjectType(className(/*S*/O)), // String doesn't work
    new BType[] {}, null, "get", cg.getClassName(), il, cp);
  
  // create empty StringBuffer, store in local variable
  int iBuffer = 1; // local variable index
  
  il.append(factory.createNew("java.lang.StringBuffer"));
  il.append(InstructionConst.DUP);
  il.append(factory.createInvoke("java.lang.StringBuffer", "<init>", BType.VOID, new BType[] {}, Const.INVOKESPECIAL));
  il.append(new ASTORE(iBuffer));
  
  // Append twice
  
  for (string : ll("Hello ", "world")) {
    il.append(new ALOAD(iBuffer));
    il.append(new PUSH(cp, string));
    il.append(factory.createAppend(org.apache.bcel.generic.Type.OBJECT));
  }
  
  // call StringBuffer.toString() and return
  
  il.append(new ALOAD(iBuffer));
  il.append(factory.createInvoke("java.lang.StringBuffer", "toString",
    new ObjectType("java.lang.String"), new BType[] {}, Const.INVOKEVIRTUAL));
  il.append(InstructionConst.ARETURN);

  mg.stripAttributes(true);
  mg.setMaxStack();
  mg.setMaxLocals();
  cg.addMethod(mg.getMethod());
  
  // bake class
  JavaClass baked = cg.getJavaClass();
  
  // print class overview
  print(baked);
  
  // print the methods
  for (method : baked.getMethods()) {
    print("\n" + method);
    print(method.getCode());
  }
  
  byte[] bytes = baked.getBytes();
  var cl = new InMemoryClassLoader(myClassLoader());
  
  time "Defining class" {
    Class<? extends IF0> c = cast cl.defineAClass("User123", bytes);
  }
  print(c);
  
  IF0 f = newInstance(c);
  print(f);
  assertEqualsVerbose("Hello world", f!);
}

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: 292 / 1070
Version history: 23 change(s)
Referenced in: [show references]