rt/emul/mini/src/main/java/java/lang/reflect/Constructor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 28 Sep 2013 12:03:59 +0200
branchjavac
changeset 1321 7a78a84ab583
parent 772 d382dacfd73f
child 1377 5e8f58b48380
permissions -rw-r--r--
Can use Constructor.newInstance with parameters
jaroslav@601
     1
/*
jaroslav@601
     2
 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@601
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@601
     4
 *
jaroslav@601
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@601
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@601
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@601
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@601
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@601
    10
 *
jaroslav@601
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@601
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@601
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@601
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@601
    15
 * accompanied this code).
jaroslav@601
    16
 *
jaroslav@601
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@601
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@601
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@601
    20
 *
jaroslav@601
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@601
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@601
    23
 * questions.
jaroslav@601
    24
 */
jaroslav@601
    25
jaroslav@601
    26
package java.lang.reflect;
jaroslav@601
    27
jaroslav@601
    28
import java.lang.annotation.Annotation;
jaroslav@1321
    29
import static java.lang.reflect.Method.fromPrimitive;
jaroslav@1321
    30
import static java.lang.reflect.Method.getAccess;
jaroslav@1321
    31
import static java.lang.reflect.Method.getParameterTypes;
jaroslav@1321
    32
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@604
    33
import org.apidesign.bck2brwsr.emul.reflect.TypeProvider;
jaroslav@601
    34
jaroslav@601
    35
/**
jaroslav@601
    36
 * {@code Constructor} provides information about, and access to, a single
jaroslav@601
    37
 * constructor for a class.
jaroslav@601
    38
 *
jaroslav@601
    39
 * <p>{@code Constructor} permits widening conversions to occur when matching the
jaroslav@601
    40
 * actual parameters to newInstance() with the underlying
jaroslav@601
    41
 * constructor's formal parameters, but throws an
jaroslav@601
    42
 * {@code IllegalArgumentException} if a narrowing conversion would occur.
jaroslav@601
    43
 *
jaroslav@601
    44
 * @param <T> the class in which the constructor is declared
jaroslav@601
    45
 *
jaroslav@601
    46
 * @see Member
jaroslav@601
    47
 * @see java.lang.Class
jaroslav@601
    48
 * @see java.lang.Class#getConstructors()
jaroslav@601
    49
 * @see java.lang.Class#getConstructor(Class[])
jaroslav@601
    50
 * @see java.lang.Class#getDeclaredConstructors()
jaroslav@601
    51
 *
jaroslav@601
    52
 * @author      Kenneth Russell
jaroslav@601
    53
 * @author      Nakul Saraiya
jaroslav@601
    54
 */
jaroslav@601
    55
public final
jaroslav@601
    56
    class Constructor<T> extends AccessibleObject implements
jaroslav@601
    57
                                                    GenericDeclaration,
