rt/emul/mini/src/main/java/java/lang/reflect/Method.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 23 Sep 2014 21:52:27 +0200
changeset 1702 228f26fc1159
parent 1541 3471d74a6b99
child 1838 493d5960d520
permissions -rw-r--r--
for (var x in array) should return only expected values
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@418
    29
import java.util.Enumeration;
jaroslav@262
    30
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@555
    31
import org.apidesign.bck2brwsr.emul.reflect.AnnotationImpl;
jaroslav@555
    32
import org.apidesign.bck2brwsr.emul.reflect.MethodImpl;
jtulach@258
    33
jtulach@258
    34
/**
jtulach@258
    35
 * A {@code Method} provides information about, and access to, a single method
jtulach@258
    36
 * on a class or interface.  The reflected method may be a class method
jtulach@258
    37
 * or an instance method (including an abstract method).
jtulach@258
    38
 *
jtulach@258
    39
 * <p>A {@code Method} permits widening conversions to occur when matching the
jtulach@258
    40
 * actual parameters to invoke with the underlying method's formal
jtulach@258
    41
 * parameters, but it throws an {@code IllegalArgumentException} if a
jtulach@258
    42
 * narrowing conversion would occur.
jtulach@258
    43
 *
jtulach@258
    44
 * @see Member
jtulach@258
    45
 * @see java.lang.Class
jtulach@258
    46
 * @see java.lang.Class#getMethods()
jtulach@258
    47
 * @see java.lang.Class#getMethod(String, Class[])
jtulach@258
    48
 * @see java.lang.Class#getDeclaredMethods()
jtulach@258
    49
 * @see java.lang.Class#getDeclaredMethod(String, Class[])
jtulach@258
    50
 *
jtulach@258
    51
 * @author Kenneth Russell
jtulach@258
    52
 * @author Nakul Saraiya
jtulach@258
    53
 */
jtulach@258
    54
public final
jtulach@258
    55
    class Method extends AccessibleObject implements GenericDeclaration,
