Search in sources :

Example 1 with DIntConstant

use of soot.dava.internal.javaRep.DIntConstant in project soot by Sable.

the class SuperFirstStmtHandler method createNewASTConstructor.

public void createNewASTConstructor(ASTStatementSequenceNode initNode) {
    List<Object> newConstructorBody = new ArrayList<Object>();
    List<AugmentedStmt> newStmts = new ArrayList<AugmentedStmt>();
    /*
		 * add any definitions to live variables that might be in body X
		 */
    // we have gotten argsTwoType size() out of the handler so thats the
    // index count
    // mustInitialize has the live variables that need to be initialized
    // create new ReftType for DavaSuperHandler
    RefType type = (new SootClass("DavaSuperHandler")).getType();
    // make JimpleLocal to be used in each arg
    // takes care of
    Local jimpleLocal = new JimpleLocal("handler", type);
    // handler
    // make reference to a method of name get takes one int arg belongs to
    // DavaSuperHandler
    ArrayList tempList = new ArrayList();
    tempList.add(IntType.v());
    SootMethodRef getMethodRef = makeMethodRef("get", tempList);
    // Iterator typeIt = argsTwoTypes.iterator();
    if (mustInitialize != null) {
        Iterator<Local> initIt = mustInitialize.iterator();
        while (initIt.hasNext()) {
            Local initLocal = initIt.next();
            Type tempType = initLocal.getType();
            // takes
            DIntConstant arg = DIntConstant.v(mustInitializeIndex, IntType.v());
            // care
            // of
            // the
            // index
            mustInitializeIndex++;
            ArrayList tempArgList = new ArrayList();
            tempArgList.add(arg);
            DVirtualInvokeExpr tempInvokeExpr = new DVirtualInvokeExpr(jimpleLocal, getMethodRef, tempArgList, new HashSet<Object>());
            // NECESASARY CASTING OR RETRIEVAL OF PRIM TYPES TO BE DONE HERE
            Value toAddExpr = getProperCasting(tempType, tempInvokeExpr);
            if (toAddExpr == null)
                throw new DecompilationException("UNABLE TO CREATE TOADDEXPR:" + tempType);
            // need to create a def stmt with the local on the left and
            // toAddExpr on the right
            GAssignStmt assign = new GAssignStmt(initLocal, toAddExpr);
            newStmts.add(new AugmentedStmt(assign));
        }
    }
    // add any statements following the this.<init> statement
    Iterator<AugmentedStmt> it = initNode.getStatements().iterator();
    while (it.hasNext()) {
        AugmentedStmt augStmt = (AugmentedStmt) it.next();
        Stmt stmtTemp = augStmt.get_Stmt();
        if (stmtTemp == originalConstructorUnit) {
            break;
        }
    }
    while (it.hasNext()) {
        /*
			 * notice we dont need to clone these because these will be removed
			 * from the other method from which we are copying these
			 */
        newStmts.add(it.next());
    }
    if (newStmts.size() > 0) {
        newConstructorBody.add(new ASTStatementSequenceNode(newStmts));
    }
    // adding body Y now
    List<Object> originalASTMethodSubBodies = originalASTMethod.get_SubBodies();
    if (originalASTMethodSubBodies.size() != 1)
        throw new CorruptASTException("size of ASTMethodNode subBody not 1");
    List<Object> oldASTBody = (List<Object>) originalASTMethodSubBodies.get(0);
    Iterator<Object> itOld = oldASTBody.iterator();
    boolean sanity = false;
    while (itOld.hasNext()) {
        // going through originalASTMethodNode's ASTNodes
        ASTNode tempNode = (ASTNode) itOld.next();
        // enter only if its not the initNode
        if (tempNode instanceof ASTStatementSequenceNode) {
            if ((((ASTStatementSequenceNode) tempNode).getStatements()).equals(initNode.getStatements())) {
                sanity = true;
                break;
            }
        }
    }
    if (!sanity) {
        // means we never found the initNode which shouldnt happen
        throw new DecompilationException("never found the init node");
    }
    // Y are all the nodes following the initNode
    while (itOld.hasNext()) {
        newConstructorBody.add(itOld.next());
    }
    // setDeclarations in newNode
    // The LocalVariableCleaner which is called in the end of DavaBody will
    // clear up any declarations that are not required
    List<AugmentedStmt> newConstructorDeclarations = new ArrayList<AugmentedStmt>();
    for (AugmentedStmt as : originalASTMethod.getDeclarations().getStatements()) {
        DVariableDeclarationStmt varDecStmt = (DVariableDeclarationStmt) as.get_Stmt();
        newConstructorDeclarations.add(new AugmentedStmt((DVariableDeclarationStmt) varDecStmt.clone()));
    }
    ASTStatementSequenceNode newDecs = new ASTStatementSequenceNode(new ArrayList<AugmentedStmt>());
    if (newConstructorDeclarations.size() > 0) {
        newDecs = new ASTStatementSequenceNode(newConstructorDeclarations);
        // DONT FORGET TO SET THE DECLARATIONS IN THE METHOD ONCE IT IS
        // CREATED
        // newASTConstructorMethod.setDeclarations(newDecs);
        // declarations are always the first element
        newConstructorBody.add(0, newDecs);
    }
    // so we have any declarations followed by body Y
    // have to put the newConstructorBody into an list of subBodies which
    // goes into the newASTConstructorMethod
    newASTConstructorMethod = new ASTMethodNode(newConstructorBody);
    // dont forget to set the declarations
    newASTConstructorMethod.setDeclarations(newDecs);
}
Also used : DVariableDeclarationStmt(soot.dava.internal.javaRep.DVariableDeclarationStmt) CorruptASTException(soot.dava.CorruptASTException) ArrayList(java.util.ArrayList) DecompilationException(soot.dava.DecompilationException) ASTStatementSequenceNode(soot.dava.internal.AST.ASTStatementSequenceNode) JimpleLocal(soot.jimple.internal.JimpleLocal) GReturnStmt(soot.grimp.internal.GReturnStmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) DVariableDeclarationStmt(soot.dava.internal.javaRep.DVariableDeclarationStmt) GInvokeStmt(soot.grimp.internal.GInvokeStmt) Stmt(soot.jimple.Stmt) GAssignStmt(soot.grimp.internal.GAssignStmt) DefinitionStmt(soot.jimple.DefinitionStmt) RefType(soot.RefType) DVirtualInvokeExpr(soot.dava.internal.javaRep.DVirtualInvokeExpr) ASTNode(soot.dava.internal.AST.ASTNode) List(java.util.List) ArrayList(java.util.ArrayList) ASTMethodNode(soot.dava.internal.AST.ASTMethodNode) GAssignStmt(soot.grimp.internal.GAssignStmt) SootMethodRef(soot.SootMethodRef) JimpleLocal(soot.jimple.internal.JimpleLocal) Local(soot.Local) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) SootClass(soot.SootClass) RefType(soot.RefType) ShortType(soot.ShortType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) Type(soot.Type) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) CharType(soot.CharType) LongType(soot.LongType) PrimType(soot.PrimType) VoidType(soot.VoidType) DIntConstant(soot.dava.internal.javaRep.DIntConstant) Value(soot.Value)