jaroslav@601
    58
                                                    Member {
jaroslav@601
    59
jaroslav@1321
    60
    private final Class<T> clazz;
jaroslav@1321
    61
    private final Object data;
jaroslav@1321
    62
    private final String sig;
jaroslav@601
    63
jaroslav@601
    64
    /**
jaroslav@601
    65
     * Package-private constructor used by ReflectAccess to enable
jaroslav@601
    66
     * instantiation of these objects in Java code from the java.lang
jaroslav@601
    67
     * package via sun.reflect.LangReflectAccess.
jaroslav@601
    68
     */
jaroslav@1321
    69
    Constructor(Class<T> declaringClass, Object data, String sig)
jaroslav@601
    70
    {
jaroslav@601
    71
        this.clazz = declaringClass;
jaroslav@1321
    72
        this.data = data;
jaroslav@1321
    73
        this.sig = sig;
jaroslav@601
    74
    }
jaroslav@601
    75
jaroslav@601
    76
    /**
jaroslav@601
    77
     * Package-private routine (exposed to java.lang.Class via
jaroslav@601
    78
     * ReflectAccess) which returns a copy of this Constructor. The copy's
jaroslav@601
    79
     * "root" field points to this Constructor.
jaroslav@601
    80
     */
jaroslav@601
    81
    Constructor<T> copy() {
jaroslav@604
    82
        return this;
jaroslav@601
    83
    }
jaroslav@601
    84
jaroslav@601
    85
    /**
jaroslav@601
    86
     * Returns the {@code Class} object representing the class that declares
jaroslav@601
    87
     * the constructor represented by this {@code Constructor} object.
jaroslav@601
    88
     */
jaroslav@601
    89
    public Class<T> getDeclaringClass() {
jaroslav@601
    90
        return clazz;
jaroslav@601
    91
    }
jaroslav@601
    92
jaroslav@601
    93
    /**
jaroslav@601
    94
     * Returns the name of this constructor, as a string.  This is
jaroslav@601
    95
     * the binary name of the constructor's declaring class.
jaroslav@601
    96
     */
jaroslav@601
    97
    public String getName() {
jaroslav@601
    98
        return getDeclaringClass().getName();
jaroslav@601
    99
    }
jaroslav@601
   100
jaroslav@601
   101
    /**
jaroslav@601
   102
     * Returns the Java language modifiers for the constructor
jaroslav@601
   103
     * represented by this {@code Constructor} object, as an integer. The
jaroslav@601
   104
     * {@code Modifier} class should be used to decode the modifiers.
jaroslav@601
   105
     *
jaroslav@601
   106
     * @see Modifier
jaroslav@601
   107
     */
jaroslav@601
   108
    public int getModifiers() {
jaroslav@1321
   109
        return getAccess(data);
jaroslav@601
   110
    }
jaroslav@601
   111
jaroslav@601
   112
    /**
jaroslav@601
   113
     * Returns an array of {@code TypeVariable} objects that represent the
jaroslav@601
   114
     * type variables declared by the generic declaration represented by this
jaroslav@601
   115
     * {@code GenericDeclaration} object, in declaration order.  Returns an
jaroslav@601
   116
     * array of length 0 if the underlying generic declaration declares no type
jaroslav@601
   117
     * variables.
jaroslav@601
   118
     *
jaroslav@601
   119
     * @return an array of {@code TypeVariable} objects that represent
jaroslav@601
   120
     *     the type variables declared by this generic declaration
jaroslav@601
   121
     * @throws GenericSignatureFormatError if the generic
jaroslav@601
   122
     *     signature of this generic declaration does not conform to
jaroslav@601
   123
     *     the format specified in
jaroslav@601
   124
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
jaroslav@601
   125
     * @since 1.5
jaroslav@601
   126
     */
jaroslav@601
   127
    public TypeVariable<Constructor<T>>[] getTypeParameters() {
jaroslav@604
   128
        return TypeProvider.getDefault().getTypeParameters(this);
jaroslav@601
   129
    }
jaroslav@601
   130
jaroslav@601
   131
jaroslav@601
   132
    /**
jaroslav@601
   133
     * Returns an array of {@code Class} objects that represent the formal
jaroslav@601
   134
     * parameter types, in declaration order, of the constructor
jaroslav@601
   135
     * represented by this {@code Constructor} object.  Returns an array of
jaroslav@601
   136
     * length 0 if the underlying constructor takes no parameters.
jaroslav@601
   137
     *
jaroslav@601
   138
     * @return the parameter types for the constructor this object
jaroslav@601
   139
     * represents
jaroslav@601
   140
     */
jaroslav@601
   141
    public Class<?>[] getParameterTypes() {
jaroslav@1321
   142
        return Method.getParameterTypes(sig);
jaroslav@601
   143
    }
jaroslav@601
   144
jaroslav@601
   145
jaroslav@601
   146
    /**
jaroslav@601
   147
     * Returns an array of {@code Type} objects that represent the formal
jaroslav@601
   148
     * parameter types, in declaration order, of the method represented by
jaroslav@601
   149
     * this {@code Constructor} object. Returns an array of length 0 if the
jaroslav@601
   150
     * underlying method takes no parameters.
jaroslav@601
   151
     *
jaroslav@601
   152
     * <p>If a formal parameter type is a parameterized type,
jaroslav@601
   153
     * the {@code Type} object returned for it must accurately reflect
jaroslav@601
   154
     * the actual type parameters used in the source code.
jaroslav@601
   155
     *
jaroslav@601
   156
     * <p>If a formal parameter type is a type variable or a parameterized
jaroslav@601
   157
     * type, it is created. Otherwise, it is resolved.
jaroslav@601
   158
     *
jaroslav@601
   159
     * @return an array of {@code Type}s that represent the formal
jaroslav@601
   160
     *     parameter types of the underlying method, in declaration order
jaroslav@601
   161
     * @throws GenericSignatureFormatError
jaroslav@601
   162
     *     if the generic method signature does not conform to the format
jaroslav@601
   163
     *     specified in
jaroslav@601
   164
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
jaroslav@601
   165
     * @throws TypeNotPresentException if any of the parameter
jaroslav@601
   166
     *     types of the underlying method refers to a non-existent type
jaroslav@601
   167
     *     declaration
jaroslav@601
   168
     * @throws MalformedParameterizedTypeException if any of
jaroslav@601
   169
     *     the underlying method's parameter types refer to a parameterized
jaroslav@601
   170
     *     type that cannot be instantiated for any reason
jaroslav@601
   171
     * @since 1.5
jaroslav@601
   172
     */
jaroslav@601
   173
    public Type[] getGenericParameterTypes() {
jaroslav@604
   174
        return TypeProvider.getDefault().getGenericParameterTypes(this);
jaroslav@601
   175
    }
jaroslav@601
   176
jaroslav@601
   177
jaroslav@601
   178
    /**
jaroslav@601
   179
     * Returns an array of {@code Class} objects that represent the types
jaroslav@601
   180
     * of exceptions declared to be thrown by the underlying constructor
jaroslav@601
   181
     * represented by this {@code Constructor} object.  Returns an array of
jaroslav@601
   182
     * length 0 if the constructor declares no exceptions in its {@code throws} clause.
jaroslav@601
   183
     *
jaroslav@601
   184
     * @return the exception types declared as being thrown by the
jaroslav@601
   185
     * constructor this object represents
jaroslav@601
   186
     */
jaroslav@601
   187
    public Class<?>[] getExceptionTypes() {
jaroslav@1321
   188
        throw new UnsupportedOperationException();
jaroslav@601
   189
    }
jaroslav@601
   190
jaroslav@601
   191
jaroslav@601
   192
    /**
jaroslav@601
   193
     * Returns an array of {@code Type} objects that represent the
jaroslav@601
   194
     * exceptions declared to be thrown by this {@code Constructor} object.
jaroslav@601
   195
     * Returns an array of length 0 if the underlying method declares
jaroslav@601
   196
     * no exceptions in its {@code throws} clause.
jaroslav@601
   197
     *
jaroslav@601
   198
     * <p>If an exception type is a type variable or a parameterized
jaroslav@601
   199
     * type, it is created. Otherwise, it is resolved.
jaroslav@601
   200
     *
jaroslav@601
   201
     * @return an array of Types that represent the exception types
jaroslav@601
   202
     *     thrown by the underlying method
jaroslav@601
   203
     * @throws GenericSignatureFormatError
jaroslav@601
   204
     *     if the generic method signature does not conform to the format
jaroslav@601
   205
     *     specified in
jaroslav@601
   206
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
jaroslav@601
   207
     * @throws TypeNotPresentException if the underlying method's
jaroslav@601
   208
     *     {@code throws} clause refers to a non-existent type declaration
jaroslav@601
   209
     * @throws MalformedParameterizedTypeException if
jaroslav@601
   210
     *     the underlying method's {@code throws} clause refers to a
jaroslav@601
   211
     *     parameterized type that cannot be instantiated for any reason
jaroslav@601
   212
     * @since 1.5
jaroslav@601
   213
     */
jaroslav@601
   214
      public Type[] getGenericExceptionTypes() {
jaroslav@604
   215
          return TypeProvider.getDefault().getGenericExceptionTypes(this);
jaroslav@601
   216
      }
jaroslav@601
   217
jaroslav@601
   218
    /**
jaroslav@601
   219
     * Compares this {@code Constructor} against the specified object.
jaroslav@601
   220
     * Returns true if the objects are the same.  Two {@code Constructor} objects are
jaroslav@601
   221
     * the same if they were declared by the same class and have the
jaroslav@601
   222
     * same formal parameter types.
jaroslav@601
   223
     */
jaroslav@601
   224
    public boolean equals(Object obj) {
jaroslav@1321
   225
        if (obj instanceof Constructor) {
jaroslav@1321
   226
            Constructor other = (Constructor)obj;
jaroslav@1321
   227
            return data == other.data;
jaroslav@601
   228
        }
jaroslav@601
   229
        return false;
jaroslav@601
   230
    }
jaroslav@601
   231
jaroslav@601
   232
    /**
jaroslav@601
   233
     * Returns a hashcode for this {@code Constructor}. The hashcode is
jaroslav@601
   234
     * the same as the hashcode for the underlying constructor's
jaroslav@601
   235
     * declaring class name.
jaroslav@601
   236
     */
jaroslav@601
   237
    public int hashCode() {
jaroslav@601
   238
        return getDeclaringClass().getName().hashCode();
jaroslav@601
   239
    }
jaroslav@601
   240
jaroslav@601
   241
    /**
jaroslav@601
   242
     * Returns a string describing this {@code Constructor}.  The string is
jaroslav@601
   243
     * formatted as the constructor access modifiers, if any,
jaroslav@601
   244
     * followed by the fully-qualified name of the declaring class,
jaroslav@601
   245
     * followed by a parenthesized, comma-separated list of the
jaroslav@601
   246
     * constructor's formal parameter types.  For example:
jaroslav@601
   247
     * <pre>
jaroslav@601
   248
     *    public java.util.Hashtable(int,float)
jaroslav@601
   249
     * </pre>
jaroslav@601
   250
     *
jaroslav@601
   251
     * <p>The only possible modifiers for constructors are the access
jaroslav@601
   252
     * modifiers {@code public}, {@code protected} or
jaroslav@601
   253
     * {@code private}.  Only one of these may appear, or none if the
jaroslav@601
   254
     * constructor has default (package) access.
jaroslav@601
   255
     */
jaroslav@601
   256
    public String toString() {
jaroslav@601
   257
        try {
jaroslav@601
   258
            StringBuffer sb = new StringBuffer();
jaroslav@601
   259
            int mod = getModifiers() & Modifier.constructorModifiers();
jaroslav@601
   260
            if (mod != 0) {
jaroslav@601
   261
                sb.append(Modifier.toString(mod) + " ");
jaroslav@601
   262
            }
jaroslav@601
   263
            sb.append(Field.getTypeName(getDeclaringClass()));
jaroslav@601
   264
            sb.append("(");
jaroslav@1321
   265
            Class<?>[] params = getParameterTypes(); // avoid clone
jaroslav@601
   266
            for (int j = 0; j < params.length; j++) {
jaroslav@601
   267
                sb.append(Field.getTypeName(params[j]));
jaroslav@601
   268
                if (j < (params.length - 1))
jaroslav@601
   269
                    sb.append(",");
jaroslav@601
   270
            }
jaroslav@601
   271
            sb.append(")");
jaroslav@1321
   272
            /*
jaroslav@601
   273
            Class<?>[] exceptions = exceptionTypes; // avoid clone
jaroslav@601
   274
            if (exceptions.length > 0) {
jaroslav@601
   275
                sb.append(" throws ");
jaroslav@601
   276
                for (int k = 0; k < exceptions.length; k++) {
jaroslav@601
   277
                    sb.append(exceptions[k].getName());
jaroslav@601
   278
                    if (k < (exceptions.length - 1))
jaroslav@601
   279
                        sb.append(",");
jaroslav@601
   280
                }
jaroslav@601
   281
            }
jaroslav@1321
   282
            */
jaroslav@601
   283
            return sb.toString();
jaroslav@601
   284
        } catch (Exception e) {
jaroslav@601
   285
            return "<" + e + ">";
jaroslav@601
   286
        }
jaroslav@601
   287
    }
jaroslav@601
   288
jaroslav@601
   289
    /**
jaroslav@601
   290
     * Returns a string describing this {@code Constructor},
jaroslav@601
   291
     * including type parameters.  The string is formatted as the
jaroslav@601
   292
     * constructor access modifiers, if any, followed by an
jaroslav@601
   293
     * angle-bracketed comma separated list of the constructor's type
jaroslav@601
   294
     * parameters, if any, followed by the fully-qualified name of the
jaroslav@601
   295
     * declaring class, followed by a parenthesized, comma-separated
jaroslav@601
   296
     * list of the constructor's generic formal parameter types.
jaroslav@601
   297
     *
jaroslav@601
   298
     * If this constructor was declared to take a variable number of
jaroslav@601
   299
     * arguments, instead of denoting the last parameter as
jaroslav@601
   300
     * "<tt><i>Type</i>[]</tt>", it is denoted as
jaroslav@601
   301
     * "<tt><i>Type</i>...</tt>".
jaroslav@601
   302
     *
jaroslav@601
   303
     * A space is used to separate access modifiers from one another
jaroslav@601
   304
     * and from the type parameters or return type.  If there are no
jaroslav@601
   305
     * type parameters, the type parameter list is elided; if the type
jaroslav@601
   306
     * parameter list is present, a space separates the list from the
jaroslav@601
   307
     * class name.  If the constructor is declared to throw
jaroslav@601
   308
     * exceptions, the parameter list is followed by a space, followed
jaroslav@601
   309
     * by the word "{@code throws}" followed by a
jaroslav@601
   310
     * comma-separated list of the thrown exception types.
jaroslav@601
   311
     *
jaroslav@601
   312
     * <p>The only possible modifiers for constructors are the access
jaroslav@601
   313
     * modifiers {@code public}, {@code protected} or
jaroslav@601
   314
     * {@code private}.  Only one of these may appear, or none if the
jaroslav@601
   315
     * constructor has default (package) access.
jaroslav@601
   316
     *
jaroslav@601
   317
     * @return a string describing this {@code Constructor},
jaroslav@601
   318
     * include type parameters
jaroslav@601
   319
     *
jaroslav@601
   320
     * @since 1.5
jaroslav@601
   321
     */
jaroslav@601
   322
    public String toGenericString() {
jaroslav@601
   323
        try {
jaroslav@601
   324
            StringBuilder sb = new StringBuilder();
jaroslav@601
   325
            int mod = getModifiers() & Modifier.constructorModifiers();
jaroslav@601
   326
            if (mod != 0) {
jaroslav@601
   327
                sb.append(Modifier.toString(mod) + " ");
jaroslav@601
   328
            }
jaroslav@601
   329
            TypeVariable<?>[] typeparms = getTypeParameters();
jaroslav@601
   330
            if (typeparms.length > 0) {
jaroslav@601
   331
                boolean first = true;
jaroslav@601
   332
                sb.append("<");
jaroslav@601
   333
                for(TypeVariable<?> typeparm: typeparms) {
jaroslav@601
   334
                    if (!first)
jaroslav@601
   335
                        sb.append(",");
jaroslav@601
   336
                    // Class objects can't occur here; no need to test
jaroslav@601
   337
                    // and call Class.getName().
jaroslav@601
   338
                    sb.append(typeparm.toString());
jaroslav@601
   339
                    first = false;
jaroslav@601
   340
                }
jaroslav@601
   341
                sb.append("> ");
jaroslav@601
   342
            }
jaroslav@601
   343
            sb.append(Field.getTypeName(getDeclaringClass()));
jaroslav@601
   344
            sb.append("(");
jaroslav@601
   345
            Type[] params = getGenericParameterTypes();
jaroslav@601
   346
            for (int j = 0; j < params.length; j++) {
jaroslav@601
   347
                String param = (params[j] instanceof Class<?>)?
jaroslav@601
   348
                    Field.getTypeName((Class<?>)params[j]):
jaroslav@601
   349
                    (params[j].toString());
jaroslav@601
   350
                if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
jaroslav@601
   351
                    param = param.replaceFirst("\\[\\]$", "...");
jaroslav@601
   352
                sb.append(param);
jaroslav@601
   353
                if (j < (params.length - 1))
jaroslav@601
   354
                    sb.append(",");
jaroslav@601
   355
            }
jaroslav@601
   356
            sb.append(")");
jaroslav@601
   357
            Type[] exceptions = getGenericExceptionTypes();
jaroslav@601
   358
            if (exceptions.length > 0) {
jaroslav@601
   359
                sb.append(" throws ");
jaroslav@601
   360
                for (int k = 0; k < exceptions.length; k++) {
jaroslav@601
   361
                    sb.append((exceptions[k] instanceof Class)?
jaroslav@601
   362
                              ((Class<?>)exceptions[k]).getName():
jaroslav@601
   363
                              exceptions[k].toString());
jaroslav@601
   364
                    if (k < (exceptions.length - 1))
jaroslav@601
   365
                        sb.append(",");
jaroslav@601
   366
                }
jaroslav@601
   367
            }
jaroslav@601
   368
            return sb.toString();
jaroslav@601
   369
        } catch (Exception e) {
jaroslav@601
   370
            return "<" + e + ">";
jaroslav@601
   371
        }
jaroslav@601
   372
    }
jaroslav@601
   373
jaroslav@601
   374
    /**
jaroslav@601
   375
     * Uses the constructor represented by this {@code Constructor} object to
jaroslav@601
   376
     * create and initialize a new instance of the constructor's
jaroslav@601
   377
     * declaring class, with the specified initialization parameters.
jaroslav@601
   378
     * Individual parameters are automatically unwrapped to match
jaroslav@601
   379
     * primitive formal parameters, and both primitive and reference
jaroslav@601
   380
     * parameters are subject to method invocation conversions as necessary.
jaroslav@601
   381
     *
jaroslav@601
   382
     * <p>If the number of formal parameters required by the underlying constructor
jaroslav@601
   383
     * is 0, the supplied {@code initargs} array may be of length 0 or null.
jaroslav@601
   384
     *
jaroslav@601
   385
     * <p>If the constructor's declaring class is an inner class in a
jaroslav@601
   386
     * non-static context, the first argument to the constructor needs
jaroslav@601
   387
     * to be the enclosing instance; see section 15.9.3 of
jaroslav@601
   388
     * <cite>The Java&trade; Language Specification</cite>.
jaroslav@601
   389
     *
jaroslav@601
   390
     * <p>If the required access and argument checks succeed and the
jaroslav@601
   391
     * instantiation will proceed, the constructor's declaring class
jaroslav@601
   392
     * is initialized if it has not already been initialized.
jaroslav@601
   393
     *
jaroslav@601
   394
     * <p>If the constructor completes normally, returns the newly
jaroslav@601
   395
     * created and initialized instance.
jaroslav@601
   396
     *
jaroslav@601
   397
     * @param initargs array of objects to be passed as arguments to
jaroslav@601
   398
     * the constructor call; values of primitive types are wrapped in
jaroslav@601
   399
     * a wrapper object of the appropriate type (e.g. a {@code float}
jaroslav@601
   400
     * in a {@link java.lang.Float Float})
jaroslav@601
   401
     *
jaroslav@601
   402
     * @return a new object created by calling the constructor
jaroslav@601
   403
     * this object represents
jaroslav@601
   404
     *
jaroslav@601
   405
     * @exception IllegalAccessException    if this {@code Constructor} object
jaroslav@601
   406
     *              is enforcing Java language access control and the underlying
jaroslav@601
   407
     *              constructor is inaccessible.
jaroslav@601
   408
     * @exception IllegalArgumentException  if the number of actual
jaroslav@601
   409
     *              and formal parameters differ; if an unwrapping
jaroslav@601
   410
     *              conversion for primitive arguments fails; or if,
jaroslav@601
   411
     *              after possible unwrapping, a parameter value
jaroslav@601
   412
     *              cannot be converted to the corresponding formal
jaroslav@601
   413
     *              parameter type by a method invocation conversion; if
jaroslav@601
   414
     *              this constructor pertains to an enum type.
jaroslav@601
   415
     * @exception InstantiationException    if the class that declares the
jaroslav@601
   416
     *              underlying constructor represents an abstract class.
jaroslav@601
   417
     * @exception InvocationTargetException if the underlying constructor
jaroslav@601
   418
     *              throws an exception.
jaroslav@601
   419
     * @exception ExceptionInInitializerError if the initialization provoked
jaroslav@601
   420
     *              by this method fails.
jaroslav@601
   421
     */
jaroslav@601
   422
    public T newInstance(Object ... initargs)
jaroslav@601
   423
        throws InstantiationException, IllegalAccessException,
jaroslav@601
   424
               IllegalArgumentException, InvocationTargetException
jaroslav@601
   425
    {
jaroslav@1321
   426
        Class[] types = getParameterTypes();
jaroslav@1321
   427
        if (types.length != initargs.length) {
jaroslav@1321
   428
            throw new IllegalArgumentException("Types len " + types.length + " args: " + initargs.length);
jaroslav@1321
   429
        } else {
jaroslav@1321
   430
            initargs = initargs.clone();
jaroslav@1321
   431
            for (int i = 0; i < types.length; i++) {
jaroslav@1321
   432
                Class c = types[i];
jaroslav@1321
   433
                if (c.isPrimitive() && initargs[i] != null) {
jaroslav@1321
   434
                    initargs[i] = Method.toPrimitive(initargs[i]);
jaroslav@1321
   435
                }
jaroslav@1321
   436
            }
jaroslav@1321
   437
        }
jaroslav@1321
   438
        return (T) newInstance0(this.getDeclaringClass(), "cons__" + sig, initargs);
jaroslav@601
   439
    }
jaroslav@601
   440
jaroslav@1321
   441
    @JavaScriptBody(args = { "self", "sig", "args" }, body =
jaroslav@1321
   442
          "\nvar c = self.cnstr;"
jaroslav@1321
   443
        + "\nvar inst = c();"
jaroslav@1321
   444
        + "\nc[sig].apply(inst, args);"
jaroslav@1321
   445
        + "\nreturn inst;"
jaroslav@1321
   446
    )
jaroslav@1321
   447
    private static native Object newInstance0(Class<?> self, String sig, Object[] args);
jaroslav@1321
   448
    
jaroslav@601
   449
    /**
jaroslav@601
   450
     * Returns {@code true} if this constructor was declared to take
jaroslav@601
   451
     * a variable number of arguments; returns {@code false}
jaroslav@601
   452
     * otherwise.
jaroslav@601
   453
     *
jaroslav@601
   454
     * @return {@code true} if an only if this constructor was declared to
jaroslav@601
   455
     * take a variable number of arguments.
jaroslav@601
   456
     * @since 1.5
jaroslav@601
   457
     */
jaroslav@601
   458
    public boolean isVarArgs() {
jaroslav@601
   459
        return (getModifiers() & Modifier.VARARGS) != 0;
jaroslav@601
   460
    }
jaroslav@601
   461
jaroslav@601
   462
    /**
jaroslav@601
   463
     * Returns {@code true} if this constructor is a synthetic
jaroslav@601
   464
     * constructor; returns {@code false} otherwise.
jaroslav@601
   465
     *
jaroslav@601
   466
     * @return true if and only if this constructor is a synthetic
jaroslav@601
   467
     * constructor as defined by
jaroslav@601
   468
     * <cite>The Java&trade; Language Specification</cite>.
jaroslav@601
   469
     * @since 1.5
jaroslav@601
   470
     */
jaroslav@601
   471
    public boolean isSynthetic() {
jaroslav@601
   472
        return Modifier.isSynthetic(getModifiers());
jaroslav@601
   473
    }
jaroslav@601
   474
jaroslav@601
   475
    /**
jaroslav@601
   476
     * @throws NullPointerException {@inheritDoc}
jaroslav@601
   477
     * @since 1.5
jaroslav@601
   478
     */
jaroslav@601
   479
    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
jaroslav@601
   480
        if (annotationClass == null)
jaroslav@601
   481
            throw new NullPointerException();
jaroslav@601
   482
jaroslav@604
   483
        return null; // XXX (T) declaredAnnotations().get(annotationClass);
jaroslav@601
   484
    }
