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 | * Do statement AST node type. |
22 | * |
23 | * <pre> |
24 | * DoStatement: |
25 | * <b>do</b> Statement <b>while</b> <b>(</b> Expression <b>)</b> <b>;</b> |
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 DoStatement extends Statement { |
33 | |
34 | /** |
35 | * The "body" structural property of this node type (child type: {@link Statement}). |
36 | * @since 3.0 |
37 | */ |
38 | public static final ChildPropertyDescriptor BODY_PROPERTY = |
39 | new ChildPropertyDescriptor(DoStatement.class, "body", Statement.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
40 | |
41 | /** |
42 | * The "expression" structural property of this node type (child type: {@link Expression}). |
43 | * @since 3.0 |
44 | */ |
45 | public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = |
46 | new ChildPropertyDescriptor(DoStatement.class, "expression", Expression.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 properyList = new ArrayList(3); |
57 | createPropertyList(DoStatement.class, properyList); |
58 | addProperty(BODY_PROPERTY, properyList); |
59 | addProperty(EXPRESSION_PROPERTY, properyList); |
60 | PROPERTY_DESCRIPTORS = reapPropertyList(properyList); |
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 body statement; lazily initialized; defaults to an empty block. |
80 | */ |
81 | private Statement body = null; |
82 | |
83 | /** |
84 | * The expression; lazily initialized; defaults to an unspecified, but |
85 | * legal, expression. |
86 | */ |
87 | private Expression expression = null; |
88 | |
89 | /** |
90 | * Creates a new unparented do statement node owned by the given |
91 | * AST. By default, the expression is unspecified, but legal, |
92 | * and the body statement 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 | DoStatement(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 == BODY_PROPERTY) { |
111 | if (get) { |
112 | return getBody(); |
113 | } else { |
114 | setBody((Statement) child); |
115 | return null; |
116 | } |
117 | } |
118 | if (property == EXPRESSION_PROPERTY) { |
119 | if (get) { |
120 | return getExpression(); |
121 | } else { |
122 | setExpression((Expression) 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 DO_STATEMENT; |
133 | } |
134 | |
135 | @Override |
136 | ASTNode clone0(AST target) { |
137 | DoStatement result = new DoStatement(target); |
138 | result.setSourceRange(getStartPosition(), getLength()); |
139 | result.copyLeadingComment(this); |
140 | result.setBody((Statement) getBody().clone(target)); |
141 | result.setExpression((Expression) getExpression().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, getBody()); |
157 | acceptChild(visitor, getExpression()); |
158 | } |
159 | visitor.endVisit(this); |
160 | } |
161 | |
162 | /** |
163 | * Returns the body of this do statement. |
164 | * |
165 | * @return the body statement node |
166 | */ |
167 | public Statement getBody() { |
168 | if (this.body == null) { |
169 | // lazy init must be thread-safe for readers |
170 | synchronized (this) { |
171 | if (this.body == null) { |
172 | preLazyInit(); |
173 | this.body = new Block(this.ast); |
174 | postLazyInit(this.body, BODY_PROPERTY); |
175 | } |
176 | } |
177 | } |
178 | return this.body; |
179 | } |
180 | |
181 | /** |
182 | * Sets the body of this do statement. |
183 | * <p> |
184 | * Special note: The Java language does not allow a local variable declaration |
185 | * to appear as the body of a do statement (they may only appear within a |
186 | * block). However, the AST will allow a <code>VariableDeclarationStatement</code> |
187 | * as the body of a <code>DoStatement</code>. To get something that will |
188 | * compile, be sure to embed the <code>VariableDeclarationStatement</code> |
189 | * inside a <code>Block</code>. |
190 | * </p> |
191 | * |
192 | * @param statement the body statement 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 setBody(Statement statement) { |
201 | if (statement == null) { |
202 | throw new IllegalArgumentException(); |
203 | } |
204 | ASTNode oldChild = this.body; |
205 | preReplaceChild(oldChild, statement, BODY_PROPERTY); |
206 | this.body = statement; |
207 | postReplaceChild(oldChild, statement, BODY_PROPERTY); |
208 | } |
209 | |
210 | /** |
211 | * Returns the expression of this do statement. |
212 | * |
213 | * @return the expression node |
214 | */ |
215 | public Expression getExpression() { |
216 | if (this.expression == null) { |
217 | // lazy init must be thread-safe for readers |
218 | synchronized (this) { |
219 | if (this.expression == null) { |
220 | preLazyInit(); |
221 | this.expression = new SimpleName(this.ast); |
222 | postLazyInit(this.expression, EXPRESSION_PROPERTY); |
223 | } |
224 | } |
225 | } |
226 | return this.expression; |
227 | } |
228 | |
229 | /** |
230 | * Sets the expression of this do statement. |
231 | * |
232 | * @param expression the expression 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 setExpression(Expression expression) { |
241 | if (expression == null) { |
242 | throw new IllegalArgumentException(); |
243 | } |
244 | ASTNode oldChild = this.expression; |
245 | preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
246 | this.expression = expression; |
247 | postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
248 | } |
249 | |
250 | @Override |
251 | int memSize() { |
252 | return super.memSize() + 2 * 4; |
253 | } |
254 | |
255 | @Override |
256 | int treeSize() { |
257 | return |
258 | memSize() |
259 | + (this.expression == null ? 0 : getExpression().treeSize()) |
260 | + (this.body == null ? 0 : getBody().treeSize()); |
261 | } |
262 | } |
263 |
Members