// to hasNext().
if (!(whileCond.getHead() instanceof DefinitionStmt)) {
continue;
}
DefinitionStmt stmt = (DefinitionStmt) whileCond.getHead();
if (!(stmt.getRightOp() instanceof InterfaceInvokeExpr)) {
continue;
}
InterfaceInvokeExpr expr = (InterfaceInvokeExpr) stmt
.getRightOp();
if (expr.getMethod() != iteratorHasNextMethod) {
continue;
}
// At this point we know we have a while (hasNext()) loop.
// Now go check for iterator is defined... it should be just
// above
Local iteratorLocal = (Local) expr.getBase();
Block whilePredecessor = (Block) whileCond.getPreds().get(0);
if (whilePredecessor == block) {
whilePredecessor = (Block) whileCond.getPreds().get(1);
}
// System.out.println("whilePredecessor = " + whilePredecessor);
Unit unit = whilePredecessor.getTail();
boolean found = false;
// walk backwards until we find a definition of the iterator.
while ((unit != whilePredecessor.getHead()) && !found) {
if (unit instanceof DefinitionStmt
&& ((DefinitionStmt) unit).getLeftOp().equals(
iteratorLocal)) {
found = true;
} else {
unit = whilePredecessor.getPredOf(unit);
}
}
// System.out.println("iterator def = " + unit);
DefinitionStmt iteratorDefinition = ((DefinitionStmt) unit);
if (!(iteratorDefinition.getRightOp() instanceof InterfaceInvokeExpr)
|| !((InterfaceInvokeExpr) iteratorDefinition
.getRightOp()).getMethod().getName().equals(
"iterator")) {
continue;
}
Local collectionLocal = (Local) ((InterfaceInvokeExpr) iteratorDefinition
.getRightOp()).getBase();
// System.out.println("collection Local = " + collectionLocal);
found = false;
// Walk backward again until we reach the definition
// of the collection.
while ((unit != whilePredecessor.getHead()) && !found) {
if (unit instanceof DefinitionStmt
&& ((DefinitionStmt) unit).getLeftOp().equals(
collectionLocal)) {
found = true;
} else {
unit = whilePredecessor.getPredOf(unit);
}
}
// System.out.println("collection def = " + unit);
// System.out.println("field = " + field);
DefinitionStmt collectionDefinition = ((DefinitionStmt) unit);
if (!(collectionDefinition.getRightOp() instanceof FieldRef)
|| (((FieldRef) collectionDefinition.getRightOp())
.getField() != field)) {
continue;
}
// FINALLY we know we've found something we can unroll... :)