KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  * Try statement AST node type.
19  *
20  * <pre>
21  * TryStatement:
22  * <b>try</b> Block
23  * { CatchClause }
24  * [ <b>finally</b> Block ]
25  * </pre>
26  *
27  * @since 2.0
28  */

29 public class TryStatement extends Statement {
30     
31     /**
32      * The "body" structural property of this node type.
33      * @since 3.0
34      */

35     public static final ChildPropertyDescriptor BODY_PROPERTY =
36         new ChildPropertyDescriptor(TryStatement.class, "body", Block.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
37

38     /**
39      * The "catchClauses" structural property of this node type.
40      * @since 3.0
41      */

42     public static final ChildListPropertyDescriptor CATCH_CLAUSES_PROPERTY =
43         new ChildListPropertyDescriptor(TryStatement.class, "catchClauses", CatchClause.class, CYCLE_RISK); //$NON-NLS-1$
44

45     /**
46      * The "finally" structural property of this node type.
47      * @since 3.0
48      */

49     public static final ChildPropertyDescriptor FINALLY_PROPERTY =
50         new ChildPropertyDescriptor(TryStatement.class, "finally", Block.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
51

52     /**
53      * A list of property descriptors (element type:
54      * {@link StructuralPropertyDescriptor}),
55      * or null if uninitialized.
56      */

57     private static final List JavaDoc PROPERTY_DESCRIPTORS;
58     
59     static {
60         List JavaDoc propertyList = new ArrayList JavaDoc(4);
61         createPropertyList(TryStatement.class, propertyList);
62         addProperty(BODY_PROPERTY, propertyList);
63         addProperty(CATCH_CLAUSES_PROPERTY, propertyList);
64         addProperty(FINALLY_PROPERTY, propertyList);
65         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
66     }
67
68     /**
69      * Returns a list of structural property descriptors for this node type.
70      * Clients must not modify the result.
71      *
72      * @param apiLevel the API level; one of the
73      * <code>AST.JLS*</code> constants
74      * @return a list of property descriptors (element type:
75      * {@link StructuralPropertyDescriptor})
76      * @since 3.0
77      */

78     public static List JavaDoc propertyDescriptors(int apiLevel) {
79         return PROPERTY_DESCRIPTORS;
80     }
81             
82     /**
83      * The body; lazily initialized; defaults to an empty block.
84      */

85     private Block body = null;
86
87     /**
88      * The catch clauses (element type: <code>CatchClause</code>).
89      * Defaults to an empty list.
90      */

91     private ASTNode.NodeList catchClauses =
92         new ASTNode.NodeList(CATCH_CLAUSES_PROPERTY);
93     
94     /**
95      * The finally block, or <code>null</code> if none.
96      * Defaults to none.
97      */

98     private Block optionalFinallyBody = null;
99
100             
101     /**
102      * Creates a new AST node for a try statement owned by the given
103      * AST. By default, the try statement has an empty block, no catch
104      * clauses, and no finally block.
105      * <p>
106      * N.B. This constructor is package-private.
107      * </p>
108      *
109      * @param ast the AST that is to own this node
110      */

111     TryStatement(AST ast) {
112         super(ast);
113     }
114
115     /* (omit javadoc for this method)
116      * Method declared on ASTNode.
117      */

118     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
119         return propertyDescriptors(apiLevel);
120     }
121     
122     /* (omit javadoc for this method)
123      * Method declared on ASTNode.
124      */

125     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
126         if (property == BODY_PROPERTY) {
127             if (get) {
128                 return getBody();
129             } else {
130                 setBody((Block) child);
131                 return null;
132             }
133         }
134         if (property == FINALLY_PROPERTY) {
135             if (get) {
136                 return getFinally();
137             } else {
138                 setFinally((Block) child);
139                 return null;
140             }
141         }
142         // allow default implementation to flag the error
143
return super.internalGetSetChildProperty(property, get, child);
144     }
145     
146     /* (omit javadoc for this method)
147      * Method declared on ASTNode.
148      */

149     final List JavaDoc internalGetChildListProperty(ChildListPropertyDescriptor property) {
150         if (property == CATCH_CLAUSES_PROPERTY) {
151             return catchClauses();
152         }
153         // allow default implementation to flag the error
154
return super.internalGetChildListProperty(property);
155     }
156
157     /* (omit javadoc for this method)
158      * Method declared on ASTNode.
159      */

160     final int getNodeType0() {
161         return TRY_STATEMENT;
162     }
163
164     /* (omit javadoc for this method)
165      * Method declared on ASTNode.
166      */

167     ASTNode clone0(AST target) {
168         TryStatement result = new TryStatement(target);
169         result.setSourceRange(this.getStartPosition(), this.getLength());
170         result.copyLeadingComment(this);
171         result.setBody((Block) getBody().clone(target));
172         result.catchClauses().addAll(
173             ASTNode.copySubtrees(target, catchClauses()));
174         result.setFinally(
175             (Block) ASTNode.copySubtree(target, getFinally()));
176         return result;
177     }
178
179     /* (omit javadoc for this method)
180      * Method declared on ASTNode.
181      */

182     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
183         // dispatch to correct overloaded match method
184
return matcher.match(this, other);
185     }
186
187     /* (omit javadoc for this method)
188      * Method declared on ASTNode.
189      */