jtulach@258
    56
                                                     Member {
jaroslav@262
    57
    private final Class<?> clazz;
jaroslav@262
    58
    private final String name;
jaroslav@262
    59
    private final Object data;
jaroslav@264
    60
    private final String sig;
jtulach@258
    61
jtulach@258
    62
   // Generics infrastructure
jtulach@258
    63
jaroslav@262
    64
    private String getGenericSignature() {return null;}
jtulach@258
    65
jtulach@258
    66
    /**
jtulach@258
    67
     * Package-private constructor used by ReflectAccess to enable
jtulach@258
    68
     * instantiation of these objects in Java code from the java.lang
jtulach@258
    69
     * package via sun.reflect.LangReflectAccess.
jtulach@258
    70
     */
jaroslav@264
    71
    Method(Class<?> declaringClass, String name, Object data, String sig)
jtulach@258
    72
    {
jtulach@258
    73
        this.clazz = declaringClass;
jtulach@258
    74
        this.name = name;
jaroslav@262
    75
        this.data = data;
jaroslav@264
    76
        this.sig = sig;
jtulach@258
    77
    }
jtulach@258
    78
jtulach@258
    79
    /**
jtulach@258
    80
     * Package-private routine (exposed to java.lang.Class via
jtulach@258
    81
     * ReflectAccess) which returns a copy of this Method. The copy's
jtulach@258
    82
     * "root" field points to this Method.
jtulach@258
    83
     */
jtulach@258
    84
    Method copy() {
jaroslav@262
    85
        return this;
jtulach@258
    86
    }
jtulach@258
    87
jtulach@258
    88
    /**
jtulach@258
    89
     * Returns the {@code Class} object representing the class or interface
jtulach@258
    90
     * that declares the method represented by this {@code Method} object.
jtulach@258
    91
     */
jtulach@258
    92
    public Class<?> getDeclaringClass() {
jtulach@258
    93
        return clazz;
jtulach@258
    94
    }
jtulach@258
    95
jtulach@258
    96
    /**
jtulach@258
    97
     * Returns the name of the method represented by this {@code Method}
jtulach@258
    98
     * object, as a {@code String}.
jtulach@258
    99
     */
jtulach@258
   100
    public String getName() {
jtulach@258
   101
        return name;
jtulach@258
   102
    }
jtulach@258
   103
jtulach@258
   104
    /**
jtulach@258
   105
     * Returns the Java language modifiers for the method represented
jtulach@258
   106
     * by this {@code Method} object, as an integer. The {@code Modifier} class should
jtulach@258
   107
     * be used to decode the modifiers.
jtulach@258
   108
     *
jtulach@258
   109
     * @see Modifier
jtulach@258
   110
     */
jtulach@258
   111
    public int getModifiers() {
jaroslav@392
   112
        return getAccess(data);
jtulach@258
   113
    }
jaroslav@392
   114
    
jaroslav@392
   115
    @JavaScriptBody(args = "self", body = "return self.access;")
jaroslav@1321
   116
    static native int getAccess(Object self);
jaroslav@392
   117
    
jtulach@258
   118
    /**
jtulach@258
   119
     * Returns an array of {@code TypeVariable} objects that represent the
jtulach@258
   120
     * type variables declared by the generic declaration represented by this
jtulach@258
   121
     * {@code GenericDeclaration} object, in declaration order.  Returns an
jtulach@258
   122
     * array of length 0 if the underlying generic declaration declares no type
jtulach@258
   123
     * variables.
jtulach@258
   124
     *
jtulach@258
   125
     * @return an array of {@code TypeVariable} objects that represent
jtulach@258
   126
     *     the type variables declared by this generic declaration
jtulach@258
   127
     * @throws GenericSignatureFormatError if the generic
jtulach@258
   128
     *     signature of this generic declaration does not conform to
jtulach@258
   129
     *     the format specified in
jtulach@258
   130
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
jtulach@258
   131
     * @since 1.5
jtulach@258
   132
     */
jtulach@258
   133
    public TypeVariable<Method>[] getTypeParameters() {
jaroslav@260
   134
        throw new UnsupportedOperationException();
jtulach@258
   135
    }
jtulach@258
   136
jtulach@258
   137
    /**
jtulach@258
   138
     * Returns a {@code Class} object that represents the formal return type
jtulach@258
   139
     * of the method represented by this {@code Method} object.
jtulach@258
   140
     *
jtulach@258
   141
     * @return the return type for the method this object represents
jtulach@258
   142
     */
jtulach@258
   143
    public Class<?> getReturnType() {
jaroslav@418
   144
        return MethodImpl.signatureParser(sig).nextElement();
jtulach@258
   145
    }
jtulach@258
   146
jtulach@258
   147
    /**
jtulach@258
   148
     * Returns a {@code Type} object that represents the formal return
jtulach@258
   149
     * type of the method represented by this {@code Method} object.
jtulach@258
   150
     *
jtulach@258
   151
     * <p>If the return type is a parameterized type,
jtulach@258
   152
     * the {@code Type} object returned must accurately reflect
jtulach@258
   153
     * the actual type parameters used in the source code.
jtulach@258
   154
     *
jtulach@258
   155
     * <p>If the return type is a type variable or a parameterized type, it
jtulach@258
   156
     * is created. Otherwise, it is resolved.
jtulach@258
   157
     *
jtulach@258
   158
     * @return  a {@code Type} object that represents the formal return
jtulach@258
   159
     *     type of the underlying  method
jtulach@258
   160
     * @throws GenericSignatureFormatError
jtulach@258
   161
     *     if the generic method signature does not conform to the format
jtulach@258
   162
     *     specified in
jtulach@258
   163
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
jtulach@258
   164
     * @throws TypeNotPresentException if the underlying method's
jtulach@258
   165
     *     return type refers to a non-existent type declaration
jtulach@258
   166
     * @throws MalformedParameterizedTypeException if the
jtulach@258
   167
     *     underlying method's return typed refers to a parameterized
jtulach@258
   168
     *     type that cannot be instantiated for any reason
jtulach@258
   169
     * @since 1.5
jtulach@258
   170
     */
jtulach@258
   171
    public Type getGenericReturnType() {
jaroslav@260
   172
        throw new UnsupportedOperationException();
jtulach@258
   173
    }
jtulach@258
   174
jtulach@258
   175
jtulach@258
   176
    /**
jtulach@258
   177
     * Returns an array of {@code Class} objects that represent the formal
jtulach@258
   178
     * parameter types, in declaration order, of the method
jtulach@258
   179
     * represented by this {@code Method} object.  Returns an array of length
jtulach@258
   180
     * 0 if the underlying method takes no parameters.
jtulach@258
   181
     *
jtulach@258
   182
     * @return the parameter types for the method this object
jtulach@258
   183
     * represents
jtulach@258
   184
     */
jtulach@258
   185
    public Class<?>[] getParameterTypes() {
jaroslav@1321
   186
        return getParameterTypes(sig);
jaroslav@1321
   187
    }
jaroslav@1321
   188
    
jaroslav@1321
   189
    static Class<?>[] getParameterTypes(String sig) {
jaroslav@418
   190
        Class[] arr = new Class[MethodImpl.signatureElements(sig) - 1];
jaroslav@418
   191
        Enumeration<Class> en = MethodImpl.signatureParser(sig);
jaroslav@418
   192
        en.nextElement(); // return type
jaroslav@418
   193
        for (int i = 0; i < arr.length; i++) {
jaroslav@418
   194
            arr[i] = en.nextElement();
jaroslav@418
   195
        }
jaroslav@418
   196
        return arr;
jtulach@258
   197
    }
jtulach@258
   198
jtulach@258
   199
    /**
jtulach@258
   200
     * Returns an array of {@code Type} objects that represent the formal
jtulach@258
   201
     * parameter types, in declaration order, of the method represented by
jtulach@258
   202
     * this {@code Method} object. Returns an array of length 0 if the
jtulach@258
   203
     * underlying method takes no parameters.
jtulach@258
   204
     *
jtulach@258
   205
     * <p>If a formal parameter type is a parameterized type,
jtulach@258
   206
     * the {@code Type} object returned for it must accurately reflect
jtulach@258
   207
     * the actual type parameters used in the source code.
jtulach@258
   208
     *
jtulach@258
   209
     * <p>If a formal parameter type is a type variable or a parameterized
jtulach@258
   210
     * type, it is created. Otherwise, it is resolved.
jtulach@258
   211
     *
jtulach@258
   212
     * @return an array of Types that represent the formal
jtulach@258
   213
     *     parameter types of the underlying method, in declaration order
jtulach@258
   214
     * @throws GenericSignatureFormatError
jtulach@258
   215
     *     if the generic method signature does not conform to the format
jtulach@258
   216
     *     specified in
jtulach@258
   217
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
jtulach@258
   218
     * @throws TypeNotPresentException if any of the parameter
jtulach@258
   219
     *     types of the underlying method refers to a non-existent type
jtulach@258
   220
     *     declaration
jtulach@258
   221
     * @throws MalformedParameterizedTypeException if any of
jtulach@258
   222
     *     the underlying method's parameter types refer to a parameterized
jtulach@258
   223
     *     type that cannot be instantiated for any reason
jtulach@258
   224
     * @since 1.5
jtulach@258
   225
     */
jtulach@258
   226
    public Type[] getGenericParameterTypes() {
jaroslav@260
   227
        throw new UnsupportedOperationException();
jtulach@258
   228
    }
jtulach@258
   229
jtulach@258
   230
jtulach@258
   231
    /**
jtulach@258
   232
     * Returns an array of {@code Class} objects that represent
jtulach@258
   233
     * the types of the exceptions declared to be thrown
jtulach@258
   234
     * by the underlying method
jtulach@258
   235
     * represented by this {@code Method} object.  Returns an array of length
jtulach@258
   236
     * 0 if the method declares no exceptions in its {@code throws} clause.
jtulach@258
   237
     *
jtulach@258
   238
     * @return the exception types declared as being thrown by the
jtulach@258
   239
     * method this object represents
jtulach@258
   240
     */
jtulach@258
   241
    public Class<?>[] getExceptionTypes() {
jaroslav@1377
   242
        return new Class[0];
jtulach@258
   243
    }
jtulach@258
   244
jtulach@258
   245
    /**
jtulach@258
   246
     * Returns an array of {@code Type} objects that represent the
jtulach@258
   247
     * exceptions declared to be thrown by this {@code Method} object.
jtulach@258
   248
     * Returns an array of length 0 if the underlying method declares
jtulach@258
   249
     * no exceptions in its {@code throws} clause.
jtulach@258
   250
     *
jtulach@258
   251
     * <p>If an exception type is a type variable or a parameterized
jtulach@258
   252
     * type, it is created. Otherwise, it is resolved.
jtulach@258
   253
     *
jtulach@258
   254
     * @return an array of Types that represent the exception types
jtulach@258
   255
     *     thrown by the underlying method
jtulach@258
   256
     * @throws GenericSignatureFormatError
jtulach@258
   257
     *     if the generic method signature does not conform to the format
jtulach@258
   258
     *     specified in
jtulach@258
   259
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
jtulach@258
   260
     * @throws TypeNotPresentException if the underlying method's
jtulach@258
   261
     *     {@code throws} clause refers to a non-existent type declaration
jtulach@258
   262
     * @throws MalformedParameterizedTypeException if
jtulach@258
   263
     *     the underlying method's {@code throws} clause refers to a
jtulach@258
   264
     *     parameterized type that cannot be instantiated for any reason
jtulach@258
   265
     * @since 1.5
jtulach@258
   266
     */
jtulach@258
   267
      public Type[] getGenericExceptionTypes() {
jaroslav@260
   268
        throw new UnsupportedOperationException();
jtulach@258
   269
      }
jtulach@258
   270
jtulach@258
   271
    /**
jtulach@258
   272
     * Compares this {@code Method} against the specified object.  Returns
jtulach@258
   273
     * true if the objects are the same.  Two {@code Methods} are the same if
jtulach@258
   274
     * they were declared by the same class and have the same name
jtulach@258
   275
     * and formal parameter types and return type.
jtulach@258
   276
     */
jtulach@258
   277
    public boolean equals(Object obj) {
jtulach@258
   278
        if (obj != null && obj instanceof Method) {
jtulach@258
   279
            Method other = (Method)obj;
jaroslav@262
   280
            return data == other.data;
jtulach@258
   281
        }
jtulach@258
   282
        return false;
jtulach@258
   283
    }
jtulach@258
   284
jtulach@258
   285
    /**
jtulach@258
   286
     * Returns a hashcode for this {@code Method}.  The hashcode is computed
jtulach@258
   287
     * as the exclusive-or of the hashcodes for the underlying
jtulach@258
   288
     * method's declaring class name and the method's name.
jtulach@258
   289
     */
jtulach@258
   290
    public int hashCode() {
jtulach@258
   291
        return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
jtulach@258
   292
    }
jtulach@258
   293
jtulach@258
   294
    /**
jtulach@258
   295
     * Returns a string describing this {@code Method}.  The string is
jtulach@258
   296
     * formatted as the method access modifiers, if any, followed by
jtulach@258
   297
     * the method return type, followed by a space, followed by the
jtulach@258
   298
     * class declaring the method, followed by a period, followed by
jtulach@258
   299
     * the method name, followed by a parenthesized, comma-separated
jtulach@258
   300
     * list of the method's formal parameter types. If the method
jtulach@258
   301
     * throws checked exceptions, the parameter list is followed by a
jtulach@258
   302
     * space, followed by the word throws followed by a
jtulach@258
   303
     * comma-separated list of the thrown exception types.
jtulach@258
   304
     * For example:
jtulach@258
   305
     * <pre>
jtulach@258
   306
     *    public boolean java.lang.Object.equals(java.lang.Object)
jtulach@258
   307
     * </pre>
jtulach@258
   308
     *
jtulach@258
   309
     * <p>The access modifiers are placed in canonical order as
jtulach@258
   310
     * specified by "The Java Language Specification".  This is
jtulach@258
   311
     * {@code public}, {@code protected} or {@code private} first,
jtulach@258
   312
     * and then other modifiers in the following order:
jtulach@258
   313
     * {@code abstract}, {@code static}, {@code final},
jtulach@258
   314
     * {@code synchronized}, {@code native}, {@code strictfp}.
jtulach@258
   315
     */
jtulach@258
   316
    public String toString() {
jtulach@258
   317
        try {
jtulach@258
   318
            StringBuilder sb = new StringBuilder();
jtulach@258
   319
            int mod = getModifiers() & Modifier.methodModifiers();
jtulach@258
   320
            if (mod != 0) {
jtulach@258
   321
                sb.append(Modifier.toString(mod)).append(' ');
jtulach@258
   322
            }
jtulach@258
   323
            sb.append(Field.getTypeName(getReturnType())).append(' ');
jtulach@258
   324
            sb.append(Field.getTypeName(getDeclaringClass())).append('.');
jtulach@258
   325
            sb.append(getName()).append('(');
jaroslav@420
   326
            Class<?>[] params = getParameterTypes(); // avoid clone
jtulach@258
   327
            for (int j = 0; j < params.length; j++) {
jtulach@258
   328
                sb.append(Field.getTypeName(params[j]));
jtulach@258
   329
                if (j < (params.length - 1))
jtulach@258
   330
                    sb.append(',');
jtulach@258
   331
            }
jtulach@258
   332
            sb.append(')');
jaroslav@420
   333
            /*
jtulach@258
   334
            Class<?>[] exceptions = exceptionTypes; // avoid clone
jtulach@258
   335
            if (exceptions.length > 0) {
jtulach@258
   336
                sb.append(" throws ");
jtulach@258
   337
                for (int k = 0; k < exceptions.length; k++) {
jtulach@258
   338
                    sb.append(exceptions[k].getName());
jtulach@258
   339
                    if (k < (exceptions.length - 1))
jtulach@258
   340
                        sb.append(',');
jtulach@258
   341
                }
jtulach@258
   342
            }
jaroslav@262
   343
            */
jtulach@258
   344
            return sb.toString();
jtulach@258
   345
        } catch (Exception e) {
jtulach@258
   346
            return "<" + e + ">";
jtulach@258
   347
        }
jtulach@258
   348
    }
jtulach@258
   349
jtulach@258
   350
    /**
jtulach@258
   351
     * Returns a string describing this {@code Method}, including
jtulach@258
   352
     * type parameters.  The string is formatted as the method access
jtulach@258
   353
     * modifiers, if any, followed by an angle-bracketed
jtulach@258
   354
     * comma-separated list of the method's type parameters, if any,
jtulach@258
   355
     * followed by the method's generic return type, followed by a
jtulach@258
   356
     * space, followed by the class declaring the method, followed by
jtulach@258
   357
     * a period, followed by the method name, followed by a
jtulach@258
   358
     * parenthesized, comma-separated list of the method's generic
jtulach@258
   359
     * formal parameter types.
jtulach@258
   360
     *
jtulach@258
   361
     * If this method was declared to take a variable number of
jtulach@258
   362
     * arguments, instead of denoting the last parameter as
jtulach@258
   363
     * "<tt><i>Type</i>[]</tt>", it is denoted as
jtulach@258
   364
     * "<tt><i>Type</i>...</tt>".
jtulach@258
   365
     *
jtulach@258
   366
     * A space is used to separate access modifiers from one another
jtulach@258
   367
     * and from the type parameters or return type.  If there are no
jtulach@258
   368
     * type parameters, the type parameter list is elided; if the type
jtulach@258
   369
     * parameter list is present, a space separates the list from the
jtulach@258
   370
     * class name.  If the method is declared to throw exceptions, the
jtulach@258
   371
     * parameter list is followed by a space, followed by the word
jtulach@258
   372
     * throws followed by a comma-separated list of the generic thrown
jtulach@258
   373
     * exception types.  If there are no type parameters, the type
jtulach@258
   374
     * parameter list is elided.
jtulach@258
   375
     *
jtulach@258
   376
     * <p>The access modifiers are placed in canonical order as
jtulach@258
   377
     * specified by "The Java Language Specification".  This is
jtulach@258
   378
     * {@code public}, {@code protected} or {@code private} first,
jtulach@258
   379
     * and then other modifiers in the following order:
jtulach@258
   380
     * {@code abstract}, {@code static}, {@code final},
jtulach@258
   381
     * {@code synchronized}, {@code native}, {@code strictfp}.
jtulach@258
   382
     *
jtulach@258
   383
     * @return a string describing this {@code Method},
jtulach@258
   384
     * include type parameters
jtulach@258
   385
     *
jtulach@258
   386
     * @since 1.5
jtulach@258
   387
     */
jtulach@258
   388
    public String toGenericString() {
jtulach@258
   389
        try {
jtulach@258
   390
            StringBuilder sb = new StringBuilder();
jtulach@258
   391
            int mod = getModifiers() & Modifier.methodModifiers();
jtulach@258
   392
            if (mod != 0) {
jtulach@258
   393
                sb.append(Modifier.toString(mod)).append(' ');
jtulach@258
   394
            }
jtulach@258
   395
            TypeVariable<?>[] typeparms = getTypeParameters();
jtulach@258
   396
            if (typeparms.length > 0) {
jtulach@258
   397
                boolean first = true;
jtulach@258
   398
                sb.append('<');
jtulach@258
   399
                for(TypeVariable<?> typeparm: typeparms) {
jtulach@258
   400
                    if (!first)
jtulach@258
   401
                        sb.append(',');
jtulach@258
   402
                    // Class objects can't occur here; no need to test
jtulach@258
   403
                    // and call Class.getName().
jtulach@258
   404
                    sb.append(typeparm.toString());
jtulach@258
   405
                    first = false;
jtulach@258
   406
                }
jtulach@258
   407
                sb.append("> ");
jtulach@258
   408
            }
jtulach@258
   409
jtulach@258
   410
            Type genRetType = getGenericReturnType();
jtulach@258
   411
            sb.append( ((genRetType instanceof Class<?>)?
jtulach@258
   412
                        Field.getTypeName((Class<?>)genRetType):genRetType.toString()))
jtulach@258
   413
                    .append(' ');
jtulach@258
   414
jtulach@258
   415
            sb.append(Field.getTypeName(getDeclaringClass())).append('.');
jtulach@258
   416
            sb.append(getName()).append('(');
jtulach@258
   417
            Type[] params = getGenericParameterTypes();
jtulach@258
   418
            for (int j = 0; j < params.length; j++) {
jtulach@258
   419
                String param = (params[j] instanceof Class)?
jtulach@258
   420
                    Field.getTypeName((Class)params[j]):
jtulach@258
   421
                    (params[j].toString());
jtulach@258
   422
                if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
jtulach@258
   423
                    param = param.replaceFirst("\\[\\]$", "...");
jtulach@258
   424
                sb.append(param);
jtulach@258
   425
                if (j < (params.length - 1))
jtulach@258
   426
                    sb.append(',');
jtulach@258
   427
            }
jtulach@258
   428
            sb.append(')');
jtulach@258
   429
            Type[] exceptions = getGenericExceptionTypes();
jtulach@258
   430
            if (exceptions.length > 0) {
jtulach@258
   431
                sb.append(" throws ");
jtulach@258
   432
                for (int k = 0; k < exceptions.length; k++) {
jtulach@258
   433
                    sb.append((exceptions[k] instanceof Class)?
jtulach@258
   434
                              ((Class)exceptions[k]).getName():
jtulach@258
   435
                              exceptions[k].toString());
jtulach@258
   436
                    if (k < (exceptions.length - 1))
jtulach@258
   437
                        sb.append(',');
jtulach@258
   438
                }
jtulach@258
   439
            }
jtulach@258
   440
            return sb.toString();
jtulach@258
   441
        } catch (Exception e) {
jtulach@258
   442
            return "<" + e + ">";
jtulach@258
   443
        }
jtulach@258
   444
    }
jtulach@258
   445
jtulach@258
   446
    /**
jtulach@258
   447
     * Invokes the underlying method represented by this {@code Method}
jtulach@258
   448
     * object, on the specified object with the specified parameters.
jtulach@258
   449
     * Individual parameters are automatically unwrapped to match
jtulach@258
   450
     * primitive formal parameters, and both primitive and reference
jtulach@258
   451
     * parameters are subject to method invocation conversions as
jtulach@258
   452
     * necessary.
jtulach@258
   453
     *
jtulach@258
   454
     * <p>If the underlying method is static, then the specified {@code obj}
jtulach@258
   455
     * argument is ignored. It may be null.
jtulach@258
   456
     *
jtulach@258
   457
     * <p>If the number of formal parameters required by the underlying method is
jtulach@258
   458
     * 0, the supplied {@code args} array may be of length 0 or null.
jtulach@258
   459
     *
jtulach@258
   460
     * <p>If the underlying method is an instance method, it is invoked
jtulach@258
   461
     * using dynamic method lookup as documented in The Java Language
jtulach@258
   462
     * Specification, Second Edition, section 15.12.4.4; in particular,
jtulach@258
   463
     * overriding based on the runtime type of the target object will occur.
jtulach@258
   464
     *
jtulach@258
   465
     * <p>If the underlying method is static, the class that declared
jtulach@258
   466
     * the method is initialized if it has not already been initialized.
jtulach@258
   467
     *
jtulach@258
   468
     * <p>If the method completes normally, the value it returns is
jtulach@258
   469
     * returned to the caller of invoke; if the value has a primitive
jtulach@258
   470
     * type, it is first appropriately wrapped in an object. However,
jtulach@258
   471
     * if the value has the type of an array of a primitive type, the
jtulach@258
   472
     * elements of the array are <i>not</i> wrapped in objects; in
jtulach@258
   473
     * other words, an array of primitive type is returned.  If the
jtulach@258
   474
     * underlying method return type is void, the invocation returns
jtulach@258
   475
     * null.
jtulach@258
   476
     *
jtulach@258
   477
     * @param obj  the object the underlying method is invoked from
jtulach@258
   478
     * @param args the arguments used for the method call
jtulach@258
   479
     * @return the result of dispatching the method represented by
jtulach@258
   480
     * this object on {@code obj} with parameters
jtulach@258
   481
     * {@code args}
jtulach@258
   482
     *
jtulach@258
   483
     * @exception IllegalAccessException    if this {@code Method} object
jtulach@258
   484
     *              is enforcing Java language access control and the underlying
jtulach@258
   485
     *              method is inaccessible.
jtulach@258
   486
     * @exception IllegalArgumentException  if the method is an
jtulach@258
   487
     *              instance method and the specified object argument
jtulach@258
   488
     *              is not an instance of the class or interface
jtulach@258
   489
     *              declaring the underlying method (or of a subclass
jtulach@258
   490
     *              or implementor thereof); if the number of actual
jtulach@258
   491
     *              and formal parameters differ; if an unwrapping
jtulach@258
   492
     *              conversion for primitive arguments fails; or if,
jtulach@258
   493
     *              after possible unwrapping, a parameter value
jtulach@258
   494
     *              cannot be converted to the corresponding formal
jtulach@258
   495
     *              parameter type by a method invocation conversion.
jtulach@258
   496
     * @exception InvocationTargetException if the underlying method
jtulach@258
   497
     *              throws an exception.
jtulach@258
   498
     * @exception NullPointerException      if the specified object is null
jtulach@258
   499
     *              and the method is an instance method.
jtulach@258
   500
     * @exception ExceptionInInitializerError if the initialization
jtulach@258
   501
     * provoked by this method fails.
jtulach@258
   502
     */
jaroslav@354
   503
    public Object invoke(Object obj, Object... args)
jaroslav@354
   504
        throws IllegalAccessException, IllegalArgumentException,
jaroslav@354
   505
           InvocationTargetException
jaroslav@354
   506
    {
jaroslav@938
   507
        final boolean nonStatic = (getModifiers() & Modifier.STATIC) == 0;
jaroslav@938
   508
        if (nonStatic && obj == null) {
jaroslav@413
   509
            throw new NullPointerException();
jaroslav@413
   510
        }
jaroslav@418
   511
        Class[] types = getParameterTypes();
jaroslav@418
   512
        if (types.length != args.length) {
jaroslav@418
   513
            throw new IllegalArgumentException("Types len " + types.length + " args: " + args.length);
jaroslav@418
   514
        } else {
jaroslav@418
   515
            args = args.clone();
jaroslav@418
   516
            for (int i = 0; i < types.length; i++) {
jaroslav@418
   517
                Class c = types[i];
jaroslav@792
   518
                if (c.isPrimitive() && args[i] != null) {
jaroslav@792
   519
                    args[i] = toPrimitive(args[i]);
jaroslav@418
   520
                }
jaroslav@418
   521
            }
jaroslav@418
   522
        }
jaroslav@938
   523
        Object res = invokeTry(nonStatic, this, obj, args);
jaroslav@354
   524
        if (getReturnType().isPrimitive()) {
jaroslav@354
   525
            res = fromPrimitive(getReturnType(), res);
jaroslav@354
   526
        }
jaroslav@354
   527
        return res;
jaroslav@354
   528
    }
jaroslav@354
   529
    
jaroslav@418
   530
    @JavaScriptBody(args = { "st", "method", "self", "args" }, body =
jaroslav@1470
   531
          "var p; var cll;\n"
jaroslav@418
   532
        + "if (st) {\n"
jaroslav@1470
   533
        + "  cll = self[method._name() + '__' + method._sig()];\n"
jaroslav@418
   534
        + "  p = new Array(1);\n"
jaroslav@418
   535
        + "  p[0] = self;\n"
jaroslav@418
   536
        + "  p = p.concat(args);\n"
jaroslav@418
   537
        + "} else {\n"
jaroslav@418
   538
        + "  p = args;\n"
jaroslav@1470
   539
        + "  cll = method._data();"
jaroslav@418
   540
        + "}\n"
jaroslav@1470
   541
        + "return cll.apply(self, p);\n"
jaroslav@262
   542
    )
jaroslav@418
   543
    private static native Object invoke0(boolean isStatic, Method m, Object self, Object[] args);
jaroslav@938
   544
    
jaroslav@938
   545
    private static Object invokeTry(boolean isStatic, Method m, Object self, Object[] args)
jaroslav@938
   546
    throws InvocationTargetException {
jaroslav@938
   547
        try {
jaroslav@938
   548
            return invoke0(isStatic, m, self, args);
jaroslav@938
   549
        } catch (Throwable ex) {
jaroslav@938
   550
            throw new InvocationTargetException(ex, ex.getMessage());
jaroslav@938
   551
        }
jaroslav@938
   552
    }
jaroslav@354
   553
jaroslav@486
   554
    static Object fromPrimitive(Class<?> type, Object o) {
jaroslav@1541
   555
        if (samePrimitive(type, Integer.TYPE)) {
jaroslav@355
   556
            return fromRaw(Integer.class, "valueOf__Ljava_lang_Integer_2I", o);
jaroslav@354
   557
        }
jaroslav@1541
   558
        if (samePrimitive(type, Long.TYPE)) {
jaroslav@355
   559
            return fromRaw(Long.class, "valueOf__Ljava_lang_Long_2J", o);
jaroslav@355
   560
        }
jaroslav@1541
   561
        if (samePrimitive(type, Double.TYPE)) {
jaroslav@355
   562
            return fromRaw(Double.class, "valueOf__Ljava_lang_Double_2D", o);
jaroslav@355
   563
        }
jaroslav@1541
   564
        if (samePrimitive(type, Float.TYPE)) {
jaroslav@355
   565
            return fromRaw(Float.class, "valueOf__Ljava_lang_Float_2F", o);
jaroslav@355
   566
        }
jaroslav@1541
   567
        if (samePrimitive(type, Byte.TYPE)) {
jaroslav@355
   568
            return fromRaw(Byte.class, "valueOf__Ljava_lang_Byte_2B", o);
jaroslav@355
   569
        }
jaroslav@1541
   570
        if (samePrimitive(type, Boolean.TYPE)) {
jaroslav@355
   571
            return fromRaw(Boolean.class, "valueOf__Ljava_lang_Boolean_2Z", o);
jaroslav@355
   572
        }
jaroslav@1541
   573
        if (samePrimitive(type, Short.TYPE)) {
jaroslav@355
   574
            return fromRaw(Short.class, "valueOf__Ljava_lang_Short_2S", o);
jaroslav@355
   575
        }
jaroslav@1541
   576
        if (samePrimitive(type, Character.TYPE)) {
jaroslav@430
   577
            return fromRaw(Character.class, "valueOf__Ljava_lang_Character_2C", o);
jaroslav@430
   578
        }
jaroslav@413
   579
        if (type.getName().equals("void")) {
jaroslav@413
   580
            return null;
jaroslav@413
   581
        }
jaroslav@355
   582
        throw new IllegalStateException("Can't convert " + o);
jtulach@258
   583
    }
jaroslav@1541
   584
    static boolean samePrimitive(Class<?> c1, Class<?> c2) {
jaroslav@1541
   585
        if (c1 == c2) {
jaroslav@1541
   586
            return true;
jaroslav@1541
   587
        }
jaroslav@1541
   588
        if (c1.isPrimitive()) {
jaroslav@1541
   589
            return c1.getName().equals(c2.getName());
jaroslav@1541
   590
        }
jaroslav@1541
   591
        return false;
jaroslav@1541
   592
    }
jaroslav@1541
   593
jaroslav@1541
   594
    static String findArraySignature(Class<?> type) {
jaroslav@1541
   595
        if (!type.isPrimitive()) {
jaroslav@1541
   596
            return "[L" + type.getName().replace('.', '/') + ";";
jaroslav@1541
   597
        }
jaroslav@1541
   598
        if (samePrimitive(type, Integer.TYPE)) {
jaroslav@1541
   599
            return "[I";
jaroslav@1541
   600
        }
jaroslav@1541
   601
        if (samePrimitive(type, Long.TYPE)) {
jaroslav@1541
   602
            return "[J";
jaroslav@1541
   603
        }
jaroslav@1541
   604
        if (samePrimitive(type, Double.TYPE)) {
jaroslav@1541
   605
            return "[D";
jaroslav@1541
   606
        }
jaroslav@1541
   607
        if (samePrimitive(type, Float.TYPE)) {
jaroslav@1541
   608
            return "[F";
jaroslav@1541
   609
        }
jaroslav@1541
   610
        if (samePrimitive(type, Byte.TYPE)) {
jaroslav@1541
   611
            return "[B";
jaroslav@1541
   612
        }
jaroslav@1541
   613
        if (samePrimitive(type, Boolean.TYPE)) {
jaroslav@1541
   614
            return "[Z";
jaroslav@1541
   615
        }
jaroslav@1541
   616
        if (samePrimitive(type, Short.TYPE)) {
jaroslav@1541
   617
            return "[S";
jaroslav@1541
   618
        }
jaroslav@1541
   619
        if (samePrimitive(type, Character.TYPE)) {
jaroslav@1541
   620
            return "[C";
jaroslav@1541
   621
        }
jaroslav@1541
   622
        throw new IllegalStateException("Can't create array for " + type);
jaroslav@1541
   623
    }
jaroslav@354
   624
    
jaroslav@355
   625
    @JavaScriptBody(args = { "cls", "m", "o" }, 
jaroslav@355
   626
        body = "return cls.cnstr(false)[m](o);"
jaroslav@355
   627
    )
jaroslav@355
   628
    private static native Integer fromRaw(Class<?> cls, String m, Object o);
jaroslav@418
   629
jaroslav@792
   630
    @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
jaroslav@1321
   631
    static native Object toPrimitive(Object o);
jaroslav@354
   632
    
jtulach@258
   633
    /**
jtulach@258
   634
     * Returns {@code true} if this method is a bridge
jtulach@258
   635
     * method; returns {@code false} otherwise.
jtulach@258
   636
     *
jtulach@258
   637
     * @return true if and only if this method is a bridge
jtulach@258
   638
     * method as defined by the Java Language Specification.
jtulach@258
   639
     * @since 1.5
jtulach@258
   640
     */
jtulach@258
   641
    public boolean isBridge() {
jtulach@258
   642
        return (getModifiers() & Modifier.BRIDGE) != 0;
jtulach@258
   643
    }
jtulach@258
   644
jtulach@258
   645
    /**
jtulach@258
   646
     * Returns {@code true} if this method was declared to take
jtulach@258
   647
     * a variable number of arguments; returns {@code false}
jtulach@258
   648
     * otherwise.
jtulach@258
   649
     *
jtulach@258
   650
     * @return {@code true} if an only if this method was declared to
jtulach@258
   651
     * take a variable number of arguments.
jtulach@258
   652
     * @since 1.5
jtulach@258
   653
     */
jtulach@258
   654
    public boolean isVarArgs() {
jtulach@258
   655
        return (getModifiers() & Modifier.VARARGS) != 0;
jtulach@258
   656
    }
jtulach@258
   657
jtulach@258
   658
    /**
jtulach@258
   659
     * Returns {@code true} if this method is a synthetic
jtulach@258
   660
     * method; returns {@code false} otherwise.
jtulach@258
   661
     *
jtulach@258
   662
     * @return true if and only if this method is a synthetic
jtulach@258
   663
     * method as defined by the Java Language Specification.
jtulach@258
   664
     * @since 1.5
jtulach@258
   665
     */
jtulach@258
   666
    public boolean isSynthetic() {
jtulach@258
   667
        return Modifier.isSynthetic(getModifiers());
jtulach@258
   668
    }
jtulach@258
   669
jaroslav@443
   670
    @JavaScriptBody(args = { "ac" }, 
jaroslav@266
   671
        body = 
jaroslav@592
   672
          "var a = this._data().anno;"
jaroslav@592
   673
        + "if (a) {"
jaroslav@592
   674
        + "  return a['L' + ac.jvmName + ';'];"
jaroslav@266
   675
        + "} else return null;"
jaroslav@266
   676
    )
jaroslav@266
   677
    private Object getAnnotationData(Class<?> annotationClass) {
jaroslav@266
   678
        throw new UnsupportedOperationException();
jaroslav@266
   679
    }
jaroslav@266
   680
    
jtulach@258
   681
    /**
jtulach@258
   682
     * @throws NullPointerException {@inheritDoc}
jtulach@258
   683
     * @since 1.5
jtulach@258
   684
     */
jtulach@258
   685
    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
jaroslav@266
   686
        Object data = getAnnotationData(annotationClass);
jaroslav@266
   687
        return data == null ? null : AnnotationImpl.create(annotationClass, data);
jtulach@258
   688
    }
