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: * Return statement AST node type.
017: *
018: * <pre>
019: * ReturnStatement:
020: * <b>return</b> [ Expression ] <b>;</b>
021: * </pre>
022: *
023: * @since 2.0
024: */
025: public class ReturnStatement extends Statement {
026:
027: /**
028: * The "expression" structural property of this node type.
029: * @since 3.0
030: */
031: public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = new ChildPropertyDescriptor(
032: ReturnStatement.class,
033: "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
034:
035: /**
036: * A list of property descriptors (element type:
037: * {@link StructuralPropertyDescriptor}),
038: * or null if uninitialized.
039: */
040: private static final List PROPERTY_DESCRIPTORS;
041:
042: static {
043: List propertyList = new ArrayList(2);
044: createPropertyList(ReturnStatement.class, propertyList);
045: addProperty(EXPRESSION_PROPERTY, propertyList);
046: PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
047: }
048:
049: /**
050: * Returns a list of structural property descriptors for this node type.
051: * Clients must not modify the result.
052: *
053: * @param apiLevel the API level; one of the
054: * <code>AST.JLS*</code> constants
055: * @return a list of property descriptors (element type:
056: * {@link StructuralPropertyDescriptor})
057: * @since 3.0
058: */
059: public static List propertyDescriptors(int apiLevel) {
060: return PROPERTY_DESCRIPTORS;
061: }
062:
063: /**
064: * The expression; <code>null</code> for none; defaults to none.
065: */
066: private Expression optionalExpression = null;
067:
068: /**
069: * Creates a new AST node for a return statement owned by the
070: * given AST. By default, the statement has no expression.
071: *
072: * @param ast the AST that is to own this node
073: */
074: ReturnStatement(AST ast) {
075: super (ast);
076: }
077:
078: /* (omit javadoc for this method)
079: * Method declared on ASTNode.
080: */
081: final List internalStructuralPropertiesForType(int apiLevel) {
082: return propertyDescriptors(apiLevel);
083: }
084:
085: /* (omit javadoc for this method)
086: * Method declared on ASTNode.
087: */
088: final ASTNode internalGetSetChildProperty(
089: ChildPropertyDescriptor property, boolean get, ASTNode child) {
090: if (property == EXPRESSION_PROPERTY) {
091: if (get) {
092: return getExpression();
093: } else {
094: setExpression((Expression) child);
095: return null;
096: }
097: }
098: // allow default implementation to flag the error
099: return super .internalGetSetChildProperty(property, get, child);
100: }
101:
102: /* (omit javadoc for this method)
103: * Method declared on ASTNode.
104: */
105: final int getNodeType0() {
106: return RETURN_STATEMENT;
107: }
108:
109: /* (omit javadoc for this method)
110: * Method declared on ASTNode.
111: */
112: ASTNode clone0(AST target) {
113: ReturnStatement result = new ReturnStatement(target);
114: result
115: .setSourceRange(this .getStartPosition(), this
116: .getLength());
117: result.copyLeadingComment(this );
118: result.setExpression((Expression) ASTNode.copySubtree(target,
119: getExpression()));
120: return result;
121: }
122:
123: /* (omit javadoc for this method)
124: * Method declared on ASTNode.
125: */
126: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
127: // dispatch to correct overloaded match method
128: return matcher.match(this , other);
129: }
130:
131: /* (omit javadoc for this method)
132: * Method declared on ASTNode.
133: */
134: void accept0(ASTVisitor visitor) {
135: boolean visitChildren = visitor.visit(this );
136: if (visitChildren) {
137: acceptChild(visitor, getExpression());
138: }
139: visitor.endVisit(this );
140: }
141:
142: /**
143: * Returns the expression of this return statement, or
144: * <code>null</code> if there is none.
145: *
146: * @return the expression node, or <code>null</code> if there is none
147: */
148: public Expression getExpression() {
149: return this .optionalExpression;
150: }
151:
152: /**
153: * Sets or clears the expression of this return statement.
154: *
155: * @param expression the expression node, or <code>null</code> if
156: * there is none
157: * @exception IllegalArgumentException if:
158: * <ul>
159: * <li>the node belongs to a different AST</li>
160: * <li>the node already has a parent</li>
161: * <li>a cycle in would be created</li>
162: * </ul>
163: */
164: public void setExpression(Expression expression) {
165: ASTNode oldChild = this .optionalExpression;
166: preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
167: this .optionalExpression = expression;
168: postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
169: }
170:
171: /* (omit javadoc for this method)
172: * Method declared on ASTNode.
173: */
174: int memSize() {
175: return super .memSize() + 1 * 4;
176: }
177:
178: /* (omit javadoc for this method)
179: * Method declared on ASTNode.
180: */
181: int treeSize() {
182: return memSize()
183: + (this .optionalExpression == null ? 0
184: : getExpression().treeSize());
185: }
186: }
|