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 | * Synchronized statement AST node type. |
22 | * |
23 | * <pre> |
24 | * SynchronizedStatement: |
25 | * <b>synchronized</b> <b>(</b> Expression <b>)</b> Block |
26 | * </pre> |
27 | * |
28 | * @since 2.0 |
29 | * @noinstantiate This class is not intended to be instantiated by clients. |
30 | */ |
31 | @SuppressWarnings("rawtypes") |
32 | public class SynchronizedStatement extends Statement { |
33 | |
34 | /** |
35 | * The "expression" structural property of this node type (child type: {@link Expression}). |
36 | * @since 3.0 |
37 | */ |
38 | public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = |
39 | new ChildPropertyDescriptor(SynchronizedStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
40 | |
41 | /** |
42 | * The "body" structural property of this node type (child type: {@link Block}). |
43 | * @since 3.0 |
44 | */ |
45 | public static final ChildPropertyDescriptor BODY_PROPERTY = |
46 | new ChildPropertyDescriptor(SynchronizedStatement.class, "body", Block.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
47 | |
48 | /** |
49 | * A list of property descriptors (element type: |
50 | * {@link StructuralPropertyDescriptor}), |
51 | * or null if uninitialized. |
52 | */ |
53 | private static final List PROPERTY_DESCRIPTORS; |
54 | |
55 | static { |
56 | List propertyList = new ArrayList(3); |
57 | createPropertyList(SynchronizedStatement.class, propertyList); |
58 | addProperty(EXPRESSION_PROPERTY, propertyList); |
59 | addProperty(BODY_PROPERTY, propertyList); |
60 | PROPERTY_DESCRIPTORS = reapPropertyList(propertyList); |
61 | } |
62 | |
63 | /** |
64 | * Returns a list of structural property descriptors for this node type. |
65 | * Clients must not modify the result. |
66 | * |
67 | * @param apiLevel the API level; one of the |
68 | * <code>AST.JLS*</code> constants |
69 | |
70 | * @return a list of property descriptors (element type: |
71 | * {@link StructuralPropertyDescriptor}) |
72 | * @since 3.0 |
73 | */ |
74 | public static List propertyDescriptors(int apiLevel) { |
75 | return PROPERTY_DESCRIPTORS; |
76 | } |
77 | |
78 | /** |
79 | * The expression; lazily initialized; defaults to an unspecified, but |
80 | * legal, expression. |
81 | */ |
82 | private Expression expression = null; |
83 | |
84 | /** |
85 | * The body; lazily initialized; defaults to an empty block. |
86 | */ |
87 | private Block body = null; |
88 | |
89 | /** |
90 | * Creates a new unparented synchronized statement node owned by the given |
91 | * AST. By default, the expression is unspecified, but legal, and the |
92 | * blody is an empty block. |
93 | * <p> |
94 | * N.B. This constructor is package-private. |
95 | * </p> |
96 | * |
97 | * @param ast the AST that is to own this node |
98 | */ |
99 | SynchronizedStatement(AST ast) { |
100 | super(ast); |
101 | } |
102 | |
103 | @Override |
104 | final List internalStructuralPropertiesForType(int apiLevel) { |
105 | return propertyDescriptors(apiLevel); |
106 | } |
107 | |
108 | @Override |
109 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
110 | if (property == EXPRESSION_PROPERTY) { |
111 | if (get) { |
112 | return getExpression(); |
113 | } else { |
114 | setExpression((Expression) 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 | @Override |
131 | final int getNodeType0() { |
132 | return SYNCHRONIZED_STATEMENT; |
133 | } |
134 | |
135 | @Override |
136 | ASTNode clone0(AST target) { |
137 | SynchronizedStatement result = new SynchronizedStatement(target); |
138 | result.setSourceRange(getStartPosition(), getLength()); |
139 | result.copyLeadingComment(this); |
140 | result.setExpression((Expression) getExpression().clone(target)); |
141 | result.setBody((Block) getBody().clone(target)); |
142 | return result; |
143 | } |
144 | |
145 | @Override |
146 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
147 | // dispatch to correct overloaded match method |
148 | return matcher.match(this, other); |
149 | } |
150 | |
151 | @Override |
152 | void accept0(ASTVisitor visitor) { |
153 | boolean visitChildren = visitor.visit(this); |
154 | if (visitChildren) { |
155 | // visit children in normal left to right reading order |
156 | acceptChild(visitor, getExpression()); |
157 | acceptChild(visitor, getBody()); |
158 | } |
159 | visitor.endVisit(this); |
160 | } |
161 | |
162 | /** |
163 | * Returns the expression of this synchronized statement. |
164 | * |
165 | * @return the expression node |
166 | */ |
167 | public Expression getExpression() { |
168 | if (this.expression == null) { |
169 | // lazy init must be thread-safe for readers |
170 | synchronized (this) { |
171 | if (this.expression == null) { |
172 | preLazyInit(); |
173 | this.expression = new SimpleName(this.ast); |
174 | postLazyInit(this.expression, EXPRESSION_PROPERTY); |
175 | } |
176 | } |
177 | } |
178 | return this.expression; |
179 | } |
180 | |
181 | /** |
182 | * Sets the expression of this synchronized statement. |
183 | * |
184 | * @param expression the expression node |
185 | * @exception IllegalArgumentException if: |
186 | * <ul> |
187 | * <li>the node belongs to a different AST</li> |
188 | * <li>the node already has a parent</li> |
189 | * <li>a cycle in would be created</li> |
190 | * </ul> |
191 | */ |
192 | public void setExpression(Expression expression) { |
193 | if (expression == null) { |
194 | throw new IllegalArgumentException(); |
195 | } |
196 | ASTNode oldChild = this.expression; |
197 | preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
198 | this.expression = expression; |
199 | postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
200 | } |
201 | |
202 | /** |
203 | * Returns the body of this synchronized statement. |
204 | * |
205 | * @return the body block node |
206 | */ |
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) { |
212 | preLazyInit(); |
213 | this.body = new Block(this.ast); |
214 | postLazyInit(this.body, BODY_PROPERTY); |
215 | } |
216 | } |
217 | } |
218 | return this.body; |
219 | } |
220 | |
221 | /** |
222 | * Sets the body of this synchronized statement. |
223 | * |
224 | * @param block the body statement node |
225 | * @exception IllegalArgumentException if: |
226 | * <ul> |
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> |
230 | * </ul> |
231 | */ |
232 | public void setBody(Block block) { |
233 | if (block == null) { |
234 | throw new IllegalArgumentException(); |
235 | } |
236 | ASTNode oldChild = this.body; |
237 | preReplaceChild(oldChild, block, BODY_PROPERTY); |
238 | this.body = block; |
239 | postReplaceChild(oldChild, block, BODY_PROPERTY); |
240 | } |
241 | |
242 | @Override |
243 | int memSize() { |
244 | return super.memSize() + 2 * 4; |
245 | } |
246 | |
247 | @Override |
248 | int treeSize() { |
249 | return |
250 | memSize() |
251 | + (this.expression == null ? 0 : getExpression().treeSize()) |
252 | + (this.body == null ? 0 : getBody().treeSize()); |
253 | } |
254 | } |
255 |
Members