jaroslav@601
   485
jaroslav@601
   486
    /**
jaroslav@601
   487
     * @since 1.5
jaroslav@601
   488
     */
jaroslav@601
   489
    public Annotation[] getDeclaredAnnotations()  {
jaroslav@604
   490
        return new Annotation[0]; // XXX AnnotationParser.toArray(declaredAnnotations());
jaroslav@601
   491
    }
jaroslav@601
   492
jaroslav@601
   493
    /**
jaroslav@601
   494
     * Returns an array of arrays that represent the annotations on the formal
jaroslav@601
   495
     * parameters, in declaration order, of the method represented by
jaroslav@601
   496
     * this {@code Constructor} object. (Returns an array of length zero if the
jaroslav@601
   497
     * underlying method is parameterless.  If the method has one or more
jaroslav@601
   498
     * parameters, a nested array of length zero is returned for each parameter
jaroslav@601
   499
     * with no annotations.) The annotation objects contained in the returned
jaroslav@601
   500
     * arrays are serializable.  The caller of this method is free to modify
jaroslav@601
   501
     * the returned arrays; it will have no effect on the arrays returned to
jaroslav@601
   502
     * other callers.
jaroslav@601
   503
     *
jaroslav@601
   504
     * @return an array of arrays that represent the annotations on the formal
jaroslav@601
   505
     *    parameters, in declaration order, of the method represented by this
jaroslav@601
   506
     *    Constructor object
jaroslav@601
   507
     * @since 1.5
jaroslav@601
   508
     */
