1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2013, 2014 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 | package org.eclipse.jdt.core.dom; |
15 | |
16 | import java.util.ArrayList; |
17 | import java.util.List; |
18 | |
19 | /** |
20 | * Super method reference AST node type (added in JLS8 API). |
21 | * |
22 | * <pre> |
23 | * SuperMethodReference: |
24 | * [ ClassName <b>.</b> ] <b>super</b> <b>::</b> |
25 | * [ <b><</b> Type { <b>,</b> Type } <b>></b> ] |
26 | * Identifier |
27 | * </pre> |
28 | * |
29 | * @since 3.10 |
30 | * @noinstantiate This class is not intended to be instantiated by clients |
31 | */ |
32 | @SuppressWarnings({"rawtypes", "unchecked"}) |
33 | public class SuperMethodReference extends MethodReference { |
34 | |
35 | /** |
36 | * The "qualifier" structural property of this node type (child type: {@link Name}). |
37 | */ |
38 | public static final ChildPropertyDescriptor QUALIFIER_PROPERTY = |
39 | new ChildPropertyDescriptor(SuperMethodReference.class, "qualifier", Name.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$ |
40 | |
41 | /** |
42 | * The "typeArguments" structural property of this node type (element type: {@link Type}). |
43 | */ |
44 | public static final ChildListPropertyDescriptor TYPE_ARGUMENTS_PROPERTY = |
45 | internalTypeArgumentsFactory(SuperMethodReference.class); |
46 | |
47 | /** |
48 | * The "name" structural property of this node type (child type: {@link SimpleName}). |
49 | */ |
50 | public static final ChildPropertyDescriptor NAME_PROPERTY = |
51 | new ChildPropertyDescriptor(SuperMethodReference.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$ |
52 | |
53 | /** |
54 | * A list of property descriptors (element type: |
55 | * {@link StructuralPropertyDescriptor}), |
56 | * or null if uninitialized. |
57 | */ |
58 | private static final List PROPERTY_DESCRIPTORS_8_0; |
59 | |
60 | static { |
61 | List propertyList = new ArrayList(4); |
62 | createPropertyList(SuperMethodReference.class, propertyList); |
63 | addProperty(QUALIFIER_PROPERTY, propertyList); |
64 | addProperty(TYPE_ARGUMENTS_PROPERTY, propertyList); |
65 | addProperty(NAME_PROPERTY, propertyList); |
66 | PROPERTY_DESCRIPTORS_8_0 = reapPropertyList(propertyList); |
67 | } |
68 | |
69 | /** |
70 | * Returns a list of structural property descriptors for this node type. |
71 | * Clients must not modify the result. |
72 | * |
73 | * @param apiLevel the API level; one of the |
74 | * <code>AST.JLS*</code> constants |
75 | * @return a list of property descriptors (element type: |
76 | * {@link StructuralPropertyDescriptor}) |
77 | */ |
78 | public static List propertyDescriptors(int apiLevel) { |
79 | return PROPERTY_DESCRIPTORS_8_0; |
80 | } |
81 | |
82 | /** |
83 | * The optional qualifier; <code>null</code> for none; defaults to none. |
84 | */ |
85 | private Name optionalQualifier = null; |
86 | |
87 | /** |
88 | * The method name; lazily initialized; defaults to a unspecified, |
89 | * legal Java method name. |
90 | */ |
91 | private SimpleName methodName = null; |
92 | /** |
93 | * Creates a new AST node for a super method reference owned |
94 | * by the given AST. By default, there is no qualifier. |
95 | * <p> |
96 | * N.B. This constructor is package-private; all subclasses must be |
97 | * declared in the same package; clients are unable to declare |
98 | * additional subclasses. |
99 | * </p> |
100 | * |
101 | * @param ast the AST that is to own this node |
102 | */ |
103 | SuperMethodReference(AST ast) { |
104 | super(ast); |
105 | unsupportedIn2_3_4(); |
106 | } |
107 | |
108 | @Override |
109 | final ChildListPropertyDescriptor internalTypeArgumentsProperty() { |
110 | return TYPE_ARGUMENTS_PROPERTY; |
111 | } |
112 | |
113 | @Override |
114 | final List internalStructuralPropertiesForType(int apiLevel) { |
115 | return propertyDescriptors(apiLevel); |
116 | } |
117 | |
118 | @Override |
119 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
120 | if (property == QUALIFIER_PROPERTY) { |
121 | if (get) { |
122 | return getQualifier(); |
123 | } else { |
124 | setQualifier((Name) child); |
125 | return null; |
126 | } |
127 | } |
128 | if (property == NAME_PROPERTY) { |
129 | if (get) { |
130 | return getName(); |
131 | } else { |
132 | setName((SimpleName) child); |
133 | return null; |
134 | } |
135 | } |
136 | // allow default implementation to flag the error |
137 | return super.internalGetSetChildProperty(property, get, child); |
138 | } |
139 | |
140 | @Override |
141 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
142 | if (property == TYPE_ARGUMENTS_PROPERTY) { |
143 | return typeArguments(); |
144 | } |
145 | // allow default implementation to flag the error |
146 | return super.internalGetChildListProperty(property); |
147 | } |
148 | |
149 | @Override |
150 | final int getNodeType0() { |
151 | return SUPER_METHOD_REFERENCE; |
152 | } |
153 | |
154 | @Override |
155 | ASTNode clone0(AST target) { |
156 | SuperMethodReference result = new SuperMethodReference(target); |
157 | result.setSourceRange(getStartPosition(), getLength()); |
158 | result.setName((SimpleName) getName().clone(target)); |
159 | result.setQualifier((Name) ASTNode.copySubtree(target, getQualifier())); |
160 | result.typeArguments().addAll(ASTNode.copySubtrees(target, typeArguments())); |
161 | return result; |
162 | } |
163 | |
164 | @Override |
165 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
166 | // dispatch to correct overloaded match method |
167 | return matcher.match(this, other); |
168 | } |
169 | |
170 | @Override |
171 | void accept0(ASTVisitor visitor) { |
172 | boolean visitChildren = visitor.visit(this); |
173 | if (visitChildren) { |
174 | // visit children in normal left to right reading order |
175 | acceptChild(visitor, getQualifier()); |
176 | acceptChildren(visitor, this.typeArguments); |
177 | acceptChild(visitor, getName()); |
178 | } |
179 | visitor.endVisit(this); |
180 | } |
181 | |
182 | /** |
183 | * Returns the qualifier of this "super" method reference, or |
184 | * <code>null</code> if there is none. |
185 | * |
186 | * @return the qualifier name node, or <code>null</code> if there is none |
187 | */ |
188 | public Name getQualifier() { |
189 | return this.optionalQualifier; |
190 | } |
191 | |
192 | /** |
193 | * Sets the qualifier of this "super" method reference expression. |
194 | * |
195 | * @param name the qualifier name node, or <code>null</code> if |
196 | * there is none |
197 | * @exception IllegalArgumentException if: |
198 | * <ul> |
199 | * <li>the node belongs to a different AST</li> |
200 | * <li>the node already has a parent</li> |
201 | * </ul> |
202 | */ |
203 | public void setQualifier(Name name) { |
204 | ASTNode oldChild = this.optionalQualifier; |
205 | preReplaceChild(oldChild, name, QUALIFIER_PROPERTY); |
206 | this.optionalQualifier = name; |
207 | postReplaceChild(oldChild, name, QUALIFIER_PROPERTY); |
208 | } |
209 | |
210 | /** |
211 | * Returns the live ordered list of type arguments of this super method reference. |
212 | * |
213 | * @return the live list of type arguments |
214 | * (element type: {@link Type}) |
215 | */ |
216 | @Override |
217 | public List typeArguments() { |
218 | return this.typeArguments; |
219 | } |
220 | |
221 | /** |
222 | * Returns the name of the method referenced in this expression. |
223 | * |
224 | * @return the method name node |
225 | */ |
226 | public SimpleName getName() { |
227 | if (this.methodName == null) { |
228 | // lazy init must be thread-safe for readers |
229 | synchronized (this) { |
230 | if (this.methodName == null) { |
231 | preLazyInit(); |
232 | this.methodName = new SimpleName(this.ast); |
233 | postLazyInit(this.methodName, NAME_PROPERTY); |
234 | } |
235 | } |
236 | } |
237 | return this.methodName; |
238 | } |
239 | |
240 | /** |
241 | * Sets the name of the method referenced in this expression to the |
242 | * given name. |
243 | * |
244 | * @param name the new method name |
245 | * @exception IllegalArgumentException if: |
246 | * <ul> |
247 | * <li>the node belongs to a different AST</li> |
248 | * <li>the node already has a parent</li> |
249 | * </ul> |
250 | */ |
251 | public void setName(SimpleName name) { |
252 | if (name == null) { |
253 | throw new IllegalArgumentException(); |
254 | } |
255 | ASTNode oldChild = this.methodName; |
256 | preReplaceChild(oldChild, name, NAME_PROPERTY); |
257 | this.methodName = name; |
258 | postReplaceChild(oldChild, name, NAME_PROPERTY); |
259 | } |
260 | |
261 | @Override |
262 | int memSize() { |
263 | // treat Code as free |
264 | return BASE_NODE_SIZE + 3 * 4; |
265 | } |
266 | |
267 | @Override |
268 | int treeSize() { |
269 | return |
270 | memSize() |
271 | + (this.optionalQualifier == null ? 0 : getQualifier().treeSize()) |
272 | + (this.typeArguments == null ? 0 : this.typeArguments.listSize()) |
273 | + (this.methodName == null ? 0 : getName().treeSize()); |
274 | } |
275 | } |
276 |
Members