use of soot.dava.internal.AST.ASTTryNode 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);
}
}
use of soot.dava.internal.AST.ASTTryNode in project soot by Sable.
the class MethodCallFinder method replaceSubBody.
public boolean replaceSubBody(InvokeStmt s, List<ASTStatementSequenceNode> newChangedBodyPart, ASTParentNodeFinder finder) {
// get the stmt seq node of invoke stmt
Object stmtSeqNode = finder.getParentOf(s);
// find the parent node of the stmt seq node
Object ParentOfStmtSeq = finder.getParentOf(stmtSeqNode);
if (ParentOfStmtSeq == null) {
throw new RuntimeException("MethodCall FInder: parent of stmt seq node not found");
}
ASTNode node = (ASTNode) ParentOfStmtSeq;
if (node instanceof ASTMethodNode) {
// get the subBody to replace
List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
((ASTMethodNode) node).replaceBody(newBody);
return true;
} else if (node instanceof ASTSynchronizedBlockNode) {
// get the subBody to replace
List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
((ASTSynchronizedBlockNode) node).replaceBody(newBody);
return true;
} else if (node instanceof ASTLabeledBlockNode) {
// get the subBody to replace
List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
((ASTLabeledBlockNode) node).replaceBody(newBody);
return true;
} else if (node instanceof ASTUnconditionalLoopNode) {
// get the subBody to replace
List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
((ASTUnconditionalLoopNode) node).replaceBody(newBody);
return true;
} else if (node instanceof ASTIfNode) {
// get the subBody to replace
List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
((ASTIfNode) node).replaceBody(newBody);
return true;
} else if (node instanceof ASTWhileNode) {
// get the subBody to replace
List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
((ASTWhileNode) node).replaceBody(newBody);
return true;
} else if (node instanceof ASTDoWhileNode) {
// get the subBody to replace
List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
((ASTDoWhileNode) node).replaceBody(newBody);
return true;
} else if (node instanceof ASTForLoopNode) {
// get the subBody to replace
List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
((ASTForLoopNode) node).replaceBody(newBody);
return true;
} else if (node instanceof ASTIfElseNode) {
List<Object> subBodies = node.get_SubBodies();
if (subBodies.size() != 2)
throw new RuntimeException("Found an ifelse ASTNode which does not have two bodies");
List<Object> ifBody = (List<Object>) subBodies.get(0);
List<Object> elseBody = (List<Object>) subBodies.get(1);
// find out which of these bodies has the stmt seq node with the
// invoke stmt
int subBodyNumber = -1;
Iterator<Object> it = ifBody.iterator();
while (it.hasNext()) {
Object temp = it.next();
if (temp == stmtSeqNode) {
subBodyNumber = 0;
break;
}
}
if (subBodyNumber != 0) {
it = elseBody.iterator();
while (it.hasNext()) {
Object temp = it.next();
if (temp == stmtSeqNode) {
subBodyNumber = 1;
break;
}
}
}
List<Object> subBodyToReplace = null;
if (subBodyNumber == 0)
subBodyToReplace = ifBody;
else if (subBodyNumber == 1)
subBodyToReplace = elseBody;
else
throw new RuntimeException("Could not find the related ASTNode in the method");
List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
if (subBodyNumber == 0) {
((ASTIfElseNode) node).replaceBody(newBody, elseBody);
return true;
} else if (subBodyNumber == 1) {
((ASTIfElseNode) node).replaceBody(ifBody, newBody);
return true;
}
} else if (node instanceof ASTTryNode) {
// NOTE THAT method INLINING Is currently only done in the tryBody
// and not the catchBody
// THe only reason for this being that mostly method calls are made
// in the try and not the catch
// get try body
List<Object> tryBody = ((ASTTryNode) node).get_TryBody();
Iterator<Object> it = tryBody.iterator();
// find whether stmtSeqNode is in the tryBody
boolean inTryBody = false;
while (it.hasNext()) {
ASTNode temp = (ASTNode) it.next();
if (temp == stmtSeqNode) {
inTryBody = true;
break;
}
}
if (!inTryBody) {
// return without making any changes
return false;
}
List<Object> newBody = createNewSubBody(tryBody, newChangedBodyPart, stmtSeqNode);
((ASTTryNode) node).replaceTryBody(newBody);
return true;
} else if (node instanceof ASTSwitchNode) {
List<Object> indexList = ((ASTSwitchNode) node).getIndexList();
Map<Object, List<Object>> index2BodyList = ((ASTSwitchNode) node).getIndex2BodyList();
Iterator<Object> it = indexList.iterator();
while (it.hasNext()) {
// going through all the cases of the switch
// statement
Object currentIndex = it.next();
List<Object> body = index2BodyList.get(currentIndex);
if (body != null) {
// this body is a list of ASTNodes
// see if it contains stmtSeqNode
boolean found = false;
Iterator<Object> itBody = body.iterator();
while (itBody.hasNext()) {
ASTNode temp = (ASTNode) itBody.next();
if (temp == stmtSeqNode) {
found = true;
break;
}
}
if (found) {
// this is the body which has the stmt seq node
List<Object> newBody = createNewSubBody(body, newChangedBodyPart, stmtSeqNode);
// put this body in the Map
index2BodyList.put(currentIndex, newBody);
// replace in actual switchNode
((ASTSwitchNode) node).replaceIndex2BodyList(index2BodyList);
return true;
}
}
// if body not null
}
// going through all cases
}
return false;
}
use of soot.dava.internal.AST.ASTTryNode in project soot by Sable.
the class UnreachableCodeFinder method processASTTryNode.
@Override
public DavaFlowSet processASTTryNode(ASTTryNode node, DavaFlowSet input) {
if (DEBUG)
System.out.println("Processing try node");
if (!isReachable(input)) {
// this sequence is not reachable hence simply return inset
return input;
}
// if reachable
List<Object> tryBody = node.get_TryBody();
DavaFlowSet tryBodyOutput = process(tryBody, input);
// catch is always reachable if try is reachable
DavaFlowSet inputCatch = newInitialFlow();
List<Object> catchList = node.get_CatchList();
Iterator<Object> it = catchList.iterator();
List<DavaFlowSet> catchOutput = new ArrayList<DavaFlowSet>();
while (it.hasNext()) {
ASTTryNode.container catchBody = (ASTTryNode.container) it.next();
List body = (List) catchBody.o;
// list of ASTNodes
// result because of going through the catchBody
DavaFlowSet tempResult = process(body, cloneFlowSet(inputCatch));
// System.out.println("TempResult going through body"+tempResult);
catchOutput.add(tempResult);
}
// handle breaks
String label = getLabel(node);
List<DavaFlowSet> outList = new ArrayList<DavaFlowSet>();
// handle breaks out of tryBodyOutput
outList.add(handleBreak(label, tryBodyOutput, node));
// handling breakLists of each of the catchOutputs
for (DavaFlowSet co : catchOutput) {
DavaFlowSet temp = handleBreak(label, co, node);
outList.add(temp);
}
// merge all outList elements. since these are the outputs with breaks handled
DavaFlowSet out = tryBodyOutput;
for (DavaFlowSet oe : outList) out = merge(out, oe);
for (DavaFlowSet ce : catchOutput) out = merge(out, ce);
return out;
}
use of soot.dava.internal.AST.ASTTryNode 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;
}
use of soot.dava.internal.AST.ASTTryNode in project soot by Sable.
the class UselessAbruptStmtRemover method checkChildLastInParent.
public boolean checkChildLastInParent(ASTNode child, ASTNode parent) {
List<Object> subBodies = parent.get_SubBodies();
Iterator<Object> it = subBodies.iterator();
while (it.hasNext()) {
List subBody = null;
if (parent instanceof ASTTryNode)
subBody = (List) ((ASTTryNode.container) it.next()).o;
else
subBody = (List) it.next();
if (subBody.contains(child)) {
if (subBody.indexOf(child) != subBody.size() - 1)
return false;
else
return true;
}
}
return false;
}
Aggregations