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

35
LINES

< > BotCompany Repo | #1034298 // Test Javassist (bytecode level API, dev.)

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

Download Jar. Uses 755K of libraries. Compilation Failed (5098L/24K).

!7

set flag AllPublic.

lib 1400554 // javassist

import javassist.CtClass;
import javassist.CtClassType;
import javassist.bytecode.*;

p {
  ClassFile cf = new ClassFile(false, "User123", null);
  cf.setInterfaces(new S[] { className(IF1) });
  var cp = cf.getConstPool();
  
  // add default constructor
  MethodInfo mi = new MethodInfo(cp, "<init>",
    Descriptor.ofConstructor(new CtClass[0]));
  var code = new Bytecode(cp, 1, 0);
  CtClass ctSuperclass = new CtClassType("java/lang/Object", cp);
  code.addInvokespecial(ctSuperclass, "<init>",
    Descriptor.ofConstructor(new CtClass[0]));
  code.addReturn(null);
  code.setMaxLocals(1);
  mi.setCodeAttribute(code.toCodeAttribute());
  cf.addMethod(mi);
  
  byte[] bytes = bytesFromDataOutputStream(stream -> cf.write(stream));
  var cl = new InMemoryClassLoader(myClassLoader());
  Class<? extends IF1> c = cast cl.defineAClass("User123", bytes);
  print(c);
  
  IF1 f = newInstance(c);
  print(f);
}

Author comment

Began life as a copy of #1034295

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1034298
Snippet name: Test Javassist (bytecode level API, dev.)
Eternal ID of this version: #1034298/12
Text MD5: 9e93f325028ddeb4458863dfb986c26c
Transpilation MD5: 7c91b5a698a1c60f398356804d70840b
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 15:34:19
Source code size: 997 bytes / 35 lines
Pitched / IR pitched: No / No
Views / Downloads: 218 / 694
Version history: 11 change(s)
Referenced in: [show references]