KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > IfStatement


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.core.dom;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * If statement AST node type.
19  * <pre>
20  * IfStatement:
21  * <b>if</b> <b>(</b> Expression <b>)</b> Statement [ <b>else</b> Statement]
22  * </pre>
23  *
24  * @since 2.0
25  */

26 public class IfStatement extends Statement {
27     
28     /**
29      * The "expression" structural property of this node type.
30      * @since 3.0
31      */

32     public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
33         new ChildPropertyDescriptor(IfStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
34

35     /**
36      * The "thenStatement" structural property of this node type.
37      * @since 3.0
38      */

39     public static final ChildPropertyDescriptor THEN_STATEMENT_PROPERTY =
40         new ChildPropertyDescriptor(IfStatement.class, "thenStatement", Statement.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
41

42     /**
43      * The "elseStatement" structural property of this node type.
44      * @since 3.0
45      */

46     public static final ChildPropertyDescriptor ELSE_STATEMENT_PROPERTY =
47         new ChildPropertyDescriptor(IfStatement.class, "elseStatement", Statement.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
48

49     /**
50      * A list of property descriptors (element type:
51      * {@link StructuralPropertyDescriptor}),
52      * or null if uninitialized.
53      */

54     private static final List JavaDoc PROPERTY_DESCRIPTORS;
55     
56     static {
57         List JavaDoc properyList = new ArrayList JavaDoc(4);
58         createPropertyList(IfStatement.class, properyList);
59         addProperty(EXPRESSION_PROPERTY, properyList);
60         addProperty(THEN_STATEMENT_PROPERTY, properyList);
61         addProperty(ELSE_STATEMENT_PROPERTY, properyList);
62         PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
63     }
64
65     /**
66      * Returns a list of structural property descriptors for this node type.
67      * Clients must not modify the result.
68      *
69      * @param apiLevel the API level; one of the
70      * <code>AST.JLS&ast;</code> constants
71
72      * @return a list of property descriptors (element type:
73      * {@link StructuralPropertyDescriptor})
74      * @since 3.0
75      */

76     public static List JavaDoc propertyDescriptors(int apiLevel) {
77         return PROPERTY_DESCRIPTORS;
78     }
79             
80     /**
81      * The expression; lazily initialized; defaults to an unspecified, but
82      * legal, expression.
83      */

84     private Expression expression = null;
85
86     /**
87      * The then statement; lazily initialized; defaults to an unspecified, but
88      * legal, statement.
89      */

90     private Statement thenStatement = null;
91
92     /**
93      * The else statement; <code>null</code> for none; defaults to none.
94      */

95     private Statement optionalElseStatement = null;
96
97     /**
98      * Creates a new unparented if statement node owned by the given
99      * AST. By default, the expresssion is unspecified,
100      * but legal, the then statement is an empty block, and there is no else
101      * statement.
102      * <p>
103      * N.B. This constructor is package-private.
104      * </p>
105      *
106      * @param ast the AST that is to own this node
107      */

108     IfStatement(AST ast) {
109         super(ast);
110     }
111     
112     /* (omit javadoc for this method)
113      * Method declared on ASTNode.
114      */

115     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
116         return propertyDescriptors(apiLevel);
117     }
118     
119     /* (omit javadoc for this method)
120      * Method declared on ASTNode.
121      */

122     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
123         if (property == EXPRESSION_PROPERTY) {
124             if (get) {
125                 return getExpression();
126             } else {
127                 setExpression((Expression) child);
128                 return null;
129             }
130         }
131         if (property == THEN_STATEMENT_PROPERTY) {
132             if (get) {
133                 return getThenStatement();
134             } else {
135                 setThenStatement((Statement) child);
136                 return null;
137             }
138         }
139         if (property == ELSE_STATEMENT_PROPERTY) {
140             if (get) {
141                 return getElseStatement();
142             } else {
143                 setElseStatement((Statement) child);
144                 return null;
145             }
146         }
147         // allow default implementation to flag the error
148
return super.internalGetSetChildProperty(property, get, child);
149     }
150     
151     /* (omit javadoc for this method)
152      * Method declared on ASTNode.
153      */

154     final int getNodeType0() {
155         return IF_STATEMENT;
156     }
157
158     /* (omit javadoc for this method)
159      * Method declared on ASTNode.
160      */

161     ASTNode clone0(AST target) {
162         IfStatement result = new IfStatement(target);
163         result.setSourceRange(this.getStartPosition(), this.getLength());
164         result.copyLeadingComment(this);
165         result.setExpression((Expression) getExpression().clone(target));
166         result.setThenStatement(
167             (Statement) getThenStatement().clone(target));
168         result.setElseStatement(
169             (Statement) ASTNode.copySubtree(target, getElseStatement()));
170         return result;
171     }
172
173     /* (omit javadoc for this method)
174      * Method declared on ASTNode.
175      */

176     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
177         // dispatch to correct overloaded match method
178
return matcher.match(this, other);
179     }
180
181     /* (omit javadoc for this method)
182      * Method declared on ASTNode.
183      */

184     void accept0(ASTVisitor visitor) {
185         boolean visitChildren = visitor.visit(this);
186         if (visitChildren) {
187             // visit children in normal left to right reading order
188
acceptChild(visitor, getExpression());
189             acceptChild(visitor, getThenStatement());
190             acceptChild(visitor, getElseStatement());
191         }
192         visitor.endVisit(this);
193     }
194     
195     /**
196      * Returns the expression of this if statement.
197      *
198      * @return the expression node
199      */

200     public Expression getExpression() {
201         if (this.expression == null) {
202             // lazy init must be thread-safe for readers
203
synchronized (this) {
204                 if (this.expression == null) {
205                     preLazyInit();
206                     this.expression = new SimpleName(this.ast);
207                     postLazyInit(this.expression, EXPRESSION_PROPERTY);
208                 }
209             }
210         }
211         return this.expression;
212     }
213     
214     /**
215      * Sets the condition of this if statement.
216      *
217      * @param expression the expression node
218      * @exception IllegalArgumentException if:
219      * <ul>
220      * <li>the node belongs to a different AST</li>
221      * <li>the node already has a parent</li>
222      * <li>a cycle in would be created</li>
223      * </ul>
224      */

225     public void setExpression(Expression expression) {
226         if (expression == null) {
227             throw new IllegalArgumentException JavaDoc();
228         }
229         ASTNode oldChild = this.expression;
230         preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
231         this.expression = expression;
232         postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
233     }
234
235     /**
236      * Returns the "then" part of this if statement.
237      *
238      * @return the "then" statement node
239      */

240     public Statement getThenStatement() {
241         if (this.thenStatement == null) {
242             // lazy init must be thread-safe for readers
243
synchronized (this) {
244                 if (this.thenStatement == null) {
245                     preLazyInit();
246                     this.thenStatement = new Block(this.ast);
247                     postLazyInit(this.thenStatement, THEN_STATEMENT_PROPERTY);
248                 }
249             }
250         }
251         return this.thenStatement;
252     }
253     
254     /**
255      * Sets the "then" part of this if statement.
256      * <p>
257      * Special note: The Java language does not allow a local variable declaration
258      * to appear as the "then" part of an if statement (they may only appear within a
259      * block). However, the AST will allow a <code>VariableDeclarationStatement</code>
260      * as the thenStatement of a <code>IfStatement</code>. To get something that will
261      * compile, be sure to embed the <code>VariableDeclarationStatement</code>
262      * inside a <code>Block</code>.
263      * </p>
264      *
265      * @param statement the "then" statement node
266      * @exception IllegalArgumentException if:
267      * <ul>
268      * <li>the node belongs to a different AST</li>
269      * <li>the node already has a parent</li>
270      * <li>a cycle in would be created</li>
271      * </ul>
272      */

273     public void setThenStatement(Statement statement) {
274         if (statement == null) {
275             throw new IllegalArgumentException JavaDoc();
276         }
277         ASTNode oldChild = this.thenStatement;
278         preReplaceChild(oldChild, statement, THEN_STATEMENT_PROPERTY);
279         this.thenStatement = statement;
280         postReplaceChild(oldChild, statement, THEN_STATEMENT_PROPERTY);
281     }
282
283     /**
284      * Returns the "else" part of this if statement, or <code>null</code> if
285      * this if statement has <b>no</b> "else" part.
286      * <p>
287      * Note that there is a subtle difference between having no else
288      * statement and having an empty statement ("{}") or null statement (";").
289      * </p>
290      *
291      * @return the "else" statement node, or <code>null</code> if none
292      */

293     public Statement getElseStatement() {
294         return this.optionalElseStatement;
295     }
296
297     /**
298      * Sets or clears the "else" part of this if statement.
299      * <p>
300      * Note that there is a subtle difference between having no else part
301      * (as in <code>"if(true){}"</code>) and having an empty block (as in
302      * "if(true){}else{}") or null statement (as in "if(true){}else;").
303      * </p>
304      * <p>
305      * Special note: The Java language does not allow a local variable declaration
306      * to appear as the "else" part of an if statement (they may only appear within a
307      * block). However, the AST will allow a <code>VariableDeclarationStatement</code>
308      * as the elseStatement of a <code>IfStatement</code>. To get something that will
309      * compile, be sure to embed the <code>VariableDeclarationStatement</code>
310      * inside a <code>Block</code>.
311      * </p>
312      *
313      * @param statement the "else" statement node, or <code>null</code> if
314      * there is none
315      * @exception IllegalArgumentException if:
316      * <ul>
317      * <li>the node belongs to a different AST</li>
318      * <li>the node already has a parent</li>
319      * <li>a cycle in would be created</li>
320      * </ul>
321      */

322     public void setElseStatement(Statement statement) {
323         ASTNode oldChild = this.optionalElseStatement;
324         preReplaceChild(oldChild, statement, ELSE_STATEMENT_PROPERTY);
325         this.optionalElseStatement = statement;
326         postReplaceChild(oldChild, statement, ELSE_STATEMENT_PROPERTY);
327     }
328     
329     /* (omit javadoc for this method)
330      * Method declared on ASTNode.
331      */

332     int memSize() {
333         return super.memSize() + 3 * 4;
334     }
335     
336     /* (omit javadoc for this method)
337      * Method declared on ASTNode.
338      */

339     int treeSize() {
340         return
341             memSize()
342             + (this.expression == null ? 0 : getExpression().treeSize())
343             + (this.thenStatement == null ? 0 : getThenStatement().treeSize())
344             + (this.optionalElseStatement == null ? 0 : getElseStatement().treeSize());
345     }
346 }
347
348
Popular Tags