final ASTRewrite rewrite= ASTRewrite.create(fAst); final EnhancedForStatement statement= fAst.newEnhancedForStatement(); final List names= computeElementNames(); String name= GROUP_ELEMENT; if (fElement != null) { name= fElement.getName(); if (!names.contains(name)) names.add(0, name); } else { if (!names.isEmpty()) name= (String) names.get(0); } for (final Iterator iterator= names.iterator(); iterator.hasNext();) addLinkedPositionProposal(GROUP_ELEMENT, (String) iterator.next(), null); final Statement body= fStatement.getBody(); if (body != null) { if (body instanceof Block) { final ListRewrite list= rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY); for (final Iterator iterator= fOccurrences.iterator(); iterator.hasNext();) { final Statement parent= (Statement) ASTNodes.getParent((ASTNode) iterator.next(), Statement.class); if (parent != null && list.getRewrittenList().contains(parent)) list.remove(parent, null); } final String text= name; body.accept(new ASTVisitor() {  private boolean replace(final Expression expression) { final SimpleName node= fAst.newSimpleName(text); rewrite.replace(expression, node, null); addLinkedPosition(rewrite.track(node), false, GROUP_ELEMENT); return false; }  public final boolean visit(final MethodInvocation node) { final IMethodBinding binding= node.resolveMethodBinding(); if (binding != null && (binding.getName().equals("next") || binding.getName().equals("nextElement"))) { final Expression expression= node.getExpression(); if (expression instanceof Name) { final IBinding result= ((Name) expression).resolveBinding(); if (result != null && result.equals(fIterator)) return replace(node); } else if (expression instanceof FieldAccess) { final IBinding result= ((FieldAccess) expression).resolveFieldBinding(); if (result != null && result.equals(fIterator)) return replace(node); } } return super.visit(node); }  public final boolean visit(final SimpleName node) { if (fElement != null) { final IBinding binding= node.resolveBinding(); if (binding != null && binding.equals(fElement)) { final Statement parent= (Statement) ASTNodes.getParent(node, Statement.class); if (parent != null && list.getRewrittenList().contains(parent)) addLinkedPosition(rewrite.track(node), false, GROUP_ELEMENT); } } return false; } }); } statement.setBody((Statement) rewrite.createMoveTarget(body)); } final SingleVariableDeclaration declaration= fAst.newSingleVariableDeclaration(); final SimpleName simple= fAst.newSimpleName(name); addLinkedPosition(rewrite.track(simple), true, GROUP_ELEMENT); declaration.setName(simple); declaration.setType(getImportRewrite().addImport(getIterableType(fIterator.getType()), fAst)); statement.setParameter(declaration); statement.setExpression(getExpression(rewrite)); rewrite.replace(fStatement, statement, null); return rewrite;
return null;
	protected final ASTRewrite getRewrite() throws CoreException {		final ASTRewrite rewrite= ASTRewrite.create(fAst);		final EnhancedForStatement statement= fAst.newEnhancedForStatement();		final List names= computeElementNames();		String name= GROUP_ELEMENT;		if (fElement != null) {			name= fElement.getName();			if (!names.contains(name))				names.add(0, name);		} else {			if (!names.isEmpty())				name= (String) names.get(0);		}		for (final Iterator iterator= names.iterator(); iterator.hasNext();)			addLinkedPositionProposal(GROUP_ELEMENT, (String) iterator.next(), null);		final Statement body= fStatement.getBody();		if (body != null) {			if (body instanceof Block) {				final ListRewrite list= rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);				for (final Iterator iterator= fOccurrences.iterator(); iterator.hasNext();) {					final Statement parent= (Statement) ASTNodes.getParent((ASTNode) iterator.next(), Statement.class);					if (parent != null && list.getRewrittenList().contains(parent))						list.remove(parent, null);				}				final String text= name;				body.accept(new ASTVisitor() {					private boolean replace(final Expression expression) {						final SimpleName node= fAst.newSimpleName(text);						rewrite.replace(expression, node, null);						addLinkedPosition(rewrite.track(node), false, GROUP_ELEMENT);						return false;					}					public final boolean visit(final MethodInvocation node) {						final IMethodBinding binding= node.resolveMethodBinding();						if (binding != null && (binding.getName().equals("next") || binding.getName().equals("nextElement"))) { //$NON-NLS-1$ //$NON-NLS-2$							final Expression expression= node.getExpression();							if (expression instanceof Name) {								final IBinding result= ((Name) expression).resolveBinding();								if (result != null && result.equals(fIterator))									return replace(node);							} else if (expression instanceof FieldAccess) {								final IBinding result= ((FieldAccess) expression).resolveFieldBinding();								if (result != null && result.equals(fIterator))									return replace(node);							}						}						return super.visit(node);					}					public final boolean visit(final SimpleName node) {						if (fElement != null) {							final IBinding binding= node.resolveBinding();							if (binding != null && binding.equals(fElement)) {								final Statement parent= (Statement) ASTNodes.getParent(node, Statement.class);								if (parent != null && list.getRewrittenList().contains(parent))									addLinkedPosition(rewrite.track(node), false, GROUP_ELEMENT);							}						}						return false;					}				});			}			statement.setBody((Statement) rewrite.createMoveTarget(body));		}		final SingleVariableDeclaration declaration= fAst.newSingleVariableDeclaration();		final SimpleName simple= fAst.newSimpleName(name);		addLinkedPosition(rewrite.track(simple), true, GROUP_ELEMENT);		declaration.setName(simple);		declaration.setType(getImportRewrite().addImport(getIterableType(fIterator.getType()), fAst));		statement.setParameter(declaration);		statement.setExpression(getExpression(rewrite));		rewrite.replace(fStatement, statement, null);		return rewrite;	}