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 | * Assert statement AST node type. |
22 | * |
23 | * <pre> |
24 | * AssertStatement: |
25 | * <b>assert</b> Expression [ <b>:</b> Expression ] <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 AssertStatement 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(AssertStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
40 | |
41 | /** |
42 | * The "message" structural property of this node type (child type: {@link Expression}). |
43 | * @since 3.0 |
44 | */ |
45 | public static final ChildPropertyDescriptor MESSAGE_PROPERTY = |
46 | new ChildPropertyDescriptor(AssertStatement.class, "message", Expression.class, OPTIONAL, 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(AssertStatement.class, properyList); |
58 | addProperty(EXPRESSION_PROPERTY, properyList); |
59 | addProperty(MESSAGE_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 expression; lazily initialized; defaults to a unspecified, but legal, |
80 | * expression. |
81 | */ |
82 | private Expression expression = null; |
83 | |
84 | /** |
85 | * The message expression; <code>null</code> for none; defaults to none. |
86 | */ |
87 | private Expression optionalMessageExpression = null; |
88 | |
89 | /** |
90 | * Creates a new unparented assert statement node owned by the given |
91 | * AST. By default, the assert statement has an unspecified, but legal, |
92 | * expression, and not message expression. |
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 | AssertStatement(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 == MESSAGE_PROPERTY) { |
119 | if (get) { |
120 | return getMessage(); |
121 | } else { |
122 | setMessage((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 ASSERT_STATEMENT; |
133 | } |
134 | |
135 | @Override |
136 | ASTNode clone0(AST target) { |
137 | AssertStatement result = new AssertStatement(target); |
138 | result.setSourceRange(getStartPosition(), getLength()); |
139 | result.copyLeadingComment(this); |
140 | result.setExpression( |
141 | (Expression) ASTNode.copySubtree(target, getExpression())); |
142 | result.setMessage( |
143 | (Expression) ASTNode.copySubtree(target, getMessage())); |
144 | return result; |
145 | } |
146 | |
147 | @Override |
148 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
149 | // dispatch to correct overloaded match method |
150 | return matcher.match(this, other); |
151 | } |
152 | |
153 | @Override |
154 | void accept0(ASTVisitor visitor) { |
155 | boolean visitChildren = visitor.visit(this); |
156 | if (visitChildren) { |
157 | // visit children in normal left to right reading order |
158 | acceptChild(visitor, getExpression()); |
159 | acceptChild(visitor, getMessage()); |
160 | } |
161 | visitor.endVisit(this); |
162 | } |
163 | |
164 | /** |
165 | * Returns the first expression of this assert statement. |
166 | * |
167 | * @return the expression node |
168 | */ |
169 | public Expression getExpression() { |
170 | if (this.expression == null) { |
171 | // lazy init must be thread-safe for readers |
172 | synchronized (this) { |
173 | if (this.expression == null) { |
174 | preLazyInit(); |
175 | this.expression = new SimpleName(this.ast); |
176 | postLazyInit(this.expression, EXPRESSION_PROPERTY); |
177 | } |
178 | } |
179 | } |
180 | return this.expression; |
181 | } |
182 | |
183 | /** |
184 | * Sets the first expression of this assert statement. |
185 | * |
186 | * @param expression the new expression node |
187 | * @exception IllegalArgumentException if: |
188 | * <ul> |
189 | * <li>the node belongs to a different AST</li> |
190 | * <li>the node already has a parent</li> |
191 | * <li>a cycle in would be created</li> |
192 | * </ul> |
193 | */ |
194 | public void setExpression(Expression expression) { |
195 | if (expression == null) { |
196 | throw new IllegalArgumentException(); |
197 | } |
198 | // an AssertStatement may occur inside an Expression - must check cycles |
199 | ASTNode oldChild = this.expression; |
200 | preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
201 | this.expression = expression; |
202 | postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
203 | } |
204 | |
205 | /** |
206 | * Returns the message expression of this assert statement, or |
207 | * <code>null</code> if there is none. |
208 | * |
209 | * @return the message expression node, or <code>null</code> if there |
210 | * is none |
211 | */ |
212 | public Expression getMessage() { |
213 | return this.optionalMessageExpression; |
214 | } |
215 | |
216 | /** |
217 | * Sets or clears the message expression of this assert statement. |
218 | * |
219 | * @param expression the message expression node, or <code>null</code> if |
220 | * there is none |
221 | * @exception IllegalArgumentException if: |
222 | * <ul> |
223 | * <li>the node belongs to a different AST</li> |
224 | * <li>the node already has a parent</li> |
225 | * <li>a cycle in would be created</li> |
226 | * </ul> |
227 | */ |
228 | public void setMessage(Expression expression) { |
229 | // an AsertStatement may occur inside an Expression - must check cycles |
230 | ASTNode oldChild = this.optionalMessageExpression; |
231 | preReplaceChild(oldChild, expression, MESSAGE_PROPERTY); |
232 | this.optionalMessageExpression = expression; |
233 | postReplaceChild(oldChild, expression, MESSAGE_PROPERTY); |
234 | } |
235 | |
236 | @Override |
237 | int memSize() { |
238 | return super.memSize() + 2 * 4; |
239 | } |
240 | |
241 | @Override |
242 | int treeSize() { |
243 | return |
244 | memSize() |
245 | + (this.expression == null ? 0 : getExpression().treeSize()) |
246 | + (this.optionalMessageExpression == null ? 0 : getMessage().treeSize()); |
247 | |
248 | } |
249 | } |
250 | |
251 |
Members