Search in sources :

Example 11 with ASTNode

use of soot.dava.internal.AST.ASTNode in project soot by Sable.

the class EliminateConditions method eliminate.

public Boolean eliminate(ASTNode node) {
    ASTCondition cond = null;
    if (node instanceof ASTControlFlowNode)
        cond = ((ASTControlFlowNode) node).get_Condition();
    else
        return null;
    if (cond == null || !(cond instanceof ASTUnaryCondition))
        return null;
    ASTUnaryCondition unary = (ASTUnaryCondition) cond;
    Value unaryValue = unary.getValue();
    boolean notted = false;
    if (unaryValue instanceof DNotExpr) {
        notted = true;
        unaryValue = ((DNotExpr) unaryValue).getOp();
    }
    Boolean isBoolean = isBooleanConstant(unaryValue);
    if (isBoolean == null) {
        // not a constant
        return null;
    }
    boolean trueOrFalse = isBoolean.booleanValue();
    if (notted) {
        // since it is notted we reverse the booleans
        trueOrFalse = !trueOrFalse;
    }
    AST.apply(finder);
    Object temp = finder.getParentOf(node);
    if (temp == null)
        return null;
    ASTNode parent = (ASTNode) temp;
    List<Object> subBodies = parent.get_SubBodies();
    Iterator<Object> it = subBodies.iterator();
    int index = -1;
    while (it.hasNext()) {
        bodyContainingNode = (List<Object>) it.next();
        index = bodyContainingNode.indexOf(node);
        if (index < 0) {
            bodyContainingNode = null;
        } else {
            // bound the body containing Node
            return new Boolean(trueOrFalse);
        }
    }
    return null;
}
Also used : ASTControlFlowNode(soot.dava.internal.AST.ASTControlFlowNode) DNotExpr(soot.dava.internal.javaRep.DNotExpr) Value(soot.Value) ASTNode(soot.dava.internal.AST.ASTNode) ASTUnaryCondition(soot.dava.internal.AST.ASTUnaryCondition) ASTCondition(soot.dava.internal.AST.ASTCondition)

Example 12 with ASTNode

use of soot.dava.internal.AST.ASTNode in project soot by Sable.

the class EliminateConditions method caseASTTryNode.

public void caseASTTryNode(ASTTryNode node) {
    modified = false;
    inASTTryNode(node);
    // get try body iterator
    Iterator<Object> it = node.get_TryBody().iterator();
    Boolean returned = null;
    ASTNode temp = null;
    while (it.hasNext()) {
        temp = (ASTNode) it.next();
        // only check condition if this is a control flow node
        if (temp instanceof ASTControlFlowNode) {
            bodyContainingNode = null;
            returned = eliminateForTry(temp);
            if (returned != null && canChange(returned, temp))
                break;
            else
                bodyContainingNode = null;
        }
        temp.apply(this);
    }
    // end while
    boolean changed = change(returned, temp);
    if (changed)
        modified = true;
    // get catch list and apply on the following
    // a, type of exception caught ......... NO NEED
    // b, local of exception ............... NO NEED
    // c, catchBody
    List<Object> catchList = node.get_CatchList();
    Iterator itBody = null;
    it = catchList.iterator();
    while (it.hasNext()) {
        ASTTryNode.container catchBody = (ASTTryNode.container) it.next();
        List body = (List) catchBody.o;
        itBody = body.iterator();
        returned = null;
        temp = null;
        // go over the ASTNodes and apply
        while (itBody.hasNext()) {
            temp = (ASTNode) itBody.next();
            // only check condition if this is a control flow node
            if (temp instanceof ASTControlFlowNode) {
                bodyContainingNode = null;
                returned = eliminateForTry(temp);
                if (returned != null && canChange(returned, temp))
                    break;
                else
                    bodyContainingNode = null;
            }
            temp.apply(this);
        }
        changed = change(returned, temp);
        if (changed)
            modified = true;
    }
    outASTTryNode(node);
    if (modified) {
        // repeat the whole thing
        caseASTTryNode(node);
    }
}
Also used : ASTControlFlowNode(soot.dava.internal.AST.ASTControlFlowNode) ASTTryNode(soot.dava.internal.AST.ASTTryNode) ASTNode(soot.dava.internal.AST.ASTNode) Iterator(java.util.Iterator) List(java.util.List)

Example 13 with ASTNode

use of soot.dava.internal.AST.ASTNode in project soot by Sable.

the class EliminateConditions method dealWithSwitchNode.

public void dealWithSwitchNode(ASTSwitchNode node) {
    List<Object> indexList = node.getIndexList();
    Map<Object, List<Object>> index2BodyList = node.getIndex2BodyList();
    Iterator<Object> it = indexList.iterator();
    while (it.hasNext()) {
        // going through all the cases of the switch statement
        Object currentIndex = it.next();
        List body = index2BodyList.get(currentIndex);
        if (body != null) {
            // this body is a list of ASTNodes
            Iterator itBody = body.iterator();
            Boolean returned = null;
            ASTNode temp = null;
            while (itBody.hasNext()) {
                temp = (ASTNode) itBody.next();
                if (temp instanceof ASTControlFlowNode) {
                    bodyContainingNode = null;
                    returned = eliminate(temp);
                    if (returned != null && canChange(returned, temp))
                        break;
                    else
                        bodyContainingNode = null;
                }
                temp.apply(this);
            }
            boolean changed = change(returned, temp);
            if (changed)
                modified = true;
        }
    // end while changed
    }
}
Also used : ASTControlFlowNode(soot.dava.internal.AST.ASTControlFlowNode) Iterator(java.util.Iterator) ASTNode(soot.dava.internal.AST.ASTNode) List(java.util.List)