Example 2 with DIntConstant

use of soot.dava.internal.javaRep.DIntConstant in project soot by Sable.

the class SuperFirstStmtHandler method createCallToSuper.

/*
	 * super( (CAST-TYPE)handler.get(0),
	 * ((CAST-TYPE)handler.get(1)).CONVERSION(), .......);
	 * 
	 * returns false if we cant create the call to super
	 */
private boolean createCallToSuper() {
    // check that whether this call is even to be made or not
    if (originalConstructorExpr == null) {
        // System.out.println("originalConstructorExpr is null");
        return false;
    }
    // System.out.println("ConstructorExpr is non null...call to super has
    // to be made");
    // find the parent class of the current method being decompiled
    SootClass parentClass = originalSootClass.getSuperclass();
    // super method
    if (!(parentClass.declaresMethod("<init>", argsTwoTypes))) {
        // this name and ParamTypes");
        return false;
    }
    SootMethod superConstructor = parentClass.getMethod("<init>", argsTwoTypes);
    // create InstanceInvokeExpr
    // need Value base: this??
    // need SootMethod Ref...make sootmethoref of the super constructor
    // found
    // need list of thisLocals.........try empty arrayList since it doesnt
    // seem to be used anywhere??
    List argsForConstructor = new ArrayList();
    int count = 0;
    // have to make arg as "handler.get(0)"
    // create new ReftType for DavaSuperHandler
    RefType type = (new SootClass("DavaSuperHandler")).getType();
    // make JimpleLocal to be used in each arg
    // takes care of
    Local jimpleLocal = new JimpleLocal("handler", type);
    // handler
    // make reference to a method of name get takes one int arg belongs to
    // DavaSuperHandler
    ArrayList tempList = new ArrayList();
    tempList.add(IntType.v());
    SootMethodRef getMethodRef = makeMethodRef("get", tempList);
    List tempArgList = null;
    Iterator typeIt = argsTwoTypes.iterator();
    while (typeIt.hasNext()) {
        Type tempType = (Type) typeIt.next();
        // takes care
        DIntConstant arg = DIntConstant.v(count, IntType.v());
        // of the
        // index
        count++;
        tempArgList = new ArrayList();
        tempArgList.add(arg);
        DVirtualInvokeExpr tempInvokeExpr = new DVirtualInvokeExpr(jimpleLocal, getMethodRef, tempArgList, new HashSet<Object>());
        // NECESASARY CASTING OR RETRIEVAL OF PRIM TYPES TO BE DONE HERE
        Value toAddExpr = getProperCasting(tempType, tempInvokeExpr);
        if (toAddExpr == null)
            throw new DecompilationException("UNABLE TO CREATE TOADDEXPR:" + tempType);
        // the above virtualInvokeExpr is one of the args for the
        // constructor
        argsForConstructor.add(toAddExpr);
    }
    mustInitializeIndex = count;
    // we are done with creating all necessary args to the virtualinvoke
    // expr constructor
    DVirtualInvokeExpr virtualInvoke = new DVirtualInvokeExpr(originalConstructorExpr.getBase(), superConstructor.makeRef(), argsForConstructor, new HashSet<Object>());
    // set the constructors constructorExpr
    newConstructorDavaBody.set_ConstructorExpr(virtualInvoke);
    // create Invoke Stmt with virtualInvoke as the expression
    GInvokeStmt s = new GInvokeStmt(virtualInvoke);
    newConstructorDavaBody.set_ConstructorUnit(s);
    // return true if super call created
    return true;
}
Also used : SootMethodRef(soot.SootMethodRef) ArrayList(java.util.ArrayList) JimpleLocal(soot.jimple.internal.JimpleLocal) Local(soot.Local) DecompilationException(soot.dava.DecompilationException) SootClass(soot.SootClass) JimpleLocal(soot.jimple.internal.JimpleLocal) RefType(soot.RefType) DVirtualInvokeExpr(soot.dava.internal.javaRep.DVirtualInvokeExpr) RefType(soot.RefType) ShortType(soot.ShortType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) Type(soot.Type) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) CharType(soot.CharType) LongType(soot.LongType) PrimType(soot.PrimType) VoidType(soot.VoidType) DIntConstant(soot.dava.internal.javaRep.DIntConstant) GInvokeStmt(soot.grimp.internal.GInvokeStmt) Iterator(java.util.Iterator) Value(soot.Value) SootMethod(soot.SootMethod) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 BooleanType (soot.BooleanType)2 ByteType (soot.ByteType)2 CharType (soot.CharType)2 DoubleType (soot.DoubleType)2 FloatType (soot.FloatType)2 IntType (soot.IntType)2 Local (soot.Local)2 LongType (soot.LongType)2 PrimType (soot.PrimType)2 RefType (soot.RefType)2 ShortType (soot.ShortType)2 SootClass (soot.SootClass)2 SootMethodRef (soot.SootMethodRef)2 Type (soot.Type)2 Value (soot.Value)2 VoidType (soot.VoidType)2 DecompilationException (soot.dava.DecompilationException)2 DIntConstant (soot.dava.internal.javaRep.DIntConstant)2