Initial upgrade to Platform/JDT 3.4.1
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / AssertStatement.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.core.dom;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 /**
18  * Assert statement AST node type.
19  *
20  * <pre>
21  * AssertStatement:
22  *    <b>assert</b> Expression [ <b>:</b> Expression ] <b>;</b>
23  * </pre>
24  * 
25  * @since 2.0
26  * @noinstantiate This class is not intended to be instantiated by clients.
27  */
28 public class AssertStatement extends Statement {
29                         
30         /**
31          * The "expression" structural property of this node type.
32          * @since 3.0
33          */
34         public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = 
35                 new ChildPropertyDescriptor(AssertStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
36
37         /**
38          * The "message" structural property of this node type.
39          * @since 3.0
40          */
41         public static final ChildPropertyDescriptor MESSAGE_PROPERTY = 
42                 new ChildPropertyDescriptor(AssertStatement.class, "message", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
43
44         /**
45          * A list of property descriptors (element type: 
46          * {@link StructuralPropertyDescriptor}),
47          * or null if uninitialized.
48          */
49         private static final List PROPERTY_DESCRIPTORS;
50         
51         static {
52                 List properyList = new ArrayList(3);
53                 createPropertyList(AssertStatement.class, properyList);
54                 addProperty(EXPRESSION_PROPERTY, properyList);
55                 addProperty(MESSAGE_PROPERTY, properyList);
56                 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
57         }
58
59         /**
60          * Returns a list of structural property descriptors for this node type.
61          * Clients must not modify the result.
62          * 
63          * @param apiLevel the API level; one of the
64          * <code>AST.JLS*</code> constants
65
66          * @return a list of property descriptors (element type: 
67          * {@link StructuralPropertyDescriptor})
68          * @since 3.0
69          */
70         public static List propertyDescriptors(int apiLevel) {
71                 return PROPERTY_DESCRIPTORS;
72         }
73                         
74         /**
75          * The expression; lazily initialized; defaults to a unspecified, but legal,
76          * expression.
77          */
78         private Expression expression = null;
79
80         /**
81          * The message expression; <code>null</code> for none; defaults to none.
82          */
83         private Expression optionalMessageExpression = null;
84         
85         /**
86          * Creates a new unparented assert statement node owned by the given 
87          * AST. By default, the assert statement has an unspecified, but legal,
88          * expression, and not message expression.
89          * <p>
90          * N.B. This constructor is package-private.
91          * </p>
92          * 
93          * @param ast the AST that is to own this node
94          */
95         AssertStatement(AST ast) {
96                 super(ast);
97         }
98
99         /* (omit javadoc for this method)
100          * Method declared on ASTNode.
101          */
102         final List internalStructuralPropertiesForType(int apiLevel) {
103                 return propertyDescriptors(apiLevel);
104         }
105         
106         /* (omit javadoc for this method)
107          * Method declared on ASTNode.
108          */
109         final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
110                 if (property == EXPRESSION_PROPERTY) {
111                         if (get) {
112                                 return getExpression();
113                         } else {
114                                 setExpression((Expression) child);
115                                 return null;
116                         }
117                 }
118                 if (property == MESSAGE_PROPERTY) {
119                         if (get) {
120                                 return getMessage();
121                         } else {
122                                 setMessage((Expression) child);
123                                 return null;
124                         }
125                 }
126                 // allow default implementation to flag the error
127                 return super.internalGetSetChildProperty(property, get, child);
128         }
129
130         /* (omit javadoc for this method)
131          * Method declared on ASTNode.
132          */
133         final int getNodeType0() {
134                 return ASSERT_STATEMENT;
135         }
136
137         /* (omit javadoc for this method)
138          * Method declared on ASTNode.
139          */
140         ASTNode clone0(AST target) {
141                 AssertStatement result = new AssertStatement(target);
142                 result.setSourceRange(this.getStartPosition(), this.getLength());
143                 result.copyLeadingComment(this);
144                 result.setExpression(
145                         (Expression) ASTNode.copySubtree(target, getExpression()));
146                 result.setMessage(
147                         (Expression) ASTNode.copySubtree(target, getMessage()));
148                 return result;
149         }
150
151         /* (omit javadoc for this method)
152          * Method declared on ASTNode.
153          */
154         final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
155                 // dispatch to correct overloaded match method
156                 return matcher.match(this, other);
157         }
158
159         /* (omit javadoc for this method)
160          * Method declared on ASTNode.
161          */
162         void accept0(ASTVisitor visitor) {
163                 boolean visitChildren = visitor.visit(this);
164                 if (visitChildren) {
165                         // visit children in normal left to right reading order
166                         acceptChild(visitor, getExpression());
167                         acceptChild(visitor, getMessage());
168                 }
169                 visitor.endVisit(this);
170         }
171         
172         /**
173          * Returns the first expression of this assert statement.
174          * 
175          * @return the expression node
176          */ 
177         public Expression getExpression() {
178                 if (this.expression == null) {
179                         // lazy init must be thread-safe for readers
180                         synchronized (this) {
181                                 if (this.expression == null) {
182                                         preLazyInit();
183                                         this.expression = new SimpleName(this.ast);
184                                         postLazyInit(this.expression, EXPRESSION_PROPERTY);
185                                 }
186                         }
187                 }
188                 return expression;
189         }
190                 
191         /**
192          * Sets the first expression of this assert statement.
193          * 
194          * @param expression the new expression node
195          * @exception IllegalArgumentException if:
196          * <ul>
197          * <li>the node belongs to a different AST</li>
198          * <li>the node already has a parent</li>
199          * <li>a cycle in would be created</li>
200          * </ul>
201          */ 
202         public void setExpression(Expression expression) {
203                 if (expression == null) {
204                         throw new IllegalArgumentException();
205                 }
206                 // an AssertStatement may occur inside an Expression - must check cycles
207                 ASTNode oldChild = this.expression;
208                 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
209                 this.expression = expression;
210                 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
211         }
212
213         /**
214          * Returns the message expression of this assert statement, or 
215          * <code>null</code> if there is none.
216          * 
217          * @return the message expression node, or <code>null</code> if there 
218          *    is none
219          */ 
220         public Expression getMessage() {
221                 return this.optionalMessageExpression;
222         }
223         
224         /**
225          * Sets or clears the message expression of this assert statement.
226          * 
227          * @param expression the message expression node, or <code>null</code> if 
228          *    there is none
229          * @exception IllegalArgumentException if:
230          * <ul>
231          * <li>the node belongs to a different AST</li>
232          * <li>the node already has a parent</li>
233          * <li>a cycle in would be created</li>
234          * </ul>
235          */ 
236         public void setMessage(Expression expression) {
237                 // an AsertStatement may occur inside an Expression - must check cycles
238                 ASTNode oldChild = this.optionalMessageExpression;
239                 preReplaceChild(oldChild, expression, MESSAGE_PROPERTY);
240                 this.optionalMessageExpression = expression;
241                 postReplaceChild(oldChild, expression, MESSAGE_PROPERTY);
242         }
243         
244         /* (omit javadoc for this method)
245          * Method declared on ASTNode.
246          */
247         int memSize() {
248                 return super.memSize() + 2 * 4;
249         }
250         
251         /* (omit javadoc for this method)
252          * Method declared on ASTNode.
253          */
254         int treeSize() {
255                 return
256                         memSize()
257                         + (this.expression == null ? 0 : getExpression().treeSize())
258                         + (this.optionalMessageExpression == null ? 0 : getMessage().treeSize());
259                         
260         }
261 }
262