}
JStatement processStatement(ForeachStatement x) {
SourceInfo info = makeSourceInfo(x);
JBlock body;
JStatement action = dispProcessStatement(x.action);
if (action instanceof JBlock) {
body = (JBlock) action;
} 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(
info, dispProcessExpression(x.collection),
program.getIndexedMethod("Iterable.iterator"))));
// i$iterator.hasNext()
JExpression condition = new JMethodCall(info, createVariableRef(info,
iteratorVar), program.getIndexedMethod("Iterator.hasNext"));
// T elementVar = (T) i$iterator.next();
elementDecl.initializer = new JMethodCall(info, createVariableRef(info,
iteratorVar), program.getIndexedMethod("Iterator.next"));
// Perform any implicit reference type casts (due to generics).
// Note this occurs before potential unboxing.
if (elementVar.getType() != program.getTypeJavaLangObject()) {
TypeBinding collectionType;
try {
Field privateField = ForeachStatement.class.getDeclaredField("collectionElementType");
privateField.setAccessible(true);
collectionType = (TypeBinding) privateField.get(x);
} catch (Exception e) {
throw new InternalCompilerException(elementDecl,
"Failed to retreive collectionElementType through reflection",
e);
}
JType toType = (JType) typeMap.get(collectionType);
assert (toType instanceof JReferenceType);
elementDecl.initializer = maybeCast(toType, elementDecl.initializer);
}
body.addStmt(0, elementDecl);
result = new JForStatement(info, initializers, condition,
Collections.<JExpressionStatement> emptyList(), body);
}