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

23
LINES

< > BotCompany Repo | #1034295 // Test Javassist (make class at runtime, higher level API)

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

Download Jar. Uses 755K of libraries. Click here for Pure Java version (5029L/28K).

1  
!7
2  
3  
lib 1400554 // javassist
4  
5  
import javassist.ClassPool;
6  
import javassist.CtClass;
7  
import javassist.CtMethod;
8  
9  
p {
10  
  ClassPool pool = ClassPool.getDefault();
11  
  CtClass cc = pool.makeClass("User123");
12  
  
13  
  // The method part doesn't work yet
14  
  /*CtClass cString = pool.get("java.lang.String");
15  
  var method = new CtMethod(cString, "hello", new CtClass[0], cc);
16  
  method.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
17  
  cc.addMethod(method);*/
18  
  //Class c = cc.toClass();
19  
  byte[] bytes = cc.toBytecode();
20  
  var cl = new InMemoryClassLoader(myClassLoader());
21  
  Class c = cl.defineAClass("User123", bytes);
22  
  print(c);
23  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1034295
Snippet name: Test Javassist (make class at runtime, higher level API)
Eternal ID of this version: #1034295/6
Text MD5: 9cc90d2a86be0c4ead30c2395ffcf699
Transpilation MD5: 5795b037514a1afcf4ae78e5181930ba
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:06:10
Source code size: 642 bytes / 23 lines
Pitched / IR pitched: No / No
Views / Downloads: 193 / 793
Version history: 5 change(s)
Referenced in: [show references]