Download Jar. Uses 1311K of libraries. Click here for Pure Java version (5515L/26K).
!7 set flag AllPublic. 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 var m = new MethodMaker(cg, "get", O); // declare and create StringBuffer int buffer = m.newLocalVar(); m.newObject(StringBuffer.class).astore(buffer); // Append two strings for (string : ll("Hello ", "world")) { m.aload(buffer).stringConstant(string); m.il.append(m.factory.createAppend(org.apache.bcel.generic.Type.OBJECT)); } // call StringBuffer.toString() and return m.aload(buffer); m.invokeVirtual(StringBuffer.class, "toString", S); m.areturn().done(); // 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!); }
Began life as a copy of #1034300
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034311 |
Snippet name: | Test making class from scratch with BCEL using MethodMaker [OK] |
Eternal ID of this version: | #1034311/6 |
Text MD5: | 43fc1b77ed7e2cfd6b8c3028cb1c75b8 |
Transpilation MD5: | d1836f076f538d57b24613d038f4f154 |
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 20:38:57 |
Source code size: | 1297 bytes / 55 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 229 / 832 |
Version history: | 5 change(s) |
Referenced in: | #1034314 - ClassMaker [BCEL helper] #1034315 - Test making class implementing interface from scratch with BCEL using ClassMaker + MethodMaker [OK] |