ASTNode clone0(AST target) { EnhancedForStatement result = new EnhancedForStatement(target); result.setSourceRange(getStartPosition(), getLength()); result.copyLeadingComment(this); result.setParameter((SingleVariableDeclaration) getParameter().clone(target)); result.setExpression((Expression) getExpression().clone(target)); result.setBody( (Statement) ASTNode.copySubtree(target, getBody())); return result; }
void accept0(ASTVisitor visitor) { boolean visitChildren = visitor.visit(this); if (visitChildren) { // visit children in normal left to right reading order acceptChild(visitor, getParameter()); acceptChild(visitor, getExpression()); acceptChild(visitor, getBody()); } visitor.endVisit(this); }
final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { if (property == PARAMETER_PROPERTY) { if (get) { return getParameter(); } else { setParameter((SingleVariableDeclaration) child); return null; } } if (property == EXPRESSION_PROPERTY) { if (get) { return getExpression(); } else { setExpression((Expression) child); return null; } } if (property == BODY_PROPERTY) { if (get) { return getBody(); } else { setBody((Statement) child); return null; } } // allow default implementation to flag the error return super.internalGetSetChildProperty(property, get, child); }
@Override public boolean visit(EnhancedForStatement node) { this.fBuffer.append("for (");//$NON-NLS-1$ node.getParameter().accept(this); this.fBuffer.append(" : ");//$NON-NLS-1$ node.getExpression().accept(this); this.fBuffer.append(") ");//$NON-NLS-1$ node.getBody().accept(this); return false; }
int treeSize() { return memSize() + (this.parameter == null ? 0 : getParameter().treeSize()) + (this.expression == null ? 0 : getExpression().treeSize()) + (this.body == null ? 0 : getBody().treeSize()); } }
@Override public boolean visit(EnhancedForStatement node) { CSForEachStatement stmt = new CSForEachStatement(node.getStartPosition(), mapExpression(node.getExpression())); stmt.variable(createParameter(node.getParameter())); visitBlock(stmt.body(), node.getBody()); addStatement(stmt); return false; }
public Statement convert(ForeachStatement statement) { switch(this.ast.apiLevel) { case AST.JLS2_INTERNAL : return createFakeEmptyStatement(statement); default : EnhancedForStatement enhancedForStatement = new EnhancedForStatement(this.ast); enhancedForStatement.setParameter(convertToSingleVariableDeclaration(statement.elementVariable)); org.eclipse.jdt.internal.compiler.ast.Expression collection = statement.collection; if (collection == null) return null; enhancedForStatement.setExpression(convert(collection)); final Statement action = convert(statement.action); if (action == null) return null; enhancedForStatement.setBody(action); int start = statement.sourceStart; int end = statement.sourceEnd; enhancedForStatement.setSourceRange(start, end - start + 1); return enhancedForStatement; } }
/** * Append a Java 5 "enhanced" 'for' statement. * * @param name iteration variable name * @param type iteration variable type * @param expr iteration source expression * @param block statement body block */ public void addSugaredForStatement(String name, String type, ExpressionBuilderBase expr, BlockBuilder block) { EnhancedForStatement stmt = m_ast.newEnhancedForStatement(); stmt.setExpression(expr.getExpression()); SingleVariableDeclaration decl = m_ast.newSingleVariableDeclaration(); decl.setName(m_ast.newSimpleName(name)); decl.setType(m_source.createType(type)); stmt.setParameter(decl); stmt.setBody(block.getStatement()); m_block.statements().add(stmt); }
@Override public boolean visit(EnhancedForStatement node) { pushNode(node, node.getParameter().toString() + COLON + node.getExpression().toString()); return true; }
@Override public boolean visit(EnhancedForStatement node) { handleToken(node, TokenNameLPAREN, this.options.insert_space_before_opening_paren_in_for, this.options.insert_space_after_opening_paren_in_for); handleTokenBefore(node.getBody(), TokenNameRPAREN, this.options.insert_space_before_closing_paren_in_for, false); handleTokenAfter(node.getParameter(), TokenNameCOLON, this.options.insert_space_before_colon_in_for, this.options.insert_space_after_colon_in_for); handleLoopBody(node.getBody()); return true; }
@Override public boolean visit(final EnhancedForStatement node) { if (isArray(node.getExpression())) { return visitLoopBody(node.getBody(), new HashSet<String>()); } return VISIT_SUBTREE; }
private boolean isEnhancedForStatementVariable(Statement statement, SimpleName name) { if (statement instanceof EnhancedForStatement) { EnhancedForStatement forStatement= (EnhancedForStatement) statement; SingleVariableDeclaration param= forStatement.getParameter(); return param.getType() == name.getParent(); // strange recovery, see https://bugs.eclipse.org/180456 } return false; }
/** * Expands the form: for ( [final] Type var: iterable ) { ... } Into something equivalent to: for ( Iterator iter = * iterable.iter(); iter.hasNext(); ) { [final] Type var = (Type) iter.next(); ... } Or, in the case of an array: for ( int idx = * 0; i < iterable.length; i++ ) { [final] Type var = iterable[idx]; ... } Except that the expression "iterable" is only evaluate * once (or is it?) */ private CAstNode visit(EnhancedForStatement n, WalkContext context) { if (n.getExpression().resolveTypeBinding().isArray()) return makeArrayEnhancedForLoop(n, context); else return makeIteratorEnhancedForLoop(n, context); }
public boolean visit(EnhancedForStatement node) { if (!hasChildrenChanges(node)) { return doVisitUnchangedChildren(node); } rewriteRequiredNode(node, EnhancedForStatement.PARAMETER_PROPERTY); int pos= rewriteRequiredNode(node, EnhancedForStatement.EXPRESSION_PROPERTY); RewriteEvent bodyEvent= getEvent(node, EnhancedForStatement.BODY_PROPERTY); if (bodyEvent != null && bodyEvent.getChangeKind() == RewriteEvent.REPLACED) { int startOffset; try { startOffset= getScanner().getTokenEndOffset(TerminalTokens.TokenNameRPAREN, pos); rewriteBodyNode(node, EnhancedForStatement.BODY_PROPERTY, startOffset, -1, getIndent(node.getStartPosition()), this.formatter.FOR_BLOCK); // body } catch (CoreException e) { handleException(e); } } else { voidVisit(node, EnhancedForStatement.BODY_PROPERTY); } return false; }
@Override public boolean visit(EnhancedForStatement node) { this.fBuffer.append("for (");//$NON-NLS-1$ node.getParameter().accept(this); this.fBuffer.append(" : ");//$NON-NLS-1$ node.getExpression().accept(this); this.fBuffer.append(") ");//$NON-NLS-1$ node.getBody().accept(this); return false; }
final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { if (property == PARAMETER_PROPERTY) { if (get) { return getParameter(); } else { setParameter((SingleVariableDeclaration) child); return null; } } if (property == EXPRESSION_PROPERTY) { if (get) { return getExpression(); } else { setExpression((Expression) child); return null; } } if (property == BODY_PROPERTY) { if (get) { return getBody(); } else { setBody((Statement) child); return null; } } // allow default implementation to flag the error return super.internalGetSetChildProperty(property, get, child); }
void accept0(ASTVisitor visitor) { boolean visitChildren = visitor.visit(this); if (visitChildren) { // visit children in normal left to right reading order acceptChild(visitor, getParameter()); acceptChild(visitor, getExpression()); acceptChild(visitor, getBody()); } visitor.endVisit(this); }
int treeSize() { return memSize() + (this.parameter == null ? 0 : getParameter().treeSize()) + (this.expression == null ? 0 : getExpression().treeSize()) + (this.body == null ? 0 : getBody().treeSize()); } }
CAstNode exprNode = visitNode(n.getExpression(), context); CAstNode arrayDeclNode = makeNode(context, fFactory, n, CAstNode.DECL_STMT, fFactory.makeConstant(new InternalCAstSymbol( tmpArrayName, fTypeDict.getCAstTypeFor(n.getExpression().resolveTypeBinding()), true)), exprNode); CAstNode.VAR, fFactory.makeConstant(tmpArrayName), fFactory.makeConstant(fTypeDict.getCAstTypeFor(n.getExpression().resolveTypeBinding())))); CAstNode condNode = makeNode(context, fFactory, n, CAstNode.BINARY_EXPR, CAstOperator.OP_LT, makeNode(context, fFactory, n, CAstNode.VAR, fFactory.makeConstant(tmpIndexName), fFactory.makeConstant(JavaPrimitiveType.INT)), tmpArrayLengthNode); fFactory.makeConstant(tmpArrayName)), fFactory.makeConstant(fIdentityMapper.getTypeRef(n.getExpression() .resolveTypeBinding().getComponentType())), makeNode(context, fFactory, n, CAstNode.VAR, fFactory .makeConstant(tmpIndexName))); SingleVariableDeclaration svd = n.getParameter(); Object defaultValue = JDT2CAstUtils.defaultValueForType(svd.resolveBinding().getType()); CAstNode nextAssignNode = makeNode(context, fFactory, n, CAstNode.DECL_STMT, fFactory.makeConstant(new CAstSymbolImpl(svd .getName().getIdentifier(), fTypeDict.getCAstTypeFor(n.getExpression() .resolveTypeBinding().getComponentType()), (svd.getModifiers() & Modifier.FINAL) != 0, defaultValue)), tmpArrayAccessNode); ASTNode breakTarget = makeBreakOrContinueTarget(n, "breakLabel" + n.getStartPosition()); ASTNode continueTarget = makeBreakOrContinueTarget(n, "continueLabel" + n.getStartPosition()); String loopLabel = context.getLabelMap().get(n); WalkContext loopContext = new LoopContext(context, loopLabel, breakTarget, continueTarget); makeNode(context, fFactory, n, CAstNode.LOOP, condNode, makeNode(context, fFactory, n, CAstNode.BLOCK_STMT, makeNode(context, fFactory, n, CAstNode.LOCAL_SCOPE, makeNode(context, fFactory, n, CAstNode.BLOCK_STMT, nextAssignNode, visitNode(n.getBody(), loopContext))), visitNode(continueTarget, context), makeNode( context, fFactory, n, CAstNode.BLOCK_STMT, tmpArrayIncNode))), visitNode(breakTarget, context)))));