190     void accept0(ASTVisitor visitor) {
191         boolean visitChildren = visitor.visit(this);
192         if (visitChildren) {
193             // visit children in normal left to right reading order
194
acceptChild(visitor, getBody());
195             acceptChildren(visitor, this.catchClauses);
196             acceptChild(visitor, getFinally());
197         }
198         visitor.endVisit(this);
199     }
200     
201     /**
202      * Returns the body of this try statement.
203      *
204      * @return the try body
205      */

206     public Block getBody() {
207         if (this.body == null) {
208             // lazy init must be thread-safe for readers
209
synchronized (this) {
210                 if (this.body == null) {
211                     preLazyInit();
212                     this.body = new Block(this.ast);
213                     postLazyInit(this.body, BODY_PROPERTY);
214                 }
215             }
216         }
217         return this.body;
218     }
219     
220     /**
221      * Sets the body of this try statement.
222      *
223      * @param body the block node
224      * @exception IllegalArgumentException if:
225      * <ul>
226      * <li>the node belongs to a different AST</li>
227      * <li>the node already has a parent</li>
228      * <li>a cycle in would be created</li>
229      * </ul>
230      */

231     public void setBody(Block body) {
232         if (body == null) {
233             throw new IllegalArgumentException JavaDoc();
234         }
235         ASTNode oldChild = this.body;
236         preReplaceChild(oldChild, body, BODY_PROPERTY);
237         this.body = body;
238         postReplaceChild(oldChild, body, BODY_PROPERTY);
239     }
240
241     /**
242      * Returns the live ordered list of catch clauses for this try statement.
243      *
244      * @return the live list of catch clauses
245      * (element type: <code>CatchClause</code>)
246      */

247     public List JavaDoc catchClauses() {
248         return this.catchClauses;
249     }
250         
251     /**
252      * Returns the finally block of this try statement, or <code>null</code> if
253      * this try statement has <b>no</b> finally block.
254      *
255      * @return the finally block, or <code>null</code> if this try statement
256      * has none
257      */

258     public Block getFinally() {
259         return this.optionalFinallyBody;
260     }
261
262     /**
263      * Sets or clears the finally block of this try statement.
264      *
265      * @param block the finally block node, or <code>null</code> if
266      * there is none
267      * @exception IllegalArgumentException if:
268      * <ul>
269      * <li>the node belongs to a different AST</li>
270      * <li>the node already has a parent</li>
271      * <li>a cycle in would be created</li>
272      * </ul>
273      */

274     public void setFinally(Block block) {
275         ASTNode oldChild = this.optionalFinallyBody;
276         preReplaceChild(oldChild, block, FINALLY_PROPERTY);
277         this.optionalFinallyBody = block;
278         postReplaceChild(oldChild, block, FINALLY_PROPERTY);
279     }
280     
281     /* (omit javadoc for this method)
282      * Method declared on ASTNode.
283      */

284     int memSize() {
285         return super.memSize() + 3 * 4;
286     }
287     
288     /* (omit javadoc for this method)
289      * Method declared on ASTNode.
290      */

291     int treeSize() {
292         return
293             memSize()
294             + (this.body == null ? 0 : getBody().treeSize())
295             + this.catchClauses.listSize()
296             + (this.optionalFinallyBody == null ? 0 : getFinally().treeSize());
297     }
298 }
299
Popular Tags