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