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