001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.core.dom;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: /**
016: * Expression statement AST node type.
017: * <p>
018: * This kind of node is used to convert an expression (<code>Expression</code>)
019: * into a statement (<code>Statement</code>) by wrapping it.
020: * </p>
021: * <pre>
022: * ExpressionStatement:
023: * StatementExpression <b>;</b>
024: * </pre>
025: *
026: * @since 2.0
027: */
028: public class ExpressionStatement extends Statement {
029:
030: /**
031: * The "expression" structural property of this node type.
032: * @since 3.0
033: */
034: public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = new ChildPropertyDescriptor(
035: ExpressionStatement.class,
036: "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
037:
038: /**
039: * A list of property descriptors (element type:
040: * {@link StructuralPropertyDescriptor}),
041: * or null if uninitialized.
042: */
043: private static final List PROPERTY_DESCRIPTORS;
044:
045: static {
046: List properyList = new ArrayList(2);
047: createPropertyList(ExpressionStatement.class, properyList);
048: addProperty(EXPRESSION_PROPERTY, properyList);
049: PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
050: }
051:
052: /**
053: * Returns a list of structural property descriptors for this node type.
054: * Clients must not modify the result.
055: *
056: * @param apiLevel the API level; one of the
057: * <code>AST.JLS*</code> constants
058:
059: * @return a list of property descriptors (element type:
060: * {@link StructuralPropertyDescriptor})
061: * @since 3.0
062: */
063: public static List propertyDescriptors(int apiLevel) {
064: return PROPERTY_DESCRIPTORS;
065: }
066:
067: /**
068: * The expression; lazily initialized; defaults to a unspecified, but legal,
069: * expression.
070: */
071: private Expression expression = null;
072:
073: /**
074: * Creates a new unparented expression statement node owned by the given
075: * AST. By default, the expression statement is unspecified, but legal,
076: * method invocation expression.
077: * <p>
078: * N.B. This constructor is package-private.
079: * </p>
080: *
081: * @param ast the AST that is to own this node
082: */
083: ExpressionStatement(AST ast) {
084: super (ast);
085: }
086:
087: /* (omit javadoc for this method)
088: * Method declared on ASTNode.
089: */
090: final List internalStructuralPropertiesForType(int apiLevel) {
091: return propertyDescriptors(apiLevel);
092: }
093:
094: /* (omit javadoc for this method)
095: * Method declared on ASTNode.
096: */
097: final ASTNode internalGetSetChildProperty(
098: ChildPropertyDescriptor property, boolean get, ASTNode child) {
099: if (property == EXPRESSION_PROPERTY) {
100: if (get) {
101: return getExpression();
102: } else {
103: setExpression((Expression) child);
104: return null;
105: }
106: }
107: // allow default implementation to flag the error
108: return super .internalGetSetChildProperty(property, get, child);
109: }
110:
111: /* (omit javadoc for this method)
112: * Method declared on ASTNode.
113: */
114: final int getNodeType0() {
115: return EXPRESSION_STATEMENT;
116: }
117:
118: /* (omit javadoc for this method)
119: * Method declared on ASTNode.
120: */
121: ASTNode clone0(AST target) {
122: ExpressionStatement result = new ExpressionStatement(target);
123: result
124: .setSourceRange(this .getStartPosition(), this
125: .getLength());
126: result.copyLeadingComment(this );
127: result
128: .setExpression((Expression) getExpression().clone(
129: target));
130: return result;
131: }
132:
133: /* (omit javadoc for this method)
134: * Method declared on ASTNode.
135: */
136: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
137: // dispatch to correct overloaded match method
138: return matcher.match(this , other);
139: }
140:
141: /* (omit javadoc for this method)
142: * Method declared on ASTNode.
143: */
144: void accept0(ASTVisitor visitor) {
145: boolean visitChildren = visitor.visit(this );
146: if (visitChildren) {
147: acceptChild(visitor, getExpression());
148: }
149: visitor.endVisit(this );
150: }
151:
152: /**
153: * Returns the expression of this expression statement.
154: *
155: * @return the expression node
156: */
157: public Expression getExpression() {
158: if (this .expression == null) {
159: // lazy init must be thread-safe for readers
160: synchronized (this ) {
161: if (this .expression == null) {
162: preLazyInit();
163: this .expression = new MethodInvocation(this .ast);
164: postLazyInit(this .expression, EXPRESSION_PROPERTY);
165: }
166: }
167: }
168: return this .expression;
169: }
170:
171: /**
172: * Sets the expression of this expression statement.
173: *
174: * @param expression the new expression node
175: * @exception IllegalArgumentException if:
176: * <ul>
177: * <li>the node belongs to a different AST</li>
178: * <li>the node already has a parent</li>
179: * <li>a cycle in would be created</li>
180: * </ul>
181: */
182: public void setExpression(Expression expression) {
183: if (expression == null) {
184: throw new IllegalArgumentException();
185: }
186: ASTNode oldChild = this .expression;
187: preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
188: this .expression = expression;
189: postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
190: }
191:
192: /* (omit javadoc for this method)
193: * Method declared on ASTNode.
194: */
195: int memSize() {
196: return super .memSize() + 1 * 4;
197: }
198:
199: /* (omit javadoc for this method)
200: * Method declared on ASTNode.
201: */
202: int treeSize() {
203: return memSize()
204: + (this .expression == null ? 0 : getExpression()
205: .treeSize());
206: }
207: }
|