/* The purpose of this method is to replace the ASTIfElseNode given by the var nodeNumber with the new ASTIfNode and to add the remianing list of bodies after this ASTIfNode The new body is then returned; */ public List<Object> createNewBody(List<Object> oldSubBody, int nodeNumber) { if (newIfNode == null) return null; List<Object> newSubBody = new ArrayList<Object>(); if (oldSubBody.size() <= nodeNumber) { // something is wrong since the oldSubBody has lesser nodes than nodeNumber return null; } Iterator<Object> oldIt = oldSubBody.iterator(); int index = 0; while (index != nodeNumber) { newSubBody.add(oldIt.next()); index++; } // check to see that the next is an ASTIfElseNode ASTNode temp = (ASTNode) oldIt.next(); if (!(temp instanceof ASTIfElseNode)) return null; newSubBody.add(newIfNode); newSubBody.addAll(remainingBody); // copy any remaining nodes while (oldIt.hasNext()) { newSubBody.add(oldIt.next()); } return newSubBody; }
public void replaceIndex2BodyList(Map<Object, List<Object>> index2BodyList) { this.index2BodyList = index2BodyList; subBodies = new ArrayList<Object>(); Iterator<Object> it = indexList.iterator(); while (it.hasNext()) { List body = index2BodyList.get(it.next()); if (body != null) subBodies.add(body); } }
public void toString(UnitPrinter up) { label_toString(up); up.literal("switch"); up.literal(" "); up.literal("("); keyBox.toString(up); up.literal(")"); up.newline(); up.literal("{"); up.newline(); Iterator<Object> it = indexList.iterator(); while (it.hasNext()) { Object index = it.next(); up.incIndent(); if (index instanceof String) up.literal("default"); else { up.literal("case"); up.literal(" "); up.literal(index.toString()); } up.literal(":"); up.newline(); List<Object> subBody = index2BodyList.get(index); if (subBody != null) { up.incIndent(); body_toString(up, subBody); if (it.hasNext()) up.newline(); up.decIndent(); } up.decIndent(); } up.literal("}"); up.newline(); }
public ASTSwitchNode( SETNodeLabel label, Value key, List<Object> indexList, Map<Object, List<Object>> index2BodyList) { super(label); this.keyBox = Jimple.v().newRValueBox(key); this.indexList = indexList; this.index2BodyList = index2BodyList; Iterator<Object> it = indexList.iterator(); while (it.hasNext()) { List body = index2BodyList.get(it.next()); if (body != null) subBodies.add(body); } }
public String toString() { StringBuffer b = new StringBuffer(); b.append(label_toString()); b.append("switch ("); b.append(get_Key()); b.append(")"); b.append(NEWLINE); b.append("{"); b.append(NEWLINE); Iterator<Object> it = indexList.iterator(); while (it.hasNext()) { Object index = it.next(); b.append(TAB); if (index instanceof String) b.append("default"); else { b.append("case "); b.append(((Integer) index).toString()); } b.append(":"); b.append(NEWLINE); List<Object> subBody = index2BodyList.get(index); if (subBody != null) { b.append(body_toString(subBody)); if (it.hasNext()) b.append(NEWLINE); } } b.append("}"); b.append(NEWLINE); return b.toString(); }
/* We know this method is called when there is a loop node which has a body consisting entirely of one ASTIfElseNode */ public static List getNewNode(ASTNode loopNode, ASTIfElseNode ifElseNode) { // make sure that elsebody has only a stmtseq node List elseBody = ((ASTIfElseNode) ifElseNode).getElseBody(); if (elseBody.size() != 1) { // this is more than one we need one stmtSeq Node return null; } ASTNode tempNode = (ASTNode) elseBody.get(0); if (!(tempNode instanceof ASTStatementSequenceNode)) { // not a stmtSeq return null; } List statements = ((ASTStatementSequenceNode) tempNode).getStatements(); Iterator stmtIt = statements.iterator(); while (stmtIt.hasNext()) { AugmentedStmt as = (AugmentedStmt) stmtIt.next(); Stmt stmt = as.get_Stmt(); if (stmt instanceof DAbruptStmt) { // this is a abrupt stmt DAbruptStmt abStmt = (DAbruptStmt) stmt; if (!(abStmt.is_Break())) { // we need a break return null; } else { if (stmtIt.hasNext()) { // a break should be the last stmt return null; } SETNodeLabel label = abStmt.getLabel(); String labelBroken = label.toString(); String loopLabel = ((ASTLabeledNode) loopNode).get_Label().toString(); if (labelBroken != null && loopLabel != null) { // stmt breaks some label if (labelBroken.compareTo(loopLabel) == 0) { // we have found a break breaking this label // make sure that if the orignal was an ASTWhileNode then there was // ONLY a break statement if (loopNode instanceof ASTWhileNode) { if (statements.size() != 1) { // more than 1 statement return null; } } // pattern matched ASTWhileNode newWhileNode = makeWhileNode(ifElseNode, loopNode); if (newWhileNode == null) { return null; } List toReturn = new ArrayList(); toReturn.add(newWhileNode); // Add the statementSequenceNode AFTER the whileNode except for the laststmt if (statements.size() != 1) { // size 1 means that the only stmt is a break stmt Iterator tempIt = statements.iterator(); List newStmts = new ArrayList(); while (tempIt.hasNext()) { Object tempStmt = tempIt.next(); if (tempIt.hasNext()) { newStmts.add(tempStmt); } } toReturn.add(new ASTStatementSequenceNode(newStmts)); } return toReturn; } // labels matched } // non null labels } // end of break stmt } // stmt is an abrupt stmt else if (stmt instanceof ReturnStmt || stmt instanceof ReturnVoidStmt) { if (!(loopNode instanceof ASTUnconditionalLoopNode)) { // this pattern is only possible for while(true) return null; } if (stmtIt.hasNext()) { // returns should come in the end return null; } // pattern matched ASTWhileNode newWhileNode = makeWhileNode(ifElseNode, loopNode); if (newWhileNode == null) { return null; } List toReturn = new ArrayList(); toReturn.add(newWhileNode); // Add the statementSequenceNode AFTER the whileNode Iterator tempIt = statements.iterator(); List newStmts = new ArrayList(); while (tempIt.hasNext()) { Object tempStmt = tempIt.next(); newStmts.add(tempStmt); } toReturn.add(new ASTStatementSequenceNode(newStmts)); return toReturn; } // if stmt was a return stmt } // going through the stmts return null; } // end of method