jaroslav@601
   509
    public Annotation[][] getParameterAnnotations() {
jaroslav@1321
   510
//        int numParameters = parameterTypes.length;
jaroslav@1321
   511
//        if (parameterAnnotations == null)
jaroslav@1321
   512
//            return new Annotation[numParameters][0];
jaroslav@604
   513
        
jaroslav@1321
   514
        return new Annotation[0][0]; // XXX
jaroslav@604
   515
/*
jaroslav@601
   516
        Annotation[][] result = AnnotationParser.parseParameterAnnotations(
jaroslav@601
   517
            parameterAnnotations,
jaroslav@601
   518
            sun.misc.SharedSecrets.getJavaLangAccess().
jaroslav@601
   519
                getConstantPool(getDeclaringClass()),
jaroslav@601
   520
            getDeclaringClass());
jaroslav@601
   521
        if (result.length != numParameters) {
jaroslav@601
   522
            Class<?> declaringClass = getDeclaringClass();
jaroslav@601
   523
            if (declaringClass.isEnum() ||
jaroslav@601
   524
                declaringClass.isAnonymousClass() ||
jaroslav@601
   525
                declaringClass.isLocalClass() )
jaroslav@601
   526
                ; // Can't do reliable parameter counting
jaroslav@601
   527
            else {
jaroslav@601
   528
                if (!declaringClass.isMemberClass() || // top-level
jaroslav@601
   529
                    // Check for the enclosing instance parameter for
jaroslav@601
   530
                    // non-static member classes
jaroslav@601
   531
                    (declaringClass.isMemberClass() &&
jaroslav@601
   532
                     ((declaringClass.getModifiers() & Modifier.STATIC) == 0)  &&
jaroslav@601
   533
                     result.length + 1 != numParameters) ) {
jaroslav@601
   534
                    throw new AnnotationFormatError(
jaroslav@601
   535
                              "Parameter annotations don't match number of parameters");
jaroslav@601
   536
                }
jaroslav@601
   537
            }
jaroslav@601
   538
        }
jaroslav@601
   539
        return result;
jaroslav@604
   540
        */
jaroslav@601
   541
    }
jaroslav@601
   542
}