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 * Assert statement AST node type.
22 * <b>assert</b> Expression [ <b>:</b> Expression ] <b>;</b>
26 * @noinstantiate This class is not intended to be instantiated by clients.
28 public class AssertStatement extends Statement {
31 * The "expression" structural property of this node type.
34 public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
35 new ChildPropertyDescriptor(AssertStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
38 * The "message" structural property of this node type.
41 public static final ChildPropertyDescriptor MESSAGE_PROPERTY =
42 new ChildPropertyDescriptor(AssertStatement.class, "message", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
45 * A list of property descriptors (element type:
46 * {@link StructuralPropertyDescriptor}),
47 * or null if uninitialized.
49 private static final List PROPERTY_DESCRIPTORS;
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);
60 * Returns a list of structural property descriptors for this node type.
61 * Clients must not modify the result.
63 * @param apiLevel the API level; one of the
64 * <code>AST.JLS*</code> constants
66 * @return a list of property descriptors (element type:
67 * {@link StructuralPropertyDescriptor})
70 public static List propertyDescriptors(int apiLevel) {
71 return PROPERTY_DESCRIPTORS;
75 * The expression; lazily initialized; defaults to a unspecified, but legal,
78 private Expression expression = null;
81 * The message expression; <code>null</code> for none; defaults to none.
83 private Expression optionalMessageExpression = null;
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.
90 * N.B. This constructor is package-private.
93 * @param ast the AST that is to own this node
95 AssertStatement(AST ast) {
99 /* (omit javadoc for this method)
100 * Method declared on ASTNode.
102 final List internalStructuralPropertiesForType(int apiLevel) {
103 return propertyDescriptors(apiLevel);
106 /* (omit javadoc for this method)
107 * Method declared on ASTNode.
109 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
110 if (property == EXPRESSION_PROPERTY) {
112 return getExpression();
114 setExpression((Expression) child);
118 if (property == MESSAGE_PROPERTY) {
122 setMessage((Expression) child);
126 // allow default implementation to flag the error
127 return super.internalGetSetChildProperty(property, get, child);
130 /* (omit javadoc for this method)
131 * Method declared on ASTNode.
133 final int getNodeType0() {
134 return ASSERT_STATEMENT;
137 /* (omit javadoc for this method)
138 * Method declared on ASTNode.
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()));
147 (Expression) ASTNode.copySubtree(target, getMessage()));
151 /* (omit javadoc for this method)
152 * Method declared on ASTNode.
154 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
155 // dispatch to correct overloaded match method
156 return matcher.match(this, other);
159 /* (omit javadoc for this method)
160 * Method declared on ASTNode.
162 void accept0(ASTVisitor visitor) {
163 boolean visitChildren = visitor.visit(this);
165 // visit children in normal left to right reading order
166 acceptChild(visitor, getExpression());
167 acceptChild(visitor, getMessage());
169 visitor.endVisit(this);
173 * Returns the first expression of this assert statement.
175 * @return the expression node
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) {
183 this.expression = new SimpleName(this.ast);
184 postLazyInit(this.expression, EXPRESSION_PROPERTY);
192 * Sets the first expression of this assert statement.
194 * @param expression the new expression node
195 * @exception IllegalArgumentException if:
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>
202 public void setExpression(Expression expression) {
203 if (expression == null) {
204 throw new IllegalArgumentException();
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);
214 * Returns the message expression of this assert statement, or
215 * <code>null</code> if there is none.
217 * @return the message expression node, or <code>null</code> if there
220 public Expression getMessage() {
221 return this.optionalMessageExpression;
225 * Sets or clears the message expression of this assert statement.
227 * @param expression the message expression node, or <code>null</code> if
229 * @exception IllegalArgumentException if:
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>
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);
244 /* (omit javadoc for this method)
245 * Method declared on ASTNode.
248 return super.memSize() + 2 * 4;
251 /* (omit javadoc for this method)
252 * Method declared on ASTNode.
257 + (this.expression == null ? 0 : getExpression().treeSize())
258 + (this.optionalMessageExpression == null ? 0 : getMessage().treeSize());