jtulach@258
   689
jtulach@258
   690
    /**
jtulach@258
   691
     * @since 1.5
jtulach@258
   692
     */
jtulach@258
   693
    public Annotation[] getDeclaredAnnotations()  {
jaroslav@260
   694
        throw new UnsupportedOperationException();
jtulach@258
   695
    }
jtulach@258
   696
jtulach@258
   697
    /**
jtulach@258
   698
     * Returns the default value for the annotation member represented by
jtulach@258
   699
     * this {@code Method} instance.  If the member is of a primitive type,
jtulach@258
   700
     * an instance of the corresponding wrapper type is returned. Returns
jtulach@258
   701
     * null if no default is associated with the member, or if the method
jtulach@258
   702
     * instance does not represent a declared member of an annotation type.
jtulach@258
   703
     *
jtulach@258
   704
     * @return the default value for the annotation member represented
jtulach@258
   705
     *     by this {@code Method} instance.
jtulach@258
   706
     * @throws TypeNotPresentException if the annotation is of type
jtulach@258
   707
     *     {@link Class} and no definition can be found for the
jtulach@258
   708
     *     default class value.
jtulach@258
   709
     * @since  1.5
jtulach@258
   710
     */
jtulach@258
   711
    public Object getDefaultValue() {
jaroslav@260
   712
        throw new UnsupportedOperationException();
jtulach@258
   713
    }
