/******************************************************************************* * Copyright (c) 2000, 2008 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package net.sourceforge.phpdt.core.dom; import java.util.ArrayList; import java.util.List; /** * For statement AST node type. * *
* ForStatement: * for ( * [ ForInit ]; * [ Expression ] ; * [ ForUpdate ] ) * Statement * ForInit: * Expression { , Expression } * ForUpdate: * Expression { , Expression } **
* Note: When variables are declared in the initializer
* of a for statement such as "for (int a=1, b=2;;);
",
* they should be represented as a single
* VariableDeclarationExpression
* with two fragments, rather than being split up into a pair
* of expressions.
*
AST.JLS*
constants
* @return a list of property descriptors (element type:
* {@link StructuralPropertyDescriptor})
* @since 3.0
*/
public static List propertyDescriptors(int apiLevel) {
return PROPERTY_DESCRIPTORS;
}
/**
* The list of initializer expressions (element type:
* Expression
). Defaults to an empty list.
*/
private ASTNode.NodeList initializers =
new ASTNode.NodeList(INITIALIZERS_PROPERTY);
/**
* The condition expression; null
for none; defaults to none.
*/
private Expression optionalConditionExpression = null;
/**
* The list of update expressions (element type:
* Expression
). Defaults to an empty list.
*/
private ASTNode.NodeList updaters =
new ASTNode.NodeList(UPDATERS_PROPERTY);
/**
* The body statement; lazily initialized; defaults to an empty block
* statement.
*/
private Statement body = null;
/**
* Creates a new AST node for a for statement owned by the given AST.
* By default, there are no initializers, no condition expression,
* no updaters, and the body is an empty block.
*
* @param ast the AST that is to own this node
*/
ForStatement(AST ast) {
super(ast);
}
/* (omit javadoc for this method)
* Method declared on ASTNode.
*/
final List internalStructuralPropertiesForType(int apiLevel) {
return propertyDescriptors(apiLevel);
}
/* (omit javadoc for this method)
* Method declared on ASTNode.
*/
final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
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);
}
/* (omit javadoc for this method)
* Method declared on ASTNode.
*/
final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
if (property == INITIALIZERS_PROPERTY) {
return initializers();
}
if (property == UPDATERS_PROPERTY) {
return updaters();
}
// allow default implementation to flag the error
return super.internalGetChildListProperty(property);
}
/* (omit javadoc for this method)
* Method declared on ASTNode.
*/
final int getNodeType0() {
return FOR_STATEMENT;
}
/* (omit javadoc for this method)
* Method declared on ASTNode.
*/
ASTNode clone0(AST target) {
ForStatement result = new ForStatement(target);
result.setSourceRange(this.getStartPosition(), this.getLength());
result.copyLeadingComment(this);
result.initializers().addAll(ASTNode.copySubtrees(target, initializers()));
result.setExpression(
(Expression) ASTNode.copySubtree(target, getExpression()));
result.updaters().addAll(ASTNode.copySubtrees(target, updaters()));
result.setBody(
(Statement) ASTNode.copySubtree(target, getBody()));
return result;
}
/* (omit javadoc for this method)
* Method declared on ASTNode.
*/
final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
// dispatch to correct overloaded match method
return matcher.match(this, other);
}
/* (omit javadoc for this method)
* Method declared on ASTNode.
*/
void accept0(ASTVisitor visitor) {
boolean visitChildren = visitor.visit(this);
if (visitChildren) {
// visit children in normal left to right reading order
acceptChildren(visitor, this.initializers);
acceptChild(visitor, getExpression());
acceptChildren(visitor, this.updaters);
acceptChild(visitor, getBody());
}
visitor.endVisit(this);
}
/**
* Returns the live ordered list of initializer expressions in this for
* statement.
*
* The list should consist of either a list of so called statement
* expressions (JLS2, 14.8), or a single VariableDeclarationExpression
.
* Otherwise, the for statement would have no Java source equivalent.
*
Expression
)
*/
public List initializers() {
return this.initializers;
}
/**
* Returns the condition expression of this for statement, or
* null
if there is none.
*
* @return the condition expression node, or null
if
* there is none
*/
public Expression getExpression() {
return this.optionalConditionExpression;
}
/**
* Sets or clears the condition expression of this return statement.
*
* @param expression the condition expression node, or null
* if there is none
* @exception IllegalArgumentException if:
* * The list should consist of so called statement expressions. Otherwise, * the for statement would have no Java source equivalent. *
* * @return the live list of update expressions * (element type:Expression
)
*/
public List updaters() {
return this.updaters;
}
/**
* Returns the body of this for statement.
*
* @return the body statement node
*/
public Statement getBody() {
if (this.body == null) {
// lazy init must be thread-safe for readers
synchronized (this) {
if (this.body == null) {
preLazyInit();
this.body = new Block(this.ast);
postLazyInit(this.body, BODY_PROPERTY);
}
}
}
return this.body;
}
/**
* Sets the body of this for statement.
*
* Special note: The Java language does not allow a local variable declaration
* to appear as the body of a for statement (they may only appear within a
* block). However, the AST will allow a VariableDeclarationStatement
* as the body of a ForStatement
. To get something that will
* compile, be sure to embed the VariableDeclarationStatement
* inside a Block
.
*