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