开发者

Asm bytecode queries

开发者 https://www.devze.com 2023-02-26 07:19 出处:网络
Hey all, I am trying to use the ASM bytecode Tree Api to do static analysis for a class. I guess I have a pretty basic question. In a method say foobar(), I have a list of instructions within foobar (

Hey all, I am trying to use the ASM bytecode Tree Api to do static analysis for a class. I guess I have a pretty basic question. In a method say foobar(), I have a list of instructions within foobar (InsnList which has a List). Now I want to check if at instruction number 10, whether a function "barfoo(String args)" is being invoked.

Also seperatly, I need to verify whether a particular instruction is a conditional.

Thanks and Regards, SJ

Note: I can already read a class and reach the particular method I am interested in and iterate through each intruction of that method.

Solved (see: Greg's comments):

    AbstractInsnNode s = ...
    if(s.getType()==AbstractInsnNode.METHOD_INSN){
        MethodInsnNode methodInvocationNode = (MethodInsnNode) s;
        if(methodInvocationNode.name.equals("barfoo"开发者_如何学C))
        {
            return true;
        }
    }


From looking at the ASM javadoc, it looks like you'll have to call getType() on your instruction node, and if the type is METHOD_INSN then you can cast to a MethodInsnNode. From there, look at .name to see the method name.


Simple - once you've determined that you've invoked foobar, start incrementing a counter on each instruction you visit. On the tenth invocation, perform your check for the invocation of barfoo.

0

精彩评论

暂无评论...
验证码 换一张
取 消