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 member reference within a doc comment |
22 | * ({@link Javadoc}). The principal uses of these are in "@see" and "@link" |
23 | * tag elements, for references to field members (and occasionally to method |
24 | * and constructor members). |
25 | * <pre> |
26 | * MemberRef: |
27 | * [ Name ] <b>#</b> Identifier |
28 | * </pre> |
29 | * |
30 | * @see Javadoc |
31 | * @since 3.0 |
32 | * @noinstantiate This class is not intended to be instantiated by clients. |
33 | */ |
34 | @SuppressWarnings("rawtypes") |
35 | public class MemberRef extends ASTNode implements IDocElement { |
36 | |
37 | /** |
38 | * The "qualifier" structural property of this node type (child type: {@link Name}). |
39 | * @since 3.0 |
40 | */ |
41 | public static final ChildPropertyDescriptor QUALIFIER_PROPERTY = |
42 | new ChildPropertyDescriptor(MemberRef.class, "qualifier", Name.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$ |
43 | |
44 | /** |
45 | * The "name" structural property of this node type (child type: {@link SimpleName}). |
46 | * @since 3.0 |
47 | */ |
48 | public static final ChildPropertyDescriptor NAME_PROPERTY = |
49 | new ChildPropertyDescriptor(MemberRef.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$ |
50 | |
51 | /** |
52 | * A list of property descriptors (element type: |
53 | * {@link StructuralPropertyDescriptor}), |
54 | * or null if uninitialized. |
55 | */ |
56 | private static final List PROPERTY_DESCRIPTORS; |
57 | |
58 | static { |
59 | List propertyList = new ArrayList(3); |
60 | createPropertyList(MemberRef.class, propertyList); |
61 | addProperty(QUALIFIER_PROPERTY, propertyList); |
62 | addProperty(NAME_PROPERTY, propertyList); |
63 | PROPERTY_DESCRIPTORS = reapPropertyList(propertyList); |
64 | } |
65 | |
66 | /** |
67 | * Returns a list of structural property descriptors for this node type. |
68 | * Clients must not modify the result. |
69 | * |
70 | * @param apiLevel the API level; one of the AST.JLS* constants |
71 | * @return a list of property descriptors (element type: |
72 | * {@link StructuralPropertyDescriptor}) |
73 | * @since 3.0 |
74 | */ |
75 | public static List propertyDescriptors(int apiLevel) { |
76 | return PROPERTY_DESCRIPTORS; |
77 | } |
78 | |
79 | /** |
80 | * The optional qualifier; <code>null</code> for none; defaults to none. |
81 | */ |
82 | private Name optionalQualifier = null; |
83 | |
84 | /** |
85 | * The member name; lazily initialized; defaults to a unspecified, |
86 | * legal Java method name. |
87 | */ |
88 | private SimpleName memberName = null; |
89 | |
90 | /** |
91 | * Creates a new AST node for a member reference owned by the given |
92 | * AST. By default, the method reference is for a member with an |
93 | * unspecified, but legal, name; and no qualifier. |
94 | * <p> |
95 | * N.B. This constructor is package-private; all subclasses must be |
96 | * declared in the same package; clients are unable to declare |
97 | * additional subclasses. |
98 | * </p> |
99 | * |
100 | * @param ast the AST that is to own this node |
101 | */ |
102 | MemberRef(AST ast) { |
103 | super(ast); |
104 | } |
105 | |
106 | @Override |
107 | final List internalStructuralPropertiesForType(int apiLevel) { |
108 | return propertyDescriptors(apiLevel); |
109 | } |
110 | |
111 | @Override |
112 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
113 | if (property == QUALIFIER_PROPERTY) { |
114 | if (get) { |
115 | return getQualifier(); |
116 | } else { |
117 | setQualifier((Name) child); |
118 | return null; |
119 | } |
120 | } |
121 | if (property == NAME_PROPERTY) { |
122 | if (get) { |
123 | return getName(); |
124 | } else { |
125 | setName((SimpleName) child); |
126 | return null; |
127 | } |
128 | } |
129 | // allow default implementation to flag the error |
130 | return super.internalGetSetChildProperty(property, get, child); |
131 | } |
132 | |
133 | @Override |
134 | final int getNodeType0() { |
135 | return MEMBER_REF; |
136 | } |
137 | |
138 | @Override |
139 | ASTNode clone0(AST target) { |
140 | MemberRef result = new MemberRef(target); |
141 | result.setSourceRange(getStartPosition(), getLength()); |
142 | result.setQualifier((Name) ASTNode.copySubtree(target, getQualifier())); |
143 | result.setName((SimpleName) ASTNode.copySubtree(target, getName())); |
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, getQualifier()); |
159 | acceptChild(visitor, getName()); |
160 | } |
161 | visitor.endVisit(this); |
162 | } |
163 | |
164 | /** |
165 | * Returns the qualifier of this member reference, or |
166 | * <code>null</code> if there is none. |
167 | * |
168 | * @return the qualifier name node, or <code>null</code> if there is none |
169 | */ |
170 | public Name getQualifier() { |
171 | return this.optionalQualifier; |
172 | } |
173 | |
174 | /** |
175 | * Sets or clears the qualifier of this member reference. |
176 | * |
177 | * @param name the qualifier name node, or <code>null</code> if |
178 | * there is none |
179 | * @exception IllegalArgumentException if: |
180 | * <ul> |
181 | * <li>the node belongs to a different AST</li> |
182 | * <li>the node already has a parent</li> |
183 | * </ul> |
184 | */ |
185 | public void setQualifier(Name name) { |
186 | ASTNode oldChild = this.optionalQualifier; |
187 | preReplaceChild(oldChild, name, QUALIFIER_PROPERTY); |
188 | this.optionalQualifier = name; |
189 | postReplaceChild(oldChild, name, QUALIFIER_PROPERTY); |
190 | } |
191 | |
192 | /** |
193 | * Returns the name of the referenced member. |
194 | * |
195 | * @return the member name node |
196 | */ |
197 | public SimpleName getName() { |
198 | if (this.memberName == null) { |
199 | // lazy init must be thread-safe for readers |
200 | synchronized (this) { |
201 | if (this.memberName == null) { |
202 | preLazyInit(); |
203 | this.memberName = new SimpleName(this.ast); |
204 | postLazyInit(this.memberName, NAME_PROPERTY); |
205 | } |
206 | } |
207 | } |
208 | return this.memberName; |
209 | } |
210 | |
211 | /** |
212 | * Sets the name of the referenced member to the given name. |
213 | * |
214 | * @param name the new member name node |
215 | * @exception IllegalArgumentException if: |
216 | * <ul> |
217 | * <li>the name is <code>null</code></li> |
218 | * <li>the node belongs to a different AST</li> |
219 | * <li>the node already has a parent</li> |
220 | * </ul> |
221 | */ |
222 | public void setName(SimpleName name) { |
223 | if (name == null) { |
224 | throw new IllegalArgumentException(); |
225 | } |
226 | ASTNode oldChild = this.memberName; |
227 | preReplaceChild(oldChild, name, NAME_PROPERTY); |
228 | this.memberName = name; |
229 | postReplaceChild(oldChild, name, NAME_PROPERTY); |
230 | } |
231 | |
232 | /** |
233 | * Resolves and returns the binding for the entity referred to by |
234 | * this member reference. |
235 | * <p> |
236 | * Note that bindings are generally unavailable unless requested when the |
237 | * AST is being built. |
238 | * </p> |
239 | * |
240 | * @return the binding, or <code>null</code> if the binding cannot be |
241 | * resolved |
242 | */ |
243 | public final IBinding resolveBinding() { |
244 | return this.ast.getBindingResolver().resolveReference(this); |
245 | } |
246 | |
247 | @Override |
248 | int memSize() { |
249 | return BASE_NODE_SIZE + 2 * 4; |
250 | } |
251 | |
252 | @Override |
253 | int treeSize() { |
254 | return |
255 | memSize() |
256 | + (this.optionalQualifier == null ? 0 : getQualifier().treeSize()) |
257 | + (this.memberName == null ? 0 : getName().treeSize()); |
258 | } |
259 | } |
260 | |
261 |
Members