jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rodney Mach" <rm...@umich.edu>
Subject How to determine index of local variable if don't know name
Date Mon, 19 Jan 2004 07:31:44 GMT


Hi, I have a question regarding getting the index of a local variable when I
don't know the name of the variable apriori.

 I am going through  looking for certain pattern, in this case every time
the "add" method is called on any ArrayList, what I want to do is after that
line insert some code that modifies the referenced arraylist.

The problem I am having is I don't know how to get the index to the
arraylist being modified if I don't know the variable name apriori (if I
know it then I look through the local variable table, find the index, and
can call ALOAD with that index and everything works fine I did figure out
that much).

What is the best way to get the index? This is basically the code leading up
to where I want to figure out the index of the local variable being modified
when I see the "add" method on a "java.util.ArrayList":

InstructionList il = methodGen.getInstructionList();


InstructionHandle[] ihs = il.getInstructionHandles();


for (int j = 1; j < ihs.length; j++) {

Instruction ins = ihs[j].getInstruction();


if (ins instanceof INVOKEVIRTUAL) {




//Now look to see if this virtual instruction modifies array

INVOKEVIRTUAL invv = (INVOKEVIRTUAL) ins;


if (invv.getClassName(cpg).equals("java.util.ArrayList") &&
invv.getMethodName(cpg).equals("add")) {

***********************

//HERE: HOW DO I GET INDEX TO THE ARRAYLIST BEING MODIFIED IF I DON't KNOW
VARIABLE NAME APRIORI? I want to get the index so I can modify the arraylist
at this point

int HOWDOIGETTHISINDEX = ????

*************************

InstructionList il2 = new InstructionList();

il2.append(factory.createNew("MyNewInstrument"));
il2.append(InstructionConstants.DUP);

il2.append(factory.createInvoke("MyNewInstrument","<init>",Type.VOID,Type.NO
_ARGS,Constants.INVOKESPECIAL));


LocalVariableGen lg = methodGen.addLocalVariable("instrument",new
 ObjectType("MyNewInstrument"),null,null);

int index = lg.getIndex();
lg.setStart(il2.append(new ASTORE(index)));

//Load the local variable we just created, from the index.
il2.append(new ALOAD(index));

//Load the index to the array list that we want to modify. How to get
index??
il2.append(new ALOAD(HOWDOIGETTHISINDEX));

il2.append(factory.createInvoke("MyNewInstrument","modify", new
ObjectType("java.util.ArrayList") , new Type[] { new
ObjectType("java.util.ArrayList") } ,Constants.INVOKEVIRTUAL));


//Etc etc


}



Any help/pointers would be appreciated.

Thanks!

-Rod





---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org


Mime
View raw message