Skip to content

Instantly share code, notes, and snippets.

@mergeconflict
Created October 20, 2012 21:02
Show Gist options
  • Save mergeconflict/3924794 to your computer and use it in GitHub Desktop.
Save mergeconflict/3924794 to your computer and use it in GitHub Desktop.
hello asm
package com.mergeconflict.outclass;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ACC_STATIC;
import static org.objectweb.asm.Opcodes.GETSTATIC;
import static org.objectweb.asm.Opcodes.RETURN;
import static org.objectweb.asm.Opcodes.*;
import java.io.FileOutputStream;
import java.io.IOException;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
public class Main {
public static void main(String[] args) throws IOException {
ClassWriter c = new ClassWriter(0);
c.visit(V1_7, ACC_PUBLIC, "hello/Hello", null, "java/lang/Object", null);
MethodVisitor m = c.visitMethod(ACC_PUBLIC | ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
m.visitCode();
m.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
m.visitLdcInsn("Hello, World!");
m.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
m.visitInsn(RETURN);
m.visitMaxs(2, 1);
m.visitEnd();
c.visitEnd();
try (FileOutputStream f = new FileOutputStream("target/hello/Hello.class")) {
f.write(c.toByteArray());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment