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