jdk/src/java.base/share/classes/java/lang/reflect/Method.java
author plevart
Tue, 18 Oct 2016 20:28:58 +0200
changeset 41560 a66e7ee16cf9
parent 40175 8df87018d96a
child 42228 dd6cc832ffd4
permissions -rw-r--r--
6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
38882
efb06b82374b 5041778: (ann) AnnotationFormatError if "default" Class type not found
darcy
parents: 37363
diff changeset
     2
 * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang.reflect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29125
diff changeset
    28
import jdk.internal.HotSpotIntrinsicCandidate;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32268
diff changeset
    29
import jdk.internal.misc.SharedSecrets;
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
    30
import jdk.internal.reflect.CallerSensitive;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
    31
import jdk.internal.reflect.MethodAccessor;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
    32
import jdk.internal.reflect.Reflection;
40175
8df87018d96a 8161379: Force inline methods calling Reflection.getCallerClass
redestad
parents: 38882
diff changeset
    33
import jdk.internal.vm.annotation.ForceInline;
38882
efb06b82374b 5041778: (ann) AnnotationFormatError if "default" Class type not found
darcy
parents: 37363
diff changeset
    34
import sun.reflect.annotation.ExceptionProxy;
efb06b82374b 5041778: (ann) AnnotationFormatError if "default" Class type not found
darcy
parents: 37363
diff changeset
    35
import sun.reflect.annotation.TypeNotPresentExceptionProxy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.reflect.generics.repository.MethodRepository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.reflect.generics.factory.CoreReflectionFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.reflect.generics.factory.GenericsFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.reflect.generics.scope.MethodScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.reflect.annotation.AnnotationType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.reflect.annotation.AnnotationParser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.lang.annotation.Annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.annotation.AnnotationFormatError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * A {@code Method} provides information about, and access to, a single method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * on a class or interface.  The reflected method may be a class method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * or an instance method (including an abstract method).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>A {@code Method} permits widening conversions to occur when matching the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * actual parameters to invoke with the underlying method's formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * parameters, but it throws an {@code IllegalArgumentException} if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * narrowing conversion would occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see Member
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see java.lang.Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see java.lang.Class#getMethods()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see java.lang.Class#getMethod(String, Class[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see java.lang.Class#getDeclaredMethods()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see java.lang.Class#getDeclaredMethod(String, Class[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author Kenneth Russell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @author Nakul Saraiya
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
    66
