29 import jdk.internal.misc.VM;
30 import jdk.internal.reflect.CallerSensitive;
31 import jdk.internal.reflect.CallerSensitiveAdapter;
32 import jdk.internal.reflect.MethodAccessor;
33 import jdk.internal.reflect.Reflection;
34 import jdk.internal.vm.annotation.ForceInline;
35 import jdk.internal.vm.annotation.IntrinsicCandidate;
36 import jdk.internal.vm.annotation.Stable;
37 import sun.reflect.annotation.ExceptionProxy;
38 import sun.reflect.annotation.TypeNotPresentExceptionProxy;
39 import sun.reflect.generics.repository.GenericDeclRepository;
40 import sun.reflect.generics.repository.MethodRepository;
41 import sun.reflect.generics.factory.CoreReflectionFactory;
42 import sun.reflect.generics.factory.GenericsFactory;
43 import sun.reflect.generics.scope.MethodScope;
44 import sun.reflect.annotation.AnnotationType;
45 import sun.reflect.annotation.AnnotationParser;
46 import java.lang.annotation.Annotation;
47 import java.lang.annotation.AnnotationFormatError;
48 import java.nio.ByteBuffer;
49 import java.util.StringJoiner;
50
51 /**
52 * A {@code Method} provides information about, and access to, a single method
53 * on a class or interface. The reflected method may be a class method
54 * or an instance method (including an abstract method).
55 *
56 * <p>A {@code Method} permits widening conversions to occur when matching the
57 * actual parameters to invoke with the underlying method's formal
58 * parameters, but it throws an {@code IllegalArgumentException} if a
59 * narrowing conversion would occur.
60 *
61 * @see Member
62 * @see java.lang.Class
63 * @see java.lang.Class#getMethods()
64 * @see java.lang.Class#getMethod(String, Class[])
65 * @see java.lang.Class#getDeclaredMethods()
66 * @see java.lang.Class#getDeclaredMethod(String, Class[])
67 *
68 * @author Kenneth Russell
69 * @author Nakul Saraiya
80 private final Class<?>[] exceptionTypes;
81 private final int modifiers;
82 // Generics and annotations support
83 private final transient String signature;
84 // generic info repository; lazily initialized
85 private transient volatile MethodRepository genericInfo;
86 private final byte[] annotations;
87 private final byte[] parameterAnnotations;
88 private final byte[] annotationDefault;
89 @Stable
90 private MethodAccessor methodAccessor;
91 // For sharing of MethodAccessors. This branching structure is
92 // currently only two levels deep (i.e., one root Method and
93 // potentially many Method objects pointing to it.)
94 //
95 // If this branching structure would ever contain cycles, deadlocks can
96 // occur in annotation code.
97 private Method root;
98 // Hash code of this object
99 private int hash;
100
101 // Generics infrastructure
102 private String getGenericSignature() {return signature;}
103
104 // Accessor for factory
105 private GenericsFactory getFactory() {
106 // create scope and factory
107 return CoreReflectionFactory.make(this, MethodScope.make(this));
108 }
109
110 // Accessor for generic info repository
111 @Override
112 MethodRepository getGenericInfo() {
113 var genericInfo = this.genericInfo;
114 // lazily initialize repository if necessary
115 if (genericInfo == null) {
116 // create and cache generic info repository
117 genericInfo = MethodRepository.make(getGenericSignature(),
118 getFactory());
119 this.genericInfo = genericInfo;
215 }
216
217 /**
218 * Returns the name of the method represented by this {@code Method}
219 * object, as a {@code String}.
220 */
221 @Override
222 public String getName() {
223 return name;
224 }
225
226 /**
227 * {@inheritDoc}
228 * @jls 8.4.3 Method Modifiers
229 */
230 @Override
231 public int getModifiers() {
232 return modifiers;
233 }
234
235 /**
236 * {@inheritDoc}
237 * @throws GenericSignatureFormatError {@inheritDoc}
238 * @since 1.5
239 * @jls 8.4.4 Generic Methods
240 */
241 @Override
242 @SuppressWarnings({"rawtypes", "unchecked"})
243 public TypeVariable<Method>[] getTypeParameters() {
244 if (getGenericSignature() != null)
245 return (TypeVariable<Method>[])getGenericInfo().getTypeParameters();
246 else
247 return (TypeVariable<Method>[])GenericDeclRepository.EMPTY_TYPE_VARS;
248 }
249
250 /**
251 * Returns a {@code Class} object that represents the formal return type
252 * of the method represented by this {@code Method} object.
253 *
254 * @return the return type for the method this object represents
|
29 import jdk.internal.misc.VM;
30 import jdk.internal.reflect.CallerSensitive;
31 import jdk.internal.reflect.CallerSensitiveAdapter;
32 import jdk.internal.reflect.MethodAccessor;
33 import jdk.internal.reflect.Reflection;
34 import jdk.internal.vm.annotation.ForceInline;
35 import jdk.internal.vm.annotation.IntrinsicCandidate;
36 import jdk.internal.vm.annotation.Stable;
37 import sun.reflect.annotation.ExceptionProxy;
38 import sun.reflect.annotation.TypeNotPresentExceptionProxy;
39 import sun.reflect.generics.repository.GenericDeclRepository;
40 import sun.reflect.generics.repository.MethodRepository;
41 import sun.reflect.generics.factory.CoreReflectionFactory;
42 import sun.reflect.generics.factory.GenericsFactory;
43 import sun.reflect.generics.scope.MethodScope;
44 import sun.reflect.annotation.AnnotationType;
45 import sun.reflect.annotation.AnnotationParser;
46 import java.lang.annotation.Annotation;
47 import java.lang.annotation.AnnotationFormatError;
48 import java.nio.ByteBuffer;
49 import java.util.Optional;
50 import java.util.StringJoiner;
51 import java.util.function.Function;
52
53 /**
54 * A {@code Method} provides information about, and access to, a single method
55 * on a class or interface. The reflected method may be a class method
56 * or an instance method (including an abstract method).
57 *
58 * <p>A {@code Method} permits widening conversions to occur when matching the
59 * actual parameters to invoke with the underlying method's formal
60 * parameters, but it throws an {@code IllegalArgumentException} if a
61 * narrowing conversion would occur.
62 *
63 * @see Member
64 * @see java.lang.Class
65 * @see java.lang.Class#getMethods()
66 * @see java.lang.Class#getMethod(String, Class[])
67 * @see java.lang.Class#getDeclaredMethods()
68 * @see java.lang.Class#getDeclaredMethod(String, Class[])
69 *
70 * @author Kenneth Russell
71 * @author Nakul Saraiya
82 private final Class<?>[] exceptionTypes;
83 private final int modifiers;
84 // Generics and annotations support
85 private final transient String signature;
86 // generic info repository; lazily initialized
87 private transient volatile MethodRepository genericInfo;
88 private final byte[] annotations;
89 private final byte[] parameterAnnotations;
90 private final byte[] annotationDefault;
91 @Stable
92 private MethodAccessor methodAccessor;
93 // For sharing of MethodAccessors. This branching structure is
94 // currently only two levels deep (i.e., one root Method and
95 // potentially many Method objects pointing to it.)
96 //
97 // If this branching structure would ever contain cycles, deadlocks can
98 // occur in annotation code.
99 private Method root;
100 // Hash code of this object
101 private int hash;
102 private volatile Optional<?> codeModel;
103
104 // Generics infrastructure
105 private String getGenericSignature() {return signature;}
106
107 // Accessor for factory
108 private GenericsFactory getFactory() {
109 // create scope and factory
110 return CoreReflectionFactory.make(this, MethodScope.make(this));
111 }
112
113 // Accessor for generic info repository
114 @Override
115 MethodRepository getGenericInfo() {
116 var genericInfo = this.genericInfo;
117 // lazily initialize repository if necessary
118 if (genericInfo == null) {
119 // create and cache generic info repository
120 genericInfo = MethodRepository.make(getGenericSignature(),
121 getFactory());
122 this.genericInfo = genericInfo;
218 }
219
220 /**
221 * Returns the name of the method represented by this {@code Method}
222 * object, as a {@code String}.
223 */
224 @Override
225 public String getName() {
226 return name;
227 }
228
229 /**
230 * {@inheritDoc}
231 * @jls 8.4.3 Method Modifiers
232 */
233 @Override
234 public int getModifiers() {
235 return modifiers;
236 }
237
238 /* package */
239 Optional<?> setCodeModelIfNeeded(Function<Method, Optional<?>> modelFactory) {
240 Optional<?> localRef = codeModel;
241 if (localRef == null) {
242 synchronized (this) {
243 localRef = codeModel;
244 if (localRef == null) {
245 Optional<?> op = modelFactory.apply(this);
246 codeModel = localRef = op;
247 }
248 }
249 }
250 return localRef;
251 }
252
253 /**
254 * {@inheritDoc}
255 * @throws GenericSignatureFormatError {@inheritDoc}
256 * @since 1.5
257 * @jls 8.4.4 Generic Methods
258 */
259 @Override
260 @SuppressWarnings({"rawtypes", "unchecked"})
261 public TypeVariable<Method>[] getTypeParameters() {
262 if (getGenericSignature() != null)
263 return (TypeVariable<Method>[])getGenericInfo().getTypeParameters();
264 else
265 return (TypeVariable<Method>[])GenericDeclRepository.EMPTY_TYPE_VARS;
266 }
267
268 /**
269 * Returns a {@code Class} object that represents the formal return type
270 * of the method represented by this {@code Method} object.
271 *
272 * @return the return type for the method this object represents
|