| /******************************************************************************* |
| * 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; |
| |
| /** |
| * Expression statement AST node type. |
| * <p> |
| * This kind of node is used to convert an expression (<code>Expression</code>) |
| * into a statement (<code>Statement</code>) by wrapping it. |
| * </p> |
| * <pre> |
| * ExpressionStatement: |
| * StatementExpression <b>;</b> |
| * </pre> |
| * |
| * @since 2.0 |
| */ |
| public class ExpressionStatement extends Statement { |
| |
| /** |
| * The expression; lazily initialized; defaults to a unspecified, but legal, |
| * expression. |
| */ |
| private Expression expression = null; |
| |
| /** |
| * Creates a new unparented expression statement node owned by the given |
| * AST. By default, the expression statement is unspecified, but legal, |
| * method invocation expression. |
| * <p> |
| * N.B. This constructor is package-private. |
| * </p> |
| * |
| * @param ast the AST that is to own this node |
| */ |
| ExpressionStatement(AST ast) { |
| super(ast); |
| } |
| |
| /* (omit javadoc for this method) |
| * Method declared on ASTNode. |
| */ |
| public int getNodeType() { |
| return EXPRESSION_STATEMENT; |
| } |
| |
| /* (omit javadoc for this method) |
| * Method declared on ASTNode. |
| */ |
| ASTNode clone(AST target) { |
| ExpressionStatement result = new ExpressionStatement(target); |
| result.setSourceRange(this.getStartPosition(), this.getLength()); |
| result.copyLeadingComment(this); |
| result.setExpression((Expression) getExpression().clone(target)); |
| 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 expression statement. |
| * |
| * @return the expression node |
| */ |
| public Expression getExpression() { |
| if (expression == null) { |
| // lazy initialize - use setter to ensure parent link set too |
| long count = getAST().modificationCount(); |
| setExpression(new MethodInvocation(getAST())); |
| getAST().setModificationCount(count); |
| } |
| return expression; |
| } |
| |
| /** |
| * Sets the expression of this expression statement. |
| * |
| * @param expression the new expression node |
| * @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) { |
| if (expression == null) { |
| throw new IllegalArgumentException(); |
| } |
| // an ExpressionStatement may occur inside an Expression |
| // must check cycles |
| replaceChild(this.expression, expression, true); |
| this.expression = 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() |
| + (expression == null ? 0 : getExpression().treeSize()); |
| } |
| } |
| |