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