KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > ReturnStatement


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.core.dom;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * Return statement AST node type.
19  *
20  * <pre>
21  * ReturnStatement:
22  * <b>return</b> [ Expression ] <b>;</b>
23  * </pre>
24  *
25  * @since 2.0
26  */

27 public class ReturnStatement extends Statement {
28             
29     /**
30      * The "expression" structural property of this node type.
31      * @since 3.0
32      */

33     public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
34         new ChildPropertyDescriptor(ReturnStatement.class, "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
35

36     /**
37      * A list of property descriptors (element type:
38      * {@link StructuralPropertyDescriptor}),
39      * or null if uninitialized.
40      */

41     private static final List JavaDoc PROPERTY_DESCRIPTORS;
42     
43     static {
44         List JavaDoc propertyList = new ArrayList JavaDoc(2);
45         createPropertyList(ReturnStatement.class, propertyList);
46         addProperty(EXPRESSION_PROPERTY, propertyList);
47         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
48     }
49
50     /**
51      * Returns a list of structural property descriptors for this node type.
52      * Clients must not modify the result.
53      *
54      * @param apiLevel the API level; one of the
55      * <code>AST.JLS&ast;</code> constants
56      * @return a list of property descriptors (element type:
57      * {@link StructuralPropertyDescriptor})
58      * @since 3.0
59      */

60     public static List JavaDoc propertyDescriptors(int apiLevel) {
61         return PROPERTY_DESCRIPTORS;
62     }
63             
64     /**
65      * The expression; <code>null</code> for none; defaults to none.
66      */

67     private Expression optionalExpression = null;
68     
69     /**
70      * Creates a new AST node for a return statement owned by the
71      * given AST. By default, the statement has no expression.
72      *
73      * @param ast the AST that is to own this node
74      */

75     ReturnStatement(AST ast) {
76         super(ast);
77     }
78
79     /* (omit javadoc for this method)
80      * Method declared on ASTNode.
81      */

82     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
83         return propertyDescriptors(apiLevel);
84     }
85     
86     /* (omit javadoc for this method)
87      * Method declared on ASTNode.
88      */

89     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
90         if (property == EXPRESSION_PROPERTY) {
91             if (get) {
92                 return getExpression();
93             } else {
94                 setExpression((Expression) child);
95                 return null;
96             }
97         }
98         // allow default implementation to flag the error
99
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.setSourceRange(this.getStartPosition(), this.getLength());
115         result.copyLeadingComment(this);
116         result.setExpression(
117             (Expression) ASTNode.copySubtree(target, getExpression()));
118         return result;
119     }
120
121     /* (omit javadoc for this method)
122      * Method declared on ASTNode.
123      */

124     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
125         // dispatch to correct overloaded match method
126
return matcher.match(this, other);
127     }
128
129     /* (omit javadoc for this method)
130      * Method declared on ASTNode.
131      */

132     void accept0(ASTVisitor visitor) {
133         boolean visitChildren = visitor.visit(this);
134         if (visitChildren) {
135             acceptChild(visitor, getExpression());
136         }
137         visitor.endVisit(this);
138     }
139     
140     /**
141      * Returns the expression of this return statement, or
142      * <code>null</code> if there is none.
143      *
144      * @return the expression node, or <code>null</code> if there is none
145      */

146     public Expression getExpression() {
147         return this.optionalExpression;
148     }
149     
150     /**
151      * Sets or clears the expression of this return statement.
152      *
153      * @param expression the expression node, or <code>null</code> if
154      * there is none
155      * @exception IllegalArgumentException if:
156      * <ul>
157      * <li>the node belongs to a different AST</li>
158      * <li>the node already has a parent</li>
159      * <li>a cycle in would be created</li>
160      * </ul>
161      */

162     public void setExpression(Expression expression) {
163         ASTNode oldChild = this.optionalExpression;
164         preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
165         this.optionalExpression = expression;
166         postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
167     }
168     
169     /* (omit javadoc for this method)
170      * Method declared on ASTNode.
171      */

172     int memSize() {
173         return super.memSize() + 1 * 4;
174     }
175     
176     /* (omit javadoc for this method)
177      * Method declared on ASTNode.
178      */

179     int treeSize() {
180         return
181             memSize()
182             + (this.optionalExpression == null ? 0 : getExpression().treeSize());
183     }
184 }
185
186
Popular Tags