[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Soot 1.2.2 - How Do I construct a new instance
******
Hi Nathan,
On Thu, 23 Aug 2001, nathan gulley wrote:
> Can you tell me how to use the Soot API to create a new instance of an
> arbitrary class and assign it
> to a previously created local variable of the same type?
>
> The equivalent java code would be: com.ibm.Foo foo = new
com.ibm.Foo();
>
> I already know how to declare the local variable:
>
> Local tempRef = Jimple.v().newLocal
> ("tempRef ", RefType.v("com.ibm.Foo"));
> body.getLocals().add(tempRef );
This does, indeed, add a local variable to your method.
> But how do I instantiate and assign a new object reference to the local
> variable?
You need to create a new statement and to add it to the body. To do
this, you need also to decide where you want them to go in the body.
Creating the statement:
Stmt s = Jimple.v().newAssignStmt (tempRef,
Jimple.v().newNewExpr(RefType.v("com.ibm.Foo"));
Adding the statement to the body:
body.getUnits().addFirst(s);
This will not work, though. s may not precede the identityStmt's at the
beginning of the method. I think there's some method which inserts s at
the proper place. But I don't know what it is.
I've cc'd this message to the soot mailing list, just in case someone
else
was wondering the same thing. I am subscribed to the Soot list and I
will
answer questions there.
pat
************************************
Fri, 24 Aug 2001,
Hello,
Since an AssignStmt may not precede the identityStmt's at the
beginning of the method, I tried adding the new AssignStmt to the end of
the method
using the call sequence:
Local tempRef = Jimple.v().newLocal
("tempRef ", RefType.v("com.ibm.Foo"));
body.getLocals().add(tempRef );
AssignStmt stmt = Jimple.v().newAssignStmt (tempRef,
Jimple.v().newNewExpr(RefType.v("com.ibm.Foo"));
units.insertBefore(stmt , units.getLast());
However, the JVM throws a VerifyException when the classfile is executed:
java.lang.reflect.InvocationTargetException: java.lang.VerifyError:
(class: test
/LoadMe, method: foo signature: (Ljava/lang/String;)V) Expecting to find
object/
array on stack
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Class.java:237)
at test.TestClient.test15(TestClient.java:1009)
at java.lang.reflect.Method.invoke(Native Method)
at test.TestClient.main(TestClient.java:43)
Nathan