1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.List;
18 * Try statement AST node type.
24 * [ <b>finally</b> Block ]
28 * @noinstantiate This class is not intended to be instantiated by clients.
30 public class TryStatement extends Statement {
33 * The "body" structural property of this node type.
36 public static final ChildPropertyDescriptor BODY_PROPERTY =
37 new ChildPropertyDescriptor(TryStatement.class, "body", Block.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
40 * The "catchClauses" structural property of this node type.
43 public static final ChildListPropertyDescriptor CATCH_CLAUSES_PROPERTY =
44 new ChildListPropertyDescriptor(TryStatement.class, "catchClauses", CatchClause.class, CYCLE_RISK); //$NON-NLS-1$
47 * The "finally" structural property of this node type.
50 public static final ChildPropertyDescriptor FINALLY_PROPERTY =
51 new ChildPropertyDescriptor(TryStatement.class, "finally", Block.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
54 * A list of property descriptors (element type:
55 * {@link StructuralPropertyDescriptor}),
56 * or null if uninitialized.
58 private static final List PROPERTY_DESCRIPTORS;
61 List propertyList = new ArrayList(4);
62 createPropertyList(TryStatement.class, propertyList);
63 addProperty(BODY_PROPERTY, propertyList);
64 addProperty(CATCH_CLAUSES_PROPERTY, propertyList);
65 addProperty(FINALLY_PROPERTY, propertyList);
66 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
70 * Returns a list of structural property descriptors for this node type.
71 * Clients must not modify the result.
73 * @param apiLevel the API level; one of the
74 * <code>AST.JLS*</code> constants
75 * @return a list of property descriptors (element type:
76 * {@link StructuralPropertyDescriptor})
79 public static List propertyDescriptors(int apiLevel) {
80 return PROPERTY_DESCRIPTORS;
84 * The body; lazily initialized; defaults to an empty block.
86 private Block body = null;
89 * The catch clauses (element type: <code>CatchClause</code>).
90 * Defaults to an empty list.
92 private ASTNode.NodeList catchClauses =
93 new ASTNode.NodeList(CATCH_CLAUSES_PROPERTY);
96 * The finally block, or <code>null</code> if none.
99 private Block optionalFinallyBody = null;
103 * Creates a new AST node for a try statement owned by the given
104 * AST. By default, the try statement has an empty block, no catch
105 * clauses, and no finally block.
107 * N.B. This constructor is package-private.
110 * @param ast the AST that is to own this node
112 TryStatement(AST ast) {
116 /* (omit javadoc for this method)
117 * Method declared on ASTNode.
119 final List internalStructuralPropertiesForType(int apiLevel) {
120 return propertyDescriptors(apiLevel);
123 /* (omit javadoc for this method)
124 * Method declared on ASTNode.
126 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
127 if (property == BODY_PROPERTY) {
131 setBody((Block) child);
135 if (property == FINALLY_PROPERTY) {
139 setFinally((Block) child);
143 // allow default implementation to flag the error
144 return super.internalGetSetChildProperty(property, get, child);
147 /* (omit javadoc for this method)
148 * Method declared on ASTNode.
150 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
151 if (property == CATCH_CLAUSES_PROPERTY) {
152 return catchClauses();
154 // allow default implementation to flag the error
155 return super.internalGetChildListProperty(property);
158 /* (omit javadoc for this method)
159 * Method declared on ASTNode.
161 final int getNodeType0() {
162 return TRY_STATEMENT;
165 /* (omit javadoc for this method)
166 * Method declared on ASTNode.
168 ASTNode clone0(AST target) {
169 TryStatement result = new TryStatement(target);
170 result.setSourceRange(this.getStartPosition(), this.getLength());
171 result.copyLeadingComment(this);
172 result.setBody((Block) getBody().clone(target));
173 result.catchClauses().addAll(
174 ASTNode.copySubtrees(target, catchClauses()));
176 (Block) ASTNode.copySubtree(target, getFinally()));
180 /* (omit javadoc for this method)
181 * Method declared on ASTNode.
183 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
184 // dispatch to correct overloaded match method
185 return matcher.match(this, other);
188 /* (omit javadoc for this method)
189 * Method declared on ASTNode.
191 void accept0(ASTVisitor visitor) {
192 boolean visitChildren = visitor.visit(this);
194 // visit children in normal left to right reading order
195 acceptChild(visitor, getBody());
196 acceptChildren(visitor, this.catchClauses);
197 acceptChild(visitor, getFinally());
199 visitor.endVisit(this);
203 * Returns the body of this try statement.
205 * @return the try body
207 public Block getBody() {
208 if (this.body == null) {
209 // lazy init must be thread-safe for readers
210 synchronized (this) {
211 if (this.body == null) {
213 this.body = new Block(this.ast);
214 postLazyInit(this.body, BODY_PROPERTY);
222 * Sets the body of this try statement.
224 * @param body the block node
225 * @exception IllegalArgumentException if:
227 * <li>the node belongs to a different AST</li>
228 * <li>the node already has a parent</li>
229 * <li>a cycle in would be created</li>
232 public void setBody(Block body) {
234 throw new IllegalArgumentException();
236 ASTNode oldChild = this.body;
237 preReplaceChild(oldChild, body, BODY_PROPERTY);
239 postReplaceChild(oldChild, body, BODY_PROPERTY);
243 * Returns the live ordered list of catch clauses for this try statement.
245 * @return the live list of catch clauses
246 * (element type: <code>CatchClause</code>)
248 public List catchClauses() {
249 return this.catchClauses;
253 * Returns the finally block of this try statement, or <code>null</code> if
254 * this try statement has <b>no</b> finally block.
256 * @return the finally block, or <code>null</code> if this try statement
259 public Block getFinally() {
260 return this.optionalFinallyBody;
264 * Sets or clears the finally block of this try statement.
266 * @param block the finally block node, or <code>null</code> if
268 * @exception IllegalArgumentException if:
270 * <li>the node belongs to a different AST</li>
271 * <li>the node already has a parent</li>
272 * <li>a cycle in would be created</li>
275 public void setFinally(Block block) {
276 ASTNode oldChild = this.optionalFinallyBody;
277 preReplaceChild(oldChild, block, FINALLY_PROPERTY);
278 this.optionalFinallyBody = block;
279 postReplaceChild(oldChild, block, FINALLY_PROPERTY);
282 /* (omit javadoc for this method)
283 * Method declared on ASTNode.
286 return super.memSize() + 3 * 4;
289 /* (omit javadoc for this method)
290 * Method declared on ASTNode.
295 + (this.body == null ? 0 : getBody().treeSize())
296 + this.catchClauses.listSize()
297 + (this.optionalFinallyBody == null ? 0 : getFinally().treeSize());