jtulach@258
   714
jtulach@258
   715
    /**
jtulach@258
   716
     * Returns an array of arrays that represent the annotations on the formal
jtulach@258
   717
     * parameters, in declaration order, of the method represented by
jtulach@258
   718
     * this {@code Method} object. (Returns an array of length zero if the
jtulach@258
   719
     * underlying method is parameterless.  If the method has one or more
jtulach@258
   720
     * parameters, a nested array of length zero is returned for each parameter
jtulach@258
   721
     * with no annotations.) The annotation objects contained in the returned
jtulach@258
   722
     * arrays are serializable.  The caller of this method is free to modify
jtulach@258
   723
     * the returned arrays; it will have no effect on the arrays returned to
jtulach@258
   724
     * other callers.
jtulach@258
   725
     *
jtulach@258
   726
     * @return an array of arrays that represent the annotations on the formal
jtulach@258
   727
     *    parameters, in declaration order, of the method represented by this
jtulach@258
   728
     *    Method object
jtulach@258
   729
     * @since 1.5
jtulach@258
   730
     */
jtulach@258
   731
    public Annotation[][] getParameterAnnotations() {
jaroslav@260
   732
        throw new UnsupportedOperationException();
jtulach@258
   733
    }
jaroslav@262
   734
jaroslav@391
   735
    static {
jaroslav@391
   736
        MethodImpl.INSTANCE = new MethodImpl() {
jaroslav@391
   737
            protected Method create(Class<?> declaringClass, String name, Object data, String sig) {
jaroslav@391
   738
                return new Method(declaringClass, name, data, sig);
jaroslav@264
   739
            }
jaroslav@1321
   740
jaroslav@1321
   741
            @Override
jaroslav@1321
   742
            protected Constructor create(Class<?> declaringClass, Object data, String sig) {
jaroslav@1321
   743
                return new Constructor(declaringClass, data, sig);
jaroslav@1321
   744
            }
jaroslav@391
   745
        };
jaroslav@262
   746
    }
