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: * Throw statement AST node type.
017: *
018: * <pre>
019: * ThrowStatement:
020: * <b>throw</b> Expression <b>;</b>
021: * </pre>
022: *
023: * @since 2.0
024: */
025: public class ThrowStatement 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: ThrowStatement.class,
033: "expression", Expression.class, MANDATORY, 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(ThrowStatement.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:
056: * @return a list of property descriptors (element type:
057: * {@link StructuralPropertyDescriptor})
058: * @since 3.0
059: */
060: public static List propertyDescriptors(int apiLevel) {
061: return PROPERTY_DESCRIPTORS;
062: }
063:
064: /**
065: * The expression; lazily initialized; defaults to a unspecified, but legal,
066: * expression.
067: */
068: private Expression expression = null;
069:
070: /**
071: * Creates a new unparented throw statement node owned by the given
072: * AST. By default, the throw statement has an unspecified, but legal,
073: * expression.
074: * <p>
075: * N.B. This constructor is package-private.
076: * </p>
077: *
078: * @param ast the AST that is to own this node
079: */
080: ThrowStatement(AST ast) {
081: super (ast);
082: }
083:
084: /* (omit javadoc for this method)
085: * Method declared on ASTNode.
086: */
087: final List internalStructuralPropertiesForType(int apiLevel) {
088: return propertyDescriptors(apiLevel);
089: }
090:
091: /* (omit javadoc for this method)
092: * Method declared on ASTNode.
093: */
094: final ASTNode internalGetSetChildProperty(
095: ChildPropertyDescriptor property, boolean get, ASTNode child) {
096: if (property == EXPRESSION_PROPERTY) {
097: if (get) {
098: return getExpression();
099: } else {
100: setExpression((Expression) child);
101: return null;
102: }
103: }
104: // allow default implementation to flag the error
105: return super .internalGetSetChildProperty(property, get, child);
106: }
107:
108: /* (omit javadoc for this method)
109: * Method declared on ASTNode.
110: */
111: final int getNodeType0() {
112: return THROW_STATEMENT;
113: }
114:
115: /* (omit javadoc for this method)
116: * Method declared on ASTNode.
117: */
118: ASTNode clone0(AST target) {
119: ThrowStatement result = new ThrowStatement(target);
120: result
121: .setSourceRange(this .getStartPosition(), this
122: .getLength());
123: result.copyLeadingComment(this );
124: result
125: .setExpression((Expression) getExpression().clone(
126: target));
127: return result;
128: }
129:
130: /* (omit javadoc for this method)
131: * Method declared on ASTNode.
132: */
133: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
134: // dispatch to correct overloaded match method
135: return matcher.match(this , other);
136: }
137:
138: /* (omit javadoc for this method)
139: * Method declared on ASTNode.
140: */
141: void accept0(ASTVisitor visitor) {
142: boolean visitChildren = visitor.visit(this );
143: if (visitChildren) {
144: acceptChild(visitor, getExpression());
145: }
146: visitor.endVisit(this );
147: }
148:
149: /**
150: * Returns the expression of this throw statement.
151: *
152: * @return the expression node
153: */
154: public Expression getExpression() {
155: if (this .expression == null) {
156: // lazy init must be thread-safe for readers
157: synchronized (this ) {
158: if (this .expression == null) {
159: preLazyInit();
160: this .expression = new SimpleName(this .ast);
161: postLazyInit(this .expression, EXPRESSION_PROPERTY);
162: }
163: }
164: }
165: return this .expression;
166: }
167:
168: /**
169: * Sets the expression of this throw statement.
170: *
171: * @param expression the new expression node
172: * @exception IllegalArgumentException if:
173: * <ul>
174: * <li>the node belongs to a different AST</li>
175: * <li>the node already has a parent</li>
176: * <li>a cycle in would be created</li>
177: * </ul>
178: */
179: public void setExpression(Expression expression) {
180: if (expression == null) {
181: throw new IllegalArgumentException();
182: }
183: ASTNode oldChild = this .expression;
184: preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
185: this .expression = expression;
186: postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
187: }
188:
189: /* (omit javadoc for this method)
190: * Method declared on ASTNode.
191: */
192: int memSize() {
193: return super .memSize() + 1 * 4;
194: }
195:
196: /* (omit javadoc for this method)
197: * Method declared on ASTNode.
198: */
199: int treeSize() {
200: return memSize()
201: + (this .expression == null ? 0 : getExpression()
202: .treeSize());
203: }
204: }
|