1

I am trying to determine the MethodGen of the callee for a given InvokeInstruction in the BCEL library. The problem is that I don't know how to use the InvokeInstruction to get to the MethodGen that it is trying to invoke.

If I have a BCEL MethodGen object for the main method of a program, I can go through the list of instructions and find the ones that are InvokeInstructions:

// Assume MethodGen mainMG is given to us
Instruction[] insns = mainMG.getInstructionList().getInstructions();
for(Instruction insn : insns) {
    if(insn instanceof InvokeInstruction) {
        // great, found an invoke instruction
        InvokeInstruction invoke = (InvokeInstruction)insn;

        // what do I do with it now?
    }
}

Some of BCEL's documentation is great and other parts are kind of lacking. Any suggestions for how to link an InvokeInstruction to the MethodGen of the method being invoked?

If it simplifies things, I can assume for now that the program doesn't have any polymorphism. Though at some point I will have to deal with it (conservatively).


Clarification: I realize there isn't a direct route for doing this (e.g. invoke.getCalledMethodGen()), but I am wondering if there is some way that I can get enough distinct information from the invoke instruction (e.g. method's FQN or equiv.) that I can link it back to the method being called.
jbranchaud
  • 5,909
  • 9
  • 45
  • 70

1 Answers1

0

Generally you can't. BCEL and most of other frameworks for working with bytecode operating on a single class. So, you will have read all available classes (could do that lazily) and build your own repository of MethodGens (e.g. map of FQN method name to MethodGen instances).

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
  • Based on what I have so far, I could pretty easily build that map, I guess part of what I was hoping to find out is if there is a way to get at the method's FQN. – jbranchaud Jun 07 '12 at 18:40
  • Try invoke.getType(cpg).getSignature() + "." + invoke.getSignature(cpg) – Eugene Kuleshov Jun 07 '12 at 18:53
  • These seem to give me information about the return type of the particular method call. However, `invoke.getName(cpGen);` gives me the name of the method called. Now I just need to narrow down what the class containing the method might be. – jbranchaud Jun 07 '12 at 20:17
  • Right, getReferenceType() should return the owner. You also have to take into account method arguments when building FQN for given method. – Eugene Kuleshov Jun 07 '12 at 20:29
  • @EugeneKuleshov I am trying to find the method that is being called by an instruction. In other words, I need to extract the Method. How can I do that? – Mathew Kurian Mar 24 '14 at 17:11