1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2004, 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 | * AST node for a parameter within a method reference ({@link MethodRef}). |
22 | * These nodes only occur within doc comments ({@link Javadoc}). |
23 | * <pre> |
24 | * MethodRefParameter: |
25 | * Type [ <b>...</b> ] [ Identifier ] |
26 | * </pre> |
27 | * <p> |
28 | * Note: The 1.5 spec for the Javadoc tool does not mention the possibility |
29 | * of a variable arity indicator in method references. However, the 1.5 |
30 | * Javadoc tool itself does indeed support it. Since it makes sense to have |
31 | * a way to explicitly refer to variable arity methods, it seems more likely |
32 | * that the Javadoc spec is wrong in this case. |
33 | * </p> |
34 | * |
35 | * @see Javadoc |
36 | * @since 3.0 |
37 | * @noinstantiate This class is not intended to be instantiated by clients. |
38 | */ |
39 | @SuppressWarnings("rawtypes") |
40 | public class MethodRefParameter extends ASTNode { |
41 | |
42 | /** |
43 | * The "type" structural property of this node type (child type: {@link Type}). |
44 | * @since 3.0 |
45 | */ |
46 | public static final ChildPropertyDescriptor TYPE_PROPERTY = |
47 | new ChildPropertyDescriptor(MethodRefParameter.class, "type", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$ |
48 | |
49 | /** |
50 | * The "varargs" structural property of this node type (type: {@link Boolean}) (added in JLS3 API). |
51 | * @since 3.1 |
52 | */ |
53 | public static final SimplePropertyDescriptor VARARGS_PROPERTY = |
54 | new SimplePropertyDescriptor(MethodRefParameter.class, "varargs", boolean.class, MANDATORY); //$NON-NLS-1$ |
55 | |
56 | /** |
57 | * The "name" structural property of this node type (child type: {@link SimpleName}). |
58 | * @since 3.0 |
59 | */ |
60 | public static final ChildPropertyDescriptor NAME_PROPERTY = |
61 | new ChildPropertyDescriptor(MethodRefParameter.class, "name", SimpleName.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$ |
62 | |
63 | /** |
64 | * A list of property descriptors (element type: |
65 | * {@link StructuralPropertyDescriptor}), |
66 | * or null if uninitialized. |
67 | * @since 3.0 |
68 | */ |
69 | private static final List PROPERTY_DESCRIPTORS_2_0; |
70 | |
71 | /** |
72 | * A list of property descriptors (element type: |
73 | * {@link StructuralPropertyDescriptor}), |
74 | * or null if uninitialized. |
75 | * @since 3.1 |
76 | */ |
77 | private static final List PROPERTY_DESCRIPTORS_3_0; |
78 | |
79 | static { |
80 | List properyList = new ArrayList(3); |
81 | createPropertyList(MethodRefParameter.class, properyList); |
82 | addProperty(TYPE_PROPERTY, properyList); |
83 | addProperty(NAME_PROPERTY, properyList); |
84 | PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList); |
85 | |
86 | properyList = new ArrayList(3); |
87 | createPropertyList(MethodRefParameter.class, properyList); |
88 | addProperty(TYPE_PROPERTY, properyList); |
89 | addProperty(VARARGS_PROPERTY, properyList); |
90 | addProperty(NAME_PROPERTY, properyList); |
91 | PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList); |
92 | } |
93 | |
94 | /** |
95 | * Returns a list of structural property descriptors for this node type. |
96 | * Clients must not modify the result. |
97 | * |
98 | * @param apiLevel the API level; one of the AST.JLS* constants |
99 | * @return a list of property descriptors (element type: |
100 | * {@link StructuralPropertyDescriptor}) |
101 | * @since 3.0 |
102 | */ |
103 | public static List propertyDescriptors(int apiLevel) { |
104 | if (apiLevel == AST.JLS2_INTERNAL) { |
105 | return PROPERTY_DESCRIPTORS_2_0; |
106 | } else { |
107 | return PROPERTY_DESCRIPTORS_3_0; |
108 | } |
109 | } |
110 | |
111 | /** |
112 | * The type; lazily initialized; defaults to a unspecified, |
113 | * legal type. |
114 | */ |
115 | private Type type = null; |
116 | |
117 | /** |
118 | * Indicates the last parameter of a variable arity method; |
119 | * defaults to false. |
120 | * |
121 | * @since 3.1 |
122 | */ |
123 | private boolean variableArity = false; |
124 | |
125 | /** |
126 | * The parameter name, or <code>null</code> if none; none by |
127 | * default. |
128 | */ |
129 | private SimpleName optionalParameterName = null; |
130 | |
131 | /** |
132 | * Creates a new AST node for a method referenece parameter owned by the given |
133 | * AST. By default, the node has an unspecified (but legal) type, |
134 | * not variable arity, and no parameter name. |
135 | * <p> |
136 | * N.B. This constructor is package-private. |
137 | * </p> |
138 | * |
139 | * @param ast the AST that is to own this node |
140 | */ |
141 | MethodRefParameter(AST ast) { |
142 | super(ast); |
143 | } |
144 | |
145 | @Override |
146 | final List internalStructuralPropertiesForType(int apiLevel) { |
147 | return propertyDescriptors(apiLevel); |
148 | } |
149 | |
150 | @Override |
151 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
152 | if (property == TYPE_PROPERTY) { |
153 | if (get) { |
154 | return getType(); |
155 | } else { |
156 | setType((Type) child); |
157 | return null; |
158 | } |
159 | } |
160 | if (property == NAME_PROPERTY) { |
161 | if (get) { |
162 | return getName(); |
163 | } else { |
164 | setName((SimpleName) child); |
165 | return null; |
166 | } |
167 | } |
168 | // allow default implementation to flag the error |
169 | return super.internalGetSetChildProperty(property, get, child); |
170 | } |
171 | |
172 | @Override |
173 | final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) { |
174 | if (property == VARARGS_PROPERTY) { |
175 | if (get) { |
176 | return isVarargs(); |
177 | } else { |
178 | setVarargs(value); |
179 | return false; |
180 | } |
181 | } |
182 | // allow default implementation to flag the error |
183 | return super.internalGetSetBooleanProperty(property, get, value); |
184 | } |
185 | |
186 | @Override |
187 | final int getNodeType0() { |
188 | return METHOD_REF_PARAMETER; |
189 | } |
190 | |
191 | @Override |
192 | ASTNode clone0(AST target) { |
193 | MethodRefParameter result = new MethodRefParameter(target); |
194 | result.setSourceRange(getStartPosition(), getLength()); |
195 | result.setType((Type) ASTNode.copySubtree(target, getType())); |
196 | if (this.ast.apiLevel >= AST.JLS3_INTERNAL) { |
197 | result.setVarargs(isVarargs()); |
198 | } |
199 | result.setName((SimpleName) ASTNode.copySubtree(target, getName())); |
200 | return result; |
201 | } |
202 | |
203 | @Override |
204 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
205 | // dispatch to correct overloaded match method |
206 | return matcher.match(this, other); |
207 | } |
208 | |
209 | @Override |
210 | void accept0(ASTVisitor visitor) { |
211 | boolean visitChildren = visitor.visit(this); |
212 | if (visitChildren) { |
213 | // visit children in normal left to right reading order |
214 | acceptChild(visitor, getType()); |
215 | acceptChild(visitor, getName()); |
216 | } |
217 | visitor.endVisit(this); |
218 | } |
219 | |
220 | /** |
221 | * Returns the paramter type. |
222 | * |
223 | * @return the parameter type |
224 | */ |
225 | public Type getType() { |
226 | if (this.type == null) { |
227 | // lazy init must be thread-safe for readers |
228 | synchronized (this) { |
229 | if (this.type == null) { |
230 | preLazyInit(); |
231 | this.type = this.ast.newPrimitiveType(PrimitiveType.INT); |
232 | postLazyInit(this.type, TYPE_PROPERTY); |
233 | } |
234 | } |
235 | } |
236 | return this.type; |
237 | } |
238 | |
239 | /** |
240 | * Sets the paramter type to the given type. |
241 | * |
242 | * @param type the new type |
243 | * @exception IllegalArgumentException if: |
244 | * <ul> |
245 | * <li>the type is <code>null</code></li> |
246 | * <li>the node belongs to a different AST</li> |
247 | * <li>the node already has a parent</li> |
248 | * </ul> |
249 | */ |
250 | public void setType(Type type) { |
251 | if (type == null) { |
252 | throw new IllegalArgumentException(); |
253 | } |
254 | ASTNode oldChild = this.type; |
255 | preReplaceChild(oldChild, type, TYPE_PROPERTY); |
256 | this.type = type; |
257 | postReplaceChild(oldChild, type, TYPE_PROPERTY); |
258 | } |
259 | |
260 | /** |
261 | * Returns whether this method reference parameter is for |
262 | * the last parameter of a variable arity method (added in JLS3 API). |
263 | * <p> |
264 | * Note that the binding for the type <code>Foo</code>in the vararg method |
265 | * reference <code>#fun(Foo...)</code> is always for the type as |
266 | * written; i.e., the type binding for <code>Foo</code>. However, if you |
267 | * navigate from the MethodRef to its method binding to the |
268 | * type binding for its last parameter, the type binding for the vararg |
269 | * parameter is always an array type (i.e., <code>Foo[]</code>) reflecting |
270 | * the way vararg methods get compiled. |
271 | * </p> |
272 | * |
273 | * @return <code>true</code> if this is a variable arity parameter, |
274 | * and <code>false</code> otherwise |
275 | * @exception UnsupportedOperationException if this operation is used in |
276 | * a JLS2 AST |
277 | * @since 3.1 |
278 | */ |
279 | public boolean isVarargs() { |
280 | unsupportedIn2(); |
281 | return this.variableArity; |
282 | } |
283 | |
284 | /** |
285 | * Sets whether this method reference parameter is for the last parameter of |
286 | * a variable arity method (added in JLS3 API). |
287 | * |
288 | * @param variableArity <code>true</code> if this is a variable arity |
289 | * parameter, and <code>false</code> otherwise |
290 | * @since 3.1 |
291 | */ |
292 | public void setVarargs(boolean variableArity) { |
293 | unsupportedIn2(); |
294 | preValueChange(VARARGS_PROPERTY); |
295 | this.variableArity = variableArity; |
296 | postValueChange(VARARGS_PROPERTY); |
297 | } |
298 | |
299 | /** |
300 | * Returns the parameter name, or <code>null</code> if there is none. |
301 | * |
302 | * @return the parameter name node, or <code>null</code> if there is none |
303 | */ |
304 | public SimpleName getName() { |
305 | return this.optionalParameterName; |
306 | } |
307 | |
308 | /** |
309 | * Sets or clears the parameter name. |
310 | * |
311 | * @param name the parameter name node, or <code>null</code> if |
312 | * there is none |
313 | * @exception IllegalArgumentException if: |
314 | * <ul> |
315 | * <li>the node belongs to a different AST</li> |
316 | * <li>the node already has a parent</li> |
317 | * </ul> |
318 | */ |
319 | public void setName(SimpleName name) { |
320 | ASTNode oldChild = this.optionalParameterName; |
321 | preReplaceChild(oldChild, name, NAME_PROPERTY); |
322 | this.optionalParameterName = name; |
323 | postReplaceChild(oldChild, name, NAME_PROPERTY); |
324 | } |
325 | |
326 | @Override |
327 | int memSize() { |
328 | return BASE_NODE_SIZE + 2 * 5; |
329 | } |
330 | |
331 | @Override |
332 | int treeSize() { |
333 | return |
334 | memSize() |
335 | + (this.type == null ? 0 : getType().treeSize()) |
336 | + (this.optionalParameterName == null ? 0 : getName().treeSize()); |
337 | } |
338 | } |
339 |
Members