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