jdk/src/share/classes/java/lang/reflect/Constructor.java
author darcy
Fri, 28 Aug 2009 14:11:00 -0700
changeset 3712 8851d55adef0
parent 2174 0ffce164e4a4
child 3941 7b53330adf8f
permissions -rw-r--r--
6261502: (reflect) Add the functionality to screen out the "inappropriate" modifier bits 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 parameterized type, the {@code Type}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * object returned for it must accurately reflect the actual type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * parameters used in the source code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <p>If an exception type is a type variable or a parameterized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * type, it is created. Otherwise, it is resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return an array of Types that represent the exception types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *     thrown by the underlying method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @throws GenericSignatureFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *     if the generic method signature does not conform to the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *     specified in the Java Virtual Machine Specification, 3rd edition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @throws TypeNotPresentException if the underlying method's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *     {@code throws} clause refers to a non-existent type declaration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @throws MalformedParameterizedTypeException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *     the underlying method's {@code throws} clause refers to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *     parameterized type that cannot be instantiated for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
      public Type[] getGenericExceptionTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
          Type[] result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
          if (getSignature() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
              ( (result = getGenericInfo().getExceptionTypes()).length > 0  ))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
              return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
          else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
              return getExceptionTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Compares this {@code Constructor} against the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Returns true if the objects are the same.  Two {@code Constructor} objects are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * the same if they were declared by the same class and have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * same formal parameter types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (obj != null && obj instanceof Constructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            Constructor other = (Constructor)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (getDeclaringClass() == other.getDeclaringClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                /* Avoid unnecessary cloning */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                Class[] params1 = parameterTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                Class[] params2 = other.parameterTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                if (params1.length == params2.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    for (int i = 0; i < params1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        if (params1[i] != params2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    return true;
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
        return false;
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
     * Returns a hashcode for this {@code Constructor}. The hashcode is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * the same as the hashcode for the underlying constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * declaring class name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return getDeclaringClass().getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Returns a string describing this {@code Constructor}.  The string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * formatted as the constructor access modifiers, if any,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * followed by the fully-qualified name of the declaring class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * followed by a parenthesized, comma-separated list of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * constructor's formal parameter types.  For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *    public java.util.Hashtable(int,float)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * <p>The only possible modifiers for constructors are the access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * modifiers {@code public}, {@code protected} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * {@code private}.  Only one of these may appear, or none if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * constructor has default (package) access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            StringBuffer sb = new StringBuffer();
3712
8851d55adef0 6261502: (reflect) Add the functionality to screen out the "inappropriate" modifier bits
darcy
parents: 2174
diff changeset
   358
            int mod = getModifiers() & Modifier.constructorModifiers();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            if (mod != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                sb.append(Modifier.toString(mod) + " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            sb.append(Field.getTypeName(getDeclaringClass()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            sb.append("(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            Class[] params = parameterTypes; // avoid clone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            for (int j = 0; j < params.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                sb.append(Field.getTypeName(params[j]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                if (j < (params.length - 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    sb.append(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            sb.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            Class[] exceptions = exceptionTypes; // avoid clone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (exceptions.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                sb.append(" throws ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                for (int k = 0; k < exceptions.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    sb.append(exceptions[k].getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    if (k < (exceptions.length - 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        sb.append(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return "<" + e + ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Returns a string describing this {@code Constructor},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * including type parameters.  The string is formatted as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * constructor access modifiers, if any, followed by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * angle-bracketed comma separated list of the constructor's type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * parameters, if any, followed by the fully-qualified name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * declaring class, followed by a parenthesized, comma-separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * list of the constructor's generic formal parameter types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * If this constructor was declared to take a variable number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * arguments, instead of denoting the last parameter as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * "<tt><i>Type</i>[]</tt>", it is denoted as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * "<tt><i>Type</i>...</tt>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * A space is used to separate access modifiers from one another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * and from the type parameters or return type.  If there are no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * type parameters, the type parameter list is elided; if the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * parameter list is present, a space separates the list from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * class name.  If the constructor is declared to throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * exceptions, the parameter list is followed by a space, followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * by the word "{@code throws}" followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * comma-separated list of the thrown exception types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <p>The only possible modifiers for constructors are the access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * modifiers {@code public}, {@code protected} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * {@code private}.  Only one of these may appear, or none if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * constructor has default (package) access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @return a string describing this {@code Constructor},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * include type parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    public String toGenericString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            StringBuilder sb = new StringBuilder();
3712
8851d55adef0 6261502: (reflect) Add the functionality to screen out the "inappropriate" modifier bits
darcy
parents: 2174
diff changeset
   422
            int mod = getModifiers() & Modifier.constructorModifiers();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            if (mod != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                sb.append(Modifier.toString(mod) + " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            TypeVariable<?>[] typeparms = getTypeParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (typeparms.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                boolean first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                sb.append("<");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                for(TypeVariable<?> typeparm: typeparms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    if (!first)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        sb.append(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    // Class objects can't occur here; no need to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    // and call Class.getName().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    sb.append(typeparm.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                sb.append("> ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            sb.append(Field.getTypeName(getDeclaringClass()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            sb.append("(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            Type[] params = getGenericParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            for (int j = 0; j < params.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                String param = (params[j] instanceof Class<?>)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    Field.getTypeName((Class<?>)params[j]):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    (params[j].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    param = param.replaceFirst("\\[\\]$", "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                sb.append(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                if (j < (params.length - 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    sb.append(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            sb.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            Type[] exceptions = getGenericExceptionTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (exceptions.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                sb.append(" throws ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                for (int k = 0; k < exceptions.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    sb.append((exceptions[k] instanceof Class)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                              ((Class)exceptions[k]).getName():
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                              exceptions[k].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    if (k < (exceptions.length - 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        sb.append(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            return "<" + e + ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Uses the constructor represented by this {@code Constructor} object to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * create and initialize a new instance of the constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * declaring class, with the specified initialization parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Individual parameters are automatically unwrapped to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * primitive formal parameters, and both primitive and reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * parameters are subject to method invocation conversions as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * <p>If the number of formal parameters required by the underlying constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * is 0, the supplied {@code initargs} array may be of length 0 or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * <p>If the constructor's declaring class is an inner class in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * non-static context, the first argument to the constructor needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * to be the enclosing instance; see <i>The Java Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * Specification</i>, section 15.9.3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * <p>If the required access and argument checks succeed and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * instantiation will proceed, the constructor's declaring class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * is initialized if it has not already been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * <p>If the constructor completes normally, returns the newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * created and initialized instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @param initargs array of objects to be passed as arguments to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * the constructor call; values of primitive types are wrapped in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * a wrapper object of the appropriate type (e.g. a {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * in a {@link java.lang.Float Float})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @return a new object created by calling the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * this object represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @exception IllegalAccessException    if this {@code Constructor} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *              enforces Java language access control and the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *              constructor is inaccessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @exception IllegalArgumentException  if the number of actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *              and formal parameters differ; if an unwrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *              conversion for primitive arguments fails; or if,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *              after possible unwrapping, a parameter value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *              cannot be converted to the corresponding formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *              parameter type by a method invocation conversion; if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *              this constructor pertains to an enum type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @exception InstantiationException    if the class that declares the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *              underlying constructor represents an abstract class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @exception InvocationTargetException if the underlying constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *              throws an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @exception ExceptionInInitializerError if the initialization provoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *              by this method fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public T newInstance(Object ... initargs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        throws InstantiationException, IllegalAccessException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
               IllegalArgumentException, InvocationTargetException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (!override) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                Class caller = Reflection.getCallerClass(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                if (securityCheckCache != caller) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    Reflection.ensureMemberAccess(caller, clazz, null, modifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    securityCheckCache = caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if ((clazz.getModifiers() & Modifier.ENUM) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            throw new IllegalArgumentException("Cannot reflectively create enum objects");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        if (constructorAccessor == null) acquireConstructorAccessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        return (T) constructorAccessor.newInstance(initargs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Returns {@code true} if this constructor was declared to take
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * a variable number of arguments; returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @return {@code true} if an only if this constructor was declared to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * take a variable number of arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    public boolean isVarArgs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return (getModifiers() & Modifier.VARARGS) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Returns {@code true} if this constructor is a synthetic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * constructor; returns {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @return true if and only if this constructor is a synthetic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * constructor as defined by the Java Language Specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public boolean isSynthetic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        return Modifier.isSynthetic(getModifiers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    // NOTE that there is no synchronization used here. It is correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    // (though not efficient) to generate more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    // ConstructorAccessor for a given Constructor. However, avoiding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    // synchronization will probably make the implementation more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    // scalable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    private void acquireConstructorAccessor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        // First check to see if one has been created yet, and take it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        // if so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        ConstructorAccessor tmp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        if (root != null) tmp = root.getConstructorAccessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (tmp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            constructorAccessor = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        // Otherwise fabricate one and propagate it up to the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        tmp = reflectionFactory.newConstructorAccessor(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        setConstructorAccessor(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    // Returns ConstructorAccessor for this Constructor object, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    // looking up the chain to the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    ConstructorAccessor getConstructorAccessor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        return constructorAccessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    // Sets the ConstructorAccessor for this Constructor object and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    // (recursively) its root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    void setConstructorAccessor(ConstructorAccessor accessor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        constructorAccessor = accessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        // Propagate up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            root.setConstructorAccessor(accessor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    int getSlot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        return slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
   String getSignature() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            return signature;
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[] getRawAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        return annotations;
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[] getRawParameterAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        return parameterAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (annotationClass == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        return (T) declaredAnnotations().get(annotationClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    public Annotation[] getDeclaredAnnotations()  {
2174
0ffce164e4a4 6799230: Lazily load java.lang.annotation.Annotation class
mchung
parents: 2
diff changeset
   629
        return AnnotationParser.toArray(declaredAnnotations());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    private transient Map<Class, Annotation> declaredAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    private synchronized  Map<Class, Annotation> declaredAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        if (declaredAnnotations == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            declaredAnnotations = AnnotationParser.parseAnnotations(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                annotations, sun.misc.SharedSecrets.getJavaLangAccess().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                getConstantPool(getDeclaringClass()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        return declaredAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * Returns an array of arrays that represent the annotations on the formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * parameters, in declaration order, of the method represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * this {@code Constructor} object. (Returns an array of length zero if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * underlying method is parameterless.  If the method has one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * parameters, a nested array of length zero is returned for each parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * with no annotations.) The annotation objects contained in the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * arrays are serializable.  The caller of this method is free to modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * the returned arrays; it will have no effect on the arrays returned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * other callers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @return an array of arrays that represent the annotations on the formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *    parameters, in declaration order, of the method represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *    Constructor object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    public Annotation[][] getParameterAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        int numParameters = parameterTypes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        if (parameterAnnotations == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            return new Annotation[numParameters][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        Annotation[][] result = AnnotationParser.parseParameterAnnotations(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            parameterAnnotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            sun.misc.SharedSecrets.getJavaLangAccess().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                getConstantPool(getDeclaringClass()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        if (result.length != numParameters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            Class<?> declaringClass = getDeclaringClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            if (declaringClass.isEnum() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                declaringClass.isAnonymousClass() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                declaringClass.isLocalClass() )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                ; // Can't do reliable parameter counting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                if (!declaringClass.isMemberClass() || // top-level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    // Check for the enclosing instance parameter for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    // non-static member classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    (declaringClass.isMemberClass() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                     ((declaringClass.getModifiers() & Modifier.STATIC) == 0)  &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                     result.length + 1 != numParameters) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    throw new AnnotationFormatError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                              "Parameter annotations don't match number of parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
}