emul/src/main/java/java/lang/reflect/Method.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 04 Dec 2012 14:31:11 +0100
branchreflection
changeset 260 1d03cb35fbda
parent 258 21b390daf444
child 262 683719ffcfe7
permissions -rw-r--r--
Removing default implementation from reflection APIs
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 java.lang.annotation.Annotation;
jtulach@258
    29
jtulach@258
    30
/**
jtulach@258
    31
 * A {@code Method} provides information about, and access to, a single method
jtulach@258
    32
 * on a class or interface.  The reflected method may be a class method
jtulach@258
    33
 * or an instance method (including an abstract method).
jtulach@258
    34
 *
jtulach@258
    35
 * <p>A {@code Method} permits widening conversions to occur when matching the
jtulach@258
    36
 * actual parameters to invoke with the underlying method's formal
jtulach@258
    37
 * parameters, but it throws an {@code IllegalArgumentException} if a
jtulach@258
    38
 * narrowing conversion would occur.
jtulach@258
    39
 *
jtulach@258
    40
 * @see Member
jtulach@258
    41
 * @see java.lang.Class
jtulach@258
    42
 * @see java.lang.Class#getMethods()
jtulach@258
    43
 * @see java.lang.Class#getMethod(String, Class[])
jtulach@258
    44
 * @see java.lang.Class#getDeclaredMethods()
jtulach@258
    45
 * @see java.lang.Class#getDeclaredMethod(String, Class[])
jtulach@258
    46
 *
jtulach@258
    47
 * @author Kenneth Russell
jtulach@258
    48
 * @author Nakul Saraiya
jtulach@258
    49
 */
jtulach@258
    50
public final
jtulach@258
    51
    class Method extends AccessibleObject implements GenericDeclaration,
jtulach@258
    52
                                                     Member {
jtulach@258
    53
    private Class<?>            clazz;
jtulach@258
    54
    private int                 slot;
jtulach@258
    55
    // This is guaranteed to be interned by the VM in the 1.4
jtulach@258
    56
    // reflection implementation
jtulach@258
    57
    private String              name;
jtulach@258
    58
    private Class<?>            returnType;
jtulach@258
    59
    private Class<?>[]          parameterTypes;
jtulach@258
    60
    private Class<?>[]          exceptionTypes;
jtulach@258
    61
    private int                 modifiers;
jtulach@258
    62
    // Generics and annotations support
jtulach@258
    63
    private transient String              signature;
jtulach@258
    64
    private byte[]              annotations;
jtulach@258
    65
    private byte[]              parameterAnnotations;
jtulach@258
    66
    private byte[]              annotationDefault;
jtulach@258
    67
    // For sharing of MethodAccessors. This branching structure is
jtulach@258
    68
    // currently only two levels deep (i.e., one root Method and
jtulach@258
    69
    // potentially many Method objects pointing to it.)
jtulach@258
    70
    private Method              root;
jtulach@258
    71
jtulach@258
    72
   // Generics infrastructure
jtulach@258
    73
jtulach@258
    74
    private String getGenericSignature() {return signature;}
jtulach@258
    75
jtulach@258
    76
    /**
jtulach@258
    77
     * Package-private constructor used by ReflectAccess to enable
jtulach@258
    78
     * instantiation of these objects in Java code from the java.lang
jtulach@258
    79
     * package via sun.reflect.LangReflectAccess.
jtulach@258
    80
     */
jtulach@258
    81
    Method(Class<?> declaringClass,
jtulach@258
    82
           String name,
jtulach@258
    83
           Class<?>[] parameterTypes,
jtulach@258
    84
           Class<?> returnType,
jtulach@258
    85
           Class<?>[] checkedExceptions,
jtulach@258
    86
           int modifiers,
jtulach@258
    87
           int slot,
jtulach@258
    88
           String signature,
jtulach@258
    89
           byte[] annotations,
jtulach@258
    90
           byte[] parameterAnnotations,
jtulach@258
    91
           byte[] annotationDefault)
jtulach@258
    92
    {
jtulach@258
    93
        this.clazz = declaringClass;
jtulach@258
    94
        this.name = name;
jtulach@258
    95
        this.parameterTypes = parameterTypes;
jtulach@258
    96
        this.returnType = returnType;
jtulach@258
    97
        this.exceptionTypes = checkedExceptions;
jtulach@258
    98
        this.modifiers = modifiers;
jtulach@258
    99
        this.slot = slot;
jtulach@258
   100
        this.signature = signature;
jtulach@258
   101
        this.annotations = annotations;
jtulach@258
   102
        this.parameterAnnotations = parameterAnnotations;
jtulach@258
   103
        this.annotationDefault = annotationDefault;
jtulach@258
   104
    }
jtulach@258
   105
jtulach@258
   106
    /**
jtulach@258
   107
     * Package-private routine (exposed to java.lang.Class via
jtulach@258
   108
     * ReflectAccess) which returns a copy of this Method. The copy's
jtulach@258
   109
     * "root" field points to this Method.
jtulach@258
   110
     */
jtulach@258
   111
    Method copy() {
jtulach@258
   112
        // This routine enables sharing of MethodAccessor objects
jtulach@258
   113
        // among Method objects which refer to the same underlying
jtulach@258
   114
        // method in the VM. (All of this contortion is only necessary
jtulach@258
   115
        // because of the "accessibility" bit in AccessibleObject,
jtulach@258
   116
        // which implicitly requires that new java.lang.reflect
jtulach@258
   117
        // objects be fabricated for each reflective call on Class
jtulach@258
   118
        // objects.)
jtulach@258
   119
        Method res = new Method(clazz, name, parameterTypes, returnType,
jtulach@258
   120
                                exceptionTypes, modifiers, slot, signature,
jtulach@258
   121
                                annotations, parameterAnnotations, annotationDefault);
jtulach@258
   122
        res.root = this;
jtulach@258
   123
        return res;
jtulach@258
   124
    }
jtulach@258
   125
jtulach@258
   126
    /**
jtulach@258
   127
     * Returns the {@code Class} object representing the class or interface
jtulach@258
   128
     * that declares the method represented by this {@code Method} object.
jtulach@258
   129
     */
jtulach@258
   130
    public Class<?> getDeclaringClass() {
jtulach@258
   131
        return clazz;
jtulach@258
   132
    }
jtulach@258
   133
jtulach@258
   134
    /**
jtulach@258
   135
     * Returns the name of the method represented by this {@code Method}
jtulach@258
   136
     * object, as a {@code String}.
jtulach@258
   137
     */
jtulach@258
   138
    public String getName() {
jtulach@258
   139
        return name;
jtulach@258
   140
    }
jtulach@258
   141
jtulach@258
   142
    /**
jtulach@258
   143
     * Returns the Java language modifiers for the method represented
jtulach@258
   144
     * by this {@code Method} object, as an integer. The {@code Modifier} class should
jtulach@258
   145
     * be used to decode the modifiers.
jtulach@258
   146
     *
jtulach@258
   147
     * @see Modifier
jtulach@258
   148
     */
jtulach@258
   149
    public int getModifiers() {
jtulach@258
   150
        return modifiers;
jtulach@258
   151
    }
jtulach@258
   152
jtulach@258
   153
    /**
jtulach@258
   154
     * Returns an array of {@code TypeVariable} objects that represent the
jtulach@258
   155
     * type variables declared by the generic declaration represented by this
jtulach@258
   156
     * {@code GenericDeclaration} object, in declaration order.  Returns an
jtulach@258
   157
     * array of length 0 if the underlying generic declaration declares no type
jtulach@258
   158
     * variables.
jtulach@258
   159
     *
jtulach@258
   160
     * @return an array of {@code TypeVariable} objects that represent
jtulach@258
   161
     *     the type variables declared by this generic declaration
jtulach@258
   162
     * @throws GenericSignatureFormatError if the generic
jtulach@258
   163
     *     signature of this generic declaration does not conform to
jtulach@258
   164
     *     the format specified in
jtulach@258
   165
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
jtulach@258
   166
     * @since 1.5
jtulach@258
   167
     */
jtulach@258
   168
    public TypeVariable<Method>[] getTypeParameters() {
jaroslav@260
   169
        throw new UnsupportedOperationException();
jtulach@258
   170
    }
jtulach@258
   171
jtulach@258
   172
    /**
jtulach@258
   173
     * Returns a {@code Class} object that represents the formal return type
jtulach@258
   174
     * of the method represented by this {@code Method} object.
jtulach@258
   175
     *
jtulach@258
   176
     * @return the return type for the method this object represents
jtulach@258
   177
     */
jtulach@258
   178
    public Class<?> getReturnType() {
jtulach@258
   179
        return returnType;
jtulach@258
   180
    }
jtulach@258
   181
jtulach@258
   182
    /**
jtulach@258
   183
     * Returns a {@code Type} object that represents the formal return
jtulach@258
   184
     * type of the method represented by this {@code Method} object.
jtulach@258
   185
     *
jtulach@258
   186
     * <p>If the return type is a parameterized type,
jtulach@258
   187
     * the {@code Type} object returned must accurately reflect
jtulach@258
   188
     * the actual type parameters used in the source code.
jtulach@258
   189
     *
jtulach@258
   190
     * <p>If the return type is a type variable or a parameterized type, it
jtulach@258
   191
     * is created. Otherwise, it is resolved.
jtulach@258
   192
     *
jtulach@258
   193
     * @return  a {@code Type} object that represents the formal return
jtulach@258
   194
     *     type of the underlying  method
jtulach@258
   195
     * @throws GenericSignatureFormatError
jtulach@258
   196
     *     if the generic method signature does not conform to the format
jtulach@258
   197
     *     specified in
jtulach@258
   198
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
jtulach@258
   199
     * @throws TypeNotPresentException if the underlying method's
jtulach@258
   200
     *     return type refers to a non-existent type declaration
jtulach@258
   201
     * @throws MalformedParameterizedTypeException if the
jtulach@258
   202
     *     underlying method's return typed refers to a parameterized
jtulach@258
   203
     *     type that cannot be instantiated for any reason
jtulach@258
   204
     * @since 1.5
jtulach@258
   205
     */
jtulach@258
   206
    public Type getGenericReturnType() {
jaroslav@260
   207
        throw new UnsupportedOperationException();
jtulach@258
   208
    }
jtulach@258
   209
jtulach@258
   210
jtulach@258
   211
    /**
jtulach@258
   212
     * Returns an array of {@code Class} objects that represent the formal
jtulach@258
   213
     * parameter types, in declaration order, of the method
jtulach@258
   214
     * represented by this {@code Method} object.  Returns an array of length
jtulach@258
   215
     * 0 if the underlying method takes no parameters.
jtulach@258
   216
     *
jtulach@258
   217
     * @return the parameter types for the method this object
jtulach@258
   218
     * represents
jtulach@258
   219
     */
jtulach@258
   220
    public Class<?>[] getParameterTypes() {
jtulach@258
   221
        return (Class<?>[]) parameterTypes.clone();
jtulach@258
   222
    }
jtulach@258
   223
jtulach@258
   224
    /**
jtulach@258
   225
     * Returns an array of {@code Type} objects that represent the formal
jtulach@258
   226
     * parameter types, in declaration order, of the method represented by
jtulach@258
   227
     * this {@code Method} object. Returns an array of length 0 if the
jtulach@258
   228
     * underlying method takes no parameters.
jtulach@258
   229
     *
jtulach@258
   230
     * <p>If a formal parameter type is a parameterized type,
jtulach@258
   231
     * the {@code Type} object returned for it must accurately reflect
jtulach@258
   232
     * the actual type parameters used in the source code.
jtulach@258
   233
     *
jtulach@258
   234
     * <p>If a formal parameter type is a type variable or a parameterized
jtulach@258
   235
     * type, it is created. Otherwise, it is resolved.
jtulach@258
   236
     *
jtulach@258
   237
     * @return an array of Types that represent the formal
jtulach@258
   238
     *     parameter types of the underlying method, in declaration order
jtulach@258
   239
     * @throws GenericSignatureFormatError
jtulach@258
   240
     *     if the generic method signature does not conform to the format
jtulach@258
   241
     *     specified in
jtulach@258
   242
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
jtulach@258
   243
     * @throws TypeNotPresentException if any of the parameter
jtulach@258
   244
     *     types of the underlying method refers to a non-existent type
jtulach@258
   245
     *     declaration
jtulach@258
   246
     * @throws MalformedParameterizedTypeException if any of
jtulach@258
   247
     *     the underlying method's parameter types refer to a parameterized
jtulach@258
   248
     *     type that cannot be instantiated for any reason
jtulach@258
   249
     * @since 1.5
jtulach@258
   250
     */
jtulach@258
   251
    public Type[] getGenericParameterTypes() {
jaroslav@260
   252
        throw new UnsupportedOperationException();
jtulach@258
   253
    }
jtulach@258
   254
jtulach@258
   255
jtulach@258
   256
    /**
jtulach@258
   257
     * Returns an array of {@code Class} objects that represent
jtulach@258
   258
     * the types of the exceptions declared to be thrown
jtulach@258
   259
     * by the underlying method
jtulach@258
   260
     * represented by this {@code Method} object.  Returns an array of length
jtulach@258
   261
     * 0 if the method declares no exceptions in its {@code throws} clause.
jtulach@258
   262
     *
jtulach@258
   263
     * @return the exception types declared as being thrown by the
jtulach@258
   264
     * method this object represents
jtulach@258
   265
     */
jtulach@258
   266
    public Class<?>[] getExceptionTypes() {
jtulach@258
   267
        return (Class<?>[]) exceptionTypes.clone();
jtulach@258
   268
    }
jtulach@258
   269
jtulach@258
   270
    /**
jtulach@258
   271
     * Returns an array of {@code Type} objects that represent the
jtulach@258
   272
     * exceptions declared to be thrown by this {@code Method} object.
jtulach@258
   273
     * Returns an array of length 0 if the underlying method declares
jtulach@258
   274
     * no exceptions in its {@code throws} clause.
jtulach@258
   275
     *
jtulach@258
   276
     * <p>If an exception type is a type variable or a parameterized
jtulach@258
   277
     * type, it is created. Otherwise, it is resolved.
jtulach@258
   278
     *
jtulach@258
   279
     * @return an array of Types that represent the exception types
jtulach@258
   280
     *     thrown by the underlying method
jtulach@258
   281
     * @throws GenericSignatureFormatError
jtulach@258
   282
     *     if the generic method signature does not conform to the format
jtulach@258
   283
     *     specified in
jtulach@258
   284
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
jtulach@258
   285
     * @throws TypeNotPresentException if the underlying method's
jtulach@258
   286
     *     {@code throws} clause refers to a non-existent type declaration
jtulach@258
   287
     * @throws MalformedParameterizedTypeException if
jtulach@258
   288
     *     the underlying method's {@code throws} clause refers to a
jtulach@258
   289
     *     parameterized type that cannot be instantiated for any reason
jtulach@258
   290
     * @since 1.5
jtulach@258
   291
     */
jtulach@258
   292
      public Type[] getGenericExceptionTypes() {
jaroslav@260
   293
        throw new UnsupportedOperationException();
jtulach@258
   294
      }
jtulach@258
   295
jtulach@258
   296
    /**
jtulach@258
   297
     * Compares this {@code Method} against the specified object.  Returns
jtulach@258
   298
     * true if the objects are the same.  Two {@code Methods} are the same if
jtulach@258
   299
     * they were declared by the same class and have the same name
jtulach@258
   300
     * and formal parameter types and return type.
jtulach@258
   301
     */
jtulach@258
   302
    public boolean equals(Object obj) {
jtulach@258
   303
        if (obj != null && obj instanceof Method) {
jtulach@258
   304
            Method other = (Method)obj;
jtulach@258
   305
            if ((getDeclaringClass() == other.getDeclaringClass())
jtulach@258
   306
                && (getName() == other.getName())) {
jtulach@258
   307
                if (!returnType.equals(other.getReturnType()))
jtulach@258
   308
                    return false;
jtulach@258
   309
                /* Avoid unnecessary cloning */
jtulach@258
   310
                Class<?>[] params1 = parameterTypes;
jtulach@258
   311
                Class<?>[] params2 = other.parameterTypes;
jtulach@258
   312
                if (params1.length == params2.length) {
jtulach@258
   313
                    for (int i = 0; i < params1.length; i++) {
jtulach@258
   314
                        if (params1[i] != params2[i])
jtulach@258
   315
                            return false;
jtulach@258
   316
                    }
jtulach@258
   317
                    return true;
jtulach@258
   318
                }
jtulach@258
   319
            }
jtulach@258
   320
        }
jtulach@258
   321
        return false;
jtulach@258
   322
    }
jtulach@258
   323
jtulach@258
   324
    /**
jtulach@258
   325
     * Returns a hashcode for this {@code Method}.  The hashcode is computed
jtulach@258
   326
     * as the exclusive-or of the hashcodes for the underlying
jtulach@258
   327
     * method's declaring class name and the method's name.
jtulach@258
   328
     */
jtulach@258
   329
    public int hashCode() {
jtulach@258
   330
        return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
jtulach@258
   331
    }
jtulach@258
   332
jtulach@258
   333
    /**
jtulach@258
   334
     * Returns a string describing this {@code Method}.  The string is
jtulach@258
   335
     * formatted as the method access modifiers, if any, followed by
jtulach@258
   336
     * the method return type, followed by a space, followed by the
jtulach@258
   337
     * class declaring the method, followed by a period, followed by
jtulach@258
   338
     * the method name, followed by a parenthesized, comma-separated
jtulach@258
   339
     * list of the method's formal parameter types. If the method
jtulach@258
   340
     * throws checked exceptions, the parameter list is followed by a
jtulach@258
   341
     * space, followed by the word throws followed by a
jtulach@258
   342
     * comma-separated list of the thrown exception types.
jtulach@258
   343
     * For example:
jtulach@258
   344
     * <pre>
jtulach@258
   345
     *    public boolean java.lang.Object.equals(java.lang.Object)
jtulach@258
   346
     * </pre>
jtulach@258
   347
     *
jtulach@258
   348
     * <p>The access modifiers are placed in canonical order as
jtulach@258
   349
     * specified by "The Java Language Specification".  This is
jtulach@258
   350
     * {@code public}, {@code protected} or {@code private} first,
jtulach@258
   351
     * and then other modifiers in the following order:
jtulach@258
   352
     * {@code abstract}, {@code static}, {@code final},
jtulach@258
   353
     * {@code synchronized}, {@code native}, {@code strictfp}.
jtulach@258
   354
     */
jtulach@258
   355
    public String toString() {
jtulach@258
   356
        try {
jtulach@258
   357
            StringBuilder sb = new StringBuilder();
jtulach@258
   358
            int mod = getModifiers() & Modifier.methodModifiers();
jtulach@258
   359
            if (mod != 0) {
jtulach@258
   360
                sb.append(Modifier.toString(mod)).append(' ');
jtulach@258
   361
            }
jtulach@258
   362
            sb.append(Field.getTypeName(getReturnType())).append(' ');
jtulach@258
   363
            sb.append(Field.getTypeName(getDeclaringClass())).append('.');
jtulach@258
   364
            sb.append(getName()).append('(');
jtulach@258
   365
            Class<?>[] params = parameterTypes; // avoid clone
jtulach@258
   366
            for (int j = 0; j < params.length; j++) {
jtulach@258
   367
                sb.append(Field.getTypeName(params[j]));
jtulach@258
   368
                if (j < (params.length - 1))
jtulach@258
   369
                    sb.append(',');
jtulach@258
   370
            }
jtulach@258
   371
            sb.append(')');
jtulach@258
   372
            Class<?>[] exceptions = exceptionTypes; // avoid clone
jtulach@258
   373
            if (exceptions.length > 0) {
jtulach@258
   374
                sb.append(" throws ");
jtulach@258
   375
                for (int k = 0; k < exceptions.length; k++) {
jtulach@258
   376
                    sb.append(exceptions[k].getName());
jtulach@258
   377
                    if (k < (exceptions.length - 1))
jtulach@258
   378
                        sb.append(',');
jtulach@258
   379
                }
jtulach@258
   380
            }
jtulach@258
   381
            return sb.toString();
jtulach@258
   382
        } catch (Exception e) {
jtulach@258
   383
            return "<" + e + ">";
jtulach@258
   384
        }
jtulach@258
   385
    }
jtulach@258
   386
jtulach@258
   387
    /**
jtulach@258
   388
     * Returns a string describing this {@code Method}, including
jtulach@258
   389
     * type parameters.  The string is formatted as the method access
jtulach@258
   390
     * modifiers, if any, followed by an angle-bracketed
jtulach@258
   391
     * comma-separated list of the method's type parameters, if any,
jtulach@258
   392
     * followed by the method's generic return type, followed by a
jtulach@258
   393
     * space, followed by the class declaring the method, followed by
jtulach@258
   394
     * a period, followed by the method name, followed by a
jtulach@258
   395
     * parenthesized, comma-separated list of the method's generic
jtulach@258
   396
     * formal parameter types.
jtulach@258
   397
     *
jtulach@258
   398
     * If this method was declared to take a variable number of
jtulach@258
   399
     * arguments, instead of denoting the last parameter as
jtulach@258
   400
     * "<tt><i>Type</i>[]</tt>", it is denoted as
jtulach@258
   401
     * "<tt><i>Type</i>...</tt>".
jtulach@258
   402
     *
jtulach@258
   403
     * A space is used to separate access modifiers from one another
jtulach@258
   404
     * and from the type parameters or return type.  If there are no
jtulach@258
   405
     * type parameters, the type parameter list is elided; if the type
jtulach@258
   406
     * parameter list is present, a space separates the list from the
jtulach@258
   407
     * class name.  If the method is declared to throw exceptions, the
jtulach@258
   408
     * parameter list is followed by a space, followed by the word
jtulach@258
   409
     * throws followed by a comma-separated list of the generic thrown
jtulach@258
   410
     * exception types.  If there are no type parameters, the type
jtulach@258
   411
     * parameter list is elided.
jtulach@258
   412
     *
jtulach@258
   413
     * <p>The access modifiers are placed in canonical order as
jtulach@258
   414
     * specified by "The Java Language Specification".  This is
jtulach@258
   415
     * {@code public}, {@code protected} or {@code private} first,
jtulach@258
   416
     * and then other modifiers in the following order:
jtulach@258
   417
     * {@code abstract}, {@code static}, {@code final},
jtulach@258
   418
     * {@code synchronized}, {@code native}, {@code strictfp}.
jtulach@258
   419
     *
jtulach@258
   420
     * @return a string describing this {@code Method},
jtulach@258
   421
     * include type parameters
jtulach@258
   422
     *
jtulach@258
   423
     * @since 1.5
jtulach@258
   424
     */
jtulach@258
   425
    public String toGenericString() {
jtulach@258
   426
        try {
jtulach@258
   427
            StringBuilder sb = new StringBuilder();
jtulach@258
   428
            int mod = getModifiers() & Modifier.methodModifiers();
jtulach@258
   429
            if (mod != 0) {
jtulach@258
   430
                sb.append(Modifier.toString(mod)).append(' ');
jtulach@258
   431
            }
jtulach@258
   432
            TypeVariable<?>[] typeparms = getTypeParameters();
jtulach@258
   433
            if (typeparms.length > 0) {
jtulach@258
   434
                boolean first = true;
jtulach@258
   435
                sb.append('<');
jtulach@258
   436
                for(TypeVariable<?> typeparm: typeparms) {
jtulach@258
   437
                    if (!first)
jtulach@258
   438
                        sb.append(',');
jtulach@258
   439
                    // Class objects can't occur here; no need to test
jtulach@258
   440
                    // and call Class.getName().
jtulach@258
   441
                    sb.append(typeparm.toString());
jtulach@258
   442
                    first = false;
jtulach@258
   443
                }
jtulach@258
   444
                sb.append("> ");
jtulach@258
   445
            }
jtulach@258
   446
jtulach@258
   447
            Type genRetType = getGenericReturnType();
jtulach@258
   448
            sb.append( ((genRetType instanceof Class<?>)?
jtulach@258
   449
                        Field.getTypeName((Class<?>)genRetType):genRetType.toString()))
jtulach@258
   450
                    .append(' ');
jtulach@258
   451
jtulach@258
   452
            sb.append(Field.getTypeName(getDeclaringClass())).append('.');
jtulach@258
   453
            sb.append(getName()).append('(');
jtulach@258
   454
            Type[] params = getGenericParameterTypes();
jtulach@258
   455
            for (int j = 0; j < params.length; j++) {
jtulach@258
   456
                String param = (params[j] instanceof Class)?
jtulach@258
   457
                    Field.getTypeName((Class)params[j]):
jtulach@258
   458
                    (params[j].toString());
jtulach@258
   459
                if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
jtulach@258
   460
                    param = param.replaceFirst("\\[\\]$", "...");
jtulach@258
   461
                sb.append(param);
jtulach@258
   462
                if (j < (params.length - 1))
jtulach@258
   463
                    sb.append(',');
jtulach@258
   464
            }
jtulach@258
   465
            sb.append(')');
jtulach@258
   466
            Type[] exceptions = getGenericExceptionTypes();
jtulach@258
   467
            if (exceptions.length > 0) {
jtulach@258
   468
                sb.append(" throws ");
jtulach@258
   469
                for (int k = 0; k < exceptions.length; k++) {
jtulach@258
   470
                    sb.append((exceptions[k] instanceof Class)?
jtulach@258
   471
                              ((Class)exceptions[k]).getName():
jtulach@258
   472
                              exceptions[k].toString());
jtulach@258
   473
                    if (k < (exceptions.length - 1))
jtulach@258
   474
                        sb.append(',');
jtulach@258
   475
                }
jtulach@258
   476
            }
jtulach@258
   477
            return sb.toString();
jtulach@258
   478
        } catch (Exception e) {
jtulach@258
   479
            return "<" + e + ">";
jtulach@258
   480
        }
jtulach@258
   481
    }
jtulach@258
   482
jtulach@258
   483
    /**
jtulach@258
   484
     * Invokes the underlying method represented by this {@code Method}
jtulach@258
   485
     * object, on the specified object with the specified parameters.
jtulach@258
   486
     * Individual parameters are automatically unwrapped to match
jtulach@258
   487
     * primitive formal parameters, and both primitive and reference
jtulach@258
   488
     * parameters are subject to method invocation conversions as
jtulach@258
   489
     * necessary.
jtulach@258
   490
     *
jtulach@258
   491
     * <p>If the underlying method is static, then the specified {@code obj}
jtulach@258
   492
     * argument is ignored. It may be null.
jtulach@258
   493
     *
jtulach@258
   494
     * <p>If the number of formal parameters required by the underlying method is
jtulach@258
   495
     * 0, the supplied {@code args} array may be of length 0 or null.
jtulach@258
   496
     *
jtulach@258
   497
     * <p>If the underlying method is an instance method, it is invoked
jtulach@258
   498
     * using dynamic method lookup as documented in The Java Language
jtulach@258
   499
     * Specification, Second Edition, section 15.12.4.4; in particular,
jtulach@258
   500
     * overriding based on the runtime type of the target object will occur.
jtulach@258
   501
     *
jtulach@258
   502
     * <p>If the underlying method is static, the class that declared
jtulach@258
   503
     * the method is initialized if it has not already been initialized.
jtulach@258
   504
     *
jtulach@258
   505
     * <p>If the method completes normally, the value it returns is
jtulach@258
   506
     * returned to the caller of invoke; if the value has a primitive
jtulach@258
   507
     * type, it is first appropriately wrapped in an object. However,
jtulach@258
   508
     * if the value has the type of an array of a primitive type, the
jtulach@258
   509
     * elements of the array are <i>not</i> wrapped in objects; in
jtulach@258
   510
     * other words, an array of primitive type is returned.  If the
jtulach@258
   511
     * underlying method return type is void, the invocation returns
jtulach@258
   512
     * null.
jtulach@258
   513
     *
jtulach@258
   514
     * @param obj  the object the underlying method is invoked from
jtulach@258
   515
     * @param args the arguments used for the method call
jtulach@258
   516
     * @return the result of dispatching the method represented by
jtulach@258
   517
     * this object on {@code obj} with parameters
jtulach@258
   518
     * {@code args}
jtulach@258
   519
     *
jtulach@258
   520
     * @exception IllegalAccessException    if this {@code Method} object
jtulach@258
   521
     *              is enforcing Java language access control and the underlying
jtulach@258
   522
     *              method is inaccessible.
jtulach@258
   523
     * @exception IllegalArgumentException  if the method is an
jtulach@258
   524
     *              instance method and the specified object argument
jtulach@258
   525
     *              is not an instance of the class or interface
jtulach@258
   526
     *              declaring the underlying method (or of a subclass
jtulach@258
   527
     *              or implementor thereof); if the number of actual
jtulach@258
   528
     *              and formal parameters differ; if an unwrapping
jtulach@258
   529
     *              conversion for primitive arguments fails; or if,
jtulach@258
   530
     *              after possible unwrapping, a parameter value
jtulach@258
   531
     *              cannot be converted to the corresponding formal
jtulach@258
   532
     *              parameter type by a method invocation conversion.
jtulach@258
   533
     * @exception InvocationTargetException if the underlying method
jtulach@258
   534
     *              throws an exception.
jtulach@258
   535
     * @exception NullPointerException      if the specified object is null
jtulach@258
   536
     *              and the method is an instance method.
jtulach@258
   537
     * @exception ExceptionInInitializerError if the initialization
jtulach@258
   538
     * provoked by this method fails.
jtulach@258
   539
     */
jtulach@258
   540
    public Object invoke(Object obj, Object... args)
jtulach@258
   541
        throws IllegalAccessException, IllegalArgumentException,
jtulach@258
   542
           InvocationTargetException
jtulach@258
   543
    {
jaroslav@260
   544
        throw new UnsupportedOperationException();
jtulach@258
   545
    }
jtulach@258
   546
jtulach@258
   547
    /**
jtulach@258
   548
     * Returns {@code true} if this method is a bridge
jtulach@258
   549
     * method; returns {@code false} otherwise.
jtulach@258
   550
     *
jtulach@258
   551
     * @return true if and only if this method is a bridge
jtulach@258
   552
     * method as defined by the Java Language Specification.
jtulach@258
   553
     * @since 1.5
jtulach@258
   554
     */
jtulach@258
   555
    public boolean isBridge() {
jtulach@258
   556
        return (getModifiers() & Modifier.BRIDGE) != 0;
jtulach@258
   557
    }
jtulach@258
   558
jtulach@258
   559
    /**
jtulach@258
   560
     * Returns {@code true} if this method was declared to take
jtulach@258
   561
     * a variable number of arguments; returns {@code false}
jtulach@258
   562
     * otherwise.
jtulach@258
   563
     *
jtulach@258
   564
     * @return {@code true} if an only if this method was declared to
jtulach@258
   565
     * take a variable number of arguments.
jtulach@258
   566
     * @since 1.5
jtulach@258
   567
     */
jtulach@258
   568
    public boolean isVarArgs() {
jtulach@258
   569
        return (getModifiers() & Modifier.VARARGS) != 0;
jtulach@258
   570
    }
jtulach@258
   571
jtulach@258
   572
    /**
jtulach@258
   573
     * Returns {@code true} if this method is a synthetic
jtulach@258
   574
     * method; returns {@code false} otherwise.
jtulach@258
   575
     *
jtulach@258
   576
     * @return true if and only if this method is a synthetic
jtulach@258
   577
     * method as defined by the Java Language Specification.
jtulach@258
   578
     * @since 1.5
jtulach@258
   579
     */
jtulach@258
   580
    public boolean isSynthetic() {
jtulach@258
   581
        return Modifier.isSynthetic(getModifiers());
jtulach@258
   582
    }
jtulach@258
   583
jtulach@258
   584
    /**
jtulach@258
   585
     * @throws NullPointerException {@inheritDoc}
jtulach@258
   586
     * @since 1.5
jtulach@258
   587
     */
jtulach@258
   588
    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
jtulach@258
   589
        if (annotationClass == null)
jtulach@258
   590
            throw new NullPointerException();
jtulach@258
   591
jaroslav@260
   592
        throw new UnsupportedOperationException();
jtulach@258
   593
    }