jaroslav@1702
   747
    
jaroslav@1702
   748
    //
jaroslav@1702
   749
    // helper methods for Array
jaroslav@1702
   750
    //
jaroslav@1702
   751
    
jaroslav@1702
   752
    @JavaScriptBody(args = { "primitive", "sig", "fn", "length" }, body =
jaroslav@1702
   753
          "var arr = new Array(length);\n"
jaroslav@1702
   754
        + "var value = primitive ? 0 : null;\n"
jaroslav@1702
   755
        + "for(var i = 0; i < length; i++) arr[i] = value;\n"
jaroslav@1702
   756
        + "Object.defineProperty(arr, 'jvmName', { 'configurable': true, 'writable': true, 'value': sig });\n"
jaroslav@1702
   757
        + "Object.defineProperty(arr, 'fnc', { 'configurable': true, 'writable': true, 'value' : fn });\n"
jaroslav@1702
   758
//        + "java.lang.System.out.println('Assigned ' + arr.jvmName + ' fn: ' + (!!arr.fnc));\n"
jaroslav@1702
   759
        + "return arr;"
jaroslav@1702
   760
    )
jaroslav@1702
   761
    static native Object newArray(boolean primitive, String sig, Object fn, int length);
jaroslav@1702
   762
    
jaroslav@1702
   763
    @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
jaroslav@1702
   764
    static native int arrayLength(Object arr);
jaroslav@1702
   765
    
jaroslav@1702
   766
    @JavaScriptBody(args = { "cntstr" }, body = "return cntstr(false).constructor.$class;")
jaroslav@1702
   767
    static native Class<?> classFromFn(Object cntstr);
jaroslav@1702
   768
    @JavaScriptBody(args = { "array", "index" }, body = "return array[index];")
jaroslav@1702
   769
    static native Object atArray(Object array, int index);
jaroslav@1702
   770
jaroslav@1702
   771
    @JavaScriptBody(args = { "array", "index", "v" }, body = "array[index] = v;")
jaroslav@1702
   772
    static native Object setArray(Object array, int index, Object v);
jaroslav@1702
   773
    
jtulach@258
   774
}