emul/mini/src/main/java/java/lang/reflect/Constructor.java
branchjdk7-b147
changeset 601 5198affdb915
child 604 3fcc279c921b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/mini/src/main/java/java/lang/reflect/Constructor.java	Mon Jan 28 18:12:47 2013 +0100
     1.3 @@ -0,0 +1,683 @@
     1.4 +/*
     1.5 + * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang.reflect;
    1.30 +
    1.31 +import sun.reflect.ConstructorAccessor;
    1.32 +import sun.reflect.Reflection;
    1.33 +import sun.reflect.generics.repository.ConstructorRepository;
    1.34 +import sun.reflect.generics.factory.CoreReflectionFactory;
    1.35 +import sun.reflect.generics.factory.GenericsFactory;
    1.36 +import sun.reflect.generics.scope.ConstructorScope;
    1.37 +import java.lang.annotation.Annotation;
    1.38 +import java.util.Map;
    1.39 +import sun.reflect.annotation.AnnotationParser;
    1.40 +import java.lang.annotation.AnnotationFormatError;
    1.41 +import java.lang.reflect.Modifier;
    1.42 +
    1.43 +/**
    1.44 + * {@code Constructor} provides information about, and access to, a single
    1.45 + * constructor for a class.
    1.46 + *
    1.47 + * <p>{@code Constructor} permits widening conversions to occur when matching the
    1.48 + * actual parameters to newInstance() with the underlying
    1.49 + * constructor's formal parameters, but throws an
    1.50 + * {@code IllegalArgumentException} if a narrowing conversion would occur.
    1.51 + *
    1.52 + * @param <T> the class in which the constructor is declared
    1.53 + *
    1.54 + * @see Member
    1.55 + * @see java.lang.Class
    1.56 + * @see java.lang.Class#getConstructors()
    1.57 + * @see java.lang.Class#getConstructor(Class[])
    1.58 + * @see java.lang.Class#getDeclaredConstructors()
    1.59 + *
    1.60 + * @author      Kenneth Russell
    1.61 + * @author      Nakul Saraiya
    1.62 + */
    1.63 +public final
    1.64 +    class Constructor<T> extends AccessibleObject implements
    1.65 +                                                    GenericDeclaration,
    1.66 +                                                    Member {
    1.67 +
    1.68 +    private Class<T>            clazz;
    1.69 +    private int                 slot;
    1.70 +    private Class<?>[]          parameterTypes;
    1.71 +    private Class<?>[]          exceptionTypes;
    1.72 +    private int                 modifiers;
    1.73 +    // Generics and annotations support
    1.74 +    private transient String    signature;
    1.75 +    // generic info repository; lazily initialized
    1.76 +    private transient ConstructorRepository genericInfo;
    1.77 +    private byte[]              annotations;
    1.78 +    private byte[]              parameterAnnotations;
    1.79 +
    1.80 +    // Generics infrastructure
    1.81 +    // Accessor for factory
    1.82 +    private GenericsFactory getFactory() {
    1.83 +        // create scope and factory
    1.84 +        return CoreReflectionFactory.make(this, ConstructorScope.make(this));
    1.85 +    }
    1.86 +
    1.87 +    // Accessor for generic info repository
    1.88 +    private ConstructorRepository getGenericInfo() {
    1.89 +        // lazily initialize repository if necessary
    1.90 +        if (genericInfo == null) {
    1.91 +            // create and cache generic info repository
    1.92 +            genericInfo =
    1.93 +                ConstructorRepository.make(getSignature(),
    1.94 +                                           getFactory());
    1.95 +        }
    1.96 +        return genericInfo; //return cached repository
    1.97 +    }
    1.98 +
    1.99 +    private volatile ConstructorAccessor constructorAccessor;
   1.100 +    // For sharing of ConstructorAccessors. This branching structure
   1.101 +    // is currently only two levels deep (i.e., one root Constructor
   1.102 +    // and potentially many Constructor objects pointing to it.)
   1.103 +    private Constructor<T>      root;
   1.104 +
   1.105 +    /**
   1.106 +     * Package-private constructor used by ReflectAccess to enable
   1.107 +     * instantiation of these objects in Java code from the java.lang
   1.108 +     * package via sun.reflect.LangReflectAccess.
   1.109 +     */
   1.110 +    Constructor(Class<T> declaringClass,
   1.111 +                Class<?>[] parameterTypes,
   1.112 +                Class<?>[] checkedExceptions,
   1.113 +                int modifiers,
   1.114 +                int slot,
   1.115 +                String signature,
   1.116 +                byte[] annotations,
   1.117 +                byte[] parameterAnnotations)
   1.118 +    {
   1.119 +        this.clazz = declaringClass;
   1.120 +        this.parameterTypes = parameterTypes;
   1.121 +        this.exceptionTypes = checkedExceptions;
   1.122 +        this.modifiers = modifiers;
   1.123 +        this.slot = slot;
   1.124 +        this.signature = signature;
   1.125 +        this.annotations = annotations;
   1.126 +        this.parameterAnnotations = parameterAnnotations;
   1.127 +    }
   1.128 +
   1.129 +    /**
   1.130 +     * Package-private routine (exposed to java.lang.Class via
   1.131 +     * ReflectAccess) which returns a copy of this Constructor. The copy's
   1.132 +     * "root" field points to this Constructor.
   1.133 +     */
   1.134 +    Constructor<T> copy() {
   1.135 +        // This routine enables sharing of ConstructorAccessor objects
   1.136 +        // among Constructor objects which refer to the same underlying
   1.137 +        // method in the VM. (All of this contortion is only necessary
   1.138 +        // because of the "accessibility" bit in AccessibleObject,
   1.139 +        // which implicitly requires that new java.lang.reflect
   1.140 +        // objects be fabricated for each reflective call on Class
   1.141 +        // objects.)
   1.142 +        Constructor<T> res = new Constructor<>(clazz,
   1.143 +                                                parameterTypes,
   1.144 +                                                exceptionTypes, modifiers, slot,
   1.145 +                                                signature,
   1.146 +                                                annotations,
   1.147 +                                                parameterAnnotations);
   1.148 +        res.root = this;
   1.149 +        // Might as well eagerly propagate this if already present
   1.150 +        res.constructorAccessor = constructorAccessor;
   1.151 +        return res;
   1.152 +    }
   1.153 +
   1.154 +    /**
   1.155 +     * Returns the {@code Class} object representing the class that declares
   1.156 +     * the constructor represented by this {@code Constructor} object.
   1.157 +     */
   1.158 +    public Class<T> getDeclaringClass() {
   1.159 +        return clazz;
   1.160 +    }
   1.161 +
   1.162 +    /**
   1.163 +     * Returns the name of this constructor, as a string.  This is
   1.164 +     * the binary name of the constructor's declaring class.
   1.165 +     */
   1.166 +    public String getName() {
   1.167 +        return getDeclaringClass().getName();
   1.168 +    }
   1.169 +
   1.170 +    /**
   1.171 +     * Returns the Java language modifiers for the constructor
   1.172 +     * represented by this {@code Constructor} object, as an integer. The
   1.173 +     * {@code Modifier} class should be used to decode the modifiers.
   1.174 +     *
   1.175 +     * @see Modifier
   1.176 +     */
   1.177 +    public int getModifiers() {
   1.178 +        return modifiers;
   1.179 +    }
   1.180 +
   1.181 +    /**
   1.182 +     * Returns an array of {@code TypeVariable} objects that represent the
   1.183 +     * type variables declared by the generic declaration represented by this
   1.184 +     * {@code GenericDeclaration} object, in declaration order.  Returns an
   1.185 +     * array of length 0 if the underlying generic declaration declares no type
   1.186 +     * variables.
   1.187 +     *
   1.188 +     * @return an array of {@code TypeVariable} objects that represent
   1.189 +     *     the type variables declared by this generic declaration
   1.190 +     * @throws GenericSignatureFormatError if the generic
   1.191 +     *     signature of this generic declaration does not conform to
   1.192 +     *     the format specified in
   1.193 +     *     <cite>The Java&trade; Virtual Machine Specification</cite>
   1.194 +     * @since 1.5
   1.195 +     */
   1.196 +    public TypeVariable<Constructor<T>>[] getTypeParameters() {
   1.197 +      if (getSignature() != null) {
   1.198 +        return (TypeVariable<Constructor<T>>[])getGenericInfo().getTypeParameters();
   1.199 +      } else
   1.200 +          return (TypeVariable<Constructor<T>>[])new TypeVariable[0];
   1.201 +    }
   1.202 +
   1.203 +
   1.204 +    /**
   1.205 +     * Returns an array of {@code Class} objects that represent the formal
   1.206 +     * parameter types, in declaration order, of the constructor
   1.207 +     * represented by this {@code Constructor} object.  Returns an array of
   1.208 +     * length 0 if the underlying constructor takes no parameters.
   1.209 +     *
   1.210 +     * @return the parameter types for the constructor this object
   1.211 +     * represents
   1.212 +     */
   1.213 +    public Class<?>[] getParameterTypes() {
   1.214 +        return (Class<?>[]) parameterTypes.clone();
   1.215 +    }
   1.216 +
   1.217 +
   1.218 +    /**
   1.219 +     * Returns an array of {@code Type} objects that represent the formal
   1.220 +     * parameter types, in declaration order, of the method represented by
   1.221 +     * this {@code Constructor} object. Returns an array of length 0 if the
   1.222 +     * underlying method takes no parameters.
   1.223 +     *
   1.224 +     * <p>If a formal parameter type is a parameterized type,
   1.225 +     * the {@code Type} object returned for it must accurately reflect
   1.226 +     * the actual type parameters used in the source code.
   1.227 +     *
   1.228 +     * <p>If a formal parameter type is a type variable or a parameterized
   1.229 +     * type, it is created. Otherwise, it is resolved.
   1.230 +     *
   1.231 +     * @return an array of {@code Type}s that represent the formal
   1.232 +     *     parameter types of the underlying method, in declaration order
   1.233 +     * @throws GenericSignatureFormatError
   1.234 +     *     if the generic method signature does not conform to the format
   1.235 +     *     specified in
   1.236 +     *     <cite>The Java&trade; Virtual Machine Specification</cite>
   1.237 +     * @throws TypeNotPresentException if any of the parameter
   1.238 +     *     types of the underlying method refers to a non-existent type
   1.239 +     *     declaration
   1.240 +     * @throws MalformedParameterizedTypeException if any of
   1.241 +     *     the underlying method's parameter types refer to a parameterized
   1.242 +     *     type that cannot be instantiated for any reason
   1.243 +     * @since 1.5
   1.244 +     */
   1.245 +    public Type[] getGenericParameterTypes() {
   1.246 +        if (getSignature() != null)
   1.247 +            return getGenericInfo().getParameterTypes();
   1.248 +        else
   1.249 +            return getParameterTypes();
   1.250 +    }
   1.251 +
   1.252 +
   1.253 +    /**
   1.254 +     * Returns an array of {@code Class} objects that represent the types
   1.255 +     * of exceptions declared to be thrown by the underlying constructor
   1.256 +     * represented by this {@code Constructor} object.  Returns an array of
   1.257 +     * length 0 if the constructor declares no exceptions in its {@code throws} clause.
   1.258 +     *
   1.259 +     * @return the exception types declared as being thrown by the
   1.260 +     * constructor this object represents
   1.261 +     */
   1.262 +    public Class<?>[] getExceptionTypes() {
   1.263 +        return (Class<?>[])exceptionTypes.clone();
   1.264 +    }
   1.265 +
   1.266 +
   1.267 +    /**
   1.268 +     * Returns an array of {@code Type} objects that represent the
   1.269 +     * exceptions declared to be thrown by this {@code Constructor} object.
   1.270 +     * Returns an array of length 0 if the underlying method declares
   1.271 +     * no exceptions in its {@code throws} clause.
   1.272 +     *
   1.273 +     * <p>If an exception type is a type variable or a parameterized
   1.274 +     * type, it is created. Otherwise, it is resolved.
   1.275 +     *
   1.276 +     * @return an array of Types that represent the exception types
   1.277 +     *     thrown by the underlying method
   1.278 +     * @throws GenericSignatureFormatError
   1.279 +     *     if the generic method signature does not conform to the format
   1.280 +     *     specified in
   1.281 +     *     <cite>The Java&trade; Virtual Machine Specification</cite>
   1.282 +     * @throws TypeNotPresentException if the underlying method's
   1.283 +     *     {@code throws} clause refers to a non-existent type declaration
   1.284 +     * @throws MalformedParameterizedTypeException if
   1.285 +     *     the underlying method's {@code throws} clause refers to a
   1.286 +     *     parameterized type that cannot be instantiated for any reason
   1.287 +     * @since 1.5
   1.288 +     */
   1.289 +      public Type[] getGenericExceptionTypes() {
   1.290 +          Type[] result;
   1.291 +          if (getSignature() != null &&
   1.292 +              ( (result = getGenericInfo().getExceptionTypes()).length > 0  ))
   1.293 +              return result;
   1.294 +          else
   1.295 +              return getExceptionTypes();
   1.296 +      }
   1.297 +
   1.298 +    /**
   1.299 +     * Compares this {@code Constructor} against the specified object.
   1.300 +     * Returns true if the objects are the same.  Two {@code Constructor} objects are
   1.301 +     * the same if they were declared by the same class and have the
   1.302 +     * same formal parameter types.
   1.303 +     */
   1.304 +    public boolean equals(Object obj) {
   1.305 +        if (obj != null && obj instanceof Constructor) {
   1.306 +            Constructor<?> other = (Constructor<?>)obj;
   1.307 +            if (getDeclaringClass() == other.getDeclaringClass()) {
   1.308 +                /* Avoid unnecessary cloning */
   1.309 +                Class<?>[] params1 = parameterTypes;
   1.310 +                Class<?>[] params2 = other.parameterTypes;
   1.311 +                if (params1.length == params2.length) {
   1.312 +                    for (int i = 0; i < params1.length; i++) {
   1.313 +                        if (params1[i] != params2[i])
   1.314 +                            return false;
   1.315 +                    }
   1.316 +                    return true;
   1.317 +                }
   1.318 +            }
   1.319 +        }
   1.320 +        return false;
   1.321 +    }
   1.322 +
   1.323 +    /**
   1.324 +     * Returns a hashcode for this {@code Constructor}. The hashcode is
   1.325 +     * the same as the hashcode for the underlying constructor's
   1.326 +     * declaring class name.
   1.327 +     */
   1.328 +    public int hashCode() {
   1.329 +        return getDeclaringClass().getName().hashCode();
   1.330 +    }
   1.331 +
   1.332 +    /**
   1.333 +     * Returns a string describing this {@code Constructor}.  The string is
   1.334 +     * formatted as the constructor access modifiers, if any,
   1.335 +     * followed by the fully-qualified name of the declaring class,
   1.336 +     * followed by a parenthesized, comma-separated list of the
   1.337 +     * constructor's formal parameter types.  For example:
   1.338 +     * <pre>
   1.339 +     *    public java.util.Hashtable(int,float)
   1.340 +     * </pre>
   1.341 +     *
   1.342 +     * <p>The only possible modifiers for constructors are the access
   1.343 +     * modifiers {@code public}, {@code protected} or
   1.344 +     * {@code private}.  Only one of these may appear, or none if the
   1.345 +     * constructor has default (package) access.
   1.346 +     */
   1.347 +    public String toString() {
   1.348 +        try {
   1.349 +            StringBuffer sb = new StringBuffer();
   1.350 +            int mod = getModifiers() & Modifier.constructorModifiers();
   1.351 +            if (mod != 0) {
   1.352 +                sb.append(Modifier.toString(mod) + " ");
   1.353 +            }
   1.354 +            sb.append(Field.getTypeName(getDeclaringClass()));
   1.355 +            sb.append("(");
   1.356 +            Class<?>[] params = parameterTypes; // avoid clone
   1.357 +            for (int j = 0; j < params.length; j++) {
   1.358 +                sb.append(Field.getTypeName(params[j]));
   1.359 +                if (j < (params.length - 1))
   1.360 +                    sb.append(",");
   1.361 +            }
   1.362 +            sb.append(")");
   1.363 +            Class<?>[] exceptions = exceptionTypes; // avoid clone
   1.364 +            if (exceptions.length > 0) {
   1.365 +                sb.append(" throws ");
   1.366 +                for (int k = 0; k < exceptions.length; k++) {
   1.367 +                    sb.append(exceptions[k].getName());
   1.368 +                    if (k < (exceptions.length - 1))
   1.369 +                        sb.append(",");
   1.370 +                }
   1.371 +            }
   1.372 +            return sb.toString();
   1.373 +        } catch (Exception e) {
   1.374 +            return "<" + e + ">";
   1.375 +        }
   1.376 +    }
   1.377 +
   1.378 +    /**
   1.379 +     * Returns a string describing this {@code Constructor},
   1.380 +     * including type parameters.  The string is formatted as the
   1.381 +     * constructor access modifiers, if any, followed by an
   1.382 +     * angle-bracketed comma separated list of the constructor's type
   1.383 +     * parameters, if any, followed by the fully-qualified name of the
   1.384 +     * declaring class, followed by a parenthesized, comma-separated
   1.385 +     * list of the constructor's generic formal parameter types.
   1.386 +     *
   1.387 +     * If this constructor was declared to take a variable number of
   1.388 +     * arguments, instead of denoting the last parameter as
   1.389 +     * "<tt><i>Type</i>[]</tt>", it is denoted as
   1.390 +     * "<tt><i>Type</i>...</tt>".
   1.391 +     *
   1.392 +     * A space is used to separate access modifiers from one another
   1.393 +     * and from the type parameters or return type.  If there are no
   1.394 +     * type parameters, the type parameter list is elided; if the type
   1.395 +     * parameter list is present, a space separates the list from the
   1.396 +     * class name.  If the constructor is declared to throw
   1.397 +     * exceptions, the parameter list is followed by a space, followed
   1.398 +     * by the word "{@code throws}" followed by a
   1.399 +     * comma-separated list of the thrown exception types.
   1.400 +     *
   1.401 +     * <p>The only possible modifiers for constructors are the access
   1.402 +     * modifiers {@code public}, {@code protected} or
   1.403 +     * {@code private}.  Only one of these may appear, or none if the
   1.404 +     * constructor has default (package) access.
   1.405 +     *
   1.406 +     * @return a string describing this {@code Constructor},
   1.407 +     * include type parameters
   1.408 +     *
   1.409 +     * @since 1.5
   1.410 +     */
   1.411 +    public String toGenericString() {
   1.412 +        try {
   1.413 +            StringBuilder sb = new StringBuilder();
   1.414 +            int mod = getModifiers() & Modifier.constructorModifiers();
   1.415 +            if (mod != 0) {
   1.416 +                sb.append(Modifier.toString(mod) + " ");
   1.417 +            }
   1.418 +            TypeVariable<?>[] typeparms = getTypeParameters();
   1.419 +            if (typeparms.length > 0) {
   1.420 +                boolean first = true;
   1.421 +                sb.append("<");
   1.422 +                for(TypeVariable<?> typeparm: typeparms) {
   1.423 +                    if (!first)
   1.424 +                        sb.append(",");
   1.425 +                    // Class objects can't occur here; no need to test
   1.426 +                    // and call Class.getName().
   1.427 +                    sb.append(typeparm.toString());
   1.428 +                    first = false;
   1.429 +                }
   1.430 +                sb.append("> ");
   1.431 +            }
   1.432 +            sb.append(Field.getTypeName(getDeclaringClass()));
   1.433 +            sb.append("(");
   1.434 +            Type[] params = getGenericParameterTypes();
   1.435 +            for (int j = 0; j < params.length; j++) {
   1.436 +                String param = (params[j] instanceof Class<?>)?
   1.437 +                    Field.getTypeName((Class<?>)params[j]):
   1.438 +                    (params[j].toString());
   1.439 +                if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
   1.440 +                    param = param.replaceFirst("\\[\\]$", "...");
   1.441 +                sb.append(param);
   1.442 +                if (j < (params.length - 1))
   1.443 +                    sb.append(",");
   1.444 +            }
   1.445 +            sb.append(")");
   1.446 +            Type[] exceptions = getGenericExceptionTypes();
   1.447 +            if (exceptions.length > 0) {
   1.448 +                sb.append(" throws ");
   1.449 +                for (int k = 0; k < exceptions.length; k++) {
   1.450 +                    sb.append((exceptions[k] instanceof Class)?
   1.451 +                              ((Class<?>)exceptions[k]).getName():
   1.452 +                              exceptions[k].toString());
   1.453 +                    if (k < (exceptions.length - 1))
   1.454 +                        sb.append(",");
   1.455 +                }
   1.456 +            }
   1.457 +            return sb.toString();
   1.458 +        } catch (Exception e) {
   1.459 +            return "<" + e + ">";
   1.460 +        }
   1.461 +    }
   1.462 +
   1.463 +    /**
   1.464 +     * Uses the constructor represented by this {@code Constructor} object to
   1.465 +     * create and initialize a new instance of the constructor's
   1.466 +     * declaring class, with the specified initialization parameters.
   1.467 +     * Individual parameters are automatically unwrapped to match
   1.468 +     * primitive formal parameters, and both primitive and reference
   1.469 +     * parameters are subject to method invocation conversions as necessary.
   1.470 +     *
   1.471 +     * <p>If the number of formal parameters required by the underlying constructor
   1.472 +     * is 0, the supplied {@code initargs} array may be of length 0 or null.
   1.473 +     *
   1.474 +     * <p>If the constructor's declaring class is an inner class in a
   1.475 +     * non-static context, the first argument to the constructor needs
   1.476 +     * to be the enclosing instance; see section 15.9.3 of
   1.477 +     * <cite>The Java&trade; Language Specification</cite>.
   1.478 +     *
   1.479 +     * <p>If the required access and argument checks succeed and the
   1.480 +     * instantiation will proceed, the constructor's declaring class
   1.481 +     * is initialized if it has not already been initialized.
   1.482 +     *
   1.483 +     * <p>If the constructor completes normally, returns the newly
   1.484 +     * created and initialized instance.
   1.485 +     *
   1.486 +     * @param initargs array of objects to be passed as arguments to
   1.487 +     * the constructor call; values of primitive types are wrapped in
   1.488 +     * a wrapper object of the appropriate type (e.g. a {@code float}
   1.489 +     * in a {@link java.lang.Float Float})
   1.490 +     *
   1.491 +     * @return a new object created by calling the constructor
   1.492 +     * this object represents
   1.493 +     *
   1.494 +     * @exception IllegalAccessException    if this {@code Constructor} object
   1.495 +     *              is enforcing Java language access control and the underlying
   1.496 +     *              constructor is inaccessible.
   1.497 +     * @exception IllegalArgumentException  if the number of actual
   1.498 +     *              and formal parameters differ; if an unwrapping
   1.499 +     *              conversion for primitive arguments fails; or if,
   1.500 +     *              after possible unwrapping, a parameter value
   1.501 +     *              cannot be converted to the corresponding formal
   1.502 +     *              parameter type by a method invocation conversion; if
   1.503 +     *              this constructor pertains to an enum type.
   1.504 +     * @exception InstantiationException    if the class that declares the
   1.505 +     *              underlying constructor represents an abstract class.
   1.506 +     * @exception InvocationTargetException if the underlying constructor
   1.507 +     *              throws an exception.
   1.508 +     * @exception ExceptionInInitializerError if the initialization provoked
   1.509 +     *              by this method fails.
   1.510 +     */
   1.511 +    public T newInstance(Object ... initargs)
   1.512 +        throws InstantiationException, IllegalAccessException,
   1.513 +               IllegalArgumentException, InvocationTargetException
   1.514 +    {
   1.515 +        if (!override) {
   1.516 +            if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
   1.517 +                Class<?> caller = Reflection.getCallerClass(2);
   1.518 +
   1.519 +                checkAccess(caller, clazz, null, modifiers);
   1.520 +            }
   1.521 +        }
   1.522 +        if ((clazz.getModifiers() & Modifier.ENUM) != 0)
   1.523 +            throw new IllegalArgumentException("Cannot reflectively create enum objects");
   1.524 +        ConstructorAccessor ca = constructorAccessor;   // read volatile
   1.525 +        if (ca == null) {
   1.526 +            ca = acquireConstructorAccessor();
   1.527 +        }
   1.528 +        return (T) ca.newInstance(initargs);
   1.529 +    }
   1.530 +
   1.531 +    /**
   1.532 +     * Returns {@code true} if this constructor was declared to take
   1.533 +     * a variable number of arguments; returns {@code false}
   1.534 +     * otherwise.
   1.535 +     *
   1.536 +     * @return {@code true} if an only if this constructor was declared to
   1.537 +     * take a variable number of arguments.
   1.538 +     * @since 1.5
   1.539 +     */
   1.540 +    public boolean isVarArgs() {
   1.541 +        return (getModifiers() & Modifier.VARARGS) != 0;
   1.542 +    }
   1.543 +
   1.544 +    /**
   1.545 +     * Returns {@code true} if this constructor is a synthetic
   1.546 +     * constructor; returns {@code false} otherwise.
   1.547 +     *
   1.548 +     * @return true if and only if this constructor is a synthetic
   1.549 +     * constructor as defined by
   1.550 +     * <cite>The Java&trade; Language Specification</cite>.
   1.551 +     * @since 1.5
   1.552 +     */
   1.553 +    public boolean isSynthetic() {
   1.554 +        return Modifier.isSynthetic(getModifiers());
   1.555 +    }
   1.556 +
   1.557 +    // NOTE that there is no synchronization used here. It is correct
   1.558 +    // (though not efficient) to generate more than one
   1.559 +    // ConstructorAccessor for a given Constructor. However, avoiding
   1.560 +    // synchronization will probably make the implementation more
   1.561 +    // scalable.
   1.562 +    private ConstructorAccessor acquireConstructorAccessor() {
   1.563 +        // First check to see if one has been created yet, and take it
   1.564 +        // if so.
   1.565 +        ConstructorAccessor tmp = null;
   1.566 +        if (root != null) tmp = root.getConstructorAccessor();
   1.567 +        if (tmp != null) {
   1.568 +            constructorAccessor = tmp;
   1.569 +        } else {
   1.570 +            // Otherwise fabricate one and propagate it up to the root
   1.571 +            tmp = reflectionFactory.newConstructorAccessor(this);
   1.572 +            setConstructorAccessor(tmp);
   1.573 +        }
   1.574 +
   1.575 +        return tmp;
   1.576 +    }
   1.577 +
   1.578 +    // Returns ConstructorAccessor for this Constructor object, not
   1.579 +    // looking up the chain to the root
   1.580 +    ConstructorAccessor getConstructorAccessor() {
   1.581 +        return constructorAccessor;
   1.582 +    }
   1.583 +
   1.584 +    // Sets the ConstructorAccessor for this Constructor object and
   1.585 +    // (recursively) its root
   1.586 +    void setConstructorAccessor(ConstructorAccessor accessor) {
   1.587 +        constructorAccessor = accessor;
   1.588 +        // Propagate up
   1.589 +        if (root != null) {
   1.590 +            root.setConstructorAccessor(accessor);
   1.591 +        }
   1.592 +    }
   1.593 +
   1.594 +    int getSlot() {
   1.595 +        return slot;
   1.596 +    }
   1.597 +
   1.598 +   String getSignature() {
   1.599 +            return signature;
   1.600 +   }
   1.601 +
   1.602 +    byte[] getRawAnnotations() {
   1.603 +        return annotations;
   1.604 +    }
   1.605 +
   1.606 +    byte[] getRawParameterAnnotations() {
   1.607 +        return parameterAnnotations;
   1.608 +    }
   1.609 +
   1.610 +    /**
   1.611 +     * @throws NullPointerException {@inheritDoc}
   1.612 +     * @since 1.5
   1.613 +     */
   1.614 +    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
   1.615 +        if (annotationClass == null)
   1.616 +            throw new NullPointerException();
   1.617 +
   1.618 +        return (T) declaredAnnotations().get(annotationClass);
   1.619 +    }
   1.620 +
   1.621 +    /**
   1.622 +     * @since 1.5
   1.623 +     */
   1.624 +    public Annotation[] getDeclaredAnnotations()  {
   1.625 +        return AnnotationParser.toArray(declaredAnnotations());
   1.626 +    }
   1.627 +
   1.628 +    private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
   1.629 +
   1.630 +    private synchronized  Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
   1.631 +        if (declaredAnnotations == null) {
   1.632 +            declaredAnnotations = AnnotationParser.parseAnnotations(
   1.633 +                annotations, sun.misc.SharedSecrets.getJavaLangAccess().
   1.634 +                getConstantPool(getDeclaringClass()),
   1.635 +                getDeclaringClass());
   1.636 +        }
   1.637 +        return declaredAnnotations;
   1.638 +    }
   1.639 +
   1.640 +    /**
   1.641 +     * Returns an array of arrays that represent the annotations on the formal
   1.642 +     * parameters, in declaration order, of the method represented by
   1.643 +     * this {@code Constructor} object. (Returns an array of length zero if the
   1.644 +     * underlying method is parameterless.  If the method has one or more
   1.645 +     * parameters, a nested array of length zero is returned for each parameter
   1.646 +     * with no annotations.) The annotation objects contained in the returned
   1.647 +     * arrays are serializable.  The caller of this method is free to modify
   1.648 +     * the returned arrays; it will have no effect on the arrays returned to
   1.649 +     * other callers.
   1.650 +     *
   1.651 +     * @return an array of arrays that represent the annotations on the formal
   1.652 +     *    parameters, in declaration order, of the method represented by this
   1.653 +     *    Constructor object
   1.654 +     * @since 1.5
   1.655 +     */
   1.656 +    public Annotation[][] getParameterAnnotations() {
   1.657 +        int numParameters = parameterTypes.length;
   1.658 +        if (parameterAnnotations == null)
   1.659 +            return new Annotation[numParameters][0];
   1.660 +
   1.661 +        Annotation[][] result = AnnotationParser.parseParameterAnnotations(
   1.662 +            parameterAnnotations,
   1.663 +            sun.misc.SharedSecrets.getJavaLangAccess().
   1.664 +                getConstantPool(getDeclaringClass()),
   1.665 +            getDeclaringClass());
   1.666 +        if (result.length != numParameters) {
   1.667 +            Class<?> declaringClass = getDeclaringClass();
   1.668 +            if (declaringClass.isEnum() ||
   1.669 +                declaringClass.isAnonymousClass() ||
   1.670 +                declaringClass.isLocalClass() )
   1.671 +                ; // Can't do reliable parameter counting
   1.672 +            else {
   1.673 +                if (!declaringClass.isMemberClass() || // top-level
   1.674 +                    // Check for the enclosing instance parameter for
   1.675 +                    // non-static member classes
   1.676 +                    (declaringClass.isMemberClass() &&
   1.677 +                     ((declaringClass.getModifiers() & Modifier.STATIC) == 0)  &&
   1.678 +                     result.length + 1 != numParameters) ) {
   1.679 +                    throw new AnnotationFormatError(
   1.680 +                              "Parameter annotations don't match number of parameters");
   1.681 +                }
   1.682 +            }
   1.683 +        }
   1.684 +        return result;
   1.685 +    }
   1.686 +}