jtulach@258
   594
jtulach@258
   595
    /**
jtulach@258
   596
     * @since 1.5
jtulach@258
   597
     */
jtulach@258
   598
    public Annotation[] getDeclaredAnnotations()  {
jaroslav@260
   599
        throw new UnsupportedOperationException();
jtulach@258
   600
    }
jtulach@258
   601
jtulach@258
   602
    /**
jtulach@258
   603
     * Returns the default value for the annotation member represented by
jtulach@258
   604
     * this {@code Method} instance.  If the member is of a primitive type,
jtulach@258
   605
     * an instance of the corresponding wrapper type is returned. Returns
jtulach@258
   606
     * null if no default is associated with the member, or if the method
jtulach@258
   607
     * instance does not represent a declared member of an annotation type.
jtulach@258
   608
     *
jtulach@258
   609
     * @return the default value for the annotation member represented
jtulach@258
   610
     *     by this {@code Method} instance.
jtulach@258
   611
     * @throws TypeNotPresentException if the annotation is of type
jtulach@258
   612
     *     {@link Class} and no definition can be found for the
jtulach@258
   613
     *     default class value.
jtulach@258
   614
     * @since  1.5
jtulach@258
   615
     */
jtulach@258
   616
    public Object getDefaultValue() {
jtulach@258
   617
        if  (annotationDefault == null)
jtulach@258
   618
            return null;
jaroslav@260
   619
        throw new UnsupportedOperationException();
jtulach@258
   620
    }
