[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Method Dispatch



On Wed, 4 Dec 2002, Christopher Howell wrote:

> I was wondering how I would take an object that I already have found has a
> clone method and attach the clone method to it.

> 	AssignStmt assign_statement = (JAssignStmt)next_statement;
> 	  Value statement_rhs = assign_statement.getRightOp();
> 	  Type type = statement_rhs.getType();

> String stryingtype = type.toString();
> Scene.v().getSootClass(stringtype);

> SootMethod sootmethod = sootclass.getMethodByName("clone");

> To Sum up, is there anyway to take get an Object from an AssignStmt and
> attach a .clone() to it?

Ok, you're almost there.  You're better off creating a new AssignStmt,
because the old AssignStmt may have some expression on the RHS which is
not simple (e.g. x = o.f()) which would generate x = o.f().clone(), and
that would violate the Jimple 3-address code property.

You want to create a new Local and addLocal() it.  I think there's example
code to find a unique name.

    Local l = new-local-creation-code(...);
    body.addLocal(l);

    Value lhs = assign_statement.getLeftOp();

    assign_statement.setLeftOp(l);
    Stmt new_assign_stmt = Jimple.v().newAssignStmt(lhs,
            Jimple.v().newVirtualInvokeExpr(l, sootmethod, new
                                            LinkedList()));

    body.getUnits().insertAfter(assign_statement, new_assign_stmt);

If you just go and look up the code for getting a fresh local name,
you should be all set.  It doesn't actually technically need to have a
fresh name, but you really should give it a fresh name to avoid confusion
while looking at the Jimple source after transformation.

pat