< prev index next >

src/java.base/share/classes/java/lang/reflect/Method.java

Print this page

 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

 79     private final Class<?>[]          parameterTypes;
 80     private final Class<?>[]          exceptionTypes;
 81     private final int                 modifiers;
 82     // Generics and annotations support
 83     private final transient String    signature;
 84     private final byte[]              annotations;
 85     private final byte[]              parameterAnnotations;
 86     private final byte[]              annotationDefault;
 87 
 88     /**
 89      * Methods are mutable due to {@link AccessibleObject#setAccessible(boolean)}.
 90      * Thus, we return a new copy of a root each time a method is returned.
 91      * Some lazily initialized immutable states can be stored on root and shared to the copies.
 92      */
 93     private Method root;
 94     private transient volatile MethodRepository genericInfo;
 95     private @Stable MethodAccessor methodAccessor;
 96     // End shared states
 97     private int hash; // not shared right now, eligible if expensive
 98 


 99     // Generics infrastructure
100     private String getGenericSignature() {return signature;}
101 
102     // Accessor for factory
103     private GenericsFactory getFactory() {
104         // create scope and factory
105         return CoreReflectionFactory.make(this, MethodScope.make(this));
106     }
107 
108     // Accessor for generic info repository
109     @Override
110     MethodRepository getGenericInfo() {
111         var genericInfo = this.genericInfo;
112         if (genericInfo == null) {
113             var root = this.root;
114             if (root != null) {
115                 genericInfo = root.getGenericInfo();
116             } else {
117                 genericInfo = MethodRepository.make(getGenericSignature(), getFactory());
118             }

207     }
208 
209     /**
210      * Returns the name of the method represented by this {@code Method}
211      * object, as a {@code String}.
212      */
213     @Override
214     public String getName() {
215         return name;
216     }
217 
218     /**
219      * {@inheritDoc}
220      * @jls 8.4.3 Method Modifiers
221      */
222     @Override
223     public int getModifiers() {
224         return modifiers;
225     }
226 















227     /**
228      * {@inheritDoc}
229      * @throws GenericSignatureFormatError {@inheritDoc}
230      * @since 1.5
231      * @jls 8.4.4 Generic Methods
232      */
233     @Override
234     @SuppressWarnings("unchecked")
235     public TypeVariable<Method>[] getTypeParameters() {
236         if (getGenericSignature() != null)
237             return (TypeVariable<Method>[])getGenericInfo().getTypeParameters();
238         else
239             return (TypeVariable<Method>[])GenericDeclRepository.EMPTY_TYPE_VARS;
240     }
241 
242     /**
243      * Returns a {@code Class} object that represents the formal return type
244      * of the method represented by this {@code Method} object.
245      *
246      * @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

 81     private final Class<?>[]          parameterTypes;
 82     private final Class<?>[]          exceptionTypes;
 83     private final int                 modifiers;
 84     // Generics and annotations support
 85     private final transient String    signature;
 86     private final byte[]              annotations;
 87     private final byte[]              parameterAnnotations;
 88     private final byte[]              annotationDefault;
 89 
 90     /**
 91      * Methods are mutable due to {@link AccessibleObject#setAccessible(boolean)}.
 92      * Thus, we return a new copy of a root each time a method is returned.
 93      * Some lazily initialized immutable states can be stored on root and shared to the copies.
 94      */
 95     private Method root;
 96     private transient volatile MethodRepository genericInfo;
 97     private @Stable MethodAccessor methodAccessor;
 98     // End shared states
 99     private int hash; // not shared right now, eligible if expensive
100 
101     private volatile Optional<?>     codeModel;
102 
103     // Generics infrastructure
104     private String getGenericSignature() {return signature;}
105 
106     // Accessor for factory
107     private GenericsFactory getFactory() {
108         // create scope and factory
109         return CoreReflectionFactory.make(this, MethodScope.make(this));
110     }
111 
112     // Accessor for generic info repository
113     @Override
114     MethodRepository getGenericInfo() {
115         var genericInfo = this.genericInfo;
116         if (genericInfo == null) {
117             var root = this.root;
118             if (root != null) {
119                 genericInfo = root.getGenericInfo();
120             } else {
121                 genericInfo = MethodRepository.make(getGenericSignature(), getFactory());
122             }

211     }
212 
213     /**
214      * Returns the name of the method represented by this {@code Method}
215      * object, as a {@code String}.
216      */
217     @Override
218     public String getName() {
219         return name;
220     }
221 
222     /**
223      * {@inheritDoc}
224      * @jls 8.4.3 Method Modifiers
225      */
226     @Override
227     public int getModifiers() {
228         return modifiers;
229     }
230 
231     /* package */
232     Optional<?> setCodeModelIfNeeded(Function<Method, Optional<?>> modelFactory) {
233         Optional<?> localRef = codeModel;
234         if (localRef == null) {
235             synchronized (this) {
236                 localRef = codeModel;
237                 if (localRef == null) {
238                     Optional<?> op = modelFactory.apply(this);
239                     codeModel = localRef = op;
240                 }
241             }
242         }
243         return localRef;
244     }
245 
246     /**
247      * {@inheritDoc}
248      * @throws GenericSignatureFormatError {@inheritDoc}
249      * @since 1.5
250      * @jls 8.4.4 Generic Methods
251      */
252     @Override
253     @SuppressWarnings("unchecked")
254     public TypeVariable<Method>[] getTypeParameters() {
255         if (getGenericSignature() != null)
256             return (TypeVariable<Method>[])getGenericInfo().getTypeParameters();
257         else
258             return (TypeVariable<Method>[])GenericDeclRepository.EMPTY_TYPE_VARS;
259     }
260 
261     /**
262      * Returns a {@code Class} object that represents the formal return type
263      * of the method represented by this {@code Method} object.
264      *
265      * @return the return type for the method this object represents
< prev index next >