jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joseph Ryan <ryan....@osu.edu>
Subject Re: invokevirtual
Date Wed, 17 Sep 2003 02:58:13 GMT
Torsten Curdt wrote:

> If have an "invokevirtual" Instruction. How can I
> obtain the information which method is actually
> being called?
>
>  InstructionList il = mg.getInstructionList();
>  InstructionHandle[] ihs = il.getInstructionHandles();
>
>  for (int j = 1; j < ihs.length; j++) {
>    Instruction in = ihs[j].getInstruction();
>
>    if (in.getOpcode() == 182) {
>      // invoke virtual
>      // if (in invokes "mymethod") ...
>      System.out.println(in);
>    }
>  }
>  il.dispose(); 


Thats no good; try:

    import org.apache.bcel.generic.INVOKEVIRTUAL;
   
    // ...
   
    if (in instanceof INVOKEVIRTUAL) {
        INVOKEVIRTUAL invv = (INVOKEVIRTUAL) in.getInstruction()
        ConstantPoolGen cp = mg.getConstantPool();
       
        // for more accurate results, check the object on which the 
method is
        // being called's class to see if the object is a subclass of 
the class
        // that you want
        if (   invv.getClassName().equals("...")
            && invv.getMethodName(cp).equals(mymethod)
           ) {
               // ...
         }
    }


- Joe



Mime
View raw message