| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 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 | * Simple or qualified "super" method invocation expression AST node type. |
| 22 | * <pre> |
| 23 | * SuperMethodInvocation: |
| 24 | * [ ClassName <b>.</b> ] <b>super</b> <b>.</b> |
| 25 | * [ <b><</b> Type { <b>,</b> Type } <b>></b> ] |
| 26 | * Identifier <b>(</b> [ Expression { <b>,</b> Expression } ] <b>)</b> |
| 27 | * </pre> |
| 28 | * |
| 29 | * @since 2.0 |
| 30 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 31 | */ |
| 32 | @SuppressWarnings({"rawtypes", "unchecked"}) |
| 33 | public class SuperMethodInvocation extends Expression { |
| 34 | |
| 35 | /** |
| 36 | * The "qualifier" structural property of this node type (child type: {@link Name}). |
| 37 | * @since 3.0 |
| 38 | */ |
| 39 | public static final ChildPropertyDescriptor QUALIFIER_PROPERTY = |
| 40 | new ChildPropertyDescriptor(SuperMethodInvocation.class, "qualifier", Name.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 41 | |
| 42 | /** |
| 43 | * The "typeArguments" structural property of this node type (element type: {@link Type}) (added in JLS3 API). |
| 44 | * @since 3.1 |
| 45 | */ |
| 46 | public static final ChildListPropertyDescriptor TYPE_ARGUMENTS_PROPERTY = |
| 47 | new ChildListPropertyDescriptor(SuperMethodInvocation.class, "typeArguments", Type.class, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 48 | |
| 49 | /** |
| 50 | * The "name" structural property of this node type (child type: {@link SimpleName}). |
| 51 | * @since 3.0 |
| 52 | */ |
| 53 | public static final ChildPropertyDescriptor NAME_PROPERTY = |
| 54 | new ChildPropertyDescriptor(SuperMethodInvocation.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 55 | |
| 56 | /** |
| 57 | * The "arguments" structural property of this node type (element type: {@link Expression}). |
| 58 | * @since 3.0 |
| 59 | */ |
| 60 | public static final ChildListPropertyDescriptor ARGUMENTS_PROPERTY = |
| 61 | new ChildListPropertyDescriptor(SuperMethodInvocation.class, "arguments", Expression.class, 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 propertyList = new ArrayList(4); |
| 81 | createPropertyList(SuperMethodInvocation.class, propertyList); |
| 82 | addProperty(QUALIFIER_PROPERTY, propertyList); |
| 83 | addProperty(NAME_PROPERTY, propertyList); |
| 84 | addProperty(ARGUMENTS_PROPERTY, propertyList); |
| 85 | PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(propertyList); |
| 86 | |
| 87 | propertyList = new ArrayList(5); |
| 88 | createPropertyList(SuperMethodInvocation.class, propertyList); |
| 89 | addProperty(QUALIFIER_PROPERTY, propertyList); |
| 90 | addProperty(TYPE_ARGUMENTS_PROPERTY, propertyList); |
| 91 | addProperty(NAME_PROPERTY, propertyList); |
| 92 | addProperty(ARGUMENTS_PROPERTY, propertyList); |
| 93 | PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns a list of structural property descriptors for this node type. |
| 98 | * Clients must not modify the result. |
| 99 | * |
| 100 | * @param apiLevel the API level; one of the |
| 101 | * <code>AST.JLS*</code> constants |
| 102 | |
| 103 | * @return a list of property descriptors (element type: |
| 104 | * {@link StructuralPropertyDescriptor}) |
| 105 | * @since 3.0 |
| 106 | */ |
| 107 | public static List propertyDescriptors(int apiLevel) { |
| 108 | if (apiLevel == AST.JLS2_INTERNAL) { |
| 109 | return PROPERTY_DESCRIPTORS_2_0; |
| 110 | } else { |
| 111 | return PROPERTY_DESCRIPTORS_3_0; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * The optional qualifier; <code>null</code> for none; defaults to none. |
| 117 | */ |
| 118 | private Name optionalQualifier = null; |
| 119 | |
| 120 | /** |
| 121 | * The type arguments (element type: {@link Type}). |
| 122 | * Null in JLS2. Added in JLS3; defaults to an empty list |
| 123 | * (see constructor). |
| 124 | * @since 3.1 |
| 125 | */ |
| 126 | private ASTNode.NodeList typeArguments = null; |
| 127 | |
| 128 | /** |
| 129 | * The method name; lazily initialized; defaults to a unspecified, |
| 130 | * legal Java method name. |
| 131 | */ |
| 132 | private SimpleName methodName = null; |
| 133 | |
| 134 | /** |
| 135 | * The list of argument expressions (element type: |
| 136 | * {@link Expression}). Defaults to an empty list. |
| 137 | */ |
| 138 | private ASTNode.NodeList arguments = |
| 139 | new ASTNode.NodeList(ARGUMENTS_PROPERTY); |
| 140 | |
| 141 | /** |
| 142 | * Creates a new AST node for a "super" method invocation expression owned |
| 143 | * by the given AST. By default, no qualifier, no type arguments, |
| 144 | * an unspecified, but legal, method name, and an empty list of arguments. |
| 145 | * |
| 146 | * @param ast the AST that is to own this node |
| 147 | */ |
| 148 | SuperMethodInvocation(AST ast) { |
| 149 | super(ast); |
| 150 | if (ast.apiLevel >= AST.JLS3_INTERNAL) { |
| 151 | this.typeArguments = new ASTNode.NodeList(TYPE_ARGUMENTS_PROPERTY); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 157 | return propertyDescriptors(apiLevel); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 162 | if (property == QUALIFIER_PROPERTY) { |
| 163 | if (get) { |
| 164 | return getQualifier(); |
| 165 | } else { |
| 166 | setQualifier((Name) child); |
| 167 | return null; |
| 168 | } |
| 169 | } |
| 170 | if (property == NAME_PROPERTY) { |
| 171 | if (get) { |
| 172 | return getName(); |
| 173 | } else { |
| 174 | setName((SimpleName) child); |
| 175 | return null; |
| 176 | } |
| 177 | } |
| 178 | // allow default implementation to flag the error |
| 179 | return super.internalGetSetChildProperty(property, get, child); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
| 184 | if (property == ARGUMENTS_PROPERTY) { |
| 185 | return arguments(); |
| 186 | } |
| 187 | if (property == TYPE_ARGUMENTS_PROPERTY) { |
| 188 | return typeArguments(); |
| 189 | } |
| 190 | // allow default implementation to flag the error |
| 191 | return super.internalGetChildListProperty(property); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | final int getNodeType0() { |
| 196 | return SUPER_METHOD_INVOCATION; |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | ASTNode clone0(AST target) { |
| 201 | SuperMethodInvocation result = new SuperMethodInvocation(target); |
| 202 | result.setSourceRange(getStartPosition(), getLength()); |
| 203 | result.setName((SimpleName) getName().clone(target)); |
| 204 | result.setQualifier((Name) ASTNode.copySubtree(target, getQualifier())); |
| 205 | if (this.ast.apiLevel >= AST.JLS3_INTERNAL) { |
| 206 | result.typeArguments().addAll(ASTNode.copySubtrees(target, typeArguments())); |
| 207 | } |
| 208 | result.arguments().addAll(ASTNode.copySubtrees(target, arguments())); |
| 209 | return result; |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 214 | // dispatch to correct overloaded match method |
| 215 | return matcher.match(this, other); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | void accept0(ASTVisitor visitor) { |
| 220 | boolean visitChildren = visitor.visit(this); |
| 221 | if (visitChildren) { |
| 222 | // visit children in normal left to right reading order |
| 223 | acceptChild(visitor, getQualifier()); |
| 224 | if (this.ast.apiLevel >= AST.JLS3_INTERNAL) { |
| 225 | acceptChildren(visitor, this.typeArguments); |
| 226 | } |
| 227 | acceptChild(visitor, getName()); |
| 228 | acceptChildren(visitor, this.arguments); |
| 229 | } |
| 230 | visitor.endVisit(this); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Returns the qualifier of this "super" method invocation expression, or |
| 235 | * <code>null</code> if there is none. |
| 236 | * |
| 237 | * @return the qualifier name node, or <code>null</code> if there is none |
| 238 | */ |
| 239 | public Name getQualifier() { |
| 240 | return this.optionalQualifier; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Returns true if the resolved return type has been inferred from the assignment context (JLS3 15.12.2.8), false otherwise. |
| 245 | * <p> |
| 246 | * This information is available only when bindings are requested when the AST is being built |
| 247 | * </p>. |
| 248 | * |
| 249 | * @return true if the resolved return type has been inferred from the assignment context (JLS3 15.12.2.8), false otherwise |
| 250 | * @since 3.3 |
| 251 | */ |
| 252 | public boolean isResolvedTypeInferredFromExpectedType() { |
| 253 | return this.ast.getBindingResolver().isResolvedTypeInferredFromExpectedType(this); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | /** |
| 258 | * Sets or clears the qualifier of this "super" method invocation expression. |
| 259 | * |
| 260 | * @param name the qualifier name node, or <code>null</code> if |
| 261 | * there is none |
| 262 | * @exception IllegalArgumentException if: |
| 263 | * <ul> |
| 264 | * <li>the node belongs to a different AST</li> |
| 265 | * <li>the node already has a parent</li> |
| 266 | * </ul> |
| 267 | */ |
| 268 | public void setQualifier(Name name) { |
| 269 | ASTNode oldChild = this.optionalQualifier; |
| 270 | preReplaceChild(oldChild, name, QUALIFIER_PROPERTY); |
| 271 | this.optionalQualifier = name; |
| 272 | postReplaceChild(oldChild, name, QUALIFIER_PROPERTY); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Returns the live ordered list of type arguments of this method |
| 277 | * invocation (added in JLS3 API). |
| 278 | * |
| 279 | * @return the live list of type arguments |
| 280 | * (element type: {@link Type}) |
| 281 | * @exception UnsupportedOperationException if this operation is used in |
| 282 | * a JLS2 AST |
| 283 | * @since 3.1 |
| 284 | */ |
| 285 | public List typeArguments() { |
| 286 | // more efficient than just calling unsupportedIn2() to check |
| 287 | if (this.typeArguments == null) { |
| 288 | unsupportedIn2(); |
| 289 | } |
| 290 | return this.typeArguments; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Returns the name of the method invoked in this expression. |
| 295 | * |
| 296 | * @return the method name node |
| 297 | */ |
| 298 | public SimpleName getName() { |
| 299 | if (this.methodName == null) { |
| 300 | // lazy init must be thread-safe for readers |
| 301 | synchronized (this) { |
| 302 | if (this.methodName == null) { |
| 303 | preLazyInit(); |
| 304 | this.methodName = new SimpleName(this.ast); |
| 305 | postLazyInit(this.methodName, NAME_PROPERTY); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | return this.methodName; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Sets the name of the method invoked in this expression to the |
| 314 | * given name. |
| 315 | * |
| 316 | * @param name the new method name |
| 317 | * @exception IllegalArgumentException if: |
| 318 | * <ul> |
| 319 | * <li>the node belongs to a different AST</li> |
| 320 | * <li>the node already has a parent</li> |
| 321 | * </ul> |
| 322 | */ |
| 323 | public void setName(SimpleName name) { |
| 324 | if (name == null) { |
| 325 | throw new IllegalArgumentException(); |
| 326 | } |
| 327 | ASTNode oldChild = this.methodName; |
| 328 | preReplaceChild(oldChild, name, NAME_PROPERTY); |
| 329 | this.methodName = name; |
| 330 | postReplaceChild(oldChild, name, NAME_PROPERTY); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Returns the live ordered list of argument expressions in this |
| 335 | * "super" method invocation expression. |
| 336 | * |
| 337 | * @return the live list of argument expressions |
| 338 | * (element type: {@link Expression}) |
| 339 | */ |
| 340 | public List arguments() { |
| 341 | return this.arguments; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Resolves and returns the binding for the method invoked by this |
| 346 | * expression. |
| 347 | * <p> |
| 348 | * Note that bindings are generally unavailable unless requested when the |
| 349 | * AST is being built. |
| 350 | * </p> |
| 351 | * |
| 352 | * @return the method binding, or <code>null</code> if the binding cannot |
| 353 | * be resolved |
| 354 | * @since 2.1 |
| 355 | */ |
| 356 | public IMethodBinding resolveMethodBinding() { |
| 357 | return this.ast.getBindingResolver().resolveMethod(this); |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | int memSize() { |
| 362 | // treat Code as free |
| 363 | return BASE_NODE_SIZE + 4 * 4; |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | int treeSize() { |
| 368 | return |
| 369 | memSize() |
| 370 | + (this.optionalQualifier == null ? 0 : getQualifier().treeSize()) |
| 371 | + (this.typeArguments == null ? 0 : this.typeArguments.listSize()) |
| 372 | + (this.methodName == null ? 0 : getName().treeSize()) |
| 373 | + (this.arguments == null ? 0 : this.arguments.listSize()); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 |
Members