1 /* 2 * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang.reflect; 27 28 import jdk.internal.access.SharedSecrets; 29 import jdk.internal.value.PrimitiveClass; 30 import jdk.internal.misc.VM; 31 import jdk.internal.reflect.CallerSensitive; 32 import jdk.internal.reflect.CallerSensitiveAdapter; 33 import jdk.internal.reflect.MethodAccessor; 34 import jdk.internal.reflect.Reflection; 35 import jdk.internal.vm.annotation.ForceInline; 36 import jdk.internal.vm.annotation.IntrinsicCandidate; 37 import jdk.internal.vm.annotation.Stable; 38 import sun.reflect.annotation.ExceptionProxy; 39 import sun.reflect.annotation.TypeNotPresentExceptionProxy; 40 import sun.reflect.generics.repository.GenericDeclRepository; 41 import sun.reflect.generics.repository.MethodRepository; 42 import sun.reflect.generics.factory.CoreReflectionFactory; 43 import sun.reflect.generics.factory.GenericsFactory; 44 import sun.reflect.generics.scope.MethodScope; 45 import sun.reflect.annotation.AnnotationType; 46 import sun.reflect.annotation.AnnotationParser; 47 import java.lang.annotation.Annotation; 48 import java.lang.annotation.AnnotationFormatError; 49 import java.nio.ByteBuffer; 50 import java.util.StringJoiner; 51 52 /** 53 * A {@code Method} provides information about, and access to, a single method 54 * on a class or interface. The reflected method may be a class method 55 * or an instance method (including an abstract method). 56 * 57 * <p>A {@code Method} permits widening conversions to occur when matching the 58 * actual parameters to invoke with the underlying method's formal 59 * parameters, but it throws an {@code IllegalArgumentException} if a 60 * narrowing conversion would occur. 61 * 62 * @see Member 63 * @see java.lang.Class 64 * @see java.lang.Class#getMethods() 65 * @see java.lang.Class#getMethod(String, Class[]) 66 * @see java.lang.Class#getDeclaredMethods() 67 * @see java.lang.Class#getDeclaredMethod(String, Class[]) 68 * 69 * @author Kenneth Russell 70 * @author Nakul Saraiya 71 * @since 1.1 72 */ 73 public final class Method extends Executable { 74 private final Class<?> clazz; 75 private final int slot; 76 // This is guaranteed to be interned by the VM in the 1.4 77 // reflection implementation 78 private final String name; 79 private final Class<?> returnType; 80 private final Class<?>[] parameterTypes; 81 private final Class<?>[] exceptionTypes; 82 private final int modifiers; 83 // Generics and annotations support 84 private final transient String signature; 85 // generic info repository; lazily initialized 86 private transient volatile MethodRepository genericInfo; 87 private final byte[] annotations; 88 private final byte[] parameterAnnotations; 89 private final byte[] annotationDefault; 90 @Stable 91 private MethodAccessor methodAccessor; 92 // For sharing of MethodAccessors. This branching structure is 93 // currently only two levels deep (i.e., one root Method and 94 // potentially many Method objects pointing to it.) 95 // 96 // If this branching structure would ever contain cycles, deadlocks can 97 // occur in annotation code. 98 private Method root; 99 100 // Generics infrastructure 101 private String getGenericSignature() {return signature;} 102 103 // Accessor for factory 104 private GenericsFactory getFactory() { 105 // create scope and factory 106 return CoreReflectionFactory.make(this, MethodScope.make(this)); 107 } 108 109 // Accessor for generic info repository 110 @Override 111 MethodRepository getGenericInfo() { 112 var genericInfo = this.genericInfo; 113 // lazily initialize repository if necessary 114 if (genericInfo == null) { 115 // create and cache generic info repository 116 genericInfo = MethodRepository.make(getGenericSignature(), 117 getFactory()); 118 this.genericInfo = genericInfo; 119 } 120 return genericInfo; //return cached repository 121 } 122 123 /** 124 * Package-private constructor 125 */ 126 Method(Class<?> declaringClass, 127 String name, 128 Class<?>[] parameterTypes, 129 Class<?> returnType, 130 Class<?>[] checkedExceptions, 131 int modifiers, 132 int slot, 133 String signature, 134 byte[] annotations, 135 byte[] parameterAnnotations, 136 byte[] annotationDefault) { 137 assert PrimitiveClass.isPrimaryType(declaringClass); 138 this.clazz = declaringClass; 139 this.name = name; 140 this.parameterTypes = parameterTypes; 141 this.returnType = returnType; 142 this.exceptionTypes = checkedExceptions; 143 this.modifiers = modifiers; 144 this.slot = slot; 145 this.signature = signature; 146 this.annotations = annotations; 147 this.parameterAnnotations = parameterAnnotations; 148 this.annotationDefault = annotationDefault; 149 } 150 151 /** 152 * Package-private routine (exposed to java.lang.Class via 153 * ReflectAccess) which returns a copy of this Method. The copy's 154 * "root" field points to this Method. 155 */ 156 Method copy() { 157 // This routine enables sharing of MethodAccessor objects 158 // among Method objects which refer to the same underlying 159 // method in the VM. (All of this contortion is only necessary 160 // because of the "accessibility" bit in AccessibleObject, 161 // which implicitly requires that new java.lang.reflect 162 // objects be fabricated for each reflective call on Class 163 // objects.) 164 if (this.root != null) 165 throw new IllegalArgumentException("Can not copy a non-root Method"); 166 167 Method res = new Method(clazz, name, parameterTypes, returnType, 168 exceptionTypes, modifiers, slot, signature, 169 annotations, parameterAnnotations, annotationDefault); 170 res.root = this; 171 // Might as well eagerly propagate this if already present 172 res.methodAccessor = methodAccessor; 173 return res; 174 } 175 176 /** 177 * Make a copy of a leaf method. 178 */ 179 Method leafCopy() { 180 if (this.root == null) 181 throw new IllegalArgumentException("Can only leafCopy a non-root Method"); 182 183 Method res = new Method(clazz, name, parameterTypes, returnType, 184 exceptionTypes, modifiers, slot, signature, 185 annotations, parameterAnnotations, annotationDefault); 186 res.root = root; 187 res.methodAccessor = methodAccessor; 188 return res; 189 } 190 191 /** 192 * @throws InaccessibleObjectException {@inheritDoc} 193 * @throws SecurityException {@inheritDoc} 194 */ 195 @Override 196 @CallerSensitive 197 public void setAccessible(boolean flag) { 198 AccessibleObject.checkPermission(); 199 if (flag) checkCanSetAccessible(Reflection.getCallerClass()); 200 setAccessible0(flag); 201 } 202 203 @Override 204 void checkCanSetAccessible(Class<?> caller) { 205 checkCanSetAccessible(caller, clazz); 206 } 207 208 @Override 209 Method getRoot() { 210 return root; 211 } 212 213 @Override 214 boolean hasGenericInformation() { 215 return (getGenericSignature() != null); 216 } 217 218 @Override 219 byte[] getAnnotationBytes() { 220 return annotations; 221 } 222 223 /** 224 * Returns the {@code Class} object representing the class or interface 225 * that declares the method represented by this object. 226 */ 227 @Override 228 public Class<?> getDeclaringClass() { 229 return clazz; 230 } 231 232 /** 233 * Returns the name of the method represented by this {@code Method} 234 * object, as a {@code String}. 235 */ 236 @Override 237 public String getName() { 238 return name; 239 } 240 241 /** 242 * {@inheritDoc} 243 * @jls 8.4.3 Method Modifiers 244 */ 245 @Override 246 public int getModifiers() { 247 return modifiers; 248 } 249 250 /** 251 * {@inheritDoc} 252 * @throws GenericSignatureFormatError {@inheritDoc} 253 * @since 1.5 254 * @jls 8.4.4 Generic Methods 255 */ 256 @Override 257 @SuppressWarnings({"rawtypes", "unchecked"}) 258 public TypeVariable<Method>[] getTypeParameters() { 259 if (getGenericSignature() != null) 260 return (TypeVariable<Method>[])getGenericInfo().getTypeParameters(); 261 else 262 return (TypeVariable<Method>[])GenericDeclRepository.EMPTY_TYPE_VARS; 263 } 264 265 /** 266 * Returns a {@code Class} object that represents the formal return type 267 * of the method represented by this {@code Method} object. 268 * 269 * @return the return type for the method this object represents 270 */ 271 public Class<?> getReturnType() { 272 return returnType; 273 } 274 275 /** 276 * Returns a {@code Type} object that represents the formal return 277 * type of the method represented by this {@code Method} object. 278 * 279 * <p>If the return type is a parameterized type, 280 * the {@code Type} object returned must accurately reflect 281 * the actual type arguments used in the source code. 282 * 283 * <p>If the return type is a type variable or a parameterized type, it 284 * is created. Otherwise, it is resolved. 285 * 286 * @return a {@code Type} object that represents the formal return 287 * type of the underlying method 288 * @throws GenericSignatureFormatError 289 * if the generic method signature does not conform to the format 290 * specified in 291 * <cite>The Java Virtual Machine Specification</cite> 292 * @throws TypeNotPresentException if the underlying method's 293 * return type refers to a non-existent class or interface declaration 294 * @throws MalformedParameterizedTypeException if the 295 * underlying method's return type refers to a parameterized 296 * type that cannot be instantiated for any reason 297 * @since 1.5 298 */ 299 public Type getGenericReturnType() { 300 if (getGenericSignature() != null) { 301 return getGenericInfo().getReturnType(); 302 } else { return getReturnType();} 303 } 304 305 @Override 306 Class<?>[] getSharedParameterTypes() { 307 return parameterTypes; 308 } 309 310 @Override 311 Class<?>[] getSharedExceptionTypes() { 312 return exceptionTypes; 313 } 314 315 /** 316 * {@inheritDoc} 317 */ 318 @Override 319 public Class<?>[] getParameterTypes() { 320 return parameterTypes.clone(); 321 } 322 323 /** 324 * {@inheritDoc} 325 * @since 1.8 326 */ 327 public int getParameterCount() { return parameterTypes.length; } 328 329 330 /** 331 * {@inheritDoc} 332 * @throws GenericSignatureFormatError {@inheritDoc} 333 * @throws TypeNotPresentException {@inheritDoc} 334 * @throws MalformedParameterizedTypeException {@inheritDoc} 335 * @since 1.5 336 */ 337 @Override 338 public Type[] getGenericParameterTypes() { 339 return super.getGenericParameterTypes(); 340 } 341 342 /** 343 * {@inheritDoc} 344 */ 345 @Override 346 public Class<?>[] getExceptionTypes() { 347 return exceptionTypes.clone(); 348 } 349 350 /** 351 * {@inheritDoc} 352 * @throws GenericSignatureFormatError {@inheritDoc} 353 * @throws TypeNotPresentException {@inheritDoc} 354 * @throws MalformedParameterizedTypeException {@inheritDoc} 355 * @since 1.5 356 */ 357 @Override 358 public Type[] getGenericExceptionTypes() { 359 return super.getGenericExceptionTypes(); 360 } 361 362 /** 363 * Compares this {@code Method} against the specified object. Returns 364 * true if the objects are the same. Two {@code Methods} are the same if 365 * they were declared by the same class and have the same name 366 * and formal parameter types and return type. 367 */ 368 public boolean equals(Object obj) { 369 if (obj instanceof Method other) { 370 if ((getDeclaringClass() == other.getDeclaringClass()) 371 && (getName() == other.getName())) { 372 if (!returnType.equals(other.getReturnType())) 373 return false; 374 return equalParamTypes(parameterTypes, other.parameterTypes); 375 } 376 } 377 return false; 378 } 379 380 /** 381 * Returns a hashcode for this {@code Method}. The hashcode is computed 382 * as the exclusive-or of the hashcodes for the underlying 383 * method's declaring class name and the method's name. 384 */ 385 public int hashCode() { 386 return getDeclaringClass().getName().hashCode() ^ getName().hashCode(); 387 } 388 389 /** 390 * Returns a string describing this {@code Method}. The string is 391 * formatted as the method access modifiers, if any, followed by 392 * the method return type, followed by a space, followed by the 393 * class declaring the method, followed by a period, followed by 394 * the method name, followed by a parenthesized, comma-separated 395 * list of the method's formal parameter types. If the method 396 * throws checked exceptions, the parameter list is followed by a 397 * space, followed by the word "{@code throws}" followed by a 398 * comma-separated list of the thrown exception types. 399 * For example: 400 * <pre> 401 * public boolean java.lang.Object.equals(java.lang.Object) 402 * </pre> 403 * 404 * <p>The access modifiers are placed in canonical order as 405 * specified by "The Java Language Specification". This is 406 * {@code public}, {@code protected} or {@code private} first, 407 * and then other modifiers in the following order: 408 * {@code abstract}, {@code default}, {@code static}, {@code final}, 409 * {@code synchronized}, {@code native}, {@code strictfp}. 410 * 411 * @return a string describing this {@code Method} 412 * 413 * @jls 8.4.3 Method Modifiers 414 * @jls 9.4 Method Declarations 415 * @jls 9.6.1 Annotation Interface Elements 416 */ 417 public String toString() { 418 return sharedToString(Modifier.methodModifiers(), 419 isDefault(), 420 parameterTypes, 421 exceptionTypes); 422 } 423 424 @Override 425 void specificToStringHeader(StringBuilder sb) { 426 sb.append(getReturnType().getTypeName()).append(' '); 427 sb.append(getDeclaringClassTypeName()).append('.'); 428 sb.append(getName()); 429 } 430 431 @Override 432 String toShortString() { 433 return "method " + getDeclaringClassTypeName() + 434 '.' + toShortSignature(); 435 } 436 437 String toShortSignature() { 438 StringJoiner sj = new StringJoiner(",", getName() + "(", ")"); 439 for (Class<?> parameterType : getSharedParameterTypes()) { 440 sj.add(parameterType.getTypeName()); 441 } 442 return sj.toString(); 443 } 444 445 /** 446 * Returns a string describing this {@code Method}, including type 447 * parameters. The string is formatted as the method access 448 * modifiers, if any, followed by an angle-bracketed 449 * comma-separated list of the method's type parameters, if any, 450 * including informative bounds of the type parameters, if any, 451 * followed by the method's generic return type, followed by a 452 * space, followed by the class declaring the method, followed by 453 * a period, followed by the method name, followed by a 454 * parenthesized, comma-separated list of the method's generic 455 * formal parameter types. 456 * 457 * If this method was declared to take a variable number of 458 * arguments, instead of denoting the last parameter as 459 * "<code><i>Type</i>[]</code>", it is denoted as 460 * "<code><i>Type</i>...</code>". 461 * 462 * A space is used to separate access modifiers from one another 463 * and from the type parameters or return type. If there are no 464 * type parameters, the type parameter list is elided; if the type 465 * parameter list is present, a space separates the list from the 466 * class name. If the method is declared to throw exceptions, the 467 * parameter list is followed by a space, followed by the word 468 * "{@code throws}" followed by a comma-separated list of the generic 469 * thrown exception types. 470 * 471 * <p>The access modifiers are placed in canonical order as 472 * specified by "The Java Language Specification". This is 473 * {@code public}, {@code protected} or {@code private} first, 474 * and then other modifiers in the following order: 475 * {@code abstract}, {@code default}, {@code static}, {@code final}, 476 * {@code synchronized}, {@code native}, {@code strictfp}. 477 * 478 * @return a string describing this {@code Method}, 479 * include type parameters 480 * 481 * @since 1.5 482 * 483 * @jls 8.4.3 Method Modifiers 484 * @jls 9.4 Method Declarations 485 * @jls 9.6.1 Annotation Interface Elements 486 */ 487 @Override 488 public String toGenericString() { 489 return sharedToGenericString(Modifier.methodModifiers(), isDefault()); 490 } 491 492 @Override 493 void specificToGenericStringHeader(StringBuilder sb) { 494 Type genRetType = getGenericReturnType(); 495 sb.append(genRetType.getTypeName()).append(' '); 496 sb.append(getDeclaringClassTypeName()).append('.'); 497 sb.append(getName()); 498 } 499 500 /** 501 * Invokes the underlying method represented by this {@code Method} 502 * object, on the specified object with the specified parameters. 503 * Individual parameters are automatically unwrapped to match 504 * primitive formal parameters, and both primitive and reference 505 * parameters are subject to method invocation conversions as 506 * necessary. 507 * 508 * <p>If the underlying method is static, then the specified {@code obj} 509 * argument is ignored. It may be null. 510 * 511 * <p>If the number of formal parameters required by the underlying method is 512 * 0, the supplied {@code args} array may be of length 0 or null. 513 * 514 * <p>If the underlying method is an instance method, it is invoked 515 * using dynamic method lookup as documented in The Java Language 516 * Specification, section {@jls 15.12.4.4}; in particular, 517 * overriding based on the runtime type of the target object may occur. 518 * 519 * <p>If the underlying method is static, the class that declared 520 * the method is initialized if it has not already been initialized. 521 * 522 * <p>If the method completes normally, the value it returns is 523 * returned to the caller of invoke; if the value has a primitive 524 * type, it is first appropriately wrapped in an object. However, 525 * if the value has the type of an array of a primitive type, the 526 * elements of the array are <i>not</i> wrapped in objects; in 527 * other words, an array of primitive type is returned. If the 528 * underlying method return type is void, the invocation returns 529 * null. 530 * 531 * @param obj the object the underlying method is invoked from 532 * @param args the arguments used for the method call 533 * @return the result of dispatching the method represented by 534 * this object on {@code obj} with parameters 535 * {@code args} 536 * 537 * @throws IllegalAccessException if this {@code Method} object 538 * is enforcing Java language access control and the underlying 539 * method is inaccessible. 540 * @throws IllegalArgumentException if the method is an 541 * instance method and the specified object argument 542 * is not an instance of the class or interface 543 * declaring the underlying method (or of a subclass 544 * or implementor thereof); if the number of actual 545 * and formal parameters differ; if an unwrapping 546 * conversion for primitive arguments fails; or if, 547 * after possible unwrapping, a parameter value 548 * cannot be converted to the corresponding formal 549 * parameter type by a method invocation conversion. 550 * @throws InvocationTargetException if the underlying method 551 * throws an exception. 552 * @throws NullPointerException if the specified object is null 553 * and the method is an instance method. 554 * @throws ExceptionInInitializerError if the initialization 555 * provoked by this method fails. 556 */ 557 @CallerSensitive 558 @ForceInline // to ensure Reflection.getCallerClass optimization 559 @IntrinsicCandidate 560 public Object invoke(Object obj, Object... args) 561 throws IllegalAccessException, InvocationTargetException 562 { 563 boolean callerSensitive = isCallerSensitive(); 564 Class<?> caller = null; 565 if (!override || callerSensitive) { 566 caller = Reflection.getCallerClass(); 567 } 568 569 // Reflection::getCallerClass filters all subclasses of 570 // jdk.internal.reflect.MethodAccessorImpl and Method::invoke(Object, Object[]) 571 // Should not call Method::invoke(Object, Object[], Class) here 572 if (!override) { 573 checkAccess(caller, clazz, 574 Modifier.isStatic(modifiers) ? null : obj.getClass(), 575 modifiers); 576 } 577 MethodAccessor ma = methodAccessor; // read @Stable 578 if (ma == null) { 579 ma = acquireMethodAccessor(); 580 } 581 582 return callerSensitive ? ma.invoke(obj, args, caller) : ma.invoke(obj, args); 583 } 584 585 /** 586 * This is to support MethodHandle calling caller-sensitive Method::invoke 587 * that may invoke a caller-sensitive method in order to get the original caller 588 * class (not the injected invoker). 589 * 590 * If this adapter is not presented, MethodHandle invoking Method::invoke 591 * will get an invoker class, a hidden nestmate of the original caller class, 592 * that becomes the caller class invoking Method::invoke. 593 */ 594 @CallerSensitiveAdapter 595 private Object invoke(Object obj, Object[] args, Class<?> caller) 596 throws IllegalAccessException, InvocationTargetException 597 { 598 boolean callerSensitive = isCallerSensitive(); 599 if (!override) { 600 checkAccess(caller, clazz, 601 Modifier.isStatic(modifiers) ? null : obj.getClass(), 602 modifiers); 603 } 604 MethodAccessor ma = methodAccessor; // read @Stable 605 if (ma == null) { 606 ma = acquireMethodAccessor(); 607 } 608 609 return callerSensitive ? ma.invoke(obj, args, caller) : ma.invoke(obj, args); 610 } 611 612 // 0 = not initialized (@Stable contract) 613 // 1 = initialized, CS 614 // -1 = initialized, not CS 615 @Stable private byte callerSensitive; 616 617 private boolean isCallerSensitive() { 618 byte cs = callerSensitive; 619 if (cs == 0) { 620 callerSensitive = cs = (byte)(Reflection.isCallerSensitive(this) ? 1 : -1); 621 } 622 return (cs > 0); 623 } 624 625 /** 626 * {@return {@code true} if this method is a bridge 627 * method; returns {@code false} otherwise} 628 * 629 * @apiNote 630 * A bridge method is a {@linkplain isSynthetic synthetic} method 631 * created by a Java compiler alongside a method originating from 632 * the source code. Bridge methods are used by Java compilers in 633 * various circumstances to span differences in Java programming 634 * language semantics and JVM semantics. 635 * 636 * <p>One example use of bridge methods is as a technique for a 637 * Java compiler to support <i>covariant overrides</i>, where a 638 * subclass overrides a method and gives the new method a more 639 * specific return type than the method in the superclass. While 640 * the Java language specification forbids a class declaring two 641 * methods with the same parameter types but a different return 642 * type, the virtual machine does not. A common case where 643 * covariant overrides are used is for a {@link 644 * java.lang.Cloneable Cloneable} class where the {@link 645 * Object#clone() clone} method inherited from {@code 646 * java.lang.Object} is overridden and declared to return the type 647 * of the class. For example, {@code Object} declares 648 * <pre>{@code protected Object clone() throws CloneNotSupportedException {...}}</pre> 649 * and {@code EnumSet<E>} declares its language-level {@linkplain 650 * java.util.EnumSet#clone() covariant override} 651 * <pre>{@code public EnumSet<E> clone() {...}}</pre> 652 * If this technique was being used, the resulting class file for 653 * {@code EnumSet} would have two {@code clone} methods, one 654 * returning {@code EnumSet<E>} and the second a bridge method 655 * returning {@code Object}. The bridge method is a JVM-level 656 * override of {@code Object.clone()}. The body of the {@code 657 * clone} bridge method calls its non-bridge counterpart and 658 * returns its result. 659 * @since 1.5 660 * 661 * @jls 8.4.8.3 Requirements in Overriding and Hiding 662 * @jls 15.12.4.5 Create Frame, Synchronize, Transfer Control 663 * @jvms 4.6 Methods 664 * @see <a 665 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java 666 * programming language and JVM modeling in core reflection</a> 667 */ 668 public boolean isBridge() { 669 return (getModifiers() & Modifier.BRIDGE) != 0; 670 } 671 672 /** 673 * {@inheritDoc} 674 * @since 1.5 675 * @jls 8.4.1 Formal Parameters 676 */ 677 @Override 678 public boolean isVarArgs() { 679 return super.isVarArgs(); 680 } 681 682 /** 683 * {@inheritDoc} 684 * @jls 13.1 The Form of a Binary 685 * @jvms 4.6 Methods 686 * @see <a 687 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java 688 * programming language and JVM modeling in core reflection</a> 689 * @since 1.5 690 */ 691 @Override 692 public boolean isSynthetic() { 693 return super.isSynthetic(); 694 } 695 696 /** 697 * Returns {@code true} if this method is a default 698 * method; returns {@code false} otherwise. 699 * 700 * A default method is a public non-abstract instance method, that 701 * is, a non-static method with a body, declared in an interface. 702 * 703 * @return true if and only if this method is a default 704 * method as defined by the Java Language Specification. 705 * @since 1.8 706 * @jls 9.4 Method Declarations 707 */ 708 public boolean isDefault() { 709 // Default methods are public non-abstract instance methods 710 // declared in an interface. 711 return ((getModifiers() & (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) == 712 Modifier.PUBLIC) && getDeclaringClass().isInterface(); 713 } 714 715 // NOTE that there is no synchronization used here. It is correct 716 // (though not efficient) to generate more than one MethodAccessor 717 // for a given Method. However, avoiding synchronization will 718 // probably make the implementation more scalable. 719 private MethodAccessor acquireMethodAccessor() { 720 // First check to see if one has been created yet, and take it 721 // if so 722 Method root = this.root; 723 MethodAccessor tmp = root == null ? null : root.getMethodAccessor(); 724 if (tmp != null) { 725 methodAccessor = tmp; 726 } else { 727 // Otherwise fabricate one and propagate it up to the root 728 tmp = reflectionFactory.newMethodAccessor(this, isCallerSensitive()); 729 // set the method accessor only if it's not using native implementation 730 if (VM.isJavaLangInvokeInited()) 731 setMethodAccessor(tmp); 732 } 733 734 return tmp; 735 } 736 737 // Returns MethodAccessor for this Method object, not looking up 738 // the chain to the root 739 MethodAccessor getMethodAccessor() { 740 return methodAccessor; 741 } 742 743 // Sets the MethodAccessor for this Method object and 744 // (recursively) its root 745 void setMethodAccessor(MethodAccessor accessor) { 746 methodAccessor = accessor; 747 // Propagate up 748 Method root = this.root; 749 if (root != null) { 750 root.setMethodAccessor(accessor); 751 } 752 } 753 754 /** 755 * Returns the default value for the annotation member represented by 756 * this {@code Method} instance. If the member is of a primitive type, 757 * an instance of the corresponding wrapper type is returned. Returns 758 * null if no default is associated with the member, or if the method 759 * instance does not represent a declared member of an annotation type. 760 * 761 * @return the default value for the annotation member represented 762 * by this {@code Method} instance. 763 * @throws TypeNotPresentException if the annotation is of type 764 * {@link Class} and no definition can be found for the 765 * default class value. 766 * @since 1.5 767 * @jls 9.6.2 Defaults for Annotation Type Elements 768 */ 769 public Object getDefaultValue() { 770 if (annotationDefault == null) 771 return null; 772 Class<?> memberType = AnnotationType.invocationHandlerReturnType( 773 getReturnType()); 774 Object result = AnnotationParser.parseMemberValue( 775 memberType, ByteBuffer.wrap(annotationDefault), 776 SharedSecrets.getJavaLangAccess(). 777 getConstantPool(getDeclaringClass()), 778 getDeclaringClass()); 779 if (result instanceof ExceptionProxy) { 780 if (result instanceof TypeNotPresentExceptionProxy proxy) { 781 throw new TypeNotPresentException(proxy.typeName(), proxy.getCause()); 782 } 783 throw new AnnotationFormatError("Invalid default: " + this); 784 } 785 return result; 786 } 787 788 /** 789 * {@inheritDoc} 790 * @throws NullPointerException {@inheritDoc} 791 * @since 1.5 792 */ 793 @Override 794 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { 795 return super.getAnnotation(annotationClass); 796 } 797 798 /** 799 * {@inheritDoc} 800 * @since 1.5 801 */ 802 @Override 803 public Annotation[] getDeclaredAnnotations() { 804 return super.getDeclaredAnnotations(); 805 } 806 807 /** 808 * {@inheritDoc} 809 * @since 1.5 810 */ 811 @Override 812 public Annotation[][] getParameterAnnotations() { 813 return sharedGetParameterAnnotations(parameterTypes, parameterAnnotations); 814 } 815 816 /** 817 * {@inheritDoc} 818 * @since 1.8 819 */ 820 @Override 821 public AnnotatedType getAnnotatedReturnType() { 822 return getAnnotatedReturnType0(getGenericReturnType()); 823 } 824 825 @Override 826 boolean handleParameterNumberMismatch(int resultLength, Class<?>[] parameterTypes) { 827 throw new AnnotationFormatError("Parameter annotations don't match number of parameters"); 828 } 829 }