jdk/src/share/classes/java/lang/reflect/Method.java
author mduigou
Mon, 04 Apr 2011 11:55:05 -0700
changeset 9029 e92fcf58f684
parent 9022 b2e8758b10fd
child 9266 121fb370f179
permissions -rw-r--r--
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance Reviewed-by: alanb, dholmes, briangoetz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
     2
 * Copyright (c) 1996, 2006, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.reflect.MethodAccessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.reflect.Reflection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.reflect.generics.repository.MethodRepository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.reflect.generics.factory.CoreReflectionFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.reflect.generics.factory.GenericsFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.reflect.generics.scope.MethodScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.reflect.annotation.AnnotationType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.reflect.annotation.AnnotationParser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.annotation.Annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.annotation.AnnotationFormatError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A {@code Method} provides information about, and access to, a single method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * on a class or interface.  The reflected method may be a class method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * or an instance method (including an abstract method).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>A {@code Method} permits widening conversions to occur when matching the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * actual parameters to invoke with the underlying method's formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * parameters, but it throws an {@code IllegalArgumentException} if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * narrowing conversion would occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @see Member
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @see java.lang.Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @see java.lang.Class#getMethods()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @see java.lang.Class#getMethod(String, Class[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @see java.lang.Class#getDeclaredMethods()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see java.lang.Class#getDeclaredMethod(String, Class[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Kenneth Russell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Nakul Saraiya
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    class Method extends AccessibleObject implements GenericDeclaration,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                                     Member {
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
    64
    private Class<?>            clazz;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private int                 slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // This is guaranteed to be interned by the VM in the 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // reflection implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private String              name;
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
    69
    private Class<?>            returnType;
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
    70
    private Class<?>[]          parameterTypes;
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
    71
    private Class<?>[]          exceptionTypes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private int                 modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // Generics and annotations support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private transient String              signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // generic info repository; lazily initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private transient MethodRepository genericInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private byte[]              annotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private byte[]              parameterAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private byte[]              annotationDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private volatile MethodAccessor methodAccessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // For sharing of MethodAccessors. This branching structure is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // currently only two levels deep (i.e., one root Method and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // potentially many Method objects pointing to it.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private Method              root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
   // Generics infrastructure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private String getGenericSignature() {return signature;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // Accessor for factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private GenericsFactory getFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // create scope and factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return CoreReflectionFactory.make(this, MethodScope.make(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // Accessor for generic info repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private MethodRepository getGenericInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // lazily initialize repository if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (genericInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            // create and cache generic info repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            genericInfo = MethodRepository.make(getGenericSignature(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                                getFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return genericInfo; //return cached repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Package-private constructor used by ReflectAccess to enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * instantiation of these objects in Java code from the java.lang
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * package via sun.reflect.LangReflectAccess.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   112
    Method(Class<?> declaringClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
           String name,
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   114
           Class<?>[] parameterTypes,
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   115
           Class<?> returnType,
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   116
           Class<?>[] checkedExceptions,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
           int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
           int slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
           String signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
           byte[] annotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
           byte[] parameterAnnotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
           byte[] annotationDefault)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        this.clazz = declaringClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        this.parameterTypes = parameterTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        this.returnType = returnType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        this.exceptionTypes = checkedExceptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this.modifiers = modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.slot = slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.signature = signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.annotations = annotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.parameterAnnotations = parameterAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        this.annotationDefault = annotationDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Package-private routine (exposed to java.lang.Class via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * ReflectAccess) which returns a copy of this Method. The copy's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * "root" field points to this Method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    Method copy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // This routine enables sharing of MethodAccessor objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // among Method objects which refer to the same underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // method in the VM. (All of this contortion is only necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // because of the "accessibility" bit in AccessibleObject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // which implicitly requires that new java.lang.reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // objects be fabricated for each reflective call on Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // objects.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        Method res = new Method(clazz, name, parameterTypes, returnType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                exceptionTypes, modifiers, slot, signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                annotations, parameterAnnotations, annotationDefault);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        res.root = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // Might as well eagerly propagate this if already present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        res.methodAccessor = methodAccessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Returns the {@code Class} object representing the class or interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * that declares the method represented by this {@code Method} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public Class<?> getDeclaringClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Returns the name of the method represented by this {@code Method}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * object, as a {@code String}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Returns the Java language modifiers for the method represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * by this {@code Method} object, as an integer. The {@code Modifier} class should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * be used to decode the modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @see Modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public int getModifiers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Returns an array of {@code TypeVariable} objects that represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * type variables declared by the generic declaration represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * {@code GenericDeclaration} object, in declaration order.  Returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * array of length 0 if the underlying generic declaration declares no type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @return an array of {@code TypeVariable} objects that represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *     the type variables declared by this generic declaration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @throws GenericSignatureFormatError if the generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *     signature of this generic declaration does not conform to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *     the format specified in the Java Virtual Machine Specification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *     3rd edition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public TypeVariable<Method>[] getTypeParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (getGenericSignature() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return (TypeVariable<Method>[])getGenericInfo().getTypeParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return (TypeVariable<Method>[])new TypeVariable[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Returns a {@code Class} object that represents the formal return type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * of the method represented by this {@code Method} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @return the return type for the method this object represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public Class<?> getReturnType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return returnType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Returns a {@code Type} object that represents the formal return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * type of the method represented by this {@code Method} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <p>If the return type is a parameterized type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * the {@code Type} object returned must accurately reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * the actual type parameters used in the source code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <p>If the return type is a type variable or a parameterized type, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * is created. Otherwise, it is resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return  a {@code Type} object that represents the formal return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *     type of the underlying  method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @throws GenericSignatureFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *     if the generic method signature does not conform to the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *     specified in the Java Virtual Machine Specification, 3rd edition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @throws TypeNotPresentException if the underlying method's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *     return type refers to a non-existent type declaration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @throws MalformedParameterizedTypeException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *     underlying method's return typed refers to a parameterized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *     type that cannot be instantiated for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public Type getGenericReturnType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
      if (getGenericSignature() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return getGenericInfo().getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
      } else { return getReturnType();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Returns an array of {@code Class} objects that represent the formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * parameter types, in declaration order, of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * represented by this {@code Method} object.  Returns an array of length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * 0 if the underlying method takes no parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @return the parameter types for the method this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public Class<?>[] getParameterTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return (Class<?>[]) parameterTypes.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Returns an array of {@code Type} objects that represent the formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * parameter types, in declaration order, of the method represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * this {@code Method} object. Returns an array of length 0 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * underlying method takes no parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <p>If a formal parameter type is a parameterized type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * the {@code Type} object returned for it must accurately reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * the actual type parameters used in the source code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <p>If a formal parameter type is a type variable or a parameterized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * type, it is created. Otherwise, it is resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @return an array of Types that represent the formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *     parameter types of the underlying method, in declaration order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @throws GenericSignatureFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *     if the generic method signature does not conform to the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *     specified in the Java Virtual Machine Specification, 3rd edition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @throws TypeNotPresentException if any of the parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *     types of the underlying method refers to a non-existent type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *     declaration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @throws MalformedParameterizedTypeException if any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *     the underlying method's parameter types refer to a parameterized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *     type that cannot be instantiated for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public Type[] getGenericParameterTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (getGenericSignature() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return getGenericInfo().getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Returns an array of {@code Class} objects that represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * the types of the exceptions declared to be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * by the underlying method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * represented by this {@code Method} object.  Returns an array of length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * 0 if the method declares no exceptions in its {@code throws} clause.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @return the exception types declared as being thrown by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * method this object represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public Class<?>[] getExceptionTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return (Class<?>[]) exceptionTypes.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Returns an array of {@code Type} objects that represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * exceptions declared to be thrown by this {@code Method} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Returns an array of length 0 if the underlying method declares
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * no exceptions in its {@code throws} clause.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <p>If an exception type is a type variable or a parameterized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * type, it is created. Otherwise, it is resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @return an array of Types that represent the exception types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *     thrown by the underlying method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @throws GenericSignatureFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *     if the generic method signature does not conform to the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *     specified in the Java Virtual Machine Specification, 3rd edition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @throws TypeNotPresentException if the underlying method's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *     {@code throws} clause refers to a non-existent type declaration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @throws MalformedParameterizedTypeException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *     the underlying method's {@code throws} clause refers to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *     parameterized type that cannot be instantiated for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
      public Type[] getGenericExceptionTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
          Type[] result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
          if (getGenericSignature() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
              ((result = getGenericInfo().getExceptionTypes()).length > 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
              return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
          else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
              return getExceptionTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
      }
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                /* Avoid unnecessary cloning */
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   353
                Class<?>[] params1 = parameterTypes;
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   354
                Class<?>[] params2 = other.parameterTypes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                if (params1.length == params2.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    for (int i = 0; i < params1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        if (params1[i] != params2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return false;
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 hashcode for this {@code Method}.  The hashcode is computed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * as the exclusive-or of the hashcodes for the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * method's declaring class name and the method's name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Returns a string describing this {@code Method}.  The string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * formatted as the method access modifiers, if any, followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * the method return type, followed by a space, followed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * class declaring the method, followed by a period, followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * the method name, followed by a parenthesized, comma-separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * list of the method's formal parameter types. If the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * throws checked exceptions, the parameter list is followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * space, followed by the word throws followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * comma-separated list of the thrown exception types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *    public boolean java.lang.Object.equals(java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <p>The access modifiers are placed in canonical order as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * specified by "The Java Language Specification".  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * {@code public}, {@code protected} or {@code private} first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * and then other modifiers in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * {@code abstract}, {@code static}, {@code final},
3712
8851d55adef0 6261502: (reflect) Add the functionality to screen out the "inappropriate" modifier bits
darcy
parents: 2174
diff changeset
   396
     * {@code synchronized}, {@code native}, {@code strictfp}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        try {
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   400
            StringBuilder sb = new StringBuilder();
3712
8851d55adef0 6261502: (reflect) Add the functionality to screen out the "inappropriate" modifier bits
darcy
parents: 2174
diff changeset
   401
            int mod = getModifiers() & Modifier.methodModifiers();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (mod != 0) {
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   403
                sb.append(Modifier.toString(mod)).append(' ');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   405
            sb.append(Field.getTypeName(getReturnType())).append(' ');
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   406
            sb.append(Field.getTypeName(getDeclaringClass())).append('.');
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   407
            sb.append(getName()).append('(');
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   408
            Class<?>[] params = parameterTypes; // avoid clone
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            for (int j = 0; j < params.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                sb.append(Field.getTypeName(params[j]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                if (j < (params.length - 1))
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   412
                    sb.append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            }
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   414
            sb.append(')');
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   415
            Class<?>[] exceptions = exceptionTypes; // avoid clone
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (exceptions.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                sb.append(" throws ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                for (int k = 0; k < exceptions.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    sb.append(exceptions[k].getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    if (k < (exceptions.length - 1))
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   421
                        sb.append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            return "<" + e + ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Returns a string describing this {@code Method}, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * type parameters.  The string is formatted as the method access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * modifiers, if any, followed by an angle-bracketed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * comma-separated list of the method's type parameters, if any,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * followed by the method's generic return type, followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * space, followed by the class declaring the method, followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * a period, followed by the method name, followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * parenthesized, comma-separated list of the method's generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * formal parameter types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * If this method was declared to take a variable number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * arguments, instead of denoting the last parameter as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * "<tt><i>Type</i>[]</tt>", it is denoted as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * "<tt><i>Type</i>...</tt>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * A space is used to separate access modifiers from one another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * and from the type parameters or return type.  If there are no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * type parameters, the type parameter list is elided; if the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * parameter list is present, a space separates the list from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * class name.  If the method is declared to throw exceptions, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * parameter list is followed by a space, followed by the word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * throws followed by a comma-separated list of the generic thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * exception types.  If there are no type parameters, the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * parameter list is elided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * <p>The access modifiers are placed in canonical order as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * specified by "The Java Language Specification".  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * {@code public}, {@code protected} or {@code private} first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * and then other modifiers in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * {@code abstract}, {@code static}, {@code final},
3712
8851d55adef0 6261502: (reflect) Add the functionality to screen out the "inappropriate" modifier bits
darcy
parents: 2174
diff changeset
   461
     * {@code synchronized}, {@code native}, {@code strictfp}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @return a string describing this {@code Method},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * include type parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public String toGenericString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            StringBuilder sb = new StringBuilder();
3712
8851d55adef0 6261502: (reflect) Add the functionality to screen out the "inappropriate" modifier bits
darcy
parents: 2174
diff changeset
   471
            int mod = getModifiers() & Modifier.methodModifiers();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            if (mod != 0) {
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   473
                sb.append(Modifier.toString(mod)).append(' ');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            TypeVariable<?>[] typeparms = getTypeParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            if (typeparms.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                boolean first = true;
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   478
                sb.append('<');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                for(TypeVariable<?> typeparm: typeparms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    if (!first)
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   481
                        sb.append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    // Class objects can't occur here; no need to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    // and call Class.getName().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    sb.append(typeparm.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                sb.append("> ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            Type genRetType = getGenericReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            sb.append( ((genRetType instanceof Class<?>)?
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   492
                        Field.getTypeName((Class<?>)genRetType):genRetType.toString()))
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   493
                    .append(' ');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   495
            sb.append(Field.getTypeName(getDeclaringClass())).append('.');
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   496
            sb.append(getName()).append('(');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            Type[] params = getGenericParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            for (int j = 0; j < params.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                String param = (params[j] instanceof Class)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    Field.getTypeName((Class)params[j]):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    (params[j].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    param = param.replaceFirst("\\[\\]$", "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                sb.append(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                if (j < (params.length - 1))
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   506
                    sb.append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   508
            sb.append(')');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            Type[] exceptions = getGenericExceptionTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            if (exceptions.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                sb.append(" throws ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                for (int k = 0; k < exceptions.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    sb.append((exceptions[k] instanceof Class)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                              ((Class)exceptions[k]).getName():
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                              exceptions[k].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    if (k < (exceptions.length - 1))
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   517
                        sb.append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            return "<" + e + ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * Invokes the underlying method represented by this {@code Method}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * object, on the specified object with the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Individual parameters are automatically unwrapped to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * primitive formal parameters, and both primitive and reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * parameters are subject to method invocation conversions as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * <p>If the underlying method is static, then the specified {@code obj}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * argument is ignored. It may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * <p>If the number of formal parameters required by the underlying method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * 0, the supplied {@code args} array may be of length 0 or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * <p>If the underlying method is an instance method, it is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * using dynamic method lookup as documented in The Java Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Specification, Second Edition, section 15.12.4.4; in particular,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * overriding based on the runtime type of the target object will occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * <p>If the underlying method is static, the class that declared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * the method is initialized if it has not already been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * <p>If the method completes normally, the value it returns is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * returned to the caller of invoke; if the value has a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * type, it is first appropriately wrapped in an object. However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * if the value has the type of an array of a primitive type, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * elements of the array are <i>not</i> wrapped in objects; in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * other words, an array of primitive type is returned.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * underlying method return type is void, the invocation returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @param obj  the object the underlying method is invoked from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @param args the arguments used for the method call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @return the result of dispatching the method represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * this object on {@code obj} with parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * {@code args}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @exception IllegalAccessException    if this {@code Method} object
9022
b2e8758b10fd 6543593: (reflect) Clarify private final field mutability
darcy
parents: 5506
diff changeset
   564
     *              is enforcing Java language access control and the underlying
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *              method is inaccessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @exception IllegalArgumentException  if the method is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *              instance method and the specified object argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *              is not an instance of the class or interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *              declaring the underlying method (or of a subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *              or implementor thereof); if the number of actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *              and formal parameters differ; if an unwrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *              conversion for primitive arguments fails; or if,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *              after possible unwrapping, a parameter value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *              cannot be converted to the corresponding formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *              parameter type by a method invocation conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @exception InvocationTargetException if the underlying method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *              throws an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @exception NullPointerException      if the specified object is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *              and the method is an instance method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @exception ExceptionInInitializerError if the initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * provoked by this method fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public Object invoke(Object obj, Object... args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        throws IllegalAccessException, IllegalArgumentException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
           InvocationTargetException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (!override) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   589
                Class<?> caller = Reflection.getCallerClass(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   591
                checkAccess(caller, clazz, obj, modifiers);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   594
        MethodAccessor ma = methodAccessor;             // read volatile
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   595
        if (ma == null) {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   596
            ma = acquireMethodAccessor();
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   597
        }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   598
        return ma.invoke(obj, args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * Returns {@code true} if this method is a bridge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * method; returns {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @return true if and only if this method is a bridge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * method as defined by the Java Language Specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    public boolean isBridge() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        return (getModifiers() & Modifier.BRIDGE) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Returns {@code true} if this method was declared to take
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * a variable number of arguments; returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @return {@code true} if an only if this method was declared to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * take a variable number of arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    public boolean isVarArgs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        return (getModifiers() & Modifier.VARARGS) != 0;
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 {@code true} if this method is a synthetic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * method; returns {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @return true if and only if this method is a synthetic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * method as defined by the Java Language Specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    public boolean isSynthetic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        return Modifier.isSynthetic(getModifiers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    // NOTE that there is no synchronization used here. It is correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    // (though not efficient) to generate more than one MethodAccessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    // for a given Method. However, avoiding synchronization will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    // 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
   642
    private MethodAccessor acquireMethodAccessor() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        // First check to see if one has been created yet, and take it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        // if so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        MethodAccessor tmp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        if (root != null) tmp = root.getMethodAccessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        if (tmp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            methodAccessor = tmp;
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   649
        } else {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   650
            // 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
   651
            tmp = reflectionFactory.newMethodAccessor(this);
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   652
            setMethodAccessor(tmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   654
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 9022
diff changeset
   655
        return tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    // Returns MethodAccessor for this Method object, not looking up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    // the chain to the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    MethodAccessor getMethodAccessor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        return methodAccessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    // Sets the MethodAccessor for this Method object and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    // (recursively) its root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    void setMethodAccessor(MethodAccessor accessor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        methodAccessor = accessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        // Propagate up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            root.setMethodAccessor(accessor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        if (annotationClass == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        return (T) declaredAnnotations().get(annotationClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    public Annotation[] getDeclaredAnnotations()  {
2174
0ffce164e4a4 6799230: Lazily load java.lang.annotation.Annotation class
mchung
parents: 2
diff changeset
   689
        return AnnotationParser.toArray(declaredAnnotations());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   692
    private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   694
    private synchronized  Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        if (declaredAnnotations == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            declaredAnnotations = AnnotationParser.parseAnnotations(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                annotations, sun.misc.SharedSecrets.getJavaLangAccess().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                getConstantPool(getDeclaringClass()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        return declaredAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * Returns the default value for the annotation member represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * this {@code Method} instance.  If the member is of a primitive type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * an instance of the corresponding wrapper type is returned. Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * null if no default is associated with the member, or if the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * instance does not represent a declared member of an annotation type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @return the default value for the annotation member represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *     by this {@code Method} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @throws TypeNotPresentException if the annotation is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *     {@link Class} and no definition can be found for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     *     default class value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    public Object getDefaultValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        if  (annotationDefault == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            return null;
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3941
diff changeset
   721
        Class<?> memberType = AnnotationType.invocationHandlerReturnType(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            getReturnType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        Object result = AnnotationParser.parseMemberValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            memberType, ByteBuffer.wrap(annotationDefault),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            sun.misc.SharedSecrets.getJavaLangAccess().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                getConstantPool(getDeclaringClass()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        if (result instanceof sun.reflect.annotation.ExceptionProxy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            throw new AnnotationFormatError("Invalid default: " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * Returns an array of arrays that represent the annotations on the formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * parameters, in declaration order, of the method represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * this {@code Method} object. (Returns an array of length zero if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * underlying method is parameterless.  If the method has one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * parameters, a nested array of length zero is returned for each parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * with no annotations.) The annotation objects contained in the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * arrays are serializable.  The caller of this method is free to modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * the returned arrays; it will have no effect on the arrays returned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * other callers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @return an array of arrays that represent the annotations on the formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *    parameters, in declaration order, of the method represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *    Method object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    public Annotation[][] getParameterAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        int numParameters = parameterTypes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        if (parameterAnnotations == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            return new Annotation[numParameters][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        Annotation[][] result = AnnotationParser.parseParameterAnnotations(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            parameterAnnotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            sun.misc.SharedSecrets.getJavaLangAccess().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                getConstantPool(getDeclaringClass()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if (result.length != numParameters)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            throw new java.lang.annotation.AnnotationFormatError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                "Parameter annotations don't match number of parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
}