| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 2021 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 | * Contributors: |
| 11 | * IBM Corporation - initial API and implementation |
| 12 | * Stephan Herrmann - Contribution for |
| 13 | * Bug 429813 - [1.8][dom ast] IMethodBinding#getJavaElement() should return IMethod for lambda |
| 14 | *******************************************************************************/ |
| 15 | |
| 16 | package org.eclipse.jdt.core.dom; |
| 17 | |
| 18 | /** |
| 19 | * A method binding represents a method or constructor of a class or interface. |
| 20 | * Method bindings usually correspond directly to method or |
| 21 | * constructor declarations found in the source code. |
| 22 | * However, in certain cases of references to a generic method, |
| 23 | * the method binding may correspond to a copy of a generic method |
| 24 | * declaration with substitutions for the method's type parameters |
| 25 | * (for these, <code>getTypeArguments</code> returns a non-empty |
| 26 | * list, and either <code>isParameterizedMethod</code> or |
| 27 | * <code>isRawMethod</code> returns <code>true</code>). |
| 28 | * And in certain cases of references to a method declared in a |
| 29 | * generic type, the method binding may correspond to a copy of a |
| 30 | * method declaration with substitutions for the type's type |
| 31 | * parameters (for these, <code>getTypeArguments</code> returns |
| 32 | * an empty list, and both <code>isParameterizedMethod</code> and |
| 33 | * <code>isRawMethod</code> return <code>false</code>). |
| 34 | * |
| 35 | * @see ITypeBinding#getDeclaredMethods() |
| 36 | * @since 2.0 |
| 37 | * @noimplement This interface is not intended to be implemented by clients. |
| 38 | */ |
| 39 | public interface IMethodBinding extends IBinding { |
| 40 | |
| 41 | /** |
| 42 | * Returns whether this binding is for a constructor or a method. |
| 43 | * |
| 44 | * @return <code>true</code> if this is the binding for a constructor, |
| 45 | * and <code>false</code> if this is the binding for a method |
| 46 | */ |
| 47 | public boolean isConstructor(); |
| 48 | |
| 49 | /** |
| 50 | * Returns whether this binding is for a compact constructor or not. |
| 51 | * |
| 52 | * <p> |
| 53 | * This method returns <code>true</code> for: |
| 54 | * </p> |
| 55 | * <ul> |
| 56 | * <li>compact constructors where the binding |
| 57 | * information was obtained from a Java source file containing a compact constructor |
| 58 | * declaration</li> |
| 59 | * </ul> |
| 60 | * |
| 61 | * <p> |
| 62 | * This method returns <code>false</code> for: |
| 63 | * </p> |
| 64 | * <ul> |
| 65 | * <li>methods</li> |
| 66 | * <li>constructors</li> |
| 67 | * <li>constructors where the binding information was obtained from a Java class file (it |
| 68 | * is not possible to determine from a class file whether a constructor is a |
| 69 | * compact constructor or not</li> |
| 70 | * </ul> |
| 71 | * |
| 72 | * @return <code>true</code> if this is the binding for a compact constructor |
| 73 | * in a source file and and <code>false</code> otherwise |
| 74 | * @since 3.26 |
| 75 | */ |
| 76 | public boolean isCompactConstructor(); |
| 77 | |
| 78 | /** |
| 79 | * Returns whether this binding is for a canonical constructor or not. |
| 80 | * |
| 81 | * <p> |
| 82 | * This method returns <code>true</code> for canonical constructors |
| 83 | * </p> |
| 84 | * |
| 85 | * @return <code>true</code> if this is the binding for a canonical constructor |
| 86 | * and <code>false</code> otherwise |
| 87 | * @since 3.26 |
| 88 | */ |
| 89 | public boolean isCanonicalConstructor(); |
| 90 | |
| 91 | /** |
| 92 | * Returns whether this binding is known to be a compiler-generated |
| 93 | * default constructor. |
| 94 | * <p> |
| 95 | * This method returns <code>false</code> for: |
| 96 | * <ul> |
| 97 | * <li>methods</li> |
| 98 | * <li>constructors with more than one parameter</li> |
| 99 | * <li>0-argument constructors where the binding information was obtained |
| 100 | * from a Java source file containing an explicit 0-argument constructor |
| 101 | * declaration</li> |
| 102 | * <li>0-argument constructors where the binding information was obtained |
| 103 | * from a Java class file (it is not possible to determine from a |
| 104 | * class file whether a 0-argument constructor was present in the source |
| 105 | * code versus generated automatically by a Java compiler)</li> |
| 106 | * </ul> |
| 107 | * |
| 108 | * @return <code>true</code> if this is known to be the binding for a |
| 109 | * compiler-generated default constructor, and <code>false</code> |
| 110 | * otherwise |
| 111 | * @since 3.0 |
| 112 | */ |
| 113 | public boolean isDefaultConstructor(); |
| 114 | |
| 115 | /** |
| 116 | * Returns the name of the method declared in this binding. The method name |
| 117 | * is always a simple identifier. The name of a constructor is always the |
| 118 | * same as the declared name of its declaring class. |
| 119 | * |
| 120 | * @return the name of this method, or the declared name of this |
| 121 | * constructor's declaring class |
| 122 | */ |
| 123 | @Override |
| 124 | public String getName(); |
| 125 | |
| 126 | /** |
| 127 | * Returns the type binding representing the class or interface |
| 128 | * that declares this method or constructor. |
| 129 | * |
| 130 | * @return the binding of the class or interface that declares this method |
| 131 | * or constructor |
| 132 | */ |
| 133 | public ITypeBinding getDeclaringClass(); |
| 134 | |
| 135 | /** |
| 136 | * If this method binding represents a lambda expression then: |
| 137 | * <ul> |
| 138 | * <li>If the lambda expression is declared in the body of a method, |
| 139 | * answers the binding of that declaring method. |
| 140 | * </li> |
| 141 | * <li>Otherwise, if the lambda expression is declared in the |
| 142 | * initializer of a field, answers the binding of that declaring field. |
| 143 | * </li> |
| 144 | * <li>Otherwise, if the lambda expression is declared in a static initializer or an |
| 145 | * instance initializer, a method binding is returned to represent that initializer |
| 146 | * (selector is an empty string in this case). |
| 147 | * </li> |
| 148 | * </ul> |
| 149 | * <p> |
| 150 | * If this method binding does not represent a lambda expression, |
| 151 | * <code>null</code> is returned. |
| 152 | * </p> |
| 153 | * @return a method binding or field binding representing the member that |
| 154 | * contains the lambda expression represented by this method binding, |
| 155 | * or null for regular method bindings. |
| 156 | * @since 3.11 |
| 157 | */ |
| 158 | public IBinding getDeclaringMember(); |
| 159 | |
| 160 | /** |
| 161 | * Returns the resolved default value of an annotation type member, |
| 162 | * or <code>null</code> if the member has no default value, or if this |
| 163 | * is not the binding for an annotation type member. |
| 164 | * <p> |
| 165 | * Resolved values are represented as follows (same as for |
| 166 | * {@link IMemberValuePairBinding#getValue()}): |
| 167 | * <ul> |
| 168 | * <li>Primitive type - the equivalent boxed object</li> |
| 169 | * <li>java.lang.Class - the <code>ITypeBinding</code> for the class object</li> |
| 170 | * <li>java.lang.String - the string value itself</li> |
| 171 | * <li>enum type - the <code>IVariableBinding</code> for the enum constant</li> |
| 172 | * <li>annotation type - an <code>IAnnotationBinding</code></li> |
| 173 | * <li>array type - an <code>Object[]</code> whose elements are as per above |
| 174 | * (the language only allows single dimensional arrays in annotations)</li> |
| 175 | * </ul> |
| 176 | * |
| 177 | * @return the default value of this annotation type member, or <code>null</code> |
| 178 | * if none or not applicable |
| 179 | * @since 3.2 |
| 180 | */ |
| 181 | public Object getDefaultValue(); |
| 182 | |
| 183 | /** |
| 184 | * Returns the resolved declaration annotations of a parameter of this method. |
| 185 | * The result returned is the same regardless of whether |
| 186 | * this is a parameterized method. |
| 187 | * <p> |
| 188 | * <b>Note:</b> This method only returns declaration annotations. |
| 189 | * <em>Type annotations</em> in the sense of JLS8 9.7.4 are <em>not</em> returned. |
| 190 | * Type annotations can be retrieved from a parameter type |
| 191 | * via {@link ITypeBinding#getTypeAnnotations()}. |
| 192 | * </p> |
| 193 | * |
| 194 | * @param paramIndex the index of the parameter of interest |
| 195 | * @return the resolved declaration annotations of the <code>paramIndex</code>th parameter, |
| 196 | * or an empty list if there are none |
| 197 | * @throws ArrayIndexOutOfBoundsException if <code>paramIndex</code> is |
| 198 | * not a valid index |
| 199 | * @since 3.2 |
| 200 | */ |
| 201 | public IAnnotationBinding[] getParameterAnnotations(int paramIndex); |
| 202 | |
| 203 | /** |
| 204 | * Returns a list of type bindings representing the formal parameter types, |
| 205 | * in declaration order, of this method or constructor. Returns an array of |
| 206 | * length 0 if this method or constructor does not takes any parameters. |
| 207 | * <p> |
| 208 | * Note that the binding for the last parameter type of a vararg method |
| 209 | * declaration like <code>void fun(Foo... args)</code> is always for |
| 210 | * an array type (i.e., <code>Foo[]</code>) reflecting the the way varargs |
| 211 | * get compiled. However, the type binding obtained directly from |
| 212 | * the <code>SingleVariableDeclaration</code> for the vararg parameter |
| 213 | * is always for the type as written; i.e., the type binding for |
| 214 | * <code>Foo</code>. |
| 215 | * </p> |
| 216 | * <p> |
| 217 | * Note: The result does not include synthetic parameters introduced by |
| 218 | * inner class emulation. Explicit receiver parameters are also not included. |
| 219 | * </p> |
| 220 | * |
| 221 | * @return a (possibly empty) list of type bindings for the formal |
| 222 | * parameters of this method or constructor |
| 223 | */ |
| 224 | public ITypeBinding[] getParameterTypes(); |
| 225 | |
| 226 | /** |
| 227 | * Returns the type of this method's receiver or <code>null</code> |
| 228 | * if there is no receiver declared explicitly. |
| 229 | * |
| 230 | * @return the type of this method's receiver or <code>null</code> |
| 231 | * if there is no receiver declared explicitly. |
| 232 | * |
| 233 | * @since 3.10 |
| 234 | */ |
| 235 | public ITypeBinding getDeclaredReceiverType(); |
| 236 | |
| 237 | /** |
| 238 | * Returns the binding for the return type of this method. Returns the |
| 239 | * special primitive <code>void</code> return type for constructors. |
| 240 | * <p> |
| 241 | * For methods, the type binding that is returned contains type annotations |
| 242 | * if any. For e.g. the following code would get the type annotations on a |
| 243 | * method: <br><br> |
| 244 | * <code> IAnnotationBinding[] annots = getReturnType().getTypeAnnotations() </code> |
| 245 | * </p> |
| 246 | * For a constructor, the returned binding does not include type annotations. |
| 247 | * |
| 248 | * @return the binding for the return type of this method, or the |
| 249 | * <code>void</code> return type for constructors |
| 250 | */ |
| 251 | public ITypeBinding getReturnType(); |
| 252 | |
| 253 | /** |
| 254 | * Returns a list of type bindings representing the types of the exceptions thrown |
| 255 | * by this method or constructor. Returns an array of length 0 if this method |
| 256 | * throws no exceptions. The resulting types are in no particular order. |
| 257 | * |
| 258 | * @return a list of type bindings for exceptions |
| 259 | * thrown by this method or constructor |
| 260 | */ |
| 261 | public ITypeBinding[] getExceptionTypes(); |
| 262 | |
| 263 | /** |
| 264 | * Returns the type parameters of this method or constructor binding. |
| 265 | * <p> |
| 266 | * Note that type parameters only occur on the binding of the |
| 267 | * declaring generic method. Type bindings corresponding to a raw or |
| 268 | * parameterized reference to a generic method do not carry type |
| 269 | * parameters (they instead have non-empty type arguments |
| 270 | * and non-trivial erasure). |
| 271 | * </p> |
| 272 | * |
| 273 | * @return the list of binding for the type variables for the type |
| 274 | * parameters of this method, or otherwise the empty list |
| 275 | * @see ITypeBinding#isTypeVariable() |
| 276 | * @since 3.1 |
| 277 | */ |
| 278 | public ITypeBinding[] getTypeParameters(); |
| 279 | |
| 280 | /** |
| 281 | * Returns whether this is the binding for an annotation type member. |
| 282 | * |
| 283 | * @return <code>true</code> iff this is the binding for an annotation type member |
| 284 | * and <code>false</code> otherwise |
| 285 | * @since 3.2 |
| 286 | */ |
| 287 | public boolean isAnnotationMember(); |
| 288 | |
| 289 | /** |
| 290 | * Returns whether this method binding represents a declaration of |
| 291 | * a generic method. |
| 292 | * <p> |
| 293 | * Note that type parameters only occur on the binding of the |
| 294 | * declaring generic method; e.g., <code>public <T> T identity(T t);</code>. |
| 295 | * Method bindings corresponding to a raw or parameterized reference to a generic |
| 296 | * method do not carry type parameters (they instead have non-empty type arguments |
| 297 | * and non-trivial erasure). |
| 298 | * This method is fully equivalent to <code>getTypeParameters().length > 0)</code>. |
| 299 | * </p> |
| 300 | * <p> |
| 301 | * Note that {@link #isGenericMethod()}, |
| 302 | * {@link #isParameterizedMethod()}, |
| 303 | * and {@link #isRawMethod()} are mutually exclusive. |
| 304 | * </p> |
| 305 | * |
| 306 | * @return <code>true</code> if this method binding represents a |
| 307 | * declaration of a generic method, and <code>false</code> otherwise |
| 308 | * @see #getTypeParameters() |
| 309 | * @since 3.1 |
| 310 | */ |
| 311 | public boolean isGenericMethod(); |
| 312 | |
| 313 | /** |
| 314 | * Returns whether this method binding represents an instance of |
| 315 | * a generic method corresponding to a parameterized method reference. |
| 316 | * <p> |
| 317 | * Note that {@link #isGenericMethod()}, |
| 318 | * {@link #isParameterizedMethod()}, |
| 319 | * and {@link #isRawMethod()} are mutually exclusive. |
| 320 | * </p> |
| 321 | * |
| 322 | * @return <code>true</code> if this method binding represents a |
| 323 | * an instance of a generic method corresponding to a parameterized |
| 324 | * method reference, and <code>false</code> otherwise |
| 325 | * @see #getMethodDeclaration() |
| 326 | * @see #getTypeArguments() |
| 327 | * @since 3.1 |
| 328 | */ |
| 329 | public boolean isParameterizedMethod(); |
| 330 | |
| 331 | /** |
| 332 | * Returns the type arguments of this generic method instance, or the |
| 333 | * empty list for other method bindings. |
| 334 | * <p> |
| 335 | * Note that type arguments only occur on a method binding that represents |
| 336 | * an instance of a generic method corresponding to a raw or parameterized |
| 337 | * reference to a generic method. Do not confuse these with type parameters |
| 338 | * which only occur on the method binding corresponding directly to the |
| 339 | * declaration of a generic method. |
| 340 | * </p> |
| 341 | * |
| 342 | * @return the list of type bindings for the type arguments used to |
| 343 | * instantiate the corrresponding generic method, or otherwise the empty list |
| 344 | * @see #getMethodDeclaration() |
| 345 | * @see #isParameterizedMethod() |
| 346 | * @see #isRawMethod() |
| 347 | * @since 3.1 |
| 348 | */ |
| 349 | public ITypeBinding[] getTypeArguments(); |
| 350 | |
| 351 | /** |
| 352 | * Returns the binding for the method declaration corresponding to this |
| 353 | * method binding. |
| 354 | * <ul> |
| 355 | * <li>For parameterized methods ({@link #isParameterizedMethod()}) |
| 356 | * and raw methods ({@link #isRawMethod()}), this method returns the binding |
| 357 | * for the corresponding generic method.</li> |
| 358 | * <li>For references to the method {@link Object#getClass() Object.getClass()}, |
| 359 | * returns the binding for the method declaration which is declared to return |
| 360 | * <code>Class<?></code> or <code>Class<? extends Object></code>. In the |
| 361 | * reference binding, the return type becomes |
| 362 | * <code>Class<? extends </code><em>R</em><code>></code>, where <em>R</em> |
| 363 | * is the erasure of the static type of the receiver of the method invocation.</li> |
| 364 | * <li>For references to a signature polymorphic method from class MethodHandle, |
| 365 | * returns the declaration of the method. In the reference binding, the parameter types and |
| 366 | * the return type are determined by the concrete invocation context.</li> |
| 367 | * <li>For lambda methods, returns the (possibly parameterized) single abstract method |
| 368 | * of the functional type.</li> |
| 369 | * <li>For other method bindings, this returns the same binding.</li> |
| 370 | * </ul> |
| 371 | * |
| 372 | * @return the method binding |
| 373 | * @since 3.1 |
| 374 | */ |
| 375 | public IMethodBinding getMethodDeclaration(); |
| 376 | |
| 377 | /** |
| 378 | * Returns whether this method binding represents an instance of |
| 379 | * a generic method corresponding to a raw method reference. |
| 380 | * <p> |
| 381 | * Note that {@link #isGenericMethod()}, |
| 382 | * {@link #isParameterizedMethod()}, |
| 383 | * and {@link #isRawMethod()} are mutually exclusive. |
| 384 | * </p> |
| 385 | * |
| 386 | * @return <code>true</code> if this method binding represents a |
| 387 | * an instance of a generic method corresponding to a raw |
| 388 | * method reference, and <code>false</code> otherwise |
| 389 | * @see #getMethodDeclaration() |
| 390 | * @see #getTypeArguments() |
| 391 | * @since 3.1 |
| 392 | */ |
| 393 | public boolean isRawMethod(); |
| 394 | |
| 395 | /** |
| 396 | * Returns whether this method's signature is a subsignature of the given method as |
| 397 | * specified in section 8.4.2 of <em>The Java Language Specification, Third Edition</em> (JLS3). |
| 398 | * |
| 399 | * @return <code>true</code> if this method's signature is a subsignature of the given method |
| 400 | * @since 3.1 |
| 401 | */ |
| 402 | public boolean isSubsignature(IMethodBinding otherMethod); |
| 403 | |
| 404 | /** |
| 405 | * Returns whether this is a variable arity method. |
| 406 | * <p> |
| 407 | * Note: Variable arity ("varargs") methods were added in JLS3. |
| 408 | * </p> |
| 409 | * |
| 410 | * @return <code>true</code> if this is a variable arity method, |
| 411 | * and <code>false</code> otherwise |
| 412 | * @since 3.1 |
| 413 | */ |
| 414 | public boolean isVarargs(); |
| 415 | |
| 416 | /** |
| 417 | * Returns whether this method overrides the given method, |
| 418 | * as specified in section 8.4.8.1 of <em>The Java Language |
| 419 | * Specification, Third Edition</em> (JLS3). |
| 420 | * |
| 421 | * @param method the method that is possibly overriden |
| 422 | * @return <code>true</code> if this method overrides the given method, |
| 423 | * and <code>false</code> otherwise |
| 424 | * @since 3.1 |
| 425 | */ |
| 426 | public boolean overrides(IMethodBinding method); |
| 427 | |
| 428 | |
| 429 | /** |
| 430 | * Returns a list of variable bindings representing the synthetic outer |
| 431 | * local variables. Returns an empty array for non-lambda expressions or if |
| 432 | * this method does not have any synthetic parameters. |
| 433 | * |
| 434 | * @return a (possibly empty) list of variable bindings for the synthetic |
| 435 | * outer locals of this method if this is a lambda expression, else an empty array. |
| 436 | * @since 3.18 |
| 437 | */ |
| 438 | public IVariableBinding[] getSyntheticOuterLocals(); |
| 439 | |
| 440 | /** |
| 441 | * Returns if this is a compiler generated equals(), hashCode(), toString() or any accessor |
| 442 | * method of a Record or not. |
| 443 | * Methods equals(), hashCode() and toString() and accessor methods of a Record do not have |
| 444 | * AccSynthetic flag set for them even if they are compiler generated methods. To differentiate |
| 445 | * between these above compiler generated methods and user created methods equals(), hashCode() |
| 446 | * and toString() or accessor methods in a Record, this function can be used. |
| 447 | * |
| 448 | * @return <code>true</code> for compiler generated equals(), hashCode() and toString() or any |
| 449 | * accessor method of a Record, else it returns <code>false</code>. |
| 450 | * @since 3.26 |
| 451 | */ |
| 452 | public boolean isSyntheticRecordMethod(); |
| 453 | |
| 454 | } |
| 455 |
Members