Java Code Examples for org.eclipse.jdt.core.dom.EnhancedForStatement#setExpression()

The following examples show how to use org.eclipse.jdt.core.dom.EnhancedForStatement#setExpression() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConvertForLoopOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@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;
}
 
Example 2
Source File: GenerateForLoopAssistProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * 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;
}