removed unused private methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / CatchClause.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.core.dom;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 /**
18  * Catch clause AST node type.
19  *
20  * <pre>
21  * CatchClause:
22  *    <b>catch</b> <b>(</b> FormalParameter <b>)</b> Block
23  * </pre>
24  * 
25  * @since 2.0
26  * @noinstantiate This class is not intended to be instantiated by clients.
27  */
28 public class CatchClause extends ASTNode {
29         
30         /**
31          * The "exception" structural property of this node type.
32          * @since 3.0
33          */
34         public static final ChildPropertyDescriptor EXCEPTION_PROPERTY = 
35                 new ChildPropertyDescriptor(CatchClause.class, "exception", SingleVariableDeclaration.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
36
37         /**
38          * The "body" structural property of this node type.
39          * @since 3.0
40          */
41         public static final ChildPropertyDescriptor BODY_PROPERTY = 
42                 new ChildPropertyDescriptor(CatchClause.class, "body", Block.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
43
44         /**
45          * A list of property descriptors (element type: 
46          * {@link StructuralPropertyDescriptor}),
47          * or null if uninitialized.
48          */
49         private static final List PROPERTY_DESCRIPTORS;
50         
51         static {
52                 List properyList = new ArrayList(3);
53                 createPropertyList(CatchClause.class, properyList);
54                 addProperty(EXCEPTION_PROPERTY, properyList);
55                 addProperty(BODY_PROPERTY, properyList);
56                 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
57         }
58
59         /**
60          * Returns a list of structural property descriptors for this node type.
61          * Clients must not modify the result.
62          * 
63          * @param apiLevel the API level; one of the
64          * <code>AST.JLS*</code> constants
65
66          * @return a list of property descriptors (element type: 
67          * {@link StructuralPropertyDescriptor})
68          * @since 3.0
69          */
70         public static List propertyDescriptors(int apiLevel) {
71                 return PROPERTY_DESCRIPTORS;
72         }
73                         
74         /**
75          * The body; lazily initialized; defaults to an empty block.
76          */
77         private Block body = null;
78
79         /**
80          * The exception variable declaration; lazily initialized; defaults to a
81          * unspecified, but legal, variable declaration.
82          */
83         private SingleVariableDeclaration exceptionDecl = null;
84
85         /**
86          * Creates a new AST node for a catch clause owned by the given 
87          * AST. By default, the catch clause declares an unspecified, but legal, 
88          * exception declaration and has an empty block.
89          * <p>
90          * N.B. This constructor is package-private.
91          * </p>
92          * 
93          * @param ast the AST that is to own this node
94          */
95         CatchClause(AST ast) {
96                 super(ast);
97         }
98
99         /* (omit javadoc for this method)
100          * Method declared on ASTNode.
101          */
102         final List internalStructuralPropertiesForType(int apiLevel) {
103                 return propertyDescriptors(apiLevel);
104         }
105         
106         /* (omit javadoc for this method)
107          * Method declared on ASTNode.
108          */
109         final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
110                 if (property == EXCEPTION_PROPERTY) {
111                         if (get) {
112                                 return getException();
113                         } else {
114                                 setException((SingleVariableDeclaration) child);
115                                 return null;
116                         }
117                 }
118                 if (property == BODY_PROPERTY) {
119                         if (get) {
120                                 return getBody();
121                         } else {
122                                 setBody((Block) child);
123                                 return null;
124                         }
125                 }
126                 // allow default implementation to flag the error
127                 return super.internalGetSetChildProperty(property, get, child);
128         }
129         
130         /* (omit javadoc for this method)
131          * Method declared on ASTNode.
132          */
133         final int getNodeType0() {
134                 return CATCH_CLAUSE;
135         }
136
137         /* (omit javadoc for this method)
138          * Method declared on ASTNode.
139          */
140         ASTNode clone0(AST target) {
141                 CatchClause result = new CatchClause(target);
142                 result.setSourceRange(this.getStartPosition(), this.getLength());
143                 result.setBody((Block) getBody().clone(target));
144                 result.setException(
145                         (SingleVariableDeclaration) ASTNode.copySubtree(target, getException()));
146                 return result;
147         }
148
149         /* (omit javadoc for this method)
150          * Method declared on ASTNode.
151          */
152         final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
153                 // dispatch to correct overloaded match method
154                 return matcher.match(this, other);
155         }
156
157         /* (omit javadoc for this method)
158          * Method declared on ASTNode.
159          */
160         void accept0(ASTVisitor visitor) {
161                 boolean visitChildren = visitor.visit(this);
162                 if (visitChildren) {
163                         // visit children in normal left to right reading order
164                         acceptChild(visitor, getException());
165                         acceptChild(visitor, getBody());
166                 }
167                 visitor.endVisit(this);
168         }
169         
170         /**
171          * Returns the exception variable declaration of this catch clause.
172          * 
173          * @return the exception variable declaration node
174          */ 
175         public SingleVariableDeclaration getException() {
176                 if (this.exceptionDecl == null) {
177                         // lazy init must be thread-safe for readers
178                         synchronized (this) {
179                                 if (this.exceptionDecl == null) {
180                                         preLazyInit();
181                                         this.exceptionDecl = new SingleVariableDeclaration(this.ast);
182                                         postLazyInit(this.exceptionDecl, EXCEPTION_PROPERTY);
183                                 }
184                         }
185                 }
186                 return this.exceptionDecl;
187         }
188                 
189         /**
190          * Sets the variable declaration of this catch clause.
191          * 
192          * @param exception the exception variable declaration node
193          * @exception IllegalArgumentException if:
194          * <ul>
195          * <li>the node belongs to a different AST</li>
196          * <li>the node already has a parent</li>
197          * <li>a cycle in would be created</li>
198          * </ul>
199          */ 
200         public void setException(SingleVariableDeclaration exception) {
201                 if (exception == null) {
202                         throw new IllegalArgumentException();
203                 }
204                 ASTNode oldChild = this.exceptionDecl;
205                 preReplaceChild(oldChild, exception, EXCEPTION_PROPERTY);
206                 this.exceptionDecl= exception;
207                 postReplaceChild(oldChild, exception, EXCEPTION_PROPERTY);
208         }
209         
210         /**
211          * Returns the body of this catch clause.
212          * 
213          * @return the catch clause body
214          */ 
215         public Block getBody() {
216                 if (this.body == null) {
217                         // lazy init must be thread-safe for readers
218                         synchronized (this) {
219                                 if (this.body == null) {
220                                         preLazyInit();
221                                         this.body = new Block(this.ast);
222                                         postLazyInit(this.body, BODY_PROPERTY);
223                                 }
224                         }
225                 }
226                 return this.body;
227         }
228         
229         /**
230          * Sets the body of this catch clause.
231          * 
232          * @param body the catch clause block node
233          * @exception IllegalArgumentException if:
234          * <ul>
235          * <li>the node belongs to a different AST</li>
236          * <li>the node already has a parent</li>
237          * <li>a cycle in would be created</li>
238          * </ul>
239          */ 
240         public void setBody(Block body) {
241                 if (body == null) {
242                         throw new IllegalArgumentException();
243                 }
244                 ASTNode oldChild = this.body;
245                 preReplaceChild(oldChild, body, BODY_PROPERTY);
246                 this.body = body;
247                 postReplaceChild(oldChild, body, BODY_PROPERTY);
248         }
249
250         /* (omit javadoc for this method)
251          * Method declared on ASTNode.
252          */
253         int memSize() {
254                 // treat Code as free
255                 return BASE_NODE_SIZE + 2 * 4;
256         }
257         
258         /* (omit javadoc for this method)
259          * Method declared on ASTNode.
260          */
261         int treeSize() {
262                 return 
263                         memSize()
264                         + (this.exceptionDecl == null ? 0 : getException().treeSize())
265                         + (this.body == null ? 0 : getBody().treeSize());
266         }
267 }