Re: How to replace a complete instruction in J2ME Bytecode

Arrin Daley <[email protected]> Tue, 10 Feb 2009 12:32:16 +1100
Newsgroups gmane.comp.jakarta.bcel.user
Message-ID <[email protected]>
Hi Klaus

You'll need to find the INVOKESTATIC instruction first, if you have an 
idea which methods and classes the call(s) exist in this will help, 
InstructionFinder is about finding a pattern of instructions you could 
use it to find INVOKESTATIC instructions but that would probably be 
overkill, you could just use an iterator or something similar.

Once you have found the INVOKESTATIC instructions you could then test to 
see which one(s) match your function call, you could do this by looking 
at the classname and method name and possibly arguments the other way 
would be to look up the index for the method in the constant pool (if it 
exists in the constant pool, don't put it in if it doesn't) and compare 
this index to that of the INVOKESTATIC instruction which you have found. 
If the static method doesn't exist in the constant pool then there isn't 
a call to that static method in the code your currently looking at so 
you could move on to the next method to search.

Once you find an INVOKESTATIC that matches you need to put your new 
static method in the constant pool via a ConstantPoolGen, this will 
return an index which you can then use with the setIndex method on the 
INVOKESTATIC instruction. You can use the setIndex because you are just 
changing the target of INVOKESTATIC instructions, otherwise you would 
have to replace the instruction itself.

I'm not sure what other finalising things you will need to do, the 
ConstantPool, ConstantPoolGen  will have changed so will need to 
finalise that, and you will have to get a Method from the MethodGen 
object you previously had but I think that's about all...

Another way might be to define an InstructionVisitor overriding the 
visit method for INVOKESTATIC to find and edit the appropriate instructions.

Hope it helps

Bye Arrin

Klaus Teller wrote:
> Hi Folks,
>
> I have a J2ME Library that contains the following instruction:
>
> invokestatic javax/microedition/io/Connector/open(Ljava/lang/String;IZ)Ljavax/microedition/io/Connection;
>
>
> I would like to replace it with:
>
> invokestatic mypackage/AlphaConnector/open(Ljava/lang/String;IZ)Ljavax/microedition/io/Connection;
>
> What do i need to do?
>
> I couldn't find a way to search instruction based on the operand (that is the second part of the instruction). I tried IntructionFinder; but it couldn't help me. 
>
> I would very much appreciate any input.
>
> Thanks,
> Klaus.
>
>