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