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 * Expression statement AST node type.
20 * This kind of node is used to convert an expression (<code>Expression</code>)
21 * into a statement (<code>Statement</code>) by wrapping it.
24 * ExpressionStatement:
25 * StatementExpression <b>;</b>
29 * @noinstantiate This class is not intended to be instantiated by clients.
31 public class ExpressionStatement extends Statement {
34 * The "expression" structural property of this node type.
37 public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
38 new ChildPropertyDescriptor(ExpressionStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
41 * A list of property descriptors (element type:
42 * {@link StructuralPropertyDescriptor}),
43 * or null if uninitialized.
45 private static final List PROPERTY_DESCRIPTORS;
48 List properyList = new ArrayList(2);
49 createPropertyList(ExpressionStatement.class, properyList);
50 addProperty(EXPRESSION_PROPERTY, properyList);
51 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
55 * Returns a list of structural property descriptors for this node type.
56 * Clients must not modify the result.
58 * @param apiLevel the API level; one of the
59 * <code>AST.JLS*</code> constants
61 * @return a list of property descriptors (element type:
62 * {@link StructuralPropertyDescriptor})
65 public static List propertyDescriptors(int apiLevel) {
66 return PROPERTY_DESCRIPTORS;
70 * The expression; lazily initialized; defaults to a unspecified, but legal,
73 private Expression expression = null;
76 * Creates a new unparented expression statement node owned by the given
77 * AST. By default, the expression statement is unspecified, but legal,
78 * method invocation expression.
80 * N.B. This constructor is package-private.
83 * @param ast the AST that is to own this node
85 ExpressionStatement(AST ast) {
89 /* (omit javadoc for this method)
90 * Method declared on ASTNode.
92 final List internalStructuralPropertiesForType(int apiLevel) {
93 return propertyDescriptors(apiLevel);
96 /* (omit javadoc for this method)
97 * Method declared on ASTNode.
99 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
100 if (property == EXPRESSION_PROPERTY) {
102 return getExpression();
104 setExpression((Expression) child);
108 // allow default implementation to flag the error
109 return super.internalGetSetChildProperty(property, get, child);
112 /* (omit javadoc for this method)
113 * Method declared on ASTNode.
115 final int getNodeType0() {
116 return EXPRESSION_STATEMENT;
119 /* (omit javadoc for this method)
120 * Method declared on ASTNode.
122 ASTNode clone0(AST target) {
123 ExpressionStatement result = new ExpressionStatement(target);
124 result.setSourceRange(this.getStartPosition(), this.getLength());
125 result.copyLeadingComment(this);
126 result.setExpression((Expression) getExpression().clone(target));
130 /* (omit javadoc for this method)
131 * Method declared on ASTNode.
133 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
134 // dispatch to correct overloaded match method
135 return matcher.match(this, other);
138 /* (omit javadoc for this method)
139 * Method declared on ASTNode.
141 void accept0(ASTVisitor visitor) {
142 boolean visitChildren = visitor.visit(this);
144 acceptChild(visitor, getExpression());
146 visitor.endVisit(this);
150 * Returns the expression of this expression statement.
152 * @return the expression node
154 public Expression getExpression() {
155 if (this.expression == null) {
156 // lazy init must be thread-safe for readers
157 synchronized (this) {
158 if (this.expression == null) {
160 this.expression = new MethodInvocation(this.ast);
161 postLazyInit(this.expression, EXPRESSION_PROPERTY);
165 return this.expression;
169 * Sets the expression of this expression statement.
171 * @param expression the new expression node
172 * @exception IllegalArgumentException if:
174 * <li>the node belongs to a different AST</li>
175 * <li>the node already has a parent</li>
176 * <li>a cycle in would be created</li>
179 public void setExpression(Expression expression) {
180 if (expression == null) {
181 throw new IllegalArgumentException();
183 ASTNode oldChild = this.expression;
184 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
185 this.expression = expression;
186 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
189 /* (omit javadoc for this method)
190 * Method declared on ASTNode.
193 return super.memSize() + 1 * 4;
196 /* (omit javadoc for this method)
197 * Method declared on ASTNode.
202 + (this.expression == null ? 0 : getExpression().treeSize());