} else {
body = new JBlock(info);
body.addStmt(action);
}
JLocal elementVar = (JLocal) typeMap.get(x.elementVariable.binding);
String elementVarName = elementVar.getName();
JDeclarationStatement elementDecl = (JDeclarationStatement) processStatement(x.elementVariable);
assert (elementDecl.initializer == null);
JForStatement result;
if (x.collection.resolvedType.isArrayType()) {
/**
* <pre>
* for (T[] i$array = collection, int i$index = 0, int i$max = i$array.length;
* i$index < i$max; ++i$index) {
* T elementVar = i$array[i$index];
* // user action
* }
* </pre>
*/
JLocal arrayVar = createSyntheticLocal(info, elementVarName + "$array",
(JType) typeMap.get(x.collection.resolvedType));
JLocal indexVar = createSyntheticLocal(info, elementVarName + "$index",
program.getTypePrimitiveInt());
JLocal maxVar = createSyntheticLocal(info, elementVarName + "$max",
program.getTypePrimitiveInt());
List<JStatement> initializers = new ArrayList<JStatement>(3);
// T[] i$array = arr
initializers.add(createDeclaration(info, arrayVar,
dispProcessExpression(x.collection)));
// int i$index = 0
initializers.add(createDeclaration(info, indexVar,
program.getLiteralInt(0)));
// int i$max = i$array.length
initializers.add(createDeclaration(info, maxVar, new JFieldRef(info,
createVariableRef(info, arrayVar),
program.getIndexedField("Array.length"), currentClass)));
// i$index < i$max
JExpression condition = new JBinaryOperation(info,
program.getTypePrimitiveBoolean(), JBinaryOperator.LT,
createVariableRef(info, indexVar), createVariableRef(info, maxVar));
// ++i$index
List<JExpressionStatement> increments = new ArrayList<JExpressionStatement>(
1);
increments.add(new JPrefixOperation(info, JUnaryOperator.INC,
createVariableRef(info, indexVar)).makeStatement());
// T elementVar = i$array[i$index];
elementDecl.initializer = new JArrayRef(info, createVariableRef(info,
arrayVar), createVariableRef(info, indexVar));
body.addStmt(0, elementDecl);
result = new JForStatement(info, initializers, condition, increments,
body);
} else {
/**
* <pre>
* for (Iterator<T> i$iterator = collection.iterator(); i$iterator.hasNext(); ) {
* T elementVar = i$iterator.next();
* // user action
* }
* </pre>
*/
JLocal iteratorVar = createSyntheticLocal(info, elementVarName
+ "$iterator", program.getIndexedType("Iterator"));
List<JStatement> initializers = new ArrayList<JStatement>(1);
// Iterator<T> i$iterator = collection.iterator()
initializers.add(createDeclaration(info, iteratorVar, new JMethodCall(