Search in sources :

Example 1 with ASTControlFlowNode

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

the class EliminateConditions method normalRetrieving.

public void normalRetrieving(ASTNode node) {
    modified = false;
    if (node instanceof ASTSwitchNode) {
        do {
            modified = false;
            dealWithSwitchNode((ASTSwitchNode) node);
        } while (modified);
        return;
    }
    // from the Node get the subBodes
    Iterator<Object> sbit = node.get_SubBodies().iterator();
    while (sbit.hasNext()) {
        List subBody = (List) sbit.next();
        Iterator it = subBody.iterator();
        ASTNode temp = null;
        Boolean returned = 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 = eliminate(temp);
                if (returned != null && canChange(returned, temp)) {
                    break;
                } else {
                    if (DEBUG)
                        System.out.println("returned is null" + temp.getClass());
                    bodyContainingNode = null;
                }
            }
            temp.apply(this);
        }
        // end while going through nodes in subBody
        boolean changed = change(returned, temp);
        if (changed)
            modified = true;
    }
    if (modified) {
        // repeat the whole thing
        normalRetrieving(node);
    }
}
Also used : ASTControlFlowNode(soot.dava.internal.AST.ASTControlFlowNode) Iterator(java.util.Iterator) ASTNode(soot.dava.internal.AST.ASTNode) List(java.util.List) ASTSwitchNode(soot.dava.internal.AST.ASTSwitchNode)

Example 2 with ASTControlFlowNode

use of soot.dava.internal.AST.ASTControlFlowNode 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 3 with ASTControlFlowNode

use of soot.dava.internal.AST.ASTControlFlowNode 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 4 with ASTControlFlowNode

use of soot.dava.internal.AST.ASTControlFlowNode 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 5 with ASTControlFlowNode

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

the class EliminateConditions method eliminateForTry.

public Boolean eliminateForTry(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;
    if (!(temp instanceof ASTTryNode))
        throw new RuntimeException("eliminateTry called when parent was not a try node");
    ASTTryNode parent = (ASTTryNode) temp;
    List<Object> tryBody = parent.get_TryBody();
    int index = tryBody.indexOf(node);
    if (index >= 0) {
        // bound the body containing Node
        bodyContainingNode = tryBody;
        return new Boolean(trueOrFalse);
    }
    List<Object> catchList = parent.get_CatchList();
    Iterator<Object> it = catchList.iterator();
    while (it.hasNext()) {
        ASTTryNode.container catchBody = (ASTTryNode.container) it.next();
        List<Object> body = (List<Object>) catchBody.o;
        index = body.indexOf(node);
        if (index >= 0) {
            // bound the body containing Node
            bodyContainingNode = body;
            return new Boolean(trueOrFalse);
        }
    }
    return null;
}
Also used : ASTTryNode(soot.dava.internal.AST.ASTTryNode) DNotExpr(soot.dava.internal.javaRep.DNotExpr) ASTCondition(soot.dava.internal.AST.ASTCondition) ASTControlFlowNode(soot.dava.internal.AST.ASTControlFlowNode) Value(soot.Value) List(java.util.List) ASTUnaryCondition(soot.dava.internal.AST.ASTUnaryCondition)

Aggregations

ASTControlFlowNode (soot.dava.internal.AST.ASTControlFlowNode)5 List (java.util.List)4 ASTNode (soot.dava.internal.AST.ASTNode)4 Iterator (java.util.Iterator)3 Value (soot.Value)2 ASTCondition (soot.dava.internal.AST.ASTCondition)2 ASTTryNode (soot.dava.internal.AST.ASTTryNode)2 ASTUnaryCondition (soot.dava.internal.AST.ASTUnaryCondition)2 DNotExpr (soot.dava.internal.javaRep.DNotExpr)2 ASTSwitchNode (soot.dava.internal.AST.ASTSwitchNode)1