下面列出了org.eclipse.jdt.core.dom.EnhancedForStatement#setExpression ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected Statement convert(CompilationUnitRewrite cuRewrite, TextEditGroup group, LinkedProposalModel positionGroups) throws CoreException {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
ImportRewrite importRewrite= cuRewrite.getImportRewrite();
ForStatement forStatement= getForStatement();
IJavaProject javaProject= ((CompilationUnit)forStatement.getRoot()).getJavaElement().getJavaProject();
String[] proposals= getVariableNameProposals(fArrayAccess.resolveTypeBinding(), javaProject);
String parameterName;
if (fElementDeclaration != null) {
parameterName= fElementDeclaration.getName().getIdentifier();
} else {
parameterName= proposals[0];
}
LinkedProposalPositionGroup pg= positionGroups.getPositionGroup(parameterName, true);
if (fElementDeclaration != null)
pg.addProposal(parameterName, null, 10);
for (int i= 0; i < proposals.length; i++) {
pg.addProposal(proposals[i], null, 10);
}
AST ast= forStatement.getAST();
EnhancedForStatement result= ast.newEnhancedForStatement();
SingleVariableDeclaration parameterDeclaration= createParameterDeclaration(parameterName, fElementDeclaration, fArrayAccess, forStatement, importRewrite, rewrite, group, pg, fMakeFinal);
result.setParameter(parameterDeclaration);
result.setExpression((Expression)rewrite.createCopyTarget(fArrayAccess));
convertBody(forStatement.getBody(), fIndexBinding, fArrayBinding, parameterName, rewrite, group, pg);
result.setBody(getBody(cuRewrite, group, positionGroups));
positionGroups.setEndPosition(rewrite.track(result));
return result;
}
/**
* Helper to generate a <code>foreach</code> loop to iterate over an {@link Iterable}.
*
* @param ast the {@link AST} instance to rewrite the loop to
* @return the complete {@link ASTRewrite} object
*/
private ASTRewrite generateForEachRewrite(AST ast) {
EnhancedForStatement loopStatement= ast.newEnhancedForStatement();
ASTRewrite rewrite= ASTRewrite.create(ast);
ITypeBinding loopOverType= extractElementType(ast);
// generate name proposals and add them to the variable declaration
SimpleName forDeclarationName= resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), null, true);
SingleVariableDeclaration forLoopInitializer= ast.newSingleVariableDeclaration();
forLoopInitializer.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));
forLoopInitializer.setName(forDeclarationName);
loopStatement.setParameter(forLoopInitializer);
loopStatement.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
Block forLoopBody= ast.newBlock();
forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
loopStatement.setBody(forLoopBody);
rewrite.replace(fCurrentNode, loopStatement, null);
return rewrite;
}