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