< prev index next >

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

Print this page

  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
 23  * questions.
 24  */
 25 
 26 package java.lang.reflect;
 27 
 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;

114             genericInfo = MethodRepository.make(getGenericSignature(),
115                                                 getFactory());
116         }
117         return genericInfo; //return cached repository
118     }
119 
120     /**
121      * Package-private constructor
122      */
123     Method(Class<?> declaringClass,
124            String name,
125            Class<?>[] parameterTypes,
126            Class<?> returnType,
127            Class<?>[] checkedExceptions,
128            int modifiers,
129            int slot,
130            String signature,
131            byte[] annotations,
132            byte[] parameterAnnotations,
133            byte[] annotationDefault) {

134         this.clazz = declaringClass;
135         this.name = name;
136         this.parameterTypes = parameterTypes;
137         this.returnType = returnType;
138         this.exceptionTypes = checkedExceptions;
139         this.modifiers = modifiers;
140         this.slot = slot;
141         this.signature = signature;
142         this.annotations = annotations;
143         this.parameterAnnotations = parameterAnnotations;
144         this.annotationDefault = annotationDefault;
145     }
146 
147     /**
148      * Package-private routine (exposed to java.lang.Class via
149      * ReflectAccess) which returns a copy of this Method. The copy's
150      * "root" field points to this Method.
151      */
152     Method copy() {
153         // This routine enables sharing of MethodAccessor objects

403      * and then other modifiers in the following order:
404      * {@code abstract}, {@code default}, {@code static}, {@code final},
405      * {@code synchronized}, {@code native}, {@code strictfp}.
406      *
407      * @return a string describing this {@code Method}
408      *
409      * @jls 8.4.3 Method Modifiers
410      * @jls 9.4 Method Declarations
411      * @jls 9.6.1 Annotation Interface Elements
412      */
413     public String toString() {
414         return sharedToString(Modifier.methodModifiers(),
415                               isDefault(),
416                               parameterTypes,
417                               exceptionTypes);
418     }
419 
420     @Override
421     void specificToStringHeader(StringBuilder sb) {
422         sb.append(getReturnType().getTypeName()).append(' ');
423         sb.append(getDeclaringClass().getTypeName()).append('.');
424         sb.append(getName());
425     }
426 
427     @Override
428     String toShortString() {
429         return "method " + getDeclaringClass().getTypeName() +
430                 '.' + toShortSignature();
431     }
432 
433     String toShortSignature() {
434         StringJoiner sj = new StringJoiner(",", getName() + "(", ")");
435         for (Class<?> parameterType : getSharedParameterTypes()) {
436             sj.add(parameterType.getTypeName());
437         }
438         return sj.toString();
439     }
440 
441     /**
442      * Returns a string describing this {@code Method}, including type
443      * parameters.  The string is formatted as the method access
444      * modifiers, if any, followed by an angle-bracketed
445      * comma-separated list of the method's type parameters, if any,
446      * including informative bounds of the type parameters, if any,
447      * followed by the method's generic return type, followed by a
448      * space, followed by the class declaring the method, followed by
449      * a period, followed by the method name, followed by a

472      * {@code synchronized}, {@code native}, {@code strictfp}.
473      *
474      * @return a string describing this {@code Method},
475      * include type parameters
476      *
477      * @since 1.5
478      *
479      * @jls 8.4.3 Method Modifiers
480      * @jls 9.4 Method Declarations
481      * @jls 9.6.1 Annotation Interface Elements
482      */
483     @Override
484     public String toGenericString() {
485         return sharedToGenericString(Modifier.methodModifiers(), isDefault());
486     }
487 
488     @Override
489     void specificToGenericStringHeader(StringBuilder sb) {
490         Type genRetType = getGenericReturnType();
491         sb.append(genRetType.getTypeName()).append(' ');
492         sb.append(getDeclaringClass().getTypeName()).append('.');
493         sb.append(getName());
494     }
495 
496     /**
497      * Invokes the underlying method represented by this {@code Method}
498      * object, on the specified object with the specified parameters.
499      * Individual parameters are automatically unwrapped to match
500      * primitive formal parameters, and both primitive and reference
501      * parameters are subject to method invocation conversions as
502      * necessary.
503      *
504      * <p>If the underlying method is static, then the specified {@code obj}
505      * argument is ignored. It may be null.
506      *
507      * <p>If the number of formal parameters required by the underlying method is
508      * 0, the supplied {@code args} array may be of length 0 or null.
509      *
510      * <p>If the underlying method is an instance method, it is invoked
511      * using dynamic method lookup as documented in The Java Language
512      * Specification, section {@jls 15.12.4.4}; in particular,

  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
 23  * questions.
 24  */
 25 
 26 package java.lang.reflect;
 27 
 28 import jdk.internal.access.SharedSecrets;
 29 import jdk.internal.value.PrimitiveClass;
 30 import jdk.internal.misc.VM;
 31 import jdk.internal.reflect.CallerSensitive;
 32 import jdk.internal.reflect.CallerSensitiveAdapter;
 33 import jdk.internal.reflect.MethodAccessor;
 34 import jdk.internal.reflect.Reflection;
 35 import jdk.internal.vm.annotation.ForceInline;
 36 import jdk.internal.vm.annotation.IntrinsicCandidate;
 37 import jdk.internal.vm.annotation.Stable;
 38 import sun.reflect.annotation.ExceptionProxy;
 39 import sun.reflect.annotation.TypeNotPresentExceptionProxy;
 40 import sun.reflect.generics.repository.GenericDeclRepository;
 41 import sun.reflect.generics.repository.MethodRepository;
 42 import sun.reflect.generics.factory.CoreReflectionFactory;
 43 import sun.reflect.generics.factory.GenericsFactory;
 44 import sun.reflect.generics.scope.MethodScope;
 45 import sun.reflect.annotation.AnnotationType;
 46 import sun.reflect.annotation.AnnotationParser;
 47 import java.lang.annotation.Annotation;
 48 import java.lang.annotation.AnnotationFormatError;
 49 import java.nio.ByteBuffer;

115             genericInfo = MethodRepository.make(getGenericSignature(),
116                                                 getFactory());
117         }
118         return genericInfo; //return cached repository
119     }
120 
121     /**
122      * Package-private constructor
123      */
124     Method(Class<?> declaringClass,
125            String name,
126            Class<?>[] parameterTypes,
127            Class<?> returnType,
128            Class<?>[] checkedExceptions,
129            int modifiers,
130            int slot,
131            String signature,
132            byte[] annotations,
133            byte[] parameterAnnotations,
134            byte[] annotationDefault) {
135         assert PrimitiveClass.isPrimaryType(declaringClass);
136         this.clazz = declaringClass;
137         this.name = name;
138         this.parameterTypes = parameterTypes;
139         this.returnType = returnType;
140         this.exceptionTypes = checkedExceptions;
141         this.modifiers = modifiers;
142         this.slot = slot;
143         this.signature = signature;
144         this.annotations = annotations;
145         this.parameterAnnotations = parameterAnnotations;
146         this.annotationDefault = annotationDefault;
147     }
148 
149     /**
150      * Package-private routine (exposed to java.lang.Class via
151      * ReflectAccess) which returns a copy of this Method. The copy's
152      * "root" field points to this Method.
153      */
154     Method copy() {
155         // This routine enables sharing of MethodAccessor objects

405      * and then other modifiers in the following order:
406      * {@code abstract}, {@code default}, {@code static}, {@code final},
407      * {@code synchronized}, {@code native}, {@code strictfp}.
408      *
409      * @return a string describing this {@code Method}
410      *
411      * @jls 8.4.3 Method Modifiers
412      * @jls 9.4 Method Declarations
413      * @jls 9.6.1 Annotation Interface Elements
414      */
415     public String toString() {
416         return sharedToString(Modifier.methodModifiers(),
417                               isDefault(),
418                               parameterTypes,
419                               exceptionTypes);
420     }
421 
422     @Override
423     void specificToStringHeader(StringBuilder sb) {
424         sb.append(getReturnType().getTypeName()).append(' ');
425         sb.append(getDeclaringClassTypeName()).append('.');
426         sb.append(getName());
427     }
428 
429     @Override
430     String toShortString() {
431         return "method " + getDeclaringClassTypeName() +
432                 '.' + toShortSignature();
433     }
434 
435     String toShortSignature() {
436         StringJoiner sj = new StringJoiner(",", getName() + "(", ")");
437         for (Class<?> parameterType : getSharedParameterTypes()) {
438             sj.add(parameterType.getTypeName());
439         }
440         return sj.toString();
441     }
442 
443     /**
444      * Returns a string describing this {@code Method}, including type
445      * parameters.  The string is formatted as the method access
446      * modifiers, if any, followed by an angle-bracketed
447      * comma-separated list of the method's type parameters, if any,
448      * including informative bounds of the type parameters, if any,
449      * followed by the method's generic return type, followed by a
450      * space, followed by the class declaring the method, followed by
451      * a period, followed by the method name, followed by a

474      * {@code synchronized}, {@code native}, {@code strictfp}.
475      *
476      * @return a string describing this {@code Method},
477      * include type parameters
478      *
479      * @since 1.5
480      *
481      * @jls 8.4.3 Method Modifiers
482      * @jls 9.4 Method Declarations
483      * @jls 9.6.1 Annotation Interface Elements
484      */
485     @Override
486     public String toGenericString() {
487         return sharedToGenericString(Modifier.methodModifiers(), isDefault());
488     }
489 
490     @Override
491     void specificToGenericStringHeader(StringBuilder sb) {
492         Type genRetType = getGenericReturnType();
493         sb.append(genRetType.getTypeName()).append(' ');
494         sb.append(getDeclaringClassTypeName()).append('.');
495         sb.append(getName());
496     }
497 
498     /**
499      * Invokes the underlying method represented by this {@code Method}
500      * object, on the specified object with the specified parameters.
501      * Individual parameters are automatically unwrapped to match
502      * primitive formal parameters, and both primitive and reference
503      * parameters are subject to method invocation conversions as
504      * necessary.
505      *
506      * <p>If the underlying method is static, then the specified {@code obj}
507      * argument is ignored. It may be null.
508      *
509      * <p>If the number of formal parameters required by the underlying method is
510      * 0, the supplied {@code args} array may be of length 0 or null.
511      *
512      * <p>If the underlying method is an instance method, it is invoked
513      * using dynamic method lookup as documented in The Java Language
514      * Specification, section {@jls 15.12.4.4}; in particular,
< prev index next >