美文网首页
25 - ASM之Label实现for语句

25 - ASM之Label实现for语句

作者: 舍是境界 | 来源:发表于2022-02-04 07:45 被阅读0次

    预期目标

    public class HelloWorld {
        public void test() {
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
            }
        }
    }
    

    编码实现

    import lsieun.utils.FileUtils;
    import org.objectweb.asm.*;
    
    import static org.objectweb.asm.Opcodes.*;
    
    public class HelloWorldGenerateCore {
        public static void main(String[] args) throws Exception {
            String relative_path = "sample/HelloWorld.class";
            String filepath = FileUtils.getFilePath(relative_path);
    
            // (1) 生成byte[]内容
            byte[] bytes = dump();
    
            // (2) 保存byte[]到文件
            FileUtils.writeBytes(filepath, bytes);
        }
    
        public static byte[] dump() throws Exception {
            // (1) 创建ClassWriter对象
            ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    
            // (2) 调用visitXxx()方法
            cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "sample/HelloWorld",
                    null, "java/lang/Object", null);
    
            {
                MethodVisitor mv1 = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
                mv1.visitCode();
                mv1.visitVarInsn(ALOAD, 0);
                mv1.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
                mv1.visitInsn(RETURN);
                mv1.visitMaxs(0, 0);
                mv1.visitEnd();
            }
    
            {
                MethodVisitor methodVisitor = cw.visitMethod(ACC_PUBLIC, "test", "()V", null, null);
                Label conditionLabel = new Label();
                Label returnLabel = new Label();
    
                // 第1段
                methodVisitor.visitCode();
                methodVisitor.visitInsn(ICONST_0);
                methodVisitor.visitVarInsn(ISTORE, 1);
    
                // 第2段
                methodVisitor.visitLabel(conditionLabel);
                methodVisitor.visitVarInsn(ILOAD, 1);
                methodVisitor.visitIntInsn(BIPUSH, 10);
                methodVisitor.visitJumpInsn(IF_ICMPGE, returnLabel);
                methodVisitor.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
                methodVisitor.visitVarInsn(ILOAD, 1);
                methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(I)V", false);
                methodVisitor.visitIincInsn(1, 1);
                methodVisitor.visitJumpInsn(GOTO, conditionLabel);
    
                // 第3段
                methodVisitor.visitLabel(returnLabel);
                methodVisitor.visitInsn(RETURN);
                methodVisitor.visitMaxs(0, 0);
                methodVisitor.visitEnd();
            }
    
            cw.visitEnd();
    
            // (3) 调用toByteArray()方法
            return cw.toByteArray();
        }
    }
    

    验证结果

    import java.lang.reflect.Method;
    
    public class HelloWorldRun {
        public static void main(String[] args) throws Exception {
            Class<?> clazz = Class.forName("sample.HelloWorld");
            Object obj = clazz.newInstance();
    
            Method method = clazz.getDeclaredMethod("test");
            method.invoke(obj);
        }
    }
    

    小结

    本文通过示例我们学习到了如何通过Label类来实现for语句,希望对你能有所帮助。

    for循环是非常常用的流程控制方式,在代码实现中非常重要,本文通过示例展示了如何生成for循环语句,大家可以加以练习,熟练掌握

    相关文章

      网友评论

          本文标题:25 - ASM之Label实现for语句

          本文链接:https://www.haomeiwen.com/subject/ualvhrtx.html