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