public class Learn2 {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
classWriter.visit(V1_8, ACC_PUBLIC, "cn/enaium/learn/asm/learn2/Learn2Test", null, "java/lang/Object", null);//类信息
MethodVisitor methodVisitor = classWriter.visitMethod(ACC_PUBLIC + ACC_STATIC, "render", "()V", null, null);//插入方法
methodVisitor.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");//插入字段
methodVisitor.visitLdcInsn("Hello ASM!");//插入LDC
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);//插入方法
methodVisitor.visitMaxs(2, 1);//设置栈和局部变量大小
methodVisitor.visitInsn(RETURN);//插入返回
methodVisitor.visitEnd();//结束
classWriter.visitEnd();//结束
//生成类
try {
FileOutputStream out = new FileOutputStream(Learn2.class.getResource("/cn/enaium/learn/asm/learn2/").getPath() + "Learn2Test.class");
out.write(classWriter.toByteArray());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}