Hi all,,
This is my first question in bcel-mailing list. I have the following problem:

I write a simple code to transform a method code such that it should invoke 
another method providing its parameters as an argument to this new method. for 
example:

// original method 
public void orgMethod(String A, int B, Object C) {...}

//should be transformed to:

public void orgMethod(String A, int B, Object C) 
{
   Object[] args = new Object[]{A, B, C};
   anotherMethod("origMethod", args);
}

The funny thing at this point is that I use the BCEL library to weave the new 
method's code as follow:

////////////////////////////////////////////////////////////////////////////////////////////
LocalVariableGen selfArgs = mg2.addLocalVariable("args", new 
ArrayType(Type.OBJECT, 1), null, null);
                  // mg2 is an MethodGen instance that is initiated from 
origMethod .. :)
                 
 LocalVariableGen for_q = mg2.addLocalVariable("indx", Type.INT, null, null);
 int methodNameIndex = cpg.addString(mg2.getName());
                        il2.append(new ICONST(mg2.getArgumentNames().length));
                        il2.append(new ANEWARRAY(objectType_Index));
                        il2.append(new ASTORE(selfArgs.getIndex()));
                        il2.append(new ICONST(-1));
                        il2.append(new ISTORE(for_q.getIndex()));
                        for(int i = 1; i<= mg2.getArgumentNames().length; i++)
                        {    
                            il2.append(new IINC(for_q.getIndex(), 1));
                            il2.append(new ALOAD(selfArgs.getIndex()));
                            il2.append(new ILOAD(for_q.getIndex()));
                            
il2.append(InstructionFactory.createLoad(mg2.getArgumentTypes()[i-1], i)); 
                            
il2.append(InstructionFactory.createArrayStore(Type.OBJECT));

                        }
                 
                    il2.append(new ALOAD(0));
                    il2.append(new LDC(methodNameIndex));
                    il2.append(new ALOAD(selfArgs.getIndex()));
                    il2.append(ifact.createInvoke(fullQualifiedClassName, 
"anotherMethod", Type.OBJECT, new Type[]{Type.STRING, new 
ArrayType(Type.OBJECT,1)}, Constants.INVOKEVIRTUAL));
////////////////////////////////////////////////////////////////////////////////////////////

which produces a nice code!!.

public void orgMethod(String A, int B, Object C) 

{

   Object[] args = new Object[3];
   int indx = -1;
   indx ++;
   args[indx] = A;
   indx ++;

   args[indx] = B;
   indx ++;

   args[indx] = C;

   anotherMethod("origMethod", args);

}
 But an exception is thrown after the new origMethod.

Exception in thread "main" java.lang.VerifyError: (class: pkg/C, method: 
origMethod signature: (Ljava.lang.String;ILjava.lang.Object;)V) Expecting to 
find object/array on stack

any help will be so much appreciated.
Abdullah




      

Reply via email to