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

1  
!7
2  
3  
set flag AllPublic.
4  
5  
lib 1400554 // javassist
6  
7  
import javassist.CtClass;
8  
import javassist.CtClassType;
9  
import javassist.bytecode.*;
10  
11  
p {
12  
  ClassFile cf = new ClassFile(false, "User123", null);
13  
  cf.setInterfaces(new S[] { className(IF1) });
14  
  var cp = cf.getConstPool();
15  
  
16  
  // add default constructor
17  
  MethodInfo mi = new MethodInfo(cp, "<init>",
18  
    Descriptor.ofConstructor(new CtClass[0]));
19  
  var code = new Bytecode(cp, 1, 0);
20  
  CtClass ctSuperclass = new CtClassType("java/lang/Object", cp);
21  
  code.addInvokespecial(ctSuperclass, "<init>",
22  
    Descriptor.ofConstructor(new CtClass[0]));
23  
  code.addReturn(null);
24  
  code.setMaxLocals(1);
25  
  mi.setCodeAttribute(code.toCodeAttribute());
26  
  cf.addMethod(mi);
27  
  
28  
  byte[] bytes = bytesFromDataOutputStream(stream -> cf.write(stream));
29  
  var cl = new InMemoryClassLoader(myClassLoader());
30  
  Class<? extends IF1> c = cast cl.defineAClass("User123", bytes);
31  
  print(c);
32  
  
33  
  IF1 f = newInstance(c);
34  
  print(f);
35  
}

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: 107 / 430
Version history: 11 change(s)
Referenced in: [show references]