1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.List;
18 * If statement AST node type.
21 * <b>if</b> <b>(</b> Expression <b>)</b> Statement [ <b>else</b> Statement]
25 * @noinstantiate This class is not intended to be instantiated by clients.
27 public class IfStatement extends Statement {
30 * The "expression" structural property of this node type.
33 public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
34 new ChildPropertyDescriptor(IfStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
37 * The "thenStatement" structural property of this node type.
40 public static final ChildPropertyDescriptor THEN_STATEMENT_PROPERTY =
41 new ChildPropertyDescriptor(IfStatement.class, "thenStatement", Statement.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
44 * The "elseStatement" structural property of this node type.
47 public static final ChildPropertyDescriptor ELSE_STATEMENT_PROPERTY =
48 new ChildPropertyDescriptor(IfStatement.class, "elseStatement", Statement.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
51 * A list of property descriptors (element type:
52 * {@link StructuralPropertyDescriptor}),
53 * or null if uninitialized.
55 private static final List PROPERTY_DESCRIPTORS;
58 List properyList = new ArrayList(4);
59 createPropertyList(IfStatement.class, properyList);
60 addProperty(EXPRESSION_PROPERTY, properyList);
61 addProperty(THEN_STATEMENT_PROPERTY, properyList);
62 addProperty(ELSE_STATEMENT_PROPERTY, properyList);
63 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
67 * Returns a list of structural property descriptors for this node type.
68 * Clients must not modify the result.
70 * @param apiLevel the API level; one of the
71 * <code>AST.JLS*</code> constants
73 * @return a list of property descriptors (element type:
74 * {@link StructuralPropertyDescriptor})
77 public static List propertyDescriptors(int apiLevel) {
78 return PROPERTY_DESCRIPTORS;
82 * The expression; lazily initialized; defaults to an unspecified, but
85 private Expression expression = null;
88 * The then statement; lazily initialized; defaults to an unspecified, but
91 private Statement thenStatement = null;
94 * The else statement; <code>null</code> for none; defaults to none.
96 private Statement optionalElseStatement = null;
99 * Creates a new unparented if statement node owned by the given
100 * AST. By default, the expresssion is unspecified,
101 * but legal, the then statement is an empty block, and there is no else
104 * N.B. This constructor is package-private.
107 * @param ast the AST that is to own this node
109 IfStatement(AST ast) {
113 /* (omit javadoc for this method)
114 * Method declared on ASTNode.
116 final List internalStructuralPropertiesForType(int apiLevel) {
117 return propertyDescriptors(apiLevel);
120 /* (omit javadoc for this method)
121 * Method declared on ASTNode.
123 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
124 if (property == EXPRESSION_PROPERTY) {
126 return getExpression();
128 setExpression((Expression) child);
132 if (property == THEN_STATEMENT_PROPERTY) {
134 return getThenStatement();
136 setThenStatement((Statement) child);
140 if (property == ELSE_STATEMENT_PROPERTY) {
142 return getElseStatement();
144 setElseStatement((Statement) child);
148 // allow default implementation to flag the error
149 return super.internalGetSetChildProperty(property, get, child);
152 /* (omit javadoc for this method)
153 * Method declared on ASTNode.
155 final int getNodeType0() {
159 /* (omit javadoc for this method)
160 * Method declared on ASTNode.
162 ASTNode clone0(AST target) {
163 IfStatement result = new IfStatement(target);
164 result.setSourceRange(this.getStartPosition(), this.getLength());
165 result.copyLeadingComment(this);
166 result.setExpression((Expression) getExpression().clone(target));
167 result.setThenStatement(
168 (Statement) getThenStatement().clone(target));
169 result.setElseStatement(
170 (Statement) ASTNode.copySubtree(target, getElseStatement()));
174 /* (omit javadoc for this method)
175 * Method declared on ASTNode.
177 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
178 // dispatch to correct overloaded match method
179 return matcher.match(this, other);
182 /* (omit javadoc for this method)
183 * Method declared on ASTNode.
185 void accept0(ASTVisitor visitor) {
186 boolean visitChildren = visitor.visit(this);
188 // visit children in normal left to right reading order
189 acceptChild(visitor, getExpression());
190 acceptChild(visitor, getThenStatement());
191 acceptChild(visitor, getElseStatement());
193 visitor.endVisit(this);
197 * Returns the expression of this if statement.
199 * @return the expression node
201 public Expression getExpression() {
202 if (this.expression == null) {
203 // lazy init must be thread-safe for readers
204 synchronized (this) {
205 if (this.expression == null) {
207 this.expression = new SimpleName(this.ast);
208 postLazyInit(this.expression, EXPRESSION_PROPERTY);
212 return this.expression;
216 * Sets the condition of this if statement.
218 * @param expression the expression node
219 * @exception IllegalArgumentException if:
221 * <li>the node belongs to a different AST</li>
222 * <li>the node already has a parent</li>
223 * <li>a cycle in would be created</li>
226 public void setExpression(Expression expression) {
227 if (expression == null) {
228 throw new IllegalArgumentException();
230 ASTNode oldChild = this.expression;
231 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
232 this.expression = expression;
233 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
237 * Returns the "then" part of this if statement.
239 * @return the "then" statement node
241 public Statement getThenStatement() {
242 if (this.thenStatement == null) {
243 // lazy init must be thread-safe for readers
244 synchronized (this) {
245 if (this.thenStatement == null) {
247 this.thenStatement = new Block(this.ast);
248 postLazyInit(this.thenStatement, THEN_STATEMENT_PROPERTY);
252 return this.thenStatement;
256 * Sets the "then" part of this if statement.
258 * Special note: The Java language does not allow a local variable declaration
259 * to appear as the "then" part of an if statement (they may only appear within a
260 * block). However, the AST will allow a <code>VariableDeclarationStatement</code>
261 * as the thenStatement of a <code>IfStatement</code>. To get something that will
262 * compile, be sure to embed the <code>VariableDeclarationStatement</code>
263 * inside a <code>Block</code>.
266 * @param statement the "then" statement node
267 * @exception IllegalArgumentException if:
269 * <li>the node belongs to a different AST</li>
270 * <li>the node already has a parent</li>
271 * <li>a cycle in would be created</li>
274 public void setThenStatement(Statement statement) {
275 if (statement == null) {
276 throw new IllegalArgumentException();
278 ASTNode oldChild = this.thenStatement;
279 preReplaceChild(oldChild, statement, THEN_STATEMENT_PROPERTY);
280 this.thenStatement = statement;
281 postReplaceChild(oldChild, statement, THEN_STATEMENT_PROPERTY);
285 * Returns the "else" part of this if statement, or <code>null</code> if
286 * this if statement has <b>no</b> "else" part.
288 * Note that there is a subtle difference between having no else
289 * statement and having an empty statement ("{}") or null statement (";").
292 * @return the "else" statement node, or <code>null</code> if none
294 public Statement getElseStatement() {
295 return this.optionalElseStatement;
299 * Sets or clears the "else" part of this if statement.
301 * Note that there is a subtle difference between having no else part
302 * (as in <code>"if(true){}"</code>) and having an empty block (as in
303 * "if(true){}else{}") or null statement (as in "if(true){}else;").
306 * Special note: The Java language does not allow a local variable declaration
307 * to appear as the "else" part of an if statement (they may only appear within a
308 * block). However, the AST will allow a <code>VariableDeclarationStatement</code>
309 * as the elseStatement of a <code>IfStatement</code>. To get something that will
310 * compile, be sure to embed the <code>VariableDeclarationStatement</code>
311 * inside a <code>Block</code>.
314 * @param statement the "else" statement node, or <code>null</code> if
316 * @exception IllegalArgumentException if:
318 * <li>the node belongs to a different AST</li>
319 * <li>the node already has a parent</li>
320 * <li>a cycle in would be created</li>
323 public void setElseStatement(Statement statement) {
324 ASTNode oldChild = this.optionalElseStatement;
325 preReplaceChild(oldChild, statement, ELSE_STATEMENT_PROPERTY);
326 this.optionalElseStatement = statement;
327 postReplaceChild(oldChild, statement, ELSE_STATEMENT_PROPERTY);
330 /* (omit javadoc for this method)
331 * Method declared on ASTNode.
334 return super.memSize() + 3 * 4;
337 /* (omit javadoc for this method)
338 * Method declared on ASTNode.
343 + (this.expression == null ? 0 : getExpression().treeSize())
344 + (this.thenStatement == null ? 0 : getThenStatement().treeSize())
345 + (this.optionalElseStatement == null ? 0 : getElseStatement().treeSize());