59 * @author Nakul Saraiya |
59 * @author Nakul Saraiya |
60 */ |
60 */ |
61 public final |
61 public final |
62 class Method extends AccessibleObject implements GenericDeclaration, |
62 class Method extends AccessibleObject implements GenericDeclaration, |
63 Member { |
63 Member { |
64 private Class clazz; |
64 private Class<?> clazz; |
65 private int slot; |
65 private int slot; |
66 // This is guaranteed to be interned by the VM in the 1.4 |
66 // This is guaranteed to be interned by the VM in the 1.4 |
67 // reflection implementation |
67 // reflection implementation |
68 private String name; |
68 private String name; |
69 private Class returnType; |
69 private Class<?> returnType; |
70 private Class[] parameterTypes; |
70 private Class<?>[] parameterTypes; |
71 private Class[] exceptionTypes; |
71 private Class<?>[] exceptionTypes; |
72 private int modifiers; |
72 private int modifiers; |
73 // Generics and annotations support |
73 // Generics and annotations support |
74 private transient String signature; |
74 private transient String signature; |
75 // generic info repository; lazily initialized |
75 // generic info repository; lazily initialized |
76 private transient MethodRepository genericInfo; |
76 private transient MethodRepository genericInfo; |
83 // potentially many Method objects pointing to it.) |
83 // potentially many Method objects pointing to it.) |
84 private Method root; |
84 private Method root; |
85 |
85 |
86 // More complicated security check cache needed here than for |
86 // More complicated security check cache needed here than for |
87 // Class.newInstance() and Constructor.newInstance() |
87 // Class.newInstance() and Constructor.newInstance() |
88 private Class securityCheckCache; |
88 private Class<?> securityCheckCache; |
89 private Class securityCheckTargetClassCache; |
89 private Class<?> securityCheckTargetClassCache; |
90 |
90 |
91 // Generics infrastructure |
91 // Generics infrastructure |
92 |
92 |
93 private String getGenericSignature() {return signature;} |
93 private String getGenericSignature() {return signature;} |
94 |
94 |
112 /** |
112 /** |
113 * Package-private constructor used by ReflectAccess to enable |
113 * Package-private constructor used by ReflectAccess to enable |
114 * instantiation of these objects in Java code from the java.lang |
114 * instantiation of these objects in Java code from the java.lang |
115 * package via sun.reflect.LangReflectAccess. |
115 * package via sun.reflect.LangReflectAccess. |
116 */ |
116 */ |
117 Method(Class declaringClass, |
117 Method(Class<?> declaringClass, |
118 String name, |
118 String name, |
119 Class[] parameterTypes, |
119 Class<?>[] parameterTypes, |
120 Class returnType, |
120 Class<?> returnType, |
121 Class[] checkedExceptions, |
121 Class<?>[] checkedExceptions, |
122 int modifiers, |
122 int modifiers, |
123 int slot, |
123 int slot, |
124 String signature, |
124 String signature, |
125 byte[] annotations, |
125 byte[] annotations, |
126 byte[] parameterAnnotations, |
126 byte[] parameterAnnotations, |
353 if ((getDeclaringClass() == other.getDeclaringClass()) |
353 if ((getDeclaringClass() == other.getDeclaringClass()) |
354 && (getName() == other.getName())) { |
354 && (getName() == other.getName())) { |
355 if (!returnType.equals(other.getReturnType())) |
355 if (!returnType.equals(other.getReturnType())) |
356 return false; |
356 return false; |
357 /* Avoid unnecessary cloning */ |
357 /* Avoid unnecessary cloning */ |
358 Class[] params1 = parameterTypes; |
358 Class<?>[] params1 = parameterTypes; |
359 Class[] params2 = other.parameterTypes; |
359 Class<?>[] params2 = other.parameterTypes; |
360 if (params1.length == params2.length) { |
360 if (params1.length == params2.length) { |
361 for (int i = 0; i < params1.length; i++) { |
361 for (int i = 0; i < params1.length; i++) { |
362 if (params1[i] != params2[i]) |
362 if (params1[i] != params2[i]) |
363 return false; |
363 return false; |
364 } |
364 } |
408 sb.append(Modifier.toString(mod) + " "); |
408 sb.append(Modifier.toString(mod) + " "); |
409 } |
409 } |
410 sb.append(Field.getTypeName(getReturnType()) + " "); |
410 sb.append(Field.getTypeName(getReturnType()) + " "); |
411 sb.append(Field.getTypeName(getDeclaringClass()) + "."); |
411 sb.append(Field.getTypeName(getDeclaringClass()) + "."); |
412 sb.append(getName() + "("); |
412 sb.append(getName() + "("); |
413 Class[] params = parameterTypes; // avoid clone |
413 Class<?>[] params = parameterTypes; // avoid clone |
414 for (int j = 0; j < params.length; j++) { |
414 for (int j = 0; j < params.length; j++) { |
415 sb.append(Field.getTypeName(params[j])); |
415 sb.append(Field.getTypeName(params[j])); |
416 if (j < (params.length - 1)) |
416 if (j < (params.length - 1)) |
417 sb.append(","); |
417 sb.append(","); |
418 } |
418 } |
419 sb.append(")"); |
419 sb.append(")"); |
420 Class[] exceptions = exceptionTypes; // avoid clone |
420 Class<?>[] exceptions = exceptionTypes; // avoid clone |
421 if (exceptions.length > 0) { |
421 if (exceptions.length > 0) { |
422 sb.append(" throws "); |
422 sb.append(" throws "); |
423 for (int k = 0; k < exceptions.length; k++) { |
423 for (int k = 0; k < exceptions.length; k++) { |
424 sb.append(exceptions[k].getName()); |
424 sb.append(exceptions[k].getName()); |
425 if (k < (exceptions.length - 1)) |
425 if (k < (exceptions.length - 1)) |
588 throws IllegalAccessException, IllegalArgumentException, |
588 throws IllegalAccessException, IllegalArgumentException, |
589 InvocationTargetException |
589 InvocationTargetException |
590 { |
590 { |
591 if (!override) { |
591 if (!override) { |
592 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
592 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
593 Class caller = Reflection.getCallerClass(1); |
593 Class<?> caller = Reflection.getCallerClass(1); |
594 Class targetClass = ((obj == null || !Modifier.isProtected(modifiers)) |
594 Class<?> targetClass = ((obj == null || !Modifier.isProtected(modifiers)) |
595 ? clazz |
595 ? clazz |
596 : obj.getClass()); |
596 : obj.getClass()); |
597 |
597 |
598 boolean cached; |
598 boolean cached; |
599 synchronized (this) { |
599 synchronized (this) { |
600 cached = (securityCheckCache == caller) |
600 cached = (securityCheckCache == caller) |
601 && (securityCheckTargetClassCache == targetClass); |
601 && (securityCheckTargetClassCache == targetClass); |
700 */ |
700 */ |
701 public Annotation[] getDeclaredAnnotations() { |
701 public Annotation[] getDeclaredAnnotations() { |
702 return AnnotationParser.toArray(declaredAnnotations()); |
702 return AnnotationParser.toArray(declaredAnnotations()); |
703 } |
703 } |
704 |
704 |
705 private transient Map<Class, Annotation> declaredAnnotations; |
705 private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations; |
706 |
706 |
707 private synchronized Map<Class, Annotation> declaredAnnotations() { |
707 private synchronized Map<Class<? extends Annotation>, Annotation> declaredAnnotations() { |
708 if (declaredAnnotations == null) { |
708 if (declaredAnnotations == null) { |
709 declaredAnnotations = AnnotationParser.parseAnnotations( |
709 declaredAnnotations = AnnotationParser.parseAnnotations( |
710 annotations, sun.misc.SharedSecrets.getJavaLangAccess(). |
710 annotations, sun.misc.SharedSecrets.getJavaLangAccess(). |
711 getConstantPool(getDeclaringClass()), |
711 getConstantPool(getDeclaringClass()), |
712 getDeclaringClass()); |
712 getDeclaringClass()); |
729 * @since 1.5 |
729 * @since 1.5 |
730 */ |
730 */ |
731 public Object getDefaultValue() { |
731 public Object getDefaultValue() { |
732 if (annotationDefault == null) |
732 if (annotationDefault == null) |
733 return null; |
733 return null; |
734 Class memberType = AnnotationType.invocationHandlerReturnType( |
734 Class<?> memberType = AnnotationType.invocationHandlerReturnType( |
735 getReturnType()); |
735 getReturnType()); |
736 Object result = AnnotationParser.parseMemberValue( |
736 Object result = AnnotationParser.parseMemberValue( |
737 memberType, ByteBuffer.wrap(annotationDefault), |
737 memberType, ByteBuffer.wrap(annotationDefault), |
738 sun.misc.SharedSecrets.getJavaLangAccess(). |
738 sun.misc.SharedSecrets.getJavaLangAccess(). |
739 getConstantPool(getDeclaringClass()), |
739 getConstantPool(getDeclaringClass()), |