src/share/classes/java/lang/reflect/Constructor.java

Print this page
rev 3746 : [mq]: cr6565585.ao


  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     // For non-public members or members in package-private classes,
  78     // it is necessary to perform somewhat expensive security checks.
  79     // If the security check succeeds for a given class, it will
  80     // always succeed (it is not affected by the granting or revoking
  81     // of permissions); we speed up the check in the common case by
  82     // remembering the last Class for which the check succeeded.
  83     private volatile Class<?> securityCheckCache;
  84 
  85     // Generics infrastructure
  86     // Accessor for factory
  87     private GenericsFactory getFactory() {
  88         // create scope and factory
  89         return CoreReflectionFactory.make(this, ConstructorScope.make(this));
  90     }
  91 
  92     // Accessor for generic info repository
  93     private ConstructorRepository getGenericInfo() {
  94         // lazily initialize repository if necessary
  95         if (genericInfo == null) {
  96             // create and cache generic info repository
  97             genericInfo =
  98                 ConstructorRepository.make(getSignature(),
  99                                            getFactory());
 100         }
 101         return genericInfo; //return cached repository
 102     }
 103 
 104     private volatile ConstructorAccessor constructorAccessor;


 501      *              and formal parameters differ; if an unwrapping
 502      *              conversion for primitive arguments fails; or if,
 503      *              after possible unwrapping, a parameter value
 504      *              cannot be converted to the corresponding formal
 505      *              parameter type by a method invocation conversion; if
 506      *              this constructor pertains to an enum type.
 507      * @exception InstantiationException    if the class that declares the
 508      *              underlying constructor represents an abstract class.
 509      * @exception InvocationTargetException if the underlying constructor
 510      *              throws an exception.
 511      * @exception ExceptionInInitializerError if the initialization provoked
 512      *              by this method fails.
 513      */
 514     public T newInstance(Object ... initargs)
 515         throws InstantiationException, IllegalAccessException,
 516                IllegalArgumentException, InvocationTargetException
 517     {
 518         if (!override) {
 519             if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
 520                 Class<?> caller = Reflection.getCallerClass(2);
 521                 if (securityCheckCache != caller) {
 522                     Reflection.ensureMemberAccess(caller, clazz, null, modifiers);
 523                     securityCheckCache = caller;
 524                 }
 525             }
 526         }
 527         if ((clazz.getModifiers() & Modifier.ENUM) != 0)
 528             throw new IllegalArgumentException("Cannot reflectively create enum objects");
 529         if (constructorAccessor == null) acquireConstructorAccessor();
 530         return (T) constructorAccessor.newInstance(initargs);



 531     }
 532 
 533     /**
 534      * Returns {@code true} if this constructor was declared to take
 535      * a variable number of arguments; returns {@code false}
 536      * otherwise.
 537      *
 538      * @return {@code true} if an only if this constructor was declared to
 539      * take a variable number of arguments.
 540      * @since 1.5
 541      */
 542     public boolean isVarArgs() {
 543         return (getModifiers() & Modifier.VARARGS) != 0;
 544     }
 545 
 546     /**
 547      * Returns {@code true} if this constructor is a synthetic
 548      * constructor; returns {@code false} otherwise.
 549      *
 550      * @return true if and only if this constructor is a synthetic
 551      * constructor as defined by the Java Language Specification.
 552      * @since 1.5
 553      */
 554     public boolean isSynthetic() {
 555         return Modifier.isSynthetic(getModifiers());
 556     }
 557 
 558     // NOTE that there is no synchronization used here. It is correct
 559     // (though not efficient) to generate more than one
 560     // ConstructorAccessor for a given Constructor. However, avoiding
 561     // synchronization will probably make the implementation more
 562     // scalable.
 563     private void acquireConstructorAccessor() {
 564         // First check to see if one has been created yet, and take it
 565         // if so.
 566         ConstructorAccessor tmp = null;
 567         if (root != null) tmp = root.getConstructorAccessor();
 568         if (tmp != null) {
 569             constructorAccessor = tmp;
 570             return;
 571         }
 572         // Otherwise fabricate one and propagate it up to the root
 573         tmp = reflectionFactory.newConstructorAccessor(this);
 574         setConstructorAccessor(tmp);
 575     }
 576 



 577     // Returns ConstructorAccessor for this Constructor object, not
 578     // looking up the chain to the root
 579     ConstructorAccessor getConstructorAccessor() {
 580         return constructorAccessor;
 581     }
 582 
 583     // Sets the ConstructorAccessor for this Constructor object and
 584     // (recursively) its root
 585     void setConstructorAccessor(ConstructorAccessor accessor) {
 586         constructorAccessor = accessor;
 587         // Propagate up
 588         if (root != null) {
 589             root.setConstructorAccessor(accessor);
 590         }
 591     }
 592 
 593     int getSlot() {
 594         return slot;
 595     }
 596 




  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;


 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