KickJava   Java API By Example, From Geeks To Geeks.

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


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  * 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  */

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

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

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

40     public static final ChildPropertyDescriptor MESSAGE_PROPERTY =
41         new ChildPropertyDescriptor(AssertStatement.class, "message", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
42

43     /**
44      * A list of property descriptors (element type:
45      * {@link StructuralPropertyDescriptor}),
46      * or null if uninitialized.
47      */

48     private static final List JavaDoc PROPERTY_DESCRIPTORS;
49     
50     static {
51         List JavaDoc properyList = new ArrayList JavaDoc(3);
52         createPropertyList(AssertStatement.class, properyList);
53         addProperty(EXPRESSION_PROPERTY, properyList);
54         addProperty(MESSAGE_PROPERTY, properyList);
55         PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
56     }
57
58     /**
59      * Returns a list of structural property descriptors for this node type.
60      * Clients must not modify the result.
61      *
62      * @param apiLevel the API level; one of the
63      * <code>AST.JLS&ast;</code> constants
64
65      * @return a list of property descriptors (element type:
66      * {@link StructuralPropertyDescriptor})
67      * @since 3.0
68      */

69     public static List JavaDoc propertyDescriptors(int apiLevel) {
70         return PROPERTY_DESCRIPTORS;
71     }
72             
73     /**
74      * The expression; lazily initialized; defaults to a unspecified, but legal,
75      * expression.
76      */

77     private Expression expression = null;
78
79     /**
80      * The message expression; <code>null</code> for none; defaults to none.
81      */

82     private Expression optionalMessageExpression = null;
83     
84     /**
85      * Creates a new unparented assert statement node owned by the given
86      * AST. By default, the assert statement has an unspecified, but legal,
87      * expression, and not message expression.
88      * <p>
89      * N.B. This constructor is package-private.
90      * </p>
91      *
92      * @param ast the AST that is to own this node
93      */

94     AssertStatement(AST ast) {
95         super(ast);
96     }
97
98     /* (omit javadoc for this method)
99      * Method declared on ASTNode.
100      */

101     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
102         return propertyDescriptors(apiLevel);
103     }
104     
105     /* (omit javadoc for this method)
106      * Method declared on ASTNode.
107      */

108     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
109         if (property == EXPRESSION_PROPERTY) {
110             if (get) {
111                 return getExpression();
112             } else {
113                 setExpression((Expression) child);
114                 return null;
115             }
116         }
117         if (property == MESSAGE_PROPERTY) {
118             if (get) {
119                 return getMessage();
120             } else {
121                 setMessage((Expression) child);
122                 return null;
123             }
124         }
125         // allow default implementation to flag the error
126
return super.internalGetSetChildProperty(property, get, child);
127     }
128
129     /* (omit javadoc for this method)
130      * Method declared on ASTNode.
131      */

132     final int getNodeType0() {
133         return ASSERT_STATEMENT;
134     }
135
136     /* (omit javadoc for this method)
137      * Method declared on ASTNode.
138      */

139     ASTNode clone0(AST target) {
140         AssertStatement result = new AssertStatement(target);
141         result.setSourceRange(this.getStartPosition(), this.getLength());
142         result.copyLeadingComment(this);
143         result.setExpression(
144             (Expression) ASTNode.copySubtree(target, getExpression()));
145         result.setMessage(
146             (Expression) ASTNode.copySubtree(target, getMessage()));
147         return result;
148     }
149
150     /* (omit javadoc for this method)
151      * Method declared on ASTNode.
152      */

153     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
154         // dispatch to correct overloaded match method
155
return matcher.match(this, other);
156     }
157
158     /* (omit javadoc for this method)
159      * Method declared on ASTNode.
160      */

161     void accept0(ASTVisitor visitor) {
162         boolean visitChildren = visitor.visit(this);
163         if (visitChildren) {
164             // visit children in normal left to right reading order
165
acceptChild(visitor, getExpression());
166             acceptChild(visitor, getMessage());
167         }
168         visitor.endVisit(this);
169     }
170     
171     /**
172      * Returns the first expression of this assert statement.
173      *
174      * @return the expression node
175      */

176     public Expression getExpression() {
177         if (this.expression == null) {
178             // lazy init must be thread-safe for readers
179
synchronized (this) {
180                 if (this.expression == null) {
181                     preLazyInit();
182                     this.expression = new SimpleName(this.ast);
183                     postLazyInit(this.expression, EXPRESSION_PROPERTY);
184                 }
185             }
186         }
187         return expression;
188     }
189         
190     /**
191      * Sets the first expression of this assert statement.
192      *
193      * @param expression the new expression node
194      * @exception IllegalArgumentException if:
195      * <ul>
196      * <li>the node belongs to a different AST</li>
197      * <li>the node already has a parent</li>
198      * <li>a cycle in would be created</li>
199      * </ul>
200      */

201     public void setExpression(Expression expression) {
202         if (expression == null) {
203             throw new IllegalArgumentException JavaDoc();
204         }
205         // an AssertStatement may occur inside an Expression - must check cycles
206
ASTNode oldChild = this.expression;
207         preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
208         this.expression = expression;
209         postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
210     }
211
212     /**
213      * Returns the message expression of this assert statement, or
214      * <code>null</code> if there is none.
215      *
216      * @return the message expression node, or <code>null</code> if there
217      * is none
218      */

219     public Expression getMessage() {
220         return this.optionalMessageExpression;
221     }
222     
223     /**
224      * Sets or clears the message expression of this assert statement.
225      *
226      * @param expression the message expression node, or <code>null</code> if
227      * there is none
228      * @exception IllegalArgumentException if:
229      * <ul>
230      * <li>the node belongs to a different AST</li>
231      * <li>the node already has a parent</li>
232      * <li>a cycle in would be created</li>
233      * </ul>
234      */

235     public void setMessage(Expression expression) {
236         // an AsertStatement may occur inside an Expression - must check cycles
237
ASTNode oldChild = this.optionalMessageExpression;
238         preReplaceChild(oldChild, expression, MESSAGE_PROPERTY);
239         this.optionalMessageExpression = expression;
240         postReplaceChild(oldChild, expression, MESSAGE_PROPERTY);
241     }
242     
243     /* (omit javadoc for this method)
244      * Method declared on ASTNode.
245      */

246     int memSize() {
247         return super.memSize() + 2 * 4;
248     }
249     
250     /* (omit javadoc for this method)
251      * Method declared on ASTNode.
252      */

253     int treeSize() {
254         return
255             memSize()
256             + (this.expression == null ? 0 : getExpression().treeSize())
257             + (this.optionalMessageExpression == null ? 0 : getMessage().treeSize());
258             
259     }
260 }
261
262
Popular Tags