| 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 method or constructor reference within a doc comment |
| 22 | * ({@link Javadoc}). The principal uses of these are in "@see" and "@link" |
| 23 | * tag elements, for references to method and constructor members. |
| 24 | * <pre> |
| 25 | * MethodRef: |
| 26 | * [ Name ] <b>#</b> Identifier |
| 27 | * <b>(</b> [ MethodRefParameter | { <b>,</b> MethodRefParameter } ] <b>)</b> |
| 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", "unchecked"}) |
| 35 | public class MethodRef 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(MethodRef.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(MethodRef.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 50 | |
| 51 | /** |
| 52 | * The "parameters" structural property of this node type (element type: {@link MethodRefParameter}). |
| 53 | * @since 3.0 |
| 54 | */ |
| 55 | public static final ChildListPropertyDescriptor PARAMETERS_PROPERTY = |
| 56 | new ChildListPropertyDescriptor(MethodRef.class, "parameters", MethodRefParameter.class, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 57 | |
| 58 | /** |
| 59 | * A list of property descriptors (element type: |
| 60 | * {@link StructuralPropertyDescriptor}), |
| 61 | * or null if uninitialized. |
| 62 | */ |
| 63 | private static final List PROPERTY_DESCRIPTORS; |
| 64 | |
| 65 | static { |
| 66 | List properyList = new ArrayList(4); |
| 67 | createPropertyList(MethodRef.class, properyList); |
| 68 | addProperty(QUALIFIER_PROPERTY, properyList); |
| 69 | addProperty(NAME_PROPERTY, properyList); |
| 70 | addProperty(PARAMETERS_PROPERTY, properyList); |
| 71 | PROPERTY_DESCRIPTORS = reapPropertyList(properyList); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns a list of structural property descriptors for this node type. |
| 76 | * Clients must not modify the result. |
| 77 | * |
| 78 | * @param apiLevel the API level; one of the AST.JLS* constants |
| 79 | * @return a list of property descriptors (element type: |
| 80 | * {@link StructuralPropertyDescriptor}) |
| 81 | * @since 3.0 |
| 82 | */ |
| 83 | public static List propertyDescriptors(int apiLevel) { |
| 84 | return PROPERTY_DESCRIPTORS; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * The optional qualifier; <code>null</code> for none; defaults to none. |
| 89 | */ |
| 90 | private Name optionalQualifier = null; |
| 91 | |
| 92 | /** |
| 93 | * The method name; lazily initialized; defaults to a unspecified, |
| 94 | * legal Java method name. |
| 95 | */ |
| 96 | private SimpleName methodName = null; |
| 97 | |
| 98 | /** |
| 99 | * The parameter declarations |
| 100 | * (element type: {@link MethodRefParameter}). |
| 101 | * Defaults to an empty list. |
| 102 | */ |
| 103 | private ASTNode.NodeList parameters = |
| 104 | new ASTNode.NodeList(PARAMETERS_PROPERTY); |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | * Creates a new AST node for a method reference owned by the given |
| 109 | * AST. By default, the method reference is for a method with an |
| 110 | * unspecified, but legal, name; no qualifier; and an empty parameter |
| 111 | * list. |
| 112 | * <p> |
| 113 | * N.B. This constructor is package-private; all subclasses must be |
| 114 | * declared in the same package; clients are unable to declare |
| 115 | * additional subclasses. |
| 116 | * </p> |
| 117 | * |
| 118 | * @param ast the AST that is to own this node |
| 119 | */ |
| 120 | MethodRef(AST ast) { |
| 121 | super(ast); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 126 | return propertyDescriptors(apiLevel); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 131 | if (property == QUALIFIER_PROPERTY) { |
| 132 | if (get) { |
| 133 | return getQualifier(); |
| 134 | } else { |
| 135 | setQualifier((Name) child); |
| 136 | return null; |
| 137 | } |
| 138 | } |
| 139 | if (property == NAME_PROPERTY) { |
| 140 | if (get) { |
| 141 | return getName(); |
| 142 | } else { |
| 143 | setName((SimpleName) child); |
| 144 | return null; |
| 145 | } |
| 146 | } |
| 147 | // allow default implementation to flag the error |
| 148 | return super.internalGetSetChildProperty(property, get, child); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
| 153 | if (property == PARAMETERS_PROPERTY) { |
| 154 | return parameters(); |
| 155 | } |
| 156 | // allow default implementation to flag the error |
| 157 | return super.internalGetChildListProperty(property); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | final int getNodeType0() { |
| 162 | return METHOD_REF; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | ASTNode clone0(AST target) { |
| 167 | MethodRef result = new MethodRef(target); |
| 168 | result.setSourceRange(getStartPosition(), getLength()); |
| 169 | result.setQualifier((Name) ASTNode.copySubtree(target, getQualifier())); |
| 170 | result.setName((SimpleName) ASTNode.copySubtree(target, getName())); |
| 171 | result.parameters().addAll( |
| 172 | ASTNode.copySubtrees(target, parameters())); |
| 173 | return result; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 178 | // dispatch to correct overloaded match method |
| 179 | return matcher.match(this, other); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | void accept0(ASTVisitor visitor) { |
| 184 | boolean visitChildren = visitor.visit(this); |
| 185 | if (visitChildren) { |
| 186 | // visit children in normal left to right reading order |
| 187 | acceptChild(visitor, getQualifier()); |
| 188 | acceptChild(visitor, getName()); |
| 189 | acceptChildren(visitor, this.parameters); |
| 190 | } |
| 191 | visitor.endVisit(this); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns the qualifier of this method reference, or |
| 196 | * <code>null</code> if there is none. |
| 197 | * |
| 198 | * @return the qualifier name node, or <code>null</code> if there is none |
| 199 | */ |
| 200 | public Name getQualifier() { |
| 201 | return this.optionalQualifier; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Sets or clears the qualifier of this method reference. |
| 206 | * |
| 207 | * @param name the qualifier name node, or <code>null</code> if |
| 208 | * there is none |
| 209 | * @exception IllegalArgumentException if: |
| 210 | * <ul> |
| 211 | * <li>the node belongs to a different AST</li> |
| 212 | * <li>the node already has a parent</li> |
| 213 | * </ul> |
| 214 | */ |
| 215 | public void setQualifier(Name name) { |
| 216 | ASTNode oldChild = this.optionalQualifier; |
| 217 | preReplaceChild(oldChild, name, QUALIFIER_PROPERTY); |
| 218 | this.optionalQualifier = name; |
| 219 | postReplaceChild(oldChild, name, QUALIFIER_PROPERTY); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Returns the name of the referenced method or constructor. |
| 224 | * |
| 225 | * @return the method or constructor name node |
| 226 | */ |
| 227 | public SimpleName getName() { |
| 228 | if (this.methodName == null) { |
| 229 | // lazy init must be thread-safe for readers |
| 230 | synchronized (this) { |
| 231 | if (this.methodName == null) { |
| 232 | preLazyInit(); |
| 233 | this.methodName = new SimpleName(this.ast); |
| 234 | postLazyInit(this.methodName, NAME_PROPERTY); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | return this.methodName; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Sets the name of the referenced method or constructor to the |
| 243 | * given name. |
| 244 | * |
| 245 | * @param name the new method or constructor name node |
| 246 | * @exception IllegalArgumentException if: |
| 247 | * <ul> |
| 248 | * <li>the name is <code>null</code></li> |
| 249 | * <li>the node belongs to a different AST</li> |
| 250 | * <li>the node already has a parent</li> |
| 251 | * </ul> |
| 252 | */ |
| 253 | public void setName(SimpleName name) { |
| 254 | if (name == null) { |
| 255 | throw new IllegalArgumentException(); |
| 256 | } |
| 257 | ASTNode oldChild = this.methodName; |
| 258 | preReplaceChild(oldChild, name, NAME_PROPERTY); |
| 259 | this.methodName = name; |
| 260 | postReplaceChild(oldChild, name, NAME_PROPERTY); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Returns the live ordered list of method parameter references for this |
| 265 | * method reference. |
| 266 | * |
| 267 | * @return the live list of method parameter references |
| 268 | * (element type: {@link MethodRefParameter}) |
| 269 | */ |
| 270 | public List parameters() { |
| 271 | return this.parameters; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Resolves and returns the binding for the entity referred to by |
| 276 | * this method reference. |
| 277 | * <p> |
| 278 | * Note that bindings are generally unavailable unless requested when the |
| 279 | * AST is being built. |
| 280 | * </p> |
| 281 | * |
| 282 | * @return the binding, or <code>null</code> if the binding cannot be |
| 283 | * resolved |
| 284 | */ |
| 285 | public final IBinding resolveBinding() { |
| 286 | return this.ast.getBindingResolver().resolveReference(this); |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | int memSize() { |
| 291 | return BASE_NODE_SIZE + 3 * 4; |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | int treeSize() { |
| 296 | return |
| 297 | memSize() |
| 298 | + (this.optionalQualifier == null ? 0 : getQualifier().treeSize()) |
| 299 | + (this.methodName == null ? 0 : getName().treeSize()) |
| 300 | + this.parameters.listSize(); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 |
Members