如何在使用Asm库进行检测的方法中找到空的局部变量 [英] How to find an empty local variable in a method for instrumenting using asm library

查看:88
本文介绍了如何在使用Asm库进行检测的方法中找到空的局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为一个类的不同方法进行检测时,为了使方法在文本文件中执行写操作。我首先将字符串存储在明确定义的局部变量3160中。如何选择这些变量以防止与现有变量冲突。

While instrumenting a class for its different methods In order to make a method do a write operation in a text file. I first stored the string in a local variable 3160 explicitly defined. How to choose these variables to prevent conflicts with already existing variables.

在此代码段中,该代码完成每次将类名写入文本文件的工作任何方法。为此,必须使用变量3160将字符串s加载到堆栈上(值应保持较大,以便已定义的变量名称与变量s不冲突(3160)。我的问题是如何在方法中定义局部变量

Like in this snippet The code does the work of writing the class name to a text file every time it enters any method. In order to do so the string s had to be loaded on stack using variable 3160 ( value kept large so that the already defined variable names do not conflict with variable s (3160). My Question is how to define a local variables in a method during instrumentation with ASM Library. This question may seem kind of premature to many but that is because I am a beginner.

    String s= className;
    mv.visitLdcInsn(s);
    mv.visitVarInsn(Opcodes.ASTORE, 3160);
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder");
    mv.visitInsn(Opcodes.DUP);
    mv.visitVarInsn(Opcodes.ALOAD, 3160);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "valueOf", "(Ljava/lang/Object;)Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V");
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/me/database/dataCollectionFile/Info", "callMeAnyTime", "()Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");


推荐答案

您应使用 LocalVariablesSorter 适配器(从中扩展您自己的访客,或将其添加到在MethodWriter之前的访问者链)。然后,当您需要新变量时,可以调用 LocalVariablesSorter.newLocal()方法来获取分配的新变量插槽。另请参阅 ASM指南

You should use LocalVariablesSorter adapter (either extend your own visitor from it, or add it to the visitors chain before MethodWriter). Then when you'll need a new variable you can call LocalVariablesSorter.newLocal() method to get new variable slot allocated. Also see ASM guide for more details.

这篇关于如何在使用Asm库进行检测的方法中找到空的局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