You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by Rodney Mach <rm...@umich.edu> on 2004/01/19 08:31:44 UTC
How to determine index of local variable if don't know name
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
Re: How to determine index of local variable if don't know name
Posted by Laurent Martelli <la...@aopsys.com>.
>>>>> "Rodney" == Rodney Mach <rm...@umich.edu> writes:
Rodney> Hi, I have a question regarding getting the index of a local
Rodney> variable when I don't know the name of the variable apriori.
Rodney> I am going through looking for certain pattern, in this
Rodney> case every time the "add" method is called on any ArrayList,
Rodney> what I want to do is after that line insert some code that
Rodney> modifies the referenced arraylist.
Rodney> The problem I am having is I don't know how to get the index
Rodney> to the arraylist being modified if I don't know the variable
Rodney> name apriori (if I know it then I look through the local
Rodney> variable table, find the index, and can call ALOAD with that
Rodney> index and everything works fine I did figure out that much).
Maybe you'd better insert a DUP before the call to add so that you can
get a reference to the list. And it will work even if the list is't
referenced by a local variable.
--
Laurent Martelli
laurent@aopsys.com Java Aspect Components
http://www.aopsys.com/ http://jac.aopsys.com
---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org