Example 14 with ASTNode

use of soot.dava.internal.AST.ASTNode in project soot by Sable.

the class SuperFirstStmtHandler method finalizePreInitMethod.

// method should return false if the PreInit body(ASTBody that is) is empty
// meaning there is no need to create it all
private boolean finalizePreInitMethod() {
    // set davaBody...totally redundant but have to do as this is checked by
    // toString of ASTMethodNode
    newASTPreInitMethod.setDavaBody(newPreInitDavaBody);
    // newPreInitDavaBody is the active body
    newPreInitDavaBody.getUnits().clear();
    newPreInitDavaBody.getUnits().addLast(newASTPreInitMethod);
    // check whether there is something in side the ASTBody
    // if its empty (maybe there were only declarations and that got removed
    // then no point in creating the preInit method
    List<Object> subBodies = newASTPreInitMethod.get_SubBodies();
    if (subBodies.size() != 1)
        return false;
    List body = (List) subBodies.get(0);
    // body is NOT allowed to contain one declaration node with whatever in
    // it
    // after that it is NOT allowed all ASTStatement nodes with empty bodies
    Iterator it = body.iterator();
    // indicating that method is empty
    boolean empty = true;
    while (it.hasNext()) {
        ASTNode tempNode = (ASTNode) it.next();
        if (!(tempNode instanceof ASTStatementSequenceNode)) {
            // found some node other than stmtseq...body not empty return
            // true
            empty = false;
            break;
        }
        List<AugmentedStmt> stmts = ((ASTStatementSequenceNode) tempNode).getStatements();
        // all declaration stmts are allowed
        for (AugmentedStmt as : stmts) {
            Stmt s = as.get_Stmt();
            if (!(s instanceof DVariableDeclarationStmt)) {
                empty = false;
                break;
            }
        }
        if (!empty)
            break;
    }
    if (empty) {
        // should not be creating the method
        return false;
    }
    // about to return true enter all DavaSuperHandler stmts to make it part
    // of the preinit method
    createDavaStoreStmts();
    return true;
}
Also used : DVariableDeclarationStmt(soot.dava.internal.javaRep.DVariableDeclarationStmt) Iterator(java.util.Iterator) ASTNode(soot.dava.internal.AST.ASTNode) List(java.util.List) ArrayList(java.util.ArrayList) ASTStatementSequenceNode(soot.dava.internal.AST.ASTStatementSequenceNode) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) 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)

Example 15 with ASTNode

use of soot.dava.internal.AST.ASTNode in project soot by Sable.

the class DavaBody method applyBugFixes.

public void applyBugFixes() {
    ASTNode AST = (ASTNode) this.getUnits().getFirst();
    debug("applyBugFixes", "Applying AST analyzes for method" + this.getMethod().toString());
    AST.apply(new ShortcutIfGenerator());
    debug("applyBugFixes", "after ShortcutIfGenerator" + G.v().ASTTransformations_modified);
    AST.apply(new TypeCastingError());
    debug("applyBugFixes", "after TypeCastingError" + G.v().ASTTransformations_modified);
}
Also used : TypeCastingError(soot.dava.toolkits.base.AST.transformations.TypeCastingError) ShortcutIfGenerator(soot.dava.toolkits.base.AST.transformations.ShortcutIfGenerator) ASTNode(soot.dava.internal.AST.ASTNode)

Aggregations

ASTNode (soot.dava.internal.AST.ASTNode)29 ArrayList (java.util.ArrayList)14 List (java.util.List)13 Iterator (java.util.Iterator)12 AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)9 Stmt (soot.jimple.Stmt)9 ASTMethodNode (soot.dava.internal.AST.ASTMethodNode)7 ASTStatementSequenceNode (soot.dava.internal.AST.ASTStatementSequenceNode)7 DefinitionStmt (soot.jimple.DefinitionStmt)6 SootClass (soot.SootClass)4 SootMethod (soot.SootMethod)4 ASTControlFlowNode (soot.dava.internal.AST.ASTControlFlowNode)4 ASTSwitchNode (soot.dava.internal.AST.ASTSwitchNode)4 DVariableDeclarationStmt (soot.dava.internal.javaRep.DVariableDeclarationStmt)4 Body (soot.Body)3 Value (soot.Value)3 DavaBody (soot.dava.DavaBody)3 DecompilationException (soot.dava.DecompilationException)3 ASTTryNode (soot.dava.internal.AST.ASTTryNode)3 GAssignStmt (soot.grimp.internal.GAssignStmt)3