public final class Method extends Executable {
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
    67
    private Class<?>            clazz;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private int                 slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // This is guaranteed to be interned by the VM in the 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // reflection implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private String              name;
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
    72
    private Class<?>            returnType;
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
    73
    private Class<?>[]          parameterTypes;
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
    74
    private Class<?>[]          exceptionTypes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private int                 modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Generics and annotations support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private transient String              signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // generic info repository; lazily initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private transient MethodRepository genericInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private byte[]              annotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private byte[]              parameterAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private byte[]              annotationDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private volatile MethodAccessor methodAccessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // For sharing of MethodAccessors. This branching structure is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // currently only two levels deep (i.e., one root Method and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // potentially many Method objects pointing to it.)
26455
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents: 25859
diff changeset
    87
    //
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents: 25859
diff changeset
    88
    // If this branching structure would ever contain cycles, deadlocks can
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents: 25859
diff changeset
    89
    // occur in annotation code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private Method              root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
    92
    // Generics infrastructure
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private String getGenericSignature() {return signature;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // Accessor for factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private GenericsFactory getFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // create scope and factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return CoreReflectionFactory.make(this, MethodScope.make(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // Accessor for generic info repository
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   102
    @Override
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   103
    MethodRepository getGenericInfo() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // lazily initialize repository if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (genericInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            // create and cache generic info repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            genericInfo = MethodRepository.make(getGenericSignature(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                                getFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return genericInfo; //return cached repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Package-private constructor used by ReflectAccess to enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * instantiation of these objects in Java code from the java.lang
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * package via sun.reflect.LangReflectAccess.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   118
    Method(Class<?> declaringClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
           String name,
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   120
           Class<?>[] parameterTypes,
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   121
           Class<?> returnType,
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   122
           Class<?>[] checkedExceptions,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
           int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
           int slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
           String signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
           byte[] annotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
           byte[] parameterAnnotations,
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   128
           byte[] annotationDefault) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this.clazz = declaringClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.parameterTypes = parameterTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.returnType = returnType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.exceptionTypes = checkedExceptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        this.modifiers = modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        this.slot = slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.signature = signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        this.annotations = annotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.parameterAnnotations = parameterAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.annotationDefault = annotationDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Package-private routine (exposed to java.lang.Class via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * ReflectAccess) which returns a copy of this Method. The copy's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * "root" field points to this Method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    Method copy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // This routine enables sharing of MethodAccessor objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // among Method objects which refer to the same underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // method in the VM. (All of this contortion is only necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // because of the "accessibility" bit in AccessibleObject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // which implicitly requires that new java.lang.reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // objects be fabricated for each reflective call on Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // objects.)
26455
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents: 25859
diff changeset
   155
        if (this.root != null)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents: 25859
diff changeset
   156
            throw new IllegalArgumentException("Can not copy a non-root Method");
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents: 25859
diff changeset
   157
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        Method res = new Method(clazz, name, parameterTypes, returnType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                exceptionTypes, modifiers, slot, signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                annotations, parameterAnnotations, annotationDefault);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        res.root = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // Might as well eagerly propagate this if already present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        res.methodAccessor = methodAccessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
26455
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents: 25859
diff changeset
   167
    /**
32268
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   168
     * Make a copy of a leaf method.
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   169
     */
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   170
    Method leafCopy() {
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   171
        if (this.root == null)
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   172
            throw new IllegalArgumentException("Can only leafCopy a non-root Method");
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   173
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   174
        Method res = new Method(clazz, name, parameterTypes, returnType,
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   175
                exceptionTypes, modifiers, slot, signature,
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   176
                annotations, parameterAnnotations, annotationDefault);
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   177
        res.root = root;
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   178
        res.methodAccessor = methodAccessor;
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   179
        return res;
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   180
    }
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   181
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   182
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   183
    @CallerSensitive
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   184
    public void setAccessible(boolean flag) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   185
        AccessibleObject.checkPermission();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   186
        if (flag) checkCanSetAccessible(Reflection.getCallerClass());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   187
        setAccessible0(flag);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   188
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   189
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   190
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   191
    void checkCanSetAccessible(Class<?> caller) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   192
        checkCanSetAccessible(caller, clazz);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   193
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   194
32268
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 32033
diff changeset
   195
    /**
26455
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents: 25859
diff changeset
   196
     * Used by Excecutable for annotation sharing.
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents: 25859
diff changeset
   197
     */
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents: 25859
diff changeset
   198
    @Override
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents: 25859
diff changeset
   199
    Executable getRoot() {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents: 25859
diff changeset
   200
        return root;
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents: 25859
diff changeset
   201
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents: 25859
diff changeset
   202
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   203
    @Override
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   204
    boolean hasGenericInformation() {
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   205
        return (getGenericSignature() != null);
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   206
    }
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   207
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   208
    @Override
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   209
    byte[] getAnnotationBytes() {
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   210
        return annotations;
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   211
    }
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   212
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   214
     * {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   216
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public Class<?> getDeclaringClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Returns the name of the method represented by this {@code Method}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * object, as a {@code String}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   225
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   231
     * {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   233
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public int getModifiers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   239
     * {@inheritDoc}
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   240
     * @throws GenericSignatureFormatError {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   243
    @Override
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 10342
diff changeset
   244
    @SuppressWarnings({"rawtypes", "unchecked"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public TypeVariable<Method>[] getTypeParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (getGenericSignature() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return (TypeVariable<Method>[])getGenericInfo().getTypeParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return (TypeVariable<Method>[])new TypeVariable[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Returns a {@code Class} object that represents the formal return type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * of the method represented by this {@code Method} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @return the return type for the method this object represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public Class<?> getReturnType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return returnType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Returns a {@code Type} object that represents the formal return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * type of the method represented by this {@code Method} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * <p>If the return type is a parameterized type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * the {@code Type} object returned must accurately reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * the actual type parameters used in the source code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <p>If the return type is a type variable or a parameterized type, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * is created. Otherwise, it is resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return  a {@code Type} object that represents the formal return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *     type of the underlying  method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @throws GenericSignatureFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *     if the generic method signature does not conform to the format
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 9029
diff changeset
   277
     *     specified in
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 9029
diff changeset
   278
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @throws TypeNotPresentException if the underlying method's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *     return type refers to a non-existent type declaration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @throws MalformedParameterizedTypeException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *     underlying method's return typed refers to a parameterized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *     type that cannot be instantiated for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public Type getGenericReturnType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
      if (getGenericSignature() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return getGenericInfo().getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
      } else { return getReturnType();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   293
     * {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   295
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public Class<?>[] getParameterTypes() {
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 10127
diff changeset
   297
        return parameterTypes.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   301
     * {@inheritDoc}
22083
1ac4a52255f4 8030785: Missing "since 1.8" javadoc for java.lang.reflect.Method:getParameterCount
darcy
parents: 20482
diff changeset
   302
     * @since 1.8
15294
df55735ea03c 8004729: Add java.lang.reflect.Parameter and related changes for parameter reflection
robm
parents: 14910
diff changeset
   303
     */
df55735ea03c 8004729: Add java.lang.reflect.Parameter and related changes for parameter reflection
robm
parents: 14910
diff changeset
   304
    public int getParameterCount() { return parameterTypes.length; }
df55735ea03c 8004729: Add java.lang.reflect.Parameter and related changes for parameter reflection
robm
parents: 14910
diff changeset
   305
df55735ea03c 8004729: Add java.lang.reflect.Parameter and related changes for parameter reflection
robm
parents: 14910
diff changeset
   306
df55735ea03c 8004729: Add java.lang.reflect.Parameter and related changes for parameter reflection
robm
parents: 14910
diff changeset
   307
    /**
df55735ea03c 8004729: Add java.lang.reflect.Parameter and related changes for parameter reflection
robm
parents: 14910
diff changeset
   308
     * {@inheritDoc}
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   309
     * @throws GenericSignatureFormatError {@inheritDoc}
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   310
     * @throws TypeNotPresentException {@inheritDoc}
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   311
     * @throws MalformedParameterizedTypeException {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   314
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public Type[] getGenericParameterTypes() {
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   316
        return super.getGenericParameterTypes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   320
     * {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   322
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public Class<?>[] getExceptionTypes() {
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 10127
diff changeset
   324
        return exceptionTypes.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   328
     * {@inheritDoc}
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   329
     * @throws GenericSignatureFormatError {@inheritDoc}
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   330
     * @throws TypeNotPresentException {@inheritDoc}
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   331
     * @throws MalformedParameterizedTypeException {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   334
    @Override
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   335
    public Type[] getGenericExceptionTypes() {
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   336
        return super.getGenericExceptionTypes();
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   337
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Compares this {@code Method} against the specified object.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * true if the objects are the same.  Two {@code Methods} are the same if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * they were declared by the same class and have the same name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * and formal parameter types and return type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (obj != null && obj instanceof Method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            Method other = (Method)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if ((getDeclaringClass() == other.getDeclaringClass())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                && (getName() == other.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                if (!returnType.equals(other.getReturnType()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    return false;
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   352
                return equalParamTypes(parameterTypes, other.parameterTypes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Returns a hashcode for this {@code Method}.  The hashcode is computed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * as the exclusive-or of the hashcodes for the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * method's declaring class name and the method's name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Returns a string describing this {@code Method}.  The string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * formatted as the method access modifiers, if any, followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * the method return type, followed by a space, followed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * class declaring the method, followed by a period, followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * the method name, followed by a parenthesized, comma-separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * list of the method's formal parameter types. If the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * throws checked exceptions, the parameter list is followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * space, followed by the word throws followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * comma-separated list of the thrown exception types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *    public boolean java.lang.Object.equals(java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <p>The access modifiers are placed in canonical order as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * specified by "The Java Language Specification".  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * {@code public}, {@code protected} or {@code private} first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * and then other modifiers in the following order:
16743
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16729
diff changeset
   386
     * {@code abstract}, {@code default}, {@code static}, {@code final},
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16729
diff changeset
   387
     * {@code synchronized}, {@code native}, {@code strictfp}.
16729
3b26e313ad81 8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents: 16496
diff changeset
   388
     *
3b26e313ad81 8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents: 16496
diff changeset
   389
     * @return a string describing this {@code Method}
3b26e313ad81 8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents: 16496
diff changeset
   390
     *
3b26e313ad81 8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents: 16496
diff changeset
   391
     * @jls 8.4.3 Method Modifiers
29125
83b9bf8a6c2a 8073952: Spec of j.l.r.Method.toString/toGenericString need to be clarified
darcy
parents: 26455
diff changeset
   392
     * @jls 9.4   Method Declarations
83b9bf8a6c2a 8073952: Spec of j.l.r.Method.toString/toGenericString need to be clarified
darcy
parents: 26455
diff changeset
   393
     * @jls 9.6.1 Annotation Type Elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public String toString() {
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   396
        return sharedToString(Modifier.methodModifiers(),
16729
3b26e313ad81 8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents: 16496
diff changeset
   397
                              isDefault(),
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   398
                              parameterTypes,
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   399
                              exceptionTypes);
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   400
    }
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   401
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   402
    @Override
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   403
    void specificToStringHeader(StringBuilder sb) {
16743
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16729
diff changeset
   404
        sb.append(getReturnType().getTypeName()).append(' ');
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16729
diff changeset
   405
        sb.append(getDeclaringClass().getTypeName()).append('.');
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   406
        sb.append(getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Returns a string describing this {@code Method}, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * type parameters.  The string is formatted as the method access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * modifiers, if any, followed by an angle-bracketed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * comma-separated list of the method's type parameters, if any,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * followed by the method's generic return type, followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * space, followed by the class declaring the method, followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * a period, followed by the method name, followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * parenthesized, comma-separated list of the method's generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * formal parameter types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * If this method was declared to take a variable number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * arguments, instead of denoting the last parameter as
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31671
diff changeset
   422
     * "<code><i>Type</i>[]</code>", it is denoted as
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31671
diff changeset
   423
     * "<code><i>Type</i>...</code>".
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * A space is used to separate access modifiers from one another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * and from the type parameters or return type.  If there are no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * type parameters, the type parameter list is elided; if the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * parameter list is present, a space separates the list from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * class name.  If the method is declared to throw exceptions, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * parameter list is followed by a space, followed by the word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * throws followed by a comma-separated list of the generic thrown
16743
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16729
diff changeset
   432
     * exception types.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * <p>The access modifiers are placed in canonical order as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * specified by "The Java Language Specification".  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * {@code public}, {@code protected} or {@code private} first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * and then other modifiers in the following order:
16743
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16729
diff changeset
   438
     * {@code abstract}, {@code default}, {@code static}, {@code final},
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16729
diff changeset
   439
     * {@code synchronized}, {@code native}, {@code strictfp}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @return a string describing this {@code Method},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * include type parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @since 1.5
16729
3b26e313ad81 8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents: 16496
diff changeset
   445
     *
3b26e313ad81 8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents: 16496
diff changeset
   446
     * @jls 8.4.3 Method Modifiers
29125
83b9bf8a6c2a 8073952: Spec of j.l.r.Method.toString/toGenericString need to be clarified
darcy
parents: 26455
diff changeset
   447
     * @jls 9.4   Method Declarations
83b9bf8a6c2a 8073952: Spec of j.l.r.Method.toString/toGenericString need to be clarified
darcy
parents: 26455
diff changeset
   448
     * @jls 9.6.1 Annotation Type Elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   450
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public String toGenericString() {
16729
3b26e313ad81 8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents: 16496
diff changeset
   452
        return sharedToGenericString(Modifier.methodModifiers(), isDefault());
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   453
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   455
    @Override
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   456
    void specificToGenericStringHeader(StringBuilder sb) {
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   457
        Type genRetType = getGenericReturnType();
16743
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16729
diff changeset
   458
        sb.append(genRetType.getTypeName()).append(' ');
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16729
diff changeset
   459
        sb.append(getDeclaringClass().getTypeName()).append('.');
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   460
        sb.append(getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Invokes the underlying method represented by this {@code Method}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * object, on the specified object with the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Individual parameters are automatically unwrapped to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * primitive formal parameters, and both primitive and reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * parameters are subject to method invocation conversions as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * <p>If the underlying method is static, then the specified {@code obj}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * argument is ignored. It may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <p>If the number of formal parameters required by the underlying method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * 0, the supplied {@code args} array may be of length 0 or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * <p>If the underlying method is an instance method, it is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * using dynamic method lookup as documented in The Java Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Specification, Second Edition, section 15.12.4.4; in particular,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * overriding based on the runtime type of the target object will occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * <p>If the underlying method is static, the class that declared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * the method is initialized if it has not already been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * <p>If the method completes normally, the value it returns is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * returned to the caller of invoke; if the value has a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * type, it is first appropriately wrapped in an object. However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * if the value has the type of an array of a primitive type, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * elements of the array are <i>not</i> wrapped in objects; in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * other words, an array of primitive type is returned.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * underlying method return type is void, the invocation returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @param obj  the object the underlying method is invoked from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @param args the arguments used for the method call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @return the result of dispatching the method represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * this object on {@code obj} with parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * {@code args}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @exception IllegalAccessException    if this {@code Method} object
9022
b2e8758b10fd 6543593: (reflect) Clarify private final field mutability
darcy
parents: 5506
diff changeset
   501
     *              is enforcing Java language access control and the underlying
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *              method is inaccessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @exception IllegalArgumentException  if the method is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *              instance method and the specified object argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *              is not an instance of the class or interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *              declaring the underlying method (or of a subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *              or implementor thereof); if the number of actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *              and formal parameters differ; if an unwrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *              conversion for primitive arguments fails; or if,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *              after possible unwrapping, a parameter value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *              cannot be converted to the corresponding formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *              parameter type by a method invocation conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @exception InvocationTargetException if the underlying method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *              throws an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @exception NullPointerException      if the specified object is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *              and the method is an instance method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @exception ExceptionInInitializerError if the initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * provoked by this method fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
   520
    @CallerSensitive
40175
8df87018d96a 8161379: Force inline methods calling Reflection.getCallerClass
redestad
parents: 38882
diff changeset
   521
    @ForceInline // to ensure Reflection.getCallerClass optimization
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29125
diff changeset
   522
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    public Object invoke(Object obj, Object... args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        throws IllegalAccessException, IllegalArgumentException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
           InvocationTargetException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if (!override) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   528
            Class<?> caller = Reflection.getCallerClass();
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 40175
diff changeset
   529
            checkAccess(caller, clazz,
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 40175
diff changeset
   530
                        Modifier.isStatic(modifiers) ? null : obj.getClass(),
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 40175
diff changeset
   531
                        modifiers);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   533
        MethodAccessor ma = methodAccessor;             // read volatile
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   534
        if (ma == null) {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   535
            ma = acquireMethodAccessor();
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   536
        }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   537
        return ma.invoke(obj, args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * Returns {@code true} if this method is a bridge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * method; returns {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @return true if and only if this method is a bridge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * method as defined by the Java Language Specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    public boolean isBridge() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        return (getModifiers() & Modifier.BRIDGE) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    /**
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   553
     * {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   556
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    public boolean isVarArgs() {
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   558
        return super.isVarArgs();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    /**
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   562
     * {@inheritDoc}
14910
337380568515 8005097: Tie isSynthetic javadoc to the JLS
darcy
parents: 14905
diff changeset
   563
     * @jls 13.1 The Form of a Binary
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   566
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public boolean isSynthetic() {
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   568
        return super.isSynthetic();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
14904
6d9721836463 8005042: Add Method.isDefault to core reflection
darcy
parents: 14342
diff changeset
   571
    /**
6d9721836463 8005042: Add Method.isDefault to core reflection
darcy
parents: 14342
diff changeset
   572
     * Returns {@code true} if this method is a default
6d9721836463 8005042: Add Method.isDefault to core reflection
darcy
parents: 14342
diff changeset
   573
     * method; returns {@code false} otherwise.
6d9721836463 8005042: Add Method.isDefault to core reflection
darcy
parents: 14342
diff changeset
   574
     *
16496
53cc7486d16b 8010427: Refine Method.isDefault implementation
darcy
parents: 16058
diff changeset
   575
     * A default method is a public non-abstract instance method, that
53cc7486d16b 8010427: Refine Method.isDefault implementation
darcy
parents: 16058
diff changeset
   576
     * is, a non-static method with a body, declared in an interface
53cc7486d16b 8010427: Refine Method.isDefault implementation
darcy
parents: 16058
diff changeset
   577
     * type.
14904
6d9721836463 8005042: Add Method.isDefault to core reflection
darcy
parents: 14342
diff changeset
   578
     *
6d9721836463 8005042: Add Method.isDefault to core reflection
darcy
parents: 14342
diff changeset
   579
     * @return true if and only if this method is a default
6d9721836463 8005042: Add Method.isDefault to core reflection
darcy
parents: 14342
diff changeset
   580
     * method as defined by the Java Language Specification.
6d9721836463 8005042: Add Method.isDefault to core reflection
darcy
parents: 14342
diff changeset
   581
     * @since 1.8
6d9721836463 8005042: Add Method.isDefault to core reflection
darcy
parents: 14342
diff changeset
   582
     */
6d9721836463 8005042: Add Method.isDefault to core reflection
darcy
parents: 14342
diff changeset
   583
    public boolean isDefault() {
16496
53cc7486d16b 8010427: Refine Method.isDefault implementation
darcy
parents: 16058
diff changeset
   584
        // Default methods are public non-abstract instance methods
53cc7486d16b 8010427: Refine Method.isDefault implementation
darcy
parents: 16058
diff changeset
   585
        // declared in an interface.
53cc7486d16b 8010427: Refine Method.isDefault implementation
darcy
parents: 16058
diff changeset
   586
        return ((getModifiers() & (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) ==
53cc7486d16b 8010427: Refine Method.isDefault implementation
darcy
parents: 16058
diff changeset
   587
                Modifier.PUBLIC) && getDeclaringClass().isInterface();
14904
6d9721836463 8005042: Add Method.isDefault to core reflection
darcy
parents: 14342
diff changeset
   588
    }
6d9721836463 8005042: Add Method.isDefault to core reflection
darcy
parents: 14342
diff changeset
   589
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    // NOTE that there is no synchronization used here. It is correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    // (though not efficient) to generate more than one MethodAccessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    // for a given Method. However, avoiding synchronization will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    // probably make the implementation more scalable.
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   594
    private MethodAccessor acquireMethodAccessor() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        // First check to see if one has been created yet, and take it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        // if so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        MethodAccessor tmp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        if (root != null) tmp = root.getMethodAccessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        if (tmp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            methodAccessor = tmp;
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   601
        } else {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   602
            // Otherwise fabricate one and propagate it up to the root
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   603
            tmp = reflectionFactory.newMethodAccessor(this);
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   604
            setMethodAccessor(tmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   606
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   607
        return tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    // Returns MethodAccessor for this Method object, not looking up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    // the chain to the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    MethodAccessor getMethodAccessor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        return methodAccessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    // Sets the MethodAccessor for this Method object and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    // (recursively) its root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    void setMethodAccessor(MethodAccessor accessor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        methodAccessor = accessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        // Propagate up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        if (root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            root.setMethodAccessor(accessor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Returns the default value for the annotation member represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * this {@code Method} instance.  If the member is of a primitive type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * an instance of the corresponding wrapper type is returned. Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * null if no default is associated with the member, or if the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * instance does not represent a declared member of an annotation type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @return the default value for the annotation member represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *     by this {@code Method} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @throws TypeNotPresentException if the annotation is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *     {@link Class} and no definition can be found for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *     default class value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    public Object getDefaultValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        if  (annotationDefault == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            return null;
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   643
        Class<?> memberType = AnnotationType.invocationHandlerReturnType(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            getReturnType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        Object result = AnnotationParser.parseMemberValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            memberType, ByteBuffer.wrap(annotationDefault),
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32268
diff changeset
   647
            SharedSecrets.getJavaLangAccess().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                getConstantPool(getDeclaringClass()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            getDeclaringClass());
38882
efb06b82374b 5041778: (ann) AnnotationFormatError if "default" Class type not found
darcy
parents: 37363
diff changeset
   650
        if (result instanceof ExceptionProxy) {
efb06b82374b 5041778: (ann) AnnotationFormatError if "default" Class type not found
darcy
parents: 37363
diff changeset
   651
            if (result instanceof TypeNotPresentExceptionProxy) {
efb06b82374b 5041778: (ann) AnnotationFormatError if "default" Class type not found
darcy
parents: 37363
diff changeset
   652
                TypeNotPresentExceptionProxy proxy = (TypeNotPresentExceptionProxy)result;
efb06b82374b 5041778: (ann) AnnotationFormatError if "default" Class type not found
darcy
parents: 37363
diff changeset
   653
                throw new TypeNotPresentException(proxy.typeName(), proxy.getCause());
efb06b82374b 5041778: (ann) AnnotationFormatError if "default" Class type not found
darcy
parents: 37363
diff changeset
   654
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            throw new AnnotationFormatError("Invalid default: " + this);
38882
efb06b82374b 5041778: (ann) AnnotationFormatError if "default" Class type not found
darcy
parents: 37363
diff changeset
   656
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /**
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   661
     * {@inheritDoc}
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   662
     * @throws NullPointerException  {@inheritDoc}
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   663
     * @since 1.5
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   664
     */
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   665
    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   666
        return super.getAnnotation(annotationClass);
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   667
    }
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   668
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   669
    /**
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   670
     * {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   673
    public Annotation[] getDeclaredAnnotations()  {
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   674
        return super.getDeclaredAnnotations();
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   675
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   677
    /**
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   678
     * {@inheritDoc}
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   679
     * @since 1.5
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   680
     */
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   681
    @Override
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   682
    public Annotation[][] getParameterAnnotations() {
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   683
        return sharedGetParameterAnnotations(parameterTypes, parameterAnnotations);
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   684
    }
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   685
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15294
diff changeset
   686
    /**
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15294
diff changeset
   687
     * {@inheritDoc}
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15294
diff changeset
   688
     * @since 1.8
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15294
diff changeset
   689
     */
16058
5b8d8cec6280 8007808: Missing method: Executable.getAnnotatedReturnType()
jfranck
parents: 15510
diff changeset
   690
    @Override
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15294
diff changeset
   691
    public AnnotatedType getAnnotatedReturnType() {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15294
diff changeset
   692
        return getAnnotatedReturnType0(getGenericReturnType());
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15294
diff changeset
   693
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15294
diff changeset
   694
10127
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   695
    @Override
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   696
    void handleParameterNumberMismatch(int resultLength, int numParameters) {
c85d1ec57ee7 7007535: (reflect) Please generalize Constructor and Method
darcy
parents: 9266
diff changeset
   697
        throw new AnnotationFormatError("Parameter annotations don't match number of parameters");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
}