001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.core.dom;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: /**
016: * If statement AST node type.
017: * <pre>
018: * IfStatement:
019: * <b>if</b> <b>(</b> Expression <b>)</b> Statement [ <b>else</b> Statement]
020: * </pre>
021: *
022: * @since 2.0
023: */
024: public class IfStatement extends Statement {
025:
026: /**
027: * The "expression" structural property of this node type.
028: * @since 3.0
029: */
030: public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = new ChildPropertyDescriptor(
031: IfStatement.class,
032: "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
033:
034: /**
035: * The "thenStatement" structural property of this node type.
036: * @since 3.0
037: */
038: public static final ChildPropertyDescriptor THEN_STATEMENT_PROPERTY = new ChildPropertyDescriptor(
039: IfStatement.class,
040: "thenStatement", Statement.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
041:
042: /**
043: * The "elseStatement" structural property of this node type.
044: * @since 3.0
045: */
046: public static final ChildPropertyDescriptor ELSE_STATEMENT_PROPERTY = new ChildPropertyDescriptor(
047: IfStatement.class,
048: "elseStatement", Statement.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
049:
050: /**
051: * A list of property descriptors (element type:
052: * {@link StructuralPropertyDescriptor}),
053: * or null if uninitialized.
054: */
055: private static final List PROPERTY_DESCRIPTORS;
056:
057: static {
058: List properyList = new ArrayList(4);
059: createPropertyList(IfStatement.class, properyList);
060: addProperty(EXPRESSION_PROPERTY, properyList);
061: addProperty(THEN_STATEMENT_PROPERTY, properyList);
062: addProperty(ELSE_STATEMENT_PROPERTY, properyList);
063: PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
064: }
065:
066: /**
067: * Returns a list of structural property descriptors for this node type.
068: * Clients must not modify the result.
069: *
070: * @param apiLevel the API level; one of the
071: * <code>AST.JLS*</code> constants
072:
073: * @return a list of property descriptors (element type:
074: * {@link StructuralPropertyDescriptor})
075: * @since 3.0
076: */
077: public static List propertyDescriptors(int apiLevel) {
078: return PROPERTY_DESCRIPTORS;
079: }
080:
081: /**
082: * The expression; lazily initialized; defaults to an unspecified, but
083: * legal, expression.
084: */
085: private Expression expression = null;
086:
087: /**
088: * The then statement; lazily initialized; defaults to an unspecified, but
089: * legal, statement.
090: */
091: private Statement thenStatement = null;
092:
093: /**
094: * The else statement; <code>null</code> for none; defaults to none.
095: */
096: private Statement optionalElseStatement = null;
097:
098: /**
099: * 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
102: * statement.
103: * <p>
104: * N.B. This constructor is package-private.
105: * </p>
106: *
107: * @param ast the AST that is to own this node
108: */
109: IfStatement(AST ast) {
110: super (ast);
111: }
112:
113: /* (omit javadoc for this method)
114: * Method declared on ASTNode.
115: */
116: final List internalStructuralPropertiesForType(int apiLevel) {
117: return propertyDescriptors(apiLevel);
118: }
119:
120: /* (omit javadoc for this method)
121: * Method declared on ASTNode.
122: */
123: final ASTNode internalGetSetChildProperty(
124: ChildPropertyDescriptor property, boolean get, ASTNode child) {
125: if (property == EXPRESSION_PROPERTY) {
126: if (get) {
127: return getExpression();
128: } else {
129: setExpression((Expression) child);
130: return null;
131: }
132: }
133: if (property == THEN_STATEMENT_PROPERTY) {
134: if (get) {
135: return getThenStatement();
136: } else {
137: setThenStatement((Statement) child);
138: return null;
139: }
140: }
141: if (property == ELSE_STATEMENT_PROPERTY) {
142: if (get) {
143: return getElseStatement();
144: } else {
145: setElseStatement((Statement) child);
146: return null;
147: }
148: }
149: // allow default implementation to flag the error
150: return super .internalGetSetChildProperty(property, get, child);
151: }
152:
153: /* (omit javadoc for this method)
154: * Method declared on ASTNode.
155: */
156: final int getNodeType0() {
157: return IF_STATEMENT;
158: }
159:
160: /* (omit javadoc for this method)
161: * Method declared on ASTNode.
162: */
163: ASTNode clone0(AST target) {
164: IfStatement result = new IfStatement(target);
165: result
166: .setSourceRange(this .getStartPosition(), this
167: .getLength());
168: result.copyLeadingComment(this );
169: result
170: .setExpression((Expression) getExpression().clone(
171: target));
172: result.setThenStatement((Statement) getThenStatement().clone(
173: target));
174: result.setElseStatement((Statement) ASTNode.copySubtree(target,
175: getElseStatement()));
176: return result;
177: }
178:
179: /* (omit javadoc for this method)
180: * Method declared on ASTNode.
181: */
182: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
183: // dispatch to correct overloaded match method
184: return matcher.match(this , other);
185: }
186:
187: /* (omit javadoc for this method)
188: * Method declared on ASTNode.
189: */
190: void accept0(ASTVisitor visitor) {
191: boolean visitChildren = visitor.visit(this );
192: if (visitChildren) {
193: // visit children in normal left to right reading order
194: acceptChild(visitor, getExpression());
195: acceptChild(visitor, getThenStatement());
196: acceptChild(visitor, getElseStatement());
197: }
198: visitor.endVisit(this );
199: }
200:
201: /**
202: * Returns the expression of this if statement.
203: *
204: * @return the expression node
205: */
206: public Expression getExpression() {
207: if (this .expression == null) {
208: // lazy init must be thread-safe for readers
209: synchronized (this ) {
210: if (this .expression == null) {
211: preLazyInit();
212: this .expression = new SimpleName(this .ast);
213: postLazyInit(this .expression, EXPRESSION_PROPERTY);
214: }
215: }
216: }
217: return this .expression;
218: }
219:
220: /**
221: * Sets the condition of this if statement.
222: *
223: * @param expression the expression node
224: * @exception IllegalArgumentException if:
225: * <ul>
226: * <li>the node belongs to a different AST</li>
227: * <li>the node already has a parent</li>
228: * <li>a cycle in would be created</li>
229: * </ul>
230: */
231: public void setExpression(Expression expression) {
232: if (expression == null) {
233: throw new IllegalArgumentException();
234: }
235: ASTNode oldChild = this .expression;
236: preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
237: this .expression = expression;
238: postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
239: }
240:
241: /**
242: * Returns the "then" part of this if statement.
243: *
244: * @return the "then" statement node
245: */
246: public Statement getThenStatement() {
247: if (this .thenStatement == null) {
248: // lazy init must be thread-safe for readers
249: synchronized (this ) {
250: if (this .thenStatement == null) {
251: preLazyInit();
252: this .thenStatement = new Block(this .ast);
253: postLazyInit(this .thenStatement,
254: THEN_STATEMENT_PROPERTY);
255: }
256: }
257: }
258: return this .thenStatement;
259: }
260:
261: /**
262: * Sets the "then" part of this if statement.
263: * <p>
264: * Special note: The Java language does not allow a local variable declaration
265: * to appear as the "then" part of an if statement (they may only appear within a
266: * block). However, the AST will allow a <code>VariableDeclarationStatement</code>
267: * as the thenStatement of a <code>IfStatement</code>. To get something that will
268: * compile, be sure to embed the <code>VariableDeclarationStatement</code>
269: * inside a <code>Block</code>.
270: * </p>
271: *
272: * @param statement the "then" statement node
273: * @exception IllegalArgumentException if:
274: * <ul>
275: * <li>the node belongs to a different AST</li>
276: * <li>the node already has a parent</li>
277: * <li>a cycle in would be created</li>
278: * </ul>
279: */
280: public void setThenStatement(Statement statement) {
281: if (statement == null) {
282: throw new IllegalArgumentException();
283: }
284: ASTNode oldChild = this .thenStatement;
285: preReplaceChild(oldChild, statement, THEN_STATEMENT_PROPERTY);
286: this .thenStatement = statement;
287: postReplaceChild(oldChild, statement, THEN_STATEMENT_PROPERTY);
288: }
289:
290: /**
291: * Returns the "else" part of this if statement, or <code>null</code> if
292: * this if statement has <b>no</b> "else" part.
293: * <p>
294: * Note that there is a subtle difference between having no else
295: * statement and having an empty statement ("{}") or null statement (";").
296: * </p>
297: *
298: * @return the "else" statement node, or <code>null</code> if none
299: */
300: public Statement getElseStatement() {
301: return this .optionalElseStatement;
302: }
303:
304: /**
305: * Sets or clears the "else" part of this if statement.
306: * <p>
307: * Note that there is a subtle difference between having no else part
308: * (as in <code>"if(true){}"</code>) and having an empty block (as in
309: * "if(true){}else{}") or null statement (as in "if(true){}else;").
310: * </p>
311: * <p>
312: * Special note: The Java language does not allow a local variable declaration
313: * to appear as the "else" part of an if statement (they may only appear within a
314: * block). However, the AST will allow a <code>VariableDeclarationStatement</code>
315: * as the elseStatement of a <code>IfStatement</code>. To get something that will
316: * compile, be sure to embed the <code>VariableDeclarationStatement</code>
317: * inside a <code>Block</code>.
318: * </p>
319: *
320: * @param statement the "else" statement node, or <code>null</code> if
321: * there is none
322: * @exception IllegalArgumentException if:
323: * <ul>
324: * <li>the node belongs to a different AST</li>
325: * <li>the node already has a parent</li>
326: * <li>a cycle in would be created</li>
327: * </ul>
328: */
329: public void setElseStatement(Statement statement) {
330: ASTNode oldChild = this .optionalElseStatement;
331: preReplaceChild(oldChild, statement, ELSE_STATEMENT_PROPERTY);
332: this .optionalElseStatement = statement;
333: postReplaceChild(oldChild, statement, ELSE_STATEMENT_PROPERTY);
334: }
335:
336: /* (omit javadoc for this method)
337: * Method declared on ASTNode.
338: */
339: int memSize() {
340: return super .memSize() + 3 * 4;
341: }
342:
343: /* (omit javadoc for this method)
344: * Method declared on ASTNode.
345: */
346: int treeSize() {
347: return memSize()
348: + (this .expression == null ? 0 : getExpression()
349: .treeSize())
350: + (this .thenStatement == null ? 0 : getThenStatement()
351: .treeSize())
352: + (this .optionalElseStatement == null ? 0
353: : getElseStatement().treeSize());
354: }
355: }
|