1 7 8 package java.lang.reflect; 9 10 import sun.reflect.MethodAccessor; 11 import sun.reflect.Reflection; 12 import sun.reflect.generics.repository.MethodRepository; 13 import sun.reflect.generics.factory.CoreReflectionFactory; 14 import sun.reflect.generics.factory.GenericsFactory; 15 import sun.reflect.generics.scope.MethodScope; 16 import sun.reflect.annotation.AnnotationType; 17 import sun.reflect.annotation.AnnotationParser; 18 import java.lang.annotation.Annotation ; 19 import java.lang.annotation.AnnotationFormatError ; 20 import java.nio.ByteBuffer ; 21 import java.util.Map ; 22 23 43 public final 44 class Method extends AccessibleObject implements GenericDeclaration , 45 Member { 46 47 48 private Class clazz; 49 private int slot; 50 private String name; 53 private Class returnType; 54 private Class [] parameterTypes; 55 private Class [] exceptionTypes; 56 private int modifiers; 57 private transient String signature; 59 private transient MethodRepository genericInfo; 61 private byte[] annotations; 62 private byte[] parameterAnnotations; 63 private byte[] annotationDefault; 64 private volatile MethodAccessor methodAccessor; 65 private Method root; 69 70 private volatile Class securityCheckTargetClassCache; 73 74 76 private String getGenericSignature() {return signature;} 77 78 private GenericsFactory getFactory() { 80 return CoreReflectionFactory.make(this, MethodScope.make(this)); 82 } 83 84 private MethodRepository getGenericInfo() { 86 if (genericInfo == null) { 88 genericInfo = MethodRepository.make(getGenericSignature(), 90 getFactory()); 91 } 92 return genericInfo; } 94 95 100 Method(Class declaringClass, 101 String name, 102 Class [] parameterTypes, 103 Class returnType, 104 Class [] checkedExceptions, 105 int modifiers, 106 int slot, 107 String signature, 108 byte[] annotations, 109 byte[] parameterAnnotations, 110 byte[] annotationDefault) 111 { 112 this.clazz = declaringClass; 113 this.name = name; 114 this.parameterTypes = parameterTypes; 115 this.returnType = returnType; 116 this.exceptionTypes = checkedExceptions; 117 this.modifiers = modifiers; 118 this.slot = slot; 119 this.signature = signature; 120 this.annotations = annotations; 121 this.parameterAnnotations = parameterAnnotations; 122 this.annotationDefault = annotationDefault; 123 } 124 125 130 Method copy() { 131 Method res = new Method (clazz, name, parameterTypes, returnType, 139 exceptionTypes, modifiers, slot, signature, 140 annotations, parameterAnnotations, annotationDefault); 141 res.root = this; 142 res.methodAccessor = methodAccessor; 144 return res; 145 } 146 147 151 public Class <?> getDeclaringClass() { 152 return clazz; 153 } 154 155 159 public String getName() { 160 return name; 161 } 162 163 170 public int getModifiers() { 171 return modifiers; 172 } 173 174 189 public TypeVariable <Method >[] getTypeParameters() { 190 if (getGenericSignature() != null) 191 return (TypeVariable <Method >[])getGenericInfo().getTypeParameters(); 192 else 193 return (TypeVariable <Method >[])new TypeVariable [0]; 194 } 195 196 202 public Class <?> getReturnType() { 203 return returnType; 204 } 205 206 229 public Type getGenericReturnType() { 230 if (getGenericSignature() != null) { 231 return getGenericInfo().getReturnType(); 232 } else { return getReturnType();} 233 } 234 235 236 245 public Class <?>[] getParameterTypes() { 246 return (Class <?>[]) parameterTypes.clone(); 247 } 248 249 275 public Type [] getGenericParameterTypes() { 276 if (getGenericSignature() != null) 277 return getGenericInfo().getParameterTypes(); 278 else 279 return getParameterTypes(); 280 } 281 282 283 293 public Class <?>[] getExceptionTypes() { 294 return (Class <?>[]) exceptionTypes.clone(); 295 } 296 297 322 public Type [] getGenericExceptionTypes() { 323 Type [] result; 324 if (getGenericSignature() != null && 325 ((result = getGenericInfo().getExceptionTypes()).length > 0)) 326 return result; 327 else 328 return getExceptionTypes(); 329 } 330 331 337 public boolean equals(Object obj) { 338 if (obj != null && obj instanceof Method ) { 339 Method other = (Method )obj; 340 if ((getDeclaringClass() == other.getDeclaringClass()) 341 && (getName() == other.getName())) { 342 if (!returnType.equals(other.getReturnType())) 343 return false; 344 345 Class [] params1 = parameterTypes; 346 Class [] params2 = other.parameterTypes; 347 if (params1.length == params2.length) { 348 for (int i = 0; i < params1.length; i++) { 349 if (params1[i] != params2[i]) 350 return false; 351 } 352 return true; 353 } 354 } 355 } 356 return false; 357 } 358 359 364 public int hashCode() { 365 return getDeclaringClass().getName().hashCode() ^ getName().hashCode(); 366 } 367 368 390 public String toString() { 391 try { 392 StringBuffer sb = new StringBuffer (); 393 int mod = getModifiers(); 394 if (mod != 0) { 395 sb.append(Modifier.toString(mod) + " "); 396 } 397 sb.append(Field.getTypeName(getReturnType()) + " "); 398 sb.append(Field.getTypeName(getDeclaringClass()) + "."); 399 sb.append(getName() + "("); 400 Class [] params = parameterTypes; for (int j = 0; j < params.length; j++) { 402 sb.append(Field.getTypeName(params[j])); 403 if (j < (params.length - 1)) 404 sb.append(","); 405 } 406 sb.append(")"); 407 Class [] exceptions = exceptionTypes; if (exceptions.length > 0) { 409 sb.append(" throws "); 410 for (int k = 0; k < exceptions.length; k++) { 411 sb.append(exceptions[k].getName()); 412 if (k < (exceptions.length - 1)) 413 sb.append(","); 414 } 415 } 416 return sb.toString(); 417 } catch (Exception e) { 418 return "<" + e + ">"; 419 } 420 } 421 422 454 public String toGenericString() { 455 try { 456 StringBuilder sb = new StringBuilder (); 457 int mod = getModifiers(); 458 if (mod != 0) { 459 sb.append(Modifier.toString(mod) + " "); 460 } 461 Type [] typeparms = getTypeParameters(); 462 if (typeparms.length > 0) { 463 boolean first = true; 464 sb.append("<"); 465 for(Type typeparm: typeparms) { 466 if (!first) 467 sb.append(","); 468 if (typeparm instanceof Class ) 469 sb.append(((Class )typeparm).getName()); 470 else 471 sb.append(typeparm.toString()); 472 first = false; 473 } 474 sb.append("> "); 475 } 476 477 Type genRetType = getGenericReturnType(); 478 sb.append( ((genRetType instanceof Class )? 479 Field.getTypeName((Class )genRetType):genRetType.toString()) + " "); 480 481 sb.append(Field.getTypeName(getDeclaringClass()) + "."); 482 sb.append(getName() + "("); 483 Type [] params = getGenericParameterTypes(); 484 for (int j = 0; j < params.length; j++) { 485 sb.append((params[j] instanceof Class )? 486 Field.getTypeName((Class )params[j]): 487 (params[j].toString()) ); 488 if (j < (params.length - 1)) 489 sb.append(","); 490 } 491 sb.append(")"); 492 Type [] exceptions = getGenericExceptionTypes(); 493 if (exceptions.length > 0) { 494 sb.append(" throws "); 495 for (int k = 0; k < exceptions.length; k++) { 496 sb.append((exceptions[k] instanceof Class )? 497 ((Class )exceptions[k]).getName(): 498 exceptions[k].toString()); 499 if (k < (exceptions.length - 1)) 500 sb.append(","); 501 } 502 } 503 return sb.toString(); 504 } catch (Exception e) { 505 return "<" + e + ">"; 506 } 507 } 508 509 566 public Object invoke(Object obj, Object ... args) 567 throws IllegalAccessException , IllegalArgumentException , 568 InvocationTargetException 569 { 570 if (!override) { 571 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { 572 Class caller = Reflection.getCallerClass(1); 573 Class targetClass = ((obj == null || !Modifier.isProtected(modifiers)) 574 ? clazz 575 : obj.getClass()); 576 if (securityCheckCache != caller || 577 targetClass != securityCheckTargetClassCache) { 578 Reflection.ensureMemberAccess(caller, clazz, obj, modifiers); 579 securityCheckCache = caller; 580 securityCheckTargetClassCache = targetClass; 581 } 582 } 583 } 584 if (methodAccessor == null) acquireMethodAccessor(); 585 return methodAccessor.invoke(obj, args); 586 } 587 588 596 public boolean isBridge() { 597 return (getModifiers() & Modifier.BRIDGE) != 0; 598 } 599 600 609 public boolean isVarArgs() { 610 return (getModifiers() & Modifier.VARARGS) != 0; 611 } 612 613 621 public boolean isSynthetic() { 622 return Modifier.isSynthetic(getModifiers()); 623 } 624 625 private void acquireMethodAccessor() { 630 MethodAccessor tmp = null; 633 if (root != null) tmp = root.getMethodAccessor(); 634 if (tmp != null) { 635 methodAccessor = tmp; 636 return; 637 } 638 tmp = reflectionFactory.newMethodAccessor(this); 640 setMethodAccessor(tmp); 641 } 642 643 MethodAccessor getMethodAccessor() { 646 return methodAccessor; 647 } 648 649 void setMethodAccessor(MethodAccessor accessor) { 652 methodAccessor = accessor; 653 if (root != null) { 655 root.setMethodAccessor(accessor); 656 } 657 } 658 659 public <T extends Annotation > T getAnnotation(Class <T> annotationClass) { 660 if (annotationClass == null) 661 throw new NullPointerException (); 662 663 return (T) declaredAnnotations().get(annotationClass); 664 } 665 666 private static final Annotation [] EMPTY_ANNOTATION_ARRAY=new Annotation [0]; 667 668 public Annotation [] getDeclaredAnnotations() { 669 return declaredAnnotations().values().toArray(EMPTY_ANNOTATION_ARRAY); 670 } 671 672 private transient Map <Class , Annotation > declaredAnnotations; 673 674 private synchronized Map <Class , Annotation > declaredAnnotations() { 675 if (declaredAnnotations == null) { 676 declaredAnnotations = AnnotationParser.parseAnnotations( 677 annotations, sun.misc.SharedSecrets.getJavaLangAccess(). 678 getConstantPool(getDeclaringClass()), 679 getDeclaringClass()); 680 } 681 return declaredAnnotations; 682 } 683 684 698 public Object getDefaultValue() { 699 if (annotationDefault == null) 700 return null; 701 Class memberType = AnnotationType.invocationHandlerReturnType( 702 getReturnType()); 703 Object result = AnnotationParser.parseMemberValue( 704 memberType, ByteBuffer.wrap(annotationDefault), 705 sun.misc.SharedSecrets.getJavaLangAccess(). 706 getConstantPool(getDeclaringClass()), 707 getDeclaringClass()); 708 if (result instanceof sun.reflect.annotation.ExceptionProxy) 709 throw new AnnotationFormatError ("Invalid default: " + this); 710 return result; 711 } 712 713 729 public Annotation [][] getParameterAnnotations() { 730 int numParameters = parameterTypes.length; 731 if (parameterAnnotations == null) 732 return new Annotation [numParameters][0]; 733 734 Annotation [][] result = AnnotationParser.parseParameterAnnotations( 735 parameterAnnotations, 736 sun.misc.SharedSecrets.getJavaLangAccess(). 737 getConstantPool(getDeclaringClass()), 738 getDeclaringClass()); 739 if (result.length != numParameters) 740 throw new java.lang.annotation.AnnotationFormatError ( 741 "Parameter annotations don't match number of parameters"); 742 return result; 743 } 744 } 745 | Popular Tags |