| /******************************************************************************* |
| * Copyright (c) 2000, 2003 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Common Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/cpl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.jdt.core.dom; |
| |
| /** |
| * Return statement AST node type. |
| * |
| * <pre> |
| * ReturnStatement: |
| * <b>return</b> [ Expression ] <b>;</b> |
| * </pre> |
| * |
| * @since 2.0 |
| */ |
| public class ReturnStatement extends Statement { |
| |
| /** |
| * The expression; <code>null</code> for none; defaults to none. |
| */ |
| private Expression optionalExpression = null; |
| |
| /** |
| * Creates a new AST node for a return statement owned by the |
| * given AST. By default, the statement has no expression. |
| * |
| * @param ast the AST that is to own this node |
| */ |
| ReturnStatement(AST ast) { |
| super(ast); |
| } |
| |
| /* (omit javadoc for this method) |
| * Method declared on ASTNode. |
| */ |
| public int getNodeType() { |
| return RETURN_STATEMENT; |
| } |
| |
| /* (omit javadoc for this method) |
| * Method declared on ASTNode. |
| */ |
| ASTNode clone(AST target) { |
| ReturnStatement result = new ReturnStatement(target); |
| result.setSourceRange(this.getStartPosition(), this.getLength()); |
| result.copyLeadingComment(this); |
| result.setExpression( |
| (Expression) ASTNode.copySubtree(target, getExpression())); |
| return result; |
| } |
| |
| /* (omit javadoc for this method) |
| * Method declared on ASTNode. |
| */ |
| public boolean subtreeMatch(ASTMatcher matcher, Object other) { |
| // dispatch to correct overloaded match method |
| return matcher.match(this, other); |
| } |
| |
| /* (omit javadoc for this method) |
| * Method declared on ASTNode. |
| */ |
| void accept0(ASTVisitor visitor) { |
| boolean visitChildren = visitor.visit(this); |
| if (visitChildren) { |
| acceptChild(visitor, getExpression()); |
| } |
| visitor.endVisit(this); |
| } |
| |
| /** |
| * Returns the expression of this return statement, or |
| * <code>null</code> if there is none. |
| * |
| * @return the expression node, or <code>null</code> if there is none |
| */ |
| public Expression getExpression() { |
| return optionalExpression; |
| } |
| |
| /** |
| * Sets or clears the expression of this return statement. |
| * |
| * @param expression the expression node, or <code>null</code> if |
| * there is none |
| * @exception IllegalArgumentException if: |
| * <ul> |
| * <li>the node belongs to a different AST</li> |
| * <li>the node already has a parent</li> |
| * <li>a cycle in would be created</li> |
| * </ul> |
| */ |
| public void setExpression(Expression expression) { |
| // a ReturnStatement may occur inside an Expression - must check cycles |
| replaceChild(this.optionalExpression, expression, true); |
| this.optionalExpression = expression; |
| } |
| |
| /* (omit javadoc for this method) |
| * Method declared on ASTNode. |
| */ |
| int memSize() { |
| return super.memSize() + 1 * 4; |
| } |
| |
| /* (omit javadoc for this method) |
| * Method declared on ASTNode. |
| */ |
| int treeSize() { |
| return |
| memSize() |
| + (optionalExpression == null ? 0 : getExpression().treeSize()); |
| } |
| } |
| |