Search in sources :

Example 1 with GAddExpr

use of soot.grimp.internal.GAddExpr in project soot by Sable.

the class NewStringBufferSimplification method inExprOrRefValueBox.

public void inExprOrRefValueBox(ValueBox argBox) {
    if (DEBUG)
        System.out.println("ValBox is: " + argBox.toString());
    Value tempArgValue = argBox.getValue();
    if (DEBUG)
        System.out.println("arg value is: " + tempArgValue);
    if (!(tempArgValue instanceof DVirtualInvokeExpr)) {
        if (DEBUG)
            System.out.println("Not a DVirtualInvokeExpr" + tempArgValue.getClass());
        return;
    }
    // check this is a toString for StringBuffer
    if (DEBUG)
        System.out.println("arg value is a virtual invokeExpr");
    DVirtualInvokeExpr vInvokeExpr = ((DVirtualInvokeExpr) tempArgValue);
    // need this try catch since DavaStmtHandler expr will not have a "getMethod"
    try {
        if (!(vInvokeExpr.getMethod().toString().equals("<java.lang.StringBuffer: java.lang.String toString()>")))
            return;
    } catch (Exception e) {
        return;
    }
    if (DEBUG)
        System.out.println("Ends in toString()");
    Value base = vInvokeExpr.getBase();
    List args = new ArrayList();
    while (base instanceof DVirtualInvokeExpr) {
        DVirtualInvokeExpr tempV = (DVirtualInvokeExpr) base;
        if (DEBUG)
            System.out.println("base method is " + tempV.getMethod());
        if (!tempV.getMethod().toString().startsWith("<java.lang.StringBuffer: java.lang.StringBuffer append")) {
            if (DEBUG)
                System.out.println("Found a virtual invoke which is not a append" + tempV.getMethod());
            return;
        }
        args.add(0, tempV.getArg(0));
        // System.out.println("Append: "+((DVirtualInvokeExpr)base).getArg(0) );
        // move to next base
        base = ((DVirtualInvokeExpr) base).getBase();
    }
    if (!(base instanceof DNewInvokeExpr))
        return;
    if (DEBUG)
        System.out.println("New expr is " + ((DNewInvokeExpr) base).getMethod());
    if (!((DNewInvokeExpr) base).getMethod().toString().equals("<java.lang.StringBuffer: void <init>()>"))
        return;
    /*
    	 * The arg is a new invoke expr of StringBuffer and all the appends are present in the args list
    	 */
    if (DEBUG)
        System.out.println("Found a new StringBuffer.append list in it");
    // argBox contains the new StringBuffer
    Iterator it = args.iterator();
    Value newVal = null;
    while (it.hasNext()) {
        Value temp = (Value) it.next();
        if (newVal == null)
            newVal = temp;
        else {
            // create newVal + temp
            newVal = new GAddExpr(newVal, temp);
        }
    }
    if (DEBUG)
        System.out.println("New expression for System.out.println is" + newVal);
    argBox.setValue(newVal);
}
Also used : DVirtualInvokeExpr(soot.dava.internal.javaRep.DVirtualInvokeExpr) DNewInvokeExpr(soot.dava.internal.javaRep.DNewInvokeExpr) GAddExpr(soot.grimp.internal.GAddExpr) Value(soot.Value) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Value (soot.Value)1 DNewInvokeExpr (soot.dava.internal.javaRep.DNewInvokeExpr)1 DVirtualInvokeExpr (soot.dava.internal.javaRep.DVirtualInvokeExpr)1 GAddExpr (soot.grimp.internal.GAddExpr)1