Advertisement
bobmarley12345

java generate and load class from bytecode during runtime

Feb 10th, 2022
1,169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package reghzy.asmclass;
  2.  
  3. import org.objectweb.asm.ClassWriter;
  4. import org.objectweb.asm.MethodVisitor;
  5. import org.objectweb.asm.Opcodes;
  6. import org.objectweb.asm.Type;
  7.  
  8. public class Main {
  9.  
  10.     public static final ASMClassLoader LOADER = new ASMClassLoader();
  11.  
  12.     public static void main(String[] args) {
  13.         Class<?> clazz = generateClass();
  14.         try {
  15.             clazz.newInstance();
  16.         }
  17.         catch (InstantiationException e) {
  18.             e.printStackTrace();
  19.         }
  20.         catch (IllegalAccessException e) {
  21.             e.printStackTrace();
  22.         }
  23.     }
  24.  
  25.     public static Class<?> generateClass() {
  26.         String className = "MyCustomASMClass";
  27.         String classDescription = className; // can include packages too, using the '/' char instead of '.' (e.g my/package/MyCustomASMClass)
  28.  
  29.         ClassWriter cw = new ClassWriter(0);
  30.  
  31.         cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, classDescription, null,  "java/lang/Object", new String[0]);
  32.         cw.visitSource(".dyn", null);
  33.  
  34.         // Ljava/lang/Object;
  35.         cw.visitField(Opcodes.ACC_PUBLIC, "myField", Type.getDescriptor(Object.class), null, null).visitEnd();
  36.  
  37.         MethodVisitor ctor = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
  38.         ctor.visitCode();
  39.         ctor.visitVarInsn(Opcodes.ALOAD, 0); // load this
  40.         ctor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); // invoke super()
  41.  
  42.         // call System.out.println("hello!")
  43.  
  44.         ctor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
  45.         ctor.visitLdcInsn("hello!");
  46.         ctor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
  47.  
  48.         ctor.visitInsn(Opcodes.RETURN);
  49.  
  50.         ctor.visitMaxs(2, 1);
  51.  
  52.         ctor.visitEnd();
  53.  
  54.         cw.visitEnd();
  55.  
  56.         byte[] bytes = cw.toByteArray();
  57.  
  58.         return LOADER.defineClass(className, bytes);
  59.  
  60.         // try {
  61.         //     OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("F:\\TEMPPP\\MyCustomASMClass.class")));
  62.         //     out.write(bytes);
  63.         //     out.close();
  64.         // }
  65.         // catch (FileNotFoundException e) {
  66.         //     e.printStackTrace();
  67.         // }
  68.         // catch (IOException e) {
  69.         //     e.printStackTrace();
  70.         // }
  71.     }
  72.  
  73.     private static class ASMClassLoader extends ClassLoader {
  74.         public ASMClassLoader() {
  75.             super(Main.class.getClassLoader());
  76.         }
  77.  
  78.         public Class<?> defineClass(String name, byte[] data) {
  79.             return this.defineClass(name, data, 0, data.length);
  80.         }
  81.     }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement