1 /*
  2  * Copyright (c) 1996, 2021, 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
 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.MethodRepository;
 40 import sun.reflect.generics.factory.CoreReflectionFactory;
 41 import sun.reflect.generics.factory.GenericsFactory;
 42 import sun.reflect.generics.scope.MethodScope;
 43 import sun.reflect.annotation.AnnotationType;
 44 import sun.reflect.annotation.AnnotationParser;
 45 import java.lang.annotation.Annotation;
 46 import java.lang.annotation.AnnotationFormatError;
 47 import java.nio.ByteBuffer;
 48 import java.util.StringJoiner;
 49 
 50 /**
 51  * A {@code Method} provides information about, and access to, a single method
 52  * on a class or interface.  The reflected method may be a class method
 53  * or an instance method (including an abstract method).
 54  *
 55  * <p>A {@code Method} permits widening conversions to occur when matching the
 56  * actual parameters to invoke with the underlying method's formal
 57  * parameters, but it throws an {@code IllegalArgumentException} if a
 58  * narrowing conversion would occur.
 59  *
 60  * @see Member
 61  * @see java.lang.Class
 62  * @see java.lang.Class#getMethods()
 63  * @see java.lang.Class#getMethod(String, Class[])
 64  * @see java.lang.Class#getDeclaredMethods()
 65  * @see java.lang.Class#getDeclaredMethod(String, Class[])
 66  *
 67  * @author Kenneth Russell
 68  * @author Nakul Saraiya
 69  * @since 1.1
 70  */
 71 public final class Method extends Executable {
 72     private final Class<?>            clazz;
 73     private final int                 slot;
 74     // This is guaranteed to be interned by the VM in the 1.4
 75     // reflection implementation
 76     private final String              name;
 77     private final Class<?>            returnType;
 78     private final Class<?>[]          parameterTypes;
 79     private final Class<?>[]          exceptionTypes;
 80     private final int                 modifiers;
 81     // Generics and annotations support
 82     private final transient String    signature;
 83     // generic info repository; lazily initialized
 84     private transient MethodRepository genericInfo;
 85     private final byte[]              annotations;
 86     private final byte[]              parameterAnnotations;
 87     private final byte[]              annotationDefault;
 88     @Stable
 89     private MethodAccessor      methodAccessor;
 90     // For sharing of MethodAccessors. This branching structure is
 91     // currently only two levels deep (i.e., one root Method and
 92     // potentially many Method objects pointing to it.)
 93     //
 94     // If this branching structure would ever contain cycles, deadlocks can
 95     // occur in annotation code.
 96     private Method              root;
 97 
 98     // Generics infrastructure
 99     private String getGenericSignature() {return signature;}
100 
101     // Accessor for factory
102     private GenericsFactory getFactory() {
103         // create scope and factory
104         return CoreReflectionFactory.make(this, MethodScope.make(this));
105     }
106 
107     // Accessor for generic info repository
108     @Override
109     MethodRepository getGenericInfo() {
110         // lazily initialize repository if necessary
111         if (genericInfo == null) {
112             // create and cache generic info repository
113             genericInfo = MethodRepository.make(getGenericSignature(),
114                                                 getFactory());
115         }
116         return genericInfo; //return cached repository
117     }
118 
119     /**
120      * Package-private constructor
121      */
122     Method(Class<?> declaringClass,
123            String name,
124            Class<?>[] parameterTypes,
125            Class<?> returnType,
126            Class<?>[] checkedExceptions,
127            int modifiers,
128            int slot,
129            String signature,
130            byte[] annotations,
131            byte[] parameterAnnotations,
132            byte[] annotationDefault) {
133         this.clazz = declaringClass;
134         this.name = name;
135         this.parameterTypes = parameterTypes;
136         this.returnType = returnType;
137         this.exceptionTypes = checkedExceptions;
138         this.modifiers = modifiers;
139         this.slot = slot;
140         this.signature = signature;
141         this.annotations = annotations;
142         this.parameterAnnotations = parameterAnnotations;
143         this.annotationDefault = annotationDefault;
144     }
145 
146     /**
147      * Package-private routine (exposed to java.lang.Class via
148      * ReflectAccess) which returns a copy of this Method. The copy's
149      * "root" field points to this Method.
150      */
151     Method copy() {
152         // This routine enables sharing of MethodAccessor objects
153         // among Method objects which refer to the same underlying
154         // method in the VM. (All of this contortion is only necessary
155         // because of the "accessibility" bit in AccessibleObject,
156         // which implicitly requires that new java.lang.reflect
157         // objects be fabricated for each reflective call on Class
158         // objects.)
159         if (this.root != null)
160             throw new IllegalArgumentException("Can not copy a non-root Method");
161 
162         Method res = new Method(clazz, name, parameterTypes, returnType,
163                                 exceptionTypes, modifiers, slot, signature,
164                                 annotations, parameterAnnotations, annotationDefault);
165         res.root = this;
166         // Might as well eagerly propagate this if already present
167         res.methodAccessor = methodAccessor;
168         return res;
169     }
170 
171     /**
172      * Make a copy of a leaf method.
173      */
174     Method leafCopy() {
175         if (this.root == null)
176             throw new IllegalArgumentException("Can only leafCopy a non-root Method");
177 
178         Method res = new Method(clazz, name, parameterTypes, returnType,
179                 exceptionTypes, modifiers, slot, signature,
180                 annotations, parameterAnnotations, annotationDefault);
181         res.root = root;
182         res.methodAccessor = methodAccessor;
183         return res;
184     }
185 
186     /**
187      * @throws InaccessibleObjectException {@inheritDoc}
188      * @throws SecurityException {@inheritDoc}
189      */
190     @Override
191     @CallerSensitive
192     public void setAccessible(boolean flag) {
193         AccessibleObject.checkPermission();
194         if (flag) checkCanSetAccessible(Reflection.getCallerClass());
195         setAccessible0(flag);
196     }
197 
198     @Override
199     void checkCanSetAccessible(Class<?> caller) {
200         checkCanSetAccessible(caller, clazz);
201     }
202 
203     @Override
204     Method getRoot() {
205         return root;
206     }
207 
208     @Override
209     boolean hasGenericInformation() {
210         return (getGenericSignature() != null);
211     }
212 
213     @Override
214     byte[] getAnnotationBytes() {
215         return annotations;
216     }
217 
218     /**
219      * Returns the {@code Class} object representing the class or interface
220      * that declares the method represented by this object.
221      */
222     @Override
223     public Class<?> getDeclaringClass() {
224         return clazz;
225     }
226 
227     /**
228      * Returns the name of the method represented by this {@code Method}
229      * object, as a {@code String}.
230      */
231     @Override
232     public String getName() {
233         return name;
234     }
235 
236     /**
237      * {@inheritDoc}
238      * @jls 8.4.3 Method Modifiers
239      */
240     @Override
241     public int getModifiers() {
242         return modifiers;
243     }
244 
245     /**
246      * {@inheritDoc}
247      * @throws GenericSignatureFormatError {@inheritDoc}
248      * @since 1.5
249      * @jls 8.4.4 Generic Methods
250      */
251     @Override
252     @SuppressWarnings({"rawtypes", "unchecked"})
253     public TypeVariable<Method>[] getTypeParameters() {
254         if (getGenericSignature() != null)
255             return (TypeVariable<Method>[])getGenericInfo().getTypeParameters();
256         else
257             return (TypeVariable<Method>[])new TypeVariable[0];
258     }
259 
260     /**
261      * Returns a {@code Class} object that represents the formal return type
262      * of the method represented by this {@code Method} object.
263      *
264      * @return the return type for the method this object represents
265      */
266     public Class<?> getReturnType() {
267         return returnType;
268     }
269 
270     /**
271      * Returns a {@code Type} object that represents the formal return
272      * type of the method represented by this {@code Method} object.
273      *
274      * <p>If the return type is a parameterized type,
275      * the {@code Type} object returned must accurately reflect
276      * the actual type arguments used in the source code.
277      *
278      * <p>If the return type is a type variable or a parameterized type, it
279      * is created. Otherwise, it is resolved.
280      *
281      * @return  a {@code Type} object that represents the formal return
282      *     type of the underlying  method
283      * @throws GenericSignatureFormatError
284      *     if the generic method signature does not conform to the format
285      *     specified in
286      *     <cite>The Java Virtual Machine Specification</cite>
287      * @throws TypeNotPresentException if the underlying method's
288      *     return type refers to a non-existent class or interface declaration
289      * @throws MalformedParameterizedTypeException if the
290      *     underlying method's return type refers to a parameterized
291      *     type that cannot be instantiated for any reason
292      * @since 1.5
293      */
294     public Type getGenericReturnType() {
295       if (getGenericSignature() != null) {
296         return getGenericInfo().getReturnType();
297       } else { return getReturnType();}
298     }
299 
300     @Override
301     Class<?>[] getSharedParameterTypes() {
302         return parameterTypes;
303     }
304 
305     @Override
306     Class<?>[] getSharedExceptionTypes() {
307         return exceptionTypes;
308     }
309 
310     /**
311      * {@inheritDoc}
312      */
313     @Override
314     public Class<?>[] getParameterTypes() {
315         return parameterTypes.clone();
316     }
317 
318     /**
319      * {@inheritDoc}
320      * @since 1.8
321      */
322     public int getParameterCount() { return parameterTypes.length; }
323 
324 
325     /**
326      * {@inheritDoc}
327      * @throws GenericSignatureFormatError {@inheritDoc}
328      * @throws TypeNotPresentException {@inheritDoc}
329      * @throws MalformedParameterizedTypeException {@inheritDoc}
330      * @since 1.5
331      */
332     @Override
333     public Type[] getGenericParameterTypes() {
334         return super.getGenericParameterTypes();
335     }
336 
337     /**
338      * {@inheritDoc}
339      */
340     @Override
341     public Class<?>[] getExceptionTypes() {
342         return exceptionTypes.clone();
343     }
344 
345     /**
346      * {@inheritDoc}
347      * @throws GenericSignatureFormatError {@inheritDoc}
348      * @throws TypeNotPresentException {@inheritDoc}
349      * @throws MalformedParameterizedTypeException {@inheritDoc}
350      * @since 1.5
351      */
352     @Override
353     public Type[] getGenericExceptionTypes() {
354         return super.getGenericExceptionTypes();
355     }
356 
357     /**
358      * Compares this {@code Method} against the specified object.  Returns
359      * true if the objects are the same.  Two {@code Methods} are the same if
360      * they were declared by the same class and have the same name
361      * and formal parameter types and return type.
362      */
363     public boolean equals(Object obj) {
364         if (obj instanceof Method other) {
365             if ((getDeclaringClass() == other.getDeclaringClass())
366                 && (getName() == other.getName())) {
367                 if (!returnType.equals(other.getReturnType()))
368                     return false;
369                 return equalParamTypes(parameterTypes, other.parameterTypes);
370             }
371         }
372         return false;
373     }
374 
375     /**
376      * Returns a hashcode for this {@code Method}.  The hashcode is computed
377      * as the exclusive-or of the hashcodes for the underlying
378      * method's declaring class name and the method's name.
379      */
380     public int hashCode() {
381         return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
382     }
383 
384     /**
385      * Returns a string describing this {@code Method}.  The string is
386      * formatted as the method access modifiers, if any, followed by
387      * the method return type, followed by a space, followed by the
388      * class declaring the method, followed by a period, followed by
389      * the method name, followed by a parenthesized, comma-separated
390      * list of the method's formal parameter types. If the method
391      * throws checked exceptions, the parameter list is followed by a
392      * space, followed by the word "{@code throws}" followed by a
393      * comma-separated list of the thrown exception types.
394      * For example:
395      * <pre>
396      *    public boolean java.lang.Object.equals(java.lang.Object)
397      * </pre>
398      *
399      * <p>The access modifiers are placed in canonical order as
400      * specified by "The Java Language Specification".  This is
401      * {@code public}, {@code protected} or {@code private} first,
402      * and then other modifiers in the following order:
403      * {@code abstract}, {@code default}, {@code static}, {@code final},
404      * {@code synchronized}, {@code native}, {@code strictfp}.
405      *
406      * @return a string describing this {@code Method}
407      *
408      * @jls 8.4.3 Method Modifiers
409      * @jls 9.4 Method Declarations
410      * @jls 9.6.1 Annotation Interface Elements
411      */
412     public String toString() {
413         return sharedToString(Modifier.methodModifiers(),
414                               isDefault(),
415                               parameterTypes,
416                               exceptionTypes);
417     }
418 
419     @Override
420     void specificToStringHeader(StringBuilder sb) {
421         sb.append(getReturnType().getTypeName()).append(' ');
422         sb.append(getDeclaringClass().getTypeName()).append('.');
423         sb.append(getName());
424     }
425 
426     @Override
427     String toShortString() {
428         return "method " + getDeclaringClass().getTypeName() +
429                 '.' + toShortSignature();
430     }
431 
432     String toShortSignature() {
433         StringJoiner sj = new StringJoiner(",", getName() + "(", ")");
434         for (Class<?> parameterType : getSharedParameterTypes()) {
435             sj.add(parameterType.getTypeName());
436         }
437         return sj.toString();
438     }
439 
440     /**
441      * Returns a string describing this {@code Method}, including type
442      * parameters.  The string is formatted as the method access
443      * modifiers, if any, followed by an angle-bracketed
444      * comma-separated list of the method's type parameters, if any,
445      * including informative bounds of the type parameters, if any,
446      * followed by the method's generic return type, followed by a
447      * space, followed by the class declaring the method, followed by
448      * a period, followed by the method name, followed by a
449      * parenthesized, comma-separated list of the method's generic
450      * formal parameter types.
451      *
452      * If this method was declared to take a variable number of
453      * arguments, instead of denoting the last parameter as
454      * "<code><i>Type</i>[]</code>", it is denoted as
455      * "<code><i>Type</i>...</code>".
456      *
457      * A space is used to separate access modifiers from one another
458      * and from the type parameters or return type.  If there are no
459      * type parameters, the type parameter list is elided; if the type
460      * parameter list is present, a space separates the list from the
461      * class name.  If the method is declared to throw exceptions, the
462      * parameter list is followed by a space, followed by the word
463      * "{@code throws}" followed by a comma-separated list of the generic
464      * thrown exception types.
465      *
466      * <p>The access modifiers are placed in canonical order as
467      * specified by "The Java Language Specification".  This is
468      * {@code public}, {@code protected} or {@code private} first,
469      * and then other modifiers in the following order:
470      * {@code abstract}, {@code default}, {@code static}, {@code final},
471      * {@code synchronized}, {@code native}, {@code strictfp}.
472      *
473      * @return a string describing this {@code Method},
474      * include type parameters
475      *
476      * @since 1.5
477      *
478      * @jls 8.4.3 Method Modifiers
479      * @jls 9.4 Method Declarations
480      * @jls 9.6.1 Annotation Interface Elements
481      */
482     @Override
483     public String toGenericString() {
484         return sharedToGenericString(Modifier.methodModifiers(), isDefault());
485     }
486 
487     @Override
488     void specificToGenericStringHeader(StringBuilder sb) {
489         Type genRetType = getGenericReturnType();
490         sb.append(genRetType.getTypeName()).append(' ');
491         sb.append(getDeclaringClass().getTypeName()).append('.');
492         sb.append(getName());
493     }
494 
495     /**
496      * Invokes the underlying method represented by this {@code Method}
497      * object, on the specified object with the specified parameters.
498      * Individual parameters are automatically unwrapped to match
499      * primitive formal parameters, and both primitive and reference
500      * parameters are subject to method invocation conversions as
501      * necessary.
502      *
503      * <p>If the underlying method is static, then the specified {@code obj}
504      * argument is ignored. It may be null.
505      *
506      * <p>If the number of formal parameters required by the underlying method is
507      * 0, the supplied {@code args} array may be of length 0 or null.
508      *
509      * <p>If the underlying method is an instance method, it is invoked
510      * using dynamic method lookup as documented in The Java Language
511      * Specification, section {@jls 15.12.4.4}; in particular,
512      * overriding based on the runtime type of the target object may occur.
513      *
514      * <p>If the underlying method is static, the class that declared
515      * the method is initialized if it has not already been initialized.
516      *
517      * <p>If the method completes normally, the value it returns is
518      * returned to the caller of invoke; if the value has a primitive
519      * type, it is first appropriately wrapped in an object. However,
520      * if the value has the type of an array of a primitive type, the
521      * elements of the array are <i>not</i> wrapped in objects; in
522      * other words, an array of primitive type is returned.  If the
523      * underlying method return type is void, the invocation returns
524      * null.
525      *
526      * @param obj  the object the underlying method is invoked from
527      * @param args the arguments used for the method call
528      * @return the result of dispatching the method represented by
529      * this object on {@code obj} with parameters
530      * {@code args}
531      *
532      * @throws    IllegalAccessException    if this {@code Method} object
533      *              is enforcing Java language access control and the underlying
534      *              method is inaccessible.
535      * @throws    IllegalArgumentException  if the method is an
536      *              instance method and the specified object argument
537      *              is not an instance of the class or interface
538      *              declaring the underlying method (or of a subclass
539      *              or implementor thereof); if the number of actual
540      *              and formal parameters differ; if an unwrapping
541      *              conversion for primitive arguments fails; or if,
542      *              after possible unwrapping, a parameter value
543      *              cannot be converted to the corresponding formal
544      *              parameter type by a method invocation conversion.
545      * @throws    InvocationTargetException if the underlying method
546      *              throws an exception.
547      * @throws    NullPointerException      if the specified object is null
548      *              and the method is an instance method.
549      * @throws    ExceptionInInitializerError if the initialization
550      * provoked by this method fails.
551      */
552     @CallerSensitive
553     @ForceInline // to ensure Reflection.getCallerClass optimization
554     @IntrinsicCandidate
555     public Object invoke(Object obj, Object... args)
556         throws IllegalAccessException, InvocationTargetException
557     {
558         boolean callerSensitive = isCallerSensitive();
559         Class<?> caller = null;
560         if (!override || callerSensitive) {
561             caller = Reflection.getCallerClass();
562         }
563 
564         // Reflection::getCallerClass filters all subclasses of
565         // jdk.internal.reflect.MethodAccessorImpl and Method::invoke(Object, Object[])
566         // Should not call Method::invoke(Object, Object[], Class) here
567         if (!override) {
568             checkAccess(caller, clazz,
569                     Modifier.isStatic(modifiers) ? null : obj.getClass(),
570                     modifiers);
571         }
572         MethodAccessor ma = methodAccessor;             // read @Stable
573         if (ma == null) {
574             ma = acquireMethodAccessor();
575         }
576 
577         return callerSensitive ? ma.invoke(obj, args, caller) : ma.invoke(obj, args);
578     }
579 
580     /**
581      * This is to support MethodHandle calling caller-sensitive Method::invoke
582      * that may invoke a caller-sensitive method in order to get the original caller
583      * class (not the injected invoker).
584      *
585      * If this adapter is not presented, MethodHandle invoking Method::invoke
586      * will get an invoker class, a hidden nestmate of the original caller class,
587      * that becomes the caller class invoking Method::invoke.
588      */
589     @CallerSensitiveAdapter
590     private Object invoke(Object obj, Object[] args, Class<?> caller)
591             throws IllegalAccessException, InvocationTargetException
592     {
593         boolean callerSensitive = isCallerSensitive();
594         if (!override) {
595             checkAccess(caller, clazz,
596                         Modifier.isStatic(modifiers) ? null : obj.getClass(),
597                         modifiers);
598         }
599         MethodAccessor ma = methodAccessor;             // read @Stable
600         if (ma == null) {
601             ma = acquireMethodAccessor();
602         }
603 
604         return callerSensitive ? ma.invoke(obj, args, caller) : ma.invoke(obj, args);
605     }
606 
607     @Stable private Boolean callerSensitive;       // lazily initialize
608     private boolean isCallerSensitive() {
609         Boolean cs = callerSensitive;
610         if (cs == null) {
611             callerSensitive = cs = Reflection.isCallerSensitive(this);
612         }
613         return cs;
614     }
615 
616     /**
617      * {@return {@code true} if this method is a bridge
618      * method; returns {@code false} otherwise}
619      *
620      * @apiNote
621      * A bridge method is a {@linkplain isSynthetic synthetic} method
622      * created by a Java compiler alongside a method originating from
623      * the source code. Bridge methods are used by Java compilers in
624      * various circumstances to span differences in Java programming
625      * language semantics and JVM semantics.
626      *
627      * <p>One example use of bridge methods is as a technique for a
628      * Java compiler to support <i>covariant overrides</i>, where a
629      * subclass overrides a method and gives the new method a more
630      * specific return type than the method in the superclass.  While
631      * the Java language specification forbids a class declaring two
632      * methods with the same parameter types but a different return
633      * type, the virtual machine does not. A common case where
634      * covariant overrides are used is for a {@link
635      * java.lang.Cloneable Cloneable} class where the {@link
636      * Object#clone() clone} method inherited from {@code
637      * java.lang.Object} is overridden and declared to return the type
638      * of the class. For example, {@code Object} declares
639      * <pre>{@code protected Object clone() throws CloneNotSupportedException {...}}</pre>
640      * and {@code EnumSet<E>} declares its language-level {@linkplain
641      * java.util.EnumSet#clone() covariant override}
642      * <pre>{@code public EnumSet<E> clone() {...}}</pre>
643      * If this technique was being used, the resulting class file for
644      * {@code EnumSet} would have two {@code clone} methods, one
645      * returning {@code EnumSet<E>} and the second a bridge method
646      * returning {@code Object}. The bridge method is a JVM-level
647      * override of {@code Object.clone()}.  The body of the {@code
648      * clone} bridge method calls its non-bridge counterpart and
649      * returns its result.
650      * @since 1.5
651      *
652      * @jls 8.4.8.3 Requirements in Overriding and Hiding
653      * @jls 15.12.4.5 Create Frame, Synchronize, Transfer Control
654      * @jvms 4.6 Methods
655      * @see <a
656      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
657      * programming language and JVM modeling in core reflection</a>
658      */
659     public boolean isBridge() {
660         return (getModifiers() & Modifier.BRIDGE) != 0;
661     }
662 
663     /**
664      * {@inheritDoc}
665      * @since 1.5
666      * @jls 8.4.1 Formal Parameters
667      */
668     @Override
669     public boolean isVarArgs() {
670         return super.isVarArgs();
671     }
672 
673     /**
674      * {@inheritDoc}
675      * @jls 13.1 The Form of a Binary
676      * @jvms 4.6 Methods
677      * @see <a
678      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
679      * programming language and JVM modeling in core reflection</a>
680      * @since 1.5
681      */
682     @Override
683     public boolean isSynthetic() {
684         return super.isSynthetic();
685     }
686 
687     /**
688      * Returns {@code true} if this method is a default
689      * method; returns {@code false} otherwise.
690      *
691      * A default method is a public non-abstract instance method, that
692      * is, a non-static method with a body, declared in an interface.
693      *
694      * @return true if and only if this method is a default
695      * method as defined by the Java Language Specification.
696      * @since 1.8
697      * @jls 9.4 Method Declarations
698      */
699     public boolean isDefault() {
700         // Default methods are public non-abstract instance methods
701         // declared in an interface.
702         return ((getModifiers() & (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) ==
703                 Modifier.PUBLIC) && getDeclaringClass().isInterface();
704     }
705 
706     // NOTE that there is no synchronization used here. It is correct
707     // (though not efficient) to generate more than one MethodAccessor
708     // for a given Method. However, avoiding synchronization will
709     // probably make the implementation more scalable.
710     private MethodAccessor acquireMethodAccessor() {
711         // First check to see if one has been created yet, and take it
712         // if so
713         Method root = this.root;
714         MethodAccessor tmp = root == null ? null : root.getMethodAccessor();
715         if (tmp != null) {
716             methodAccessor = tmp;
717         } else {
718             // Otherwise fabricate one and propagate it up to the root
719             tmp = reflectionFactory.newMethodAccessor(this, isCallerSensitive());
720             // set the method accessor only if it's not using native implementation
721             if (VM.isJavaLangInvokeInited())
722                 setMethodAccessor(tmp);
723         }
724 
725         return tmp;
726     }
727 
728     // Returns MethodAccessor for this Method object, not looking up
729     // the chain to the root
730     MethodAccessor getMethodAccessor() {
731         return methodAccessor;
732     }
733 
734     // Sets the MethodAccessor for this Method object and
735     // (recursively) its root
736     void setMethodAccessor(MethodAccessor accessor) {
737         methodAccessor = accessor;
738         // Propagate up
739         Method root = this.root;
740         if (root != null) {
741             root.setMethodAccessor(accessor);
742         }
743     }
744 
745     /**
746      * Returns the default value for the annotation member represented by
747      * this {@code Method} instance.  If the member is of a primitive type,
748      * an instance of the corresponding wrapper type is returned. Returns
749      * null if no default is associated with the member, or if the method
750      * instance does not represent a declared member of an annotation type.
751      *
752      * @return the default value for the annotation member represented
753      *     by this {@code Method} instance.
754      * @throws TypeNotPresentException if the annotation is of type
755      *     {@link Class} and no definition can be found for the
756      *     default class value.
757      * @since  1.5
758      * @jls 9.6.2 Defaults for Annotation Type Elements
759      */
760     public Object getDefaultValue() {
761         if  (annotationDefault == null)
762             return null;
763         Class<?> memberType = AnnotationType.invocationHandlerReturnType(
764             getReturnType());
765         Object result = AnnotationParser.parseMemberValue(
766             memberType, ByteBuffer.wrap(annotationDefault),
767             SharedSecrets.getJavaLangAccess().
768                 getConstantPool(getDeclaringClass()),
769             getDeclaringClass());
770         if (result instanceof ExceptionProxy) {
771             if (result instanceof TypeNotPresentExceptionProxy proxy) {
772                 throw new TypeNotPresentException(proxy.typeName(), proxy.getCause());
773             }
774             throw new AnnotationFormatError("Invalid default: " + this);
775         }
776         return result;
777     }
778 
779     /**
780      * {@inheritDoc}
781      * @throws NullPointerException {@inheritDoc}
782      * @since 1.5
783      */
784     @Override
785     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
786         return super.getAnnotation(annotationClass);
787     }
788 
789     /**
790      * {@inheritDoc}
791      * @since 1.5
792      */
793     @Override
794     public Annotation[] getDeclaredAnnotations()  {
795         return super.getDeclaredAnnotations();
796     }
797 
798     /**
799      * {@inheritDoc}
800      * @since 1.5
801      */
802     @Override
803     public Annotation[][] getParameterAnnotations() {
804         return sharedGetParameterAnnotations(parameterTypes, parameterAnnotations);
805     }
806 
807     /**
808      * {@inheritDoc}
809      * @since 1.8
810      */
811     @Override
812     public AnnotatedType getAnnotatedReturnType() {
813         return getAnnotatedReturnType0(getGenericReturnType());
814     }
815 
816     @Override
817     boolean handleParameterNumberMismatch(int resultLength, Class<?>[] parameterTypes) {
818         throw new AnnotationFormatError("Parameter annotations don't match number of parameters");
819     }
820 }