[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