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