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 * Return statement AST node type.
22 * <b>return</b> [ Expression ] <b>;</b>
26 * @noinstantiate This class is not intended to be instantiated by clients.
28 public class ReturnStatement extends Statement {
31 * The "expression" structural property of this node type.
34 public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
35 new ChildPropertyDescriptor(ReturnStatement.class, "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
38 * A list of property descriptors (element type:
39 * {@link StructuralPropertyDescriptor}),
40 * or null if uninitialized.
42 private static final List PROPERTY_DESCRIPTORS;
45 List propertyList = new ArrayList(2);
46 createPropertyList(ReturnStatement.class, propertyList);
47 addProperty(EXPRESSION_PROPERTY, propertyList);
48 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
52 * Returns a list of structural property descriptors for this node type.
53 * Clients must not modify the result.
55 * @param apiLevel the API level; one of the
56 * <code>AST.JLS*</code> constants
57 * @return a list of property descriptors (element type:
58 * {@link StructuralPropertyDescriptor})
61 public static List propertyDescriptors(int apiLevel) {
62 return PROPERTY_DESCRIPTORS;
66 * The expression; <code>null</code> for none; defaults to none.
68 private Expression optionalExpression = null;
71 * Creates a new AST node for a return statement owned by the
72 * given AST. By default, the statement has no expression.
74 * @param ast the AST that is to own this node
76 ReturnStatement(AST ast) {
80 /* (omit javadoc for this method)
81 * Method declared on ASTNode.
83 final List internalStructuralPropertiesForType(int apiLevel) {
84 return propertyDescriptors(apiLevel);
87 /* (omit javadoc for this method)
88 * Method declared on ASTNode.
90 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
91 if (property == EXPRESSION_PROPERTY) {
93 return getExpression();
95 setExpression((Expression) child);
99 // allow default implementation to flag the error
100 return super.internalGetSetChildProperty(property, get, child);
103 /* (omit javadoc for this method)
104 * Method declared on ASTNode.
106 final int getNodeType0() {
107 return RETURN_STATEMENT;
110 /* (omit javadoc for this method)
111 * Method declared on ASTNode.
113 ASTNode clone0(AST target) {
114 ReturnStatement result = new ReturnStatement(target);
115 result.setSourceRange(this.getStartPosition(), this.getLength());
116 result.copyLeadingComment(this);
117 result.setExpression(
118 (Expression) ASTNode.copySubtree(target, getExpression()));
122 /* (omit javadoc for this method)
123 * Method declared on ASTNode.
125 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
126 // dispatch to correct overloaded match method
127 return matcher.match(this, other);
130 /* (omit javadoc for this method)
131 * Method declared on ASTNode.
133 void accept0(ASTVisitor visitor) {
134 boolean visitChildren = visitor.visit(this);
136 acceptChild(visitor, getExpression());
138 visitor.endVisit(this);
142 * Returns the expression of this return statement, or
143 * <code>null</code> if there is none.
145 * @return the expression node, or <code>null</code> if there is none
147 public Expression getExpression() {
148 return this.optionalExpression;
152 * Sets or clears the expression of this return statement.
154 * @param expression the expression node, or <code>null</code> if
156 * @exception IllegalArgumentException if:
158 * <li>the node belongs to a different AST</li>
159 * <li>the node already has a parent</li>
160 * <li>a cycle in would be created</li>
163 public void setExpression(Expression expression) {
164 ASTNode oldChild = this.optionalExpression;
165 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
166 this.optionalExpression = expression;
167 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
170 /* (omit javadoc for this method)
171 * Method declared on ASTNode.
174 return super.memSize() + 1 * 4;
177 /* (omit javadoc for this method)
178 * Method declared on ASTNode.
183 + (this.optionalExpression == null ? 0 : getExpression().treeSize());