< prev index next >

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

Print this page

  1 /*
  2  * Copyright (c) 1996, 2023, 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

 28 import jdk.internal.access.SharedSecrets;
 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
 70  * @since 1.1

 78     private final Class<?>            returnType;
 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     // 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 
 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         // lazily initialize repository if necessary
113         if (genericInfo == null) {
114             // create and cache generic info repository
115             genericInfo = MethodRepository.make(getGenericSignature(),
116                                                 getFactory());
117             this.genericInfo = genericInfo;

228     }
229 
230     /**
231      * Returns the name of the method represented by this {@code Method}
232      * object, as a {@code String}.
233      */
234     @Override
235     public String getName() {
236         return name;
237     }
238 
239     /**
240      * {@inheritDoc}
241      * @jls 8.4.3 Method Modifiers
242      */
243     @Override
244     public int getModifiers() {
245         return modifiers;
246     }
247 




















































248     /**
249      * {@inheritDoc}
250      * @throws GenericSignatureFormatError {@inheritDoc}
251      * @since 1.5
252      * @jls 8.4.4 Generic Methods
253      */
254     @Override
255     @SuppressWarnings({"rawtypes", "unchecked"})
256     public TypeVariable<Method>[] getTypeParameters() {
257         if (getGenericSignature() != null)
258             return (TypeVariable<Method>[])getGenericInfo().getTypeParameters();
259         else
260             return (TypeVariable<Method>[])GenericDeclRepository.EMPTY_TYPE_VARS;
261     }
262 
263     /**
264      * Returns a {@code Class} object that represents the formal return type
265      * of the method represented by this {@code Method} object.
266      *
267      * @return the return type for the method this object represents

  1 /*
  2  * Copyright (c) 1996, 2024, 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

 28 import jdk.internal.access.SharedSecrets;
 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.lang.reflect.code.op.ExtendedOp;
 49 import java.lang.reflect.code.Op;
 50 import java.lang.reflect.code.parser.OpParser;
 51 import java.nio.ByteBuffer;
 52 import java.util.List;
 53 import java.util.Optional;
 54 import java.util.StringJoiner;
 55 
 56 import static java.lang.reflect.code.op.CoreOp.*;
 57 
 58 /**
 59  * A {@code Method} provides information about, and access to, a single method
 60  * on a class or interface.  The reflected method may be a class method
 61  * or an instance method (including an abstract method).
 62  *
 63  * <p>A {@code Method} permits widening conversions to occur when matching the
 64  * actual parameters to invoke with the underlying method's formal
 65  * parameters, but it throws an {@code IllegalArgumentException} if a
 66  * narrowing conversion would occur.
 67  *
 68  * @see Member
 69  * @see java.lang.Class
 70  * @see java.lang.Class#getMethods()
 71  * @see java.lang.Class#getMethod(String, Class[])
 72  * @see java.lang.Class#getDeclaredMethods()
 73  * @see java.lang.Class#getDeclaredMethod(String, Class[])
 74  *
 75  * @author Kenneth Russell
 76  * @author Nakul Saraiya
 77  * @since 1.1

 85     private final Class<?>            returnType;
 86     private final Class<?>[]          parameterTypes;
 87     private final Class<?>[]          exceptionTypes;
 88     private final int                 modifiers;
 89     // Generics and annotations support
 90     private final transient String    signature;
 91     // generic info repository; lazily initialized
 92     private transient volatile MethodRepository genericInfo;
 93     private final byte[]              annotations;
 94     private final byte[]              parameterAnnotations;
 95     private final byte[]              annotationDefault;
 96     @Stable
 97     private MethodAccessor      methodAccessor;
 98     // For sharing of MethodAccessors. This branching structure is
 99     // currently only two levels deep (i.e., one root Method and
100     // potentially many Method objects pointing to it.)
101     //
102     // If this branching structure would ever contain cycles, deadlocks can
103     // occur in annotation code.
104     private Method              root;
105     private volatile Optional<FuncOp>     codeModel;
106 
107     // Generics infrastructure
108     private String getGenericSignature() {return signature;}
109 
110     // Accessor for factory
111     private GenericsFactory getFactory() {
112         // create scope and factory
113         return CoreReflectionFactory.make(this, MethodScope.make(this));
114     }
115 
116     // Accessor for generic info repository
117     @Override
118     MethodRepository getGenericInfo() {
119         var genericInfo = this.genericInfo;
120         // lazily initialize repository if necessary
121         if (genericInfo == null) {
122             // create and cache generic info repository
123             genericInfo = MethodRepository.make(getGenericSignature(),
124                                                 getFactory());
125             this.genericInfo = genericInfo;

236     }
237 
238     /**
239      * Returns the name of the method represented by this {@code Method}
240      * object, as a {@code String}.
241      */
242     @Override
243     public String getName() {
244         return name;
245     }
246 
247     /**
248      * {@inheritDoc}
249      * @jls 8.4.3 Method Modifiers
250      */
251     @Override
252     public int getModifiers() {
253         return modifiers;
254     }
255 
256     /**
257      * Returns the code model of the method body, if present.
258      * @return the code model of the method body.
259      * @since 99
260      */
261     // @@@ Make caller sensitive with the same access control as invoke
262     // and throwing IllegalAccessException
263 //    @CallerSensitive
264     public Optional<FuncOp> getCodeModel() {
265         Optional<FuncOp> localRef = codeModel;
266         if (localRef == null) {
267             synchronized (this) {
268                 localRef = codeModel;
269                 if (localRef == null) {
270                     Optional<FuncOp> op = createCodeModel();
271                     codeModel = localRef = op;
272                 }
273             }
274         }
275         return localRef;
276     }
277 
278     private Optional<FuncOp> createCodeModel() {
279         Class<?> dc = getDeclaringClass();
280         String fieldName = getName() + "$" + "op";
281         Field f;
282         try {
283             f = dc.getDeclaredField(fieldName);
284         } catch (NoSuchFieldException e) {
285             return Optional.empty();
286         }
287 
288         String modelText;
289         try {
290             // @@@ Use method handle with full power mode
291             f.setAccessible(true);
292             modelText = (String) f.get(null);
293         } catch (IllegalAccessException e) {
294             throw new RuntimeException(e);
295         }
296 
297         FuncOp op;
298         try {
299             List<Op> ops = OpParser.fromString(ExtendedOp.FACTORY, modelText);
300             op = (FuncOp) ops.get(0);
301         } catch (RuntimeException e) {
302             // @@@ Error or Exception?
303             throw e;
304         }
305         return Optional.of(op);
306     }
307 
308     /**
309      * {@inheritDoc}
310      * @throws GenericSignatureFormatError {@inheritDoc}
311      * @since 1.5
312      * @jls 8.4.4 Generic Methods
313      */
314     @Override
315     @SuppressWarnings({"rawtypes", "unchecked"})
316     public TypeVariable<Method>[] getTypeParameters() {
317         if (getGenericSignature() != null)
318             return (TypeVariable<Method>[])getGenericInfo().getTypeParameters();
319         else
320             return (TypeVariable<Method>[])GenericDeclRepository.EMPTY_TYPE_VARS;
321     }
322 
323     /**
324      * Returns a {@code Class} object that represents the formal return type
325      * of the method represented by this {@code Method} object.
326      *
327      * @return the return type for the method this object represents
< prev index next >