jtulach@258
   621
jtulach@258
   622
    /**
jtulach@258
   623
     * Returns an array of arrays that represent the annotations on the formal
jtulach@258
   624
     * parameters, in declaration order, of the method represented by
jtulach@258
   625
     * this {@code Method} object. (Returns an array of length zero if the
jtulach@258
   626
     * underlying method is parameterless.  If the method has one or more
jtulach@258
   627
     * parameters, a nested array of length zero is returned for each parameter
jtulach@258
   628
     * with no annotations.) The annotation objects contained in the returned
jtulach@258
   629
     * arrays are serializable.  The caller of this method is free to modify
jtulach@258
   630
     * the returned arrays; it will have no effect on the arrays returned to
jtulach@258
   631
     * other callers.
jtulach@258
   632
     *
jtulach@258
   633
     * @return an array of arrays that represent the annotations on the formal
jtulach@258
   634
     *    parameters, in declaration order, of the method represented by this
jtulach@258
   635
     *    Method object
jtulach@258
   636
     * @since 1.5
jtulach@258
   637
     */
jtulach@258
   638
    public Annotation[][] getParameterAnnotations() {
jtulach@258
   639
        int numParameters = parameterTypes.length;
jtulach@258
   640
        if (parameterAnnotations == null)
jtulach@258
   641
            return new Annotation[numParameters][0];
jtulach@258
   642
jaroslav@260
   643
        throw new UnsupportedOperationException();
jtulach@258
   644
    }
jtulach@258
   645
}