< prev index next >

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

Print this page




 164 
 165     /**
 166      * {@inheritDoc}
 167      *
 168      * <p> A {@code SecurityException} is also thrown if this object is a
 169      * {@code Constructor} object for the class {@code Class} and {@code flag}
 170      * is true. </p>
 171      *
 172      * @param flag {@inheritDoc}
 173      *
 174      * @throws InaccessibleObjectException {@inheritDoc}
 175      * @throws SecurityException if the request is denied by the security manager
 176      *         or this is a constructor for {@code java.lang.Class}
 177      *
 178      * @spec JPMS
 179      */
 180     @Override
 181     @CallerSensitive
 182     public void setAccessible(boolean flag) {
 183         AccessibleObject.checkPermission();






 184         if (flag) {
 185             checkCanSetAccessible(Reflection.getCallerClass());
 186         }
 187         setAccessible0(flag);
 188     }
 189 
 190     @Override
 191     void checkCanSetAccessible(Class<?> caller) {
 192         checkCanSetAccessible(caller, clazz);
 193         if (clazz == Class.class) {
 194             // can we change this to InaccessibleObjectException?
 195             throw new SecurityException("Cannot make a java.lang.Class"
 196                                         + " constructor accessible");
 197         }
 198     }
 199 
 200     @Override
 201     boolean hasGenericInformation() {
 202         return (getSignature() != null);
 203     }


 457      * @exception IllegalArgumentException  if the number of actual
 458      *              and formal parameters differ; if an unwrapping
 459      *              conversion for primitive arguments fails; or if,
 460      *              after possible unwrapping, a parameter value
 461      *              cannot be converted to the corresponding formal
 462      *              parameter type by a method invocation conversion; if
 463      *              this constructor pertains to an enum type.
 464      * @exception InstantiationException    if the class that declares the
 465      *              underlying constructor represents an abstract class.
 466      * @exception InvocationTargetException if the underlying constructor
 467      *              throws an exception.
 468      * @exception ExceptionInInitializerError if the initialization provoked
 469      *              by this method fails.
 470      */
 471     @CallerSensitive
 472     @ForceInline // to ensure Reflection.getCallerClass optimization
 473     public T newInstance(Object ... initargs)
 474         throws InstantiationException, IllegalAccessException,
 475                IllegalArgumentException, InvocationTargetException
 476     {





 477         if (!override) {
 478             Class<?> caller = Reflection.getCallerClass();
 479             checkAccess(caller, clazz, clazz, modifiers);
 480         }
 481         if ((clazz.getModifiers() & Modifier.ENUM) != 0)
 482             throw new IllegalArgumentException("Cannot reflectively create enum objects");
 483         ConstructorAccessor ca = constructorAccessor;   // read volatile
 484         if (ca == null) {
 485             ca = acquireConstructorAccessor();
 486         }
 487         @SuppressWarnings("unchecked")
 488         T inst = (T) ca.newInstance(initargs);
 489         return inst;
 490     }
 491 
 492     /**
 493      * {@inheritDoc}
 494      * @since 1.5
 495      */
 496     @Override




 164 
 165     /**
 166      * {@inheritDoc}
 167      *
 168      * <p> A {@code SecurityException} is also thrown if this object is a
 169      * {@code Constructor} object for the class {@code Class} and {@code flag}
 170      * is true. </p>
 171      *
 172      * @param flag {@inheritDoc}
 173      *
 174      * @throws InaccessibleObjectException {@inheritDoc}
 175      * @throws SecurityException if the request is denied by the security manager
 176      *         or this is a constructor for {@code java.lang.Class}
 177      *
 178      * @spec JPMS
 179      */
 180     @Override
 181     @CallerSensitive
 182     public void setAccessible(boolean flag) {
 183         AccessibleObject.checkPermission();
 184 
 185         if (clazz.isValue()) {
 186             throw new InaccessibleObjectException(
 187                 "Unable to make a value class constructor \"" + this + "\" accessible");
 188         }
 189 
 190         if (flag) {
 191             checkCanSetAccessible(Reflection.getCallerClass());
 192         }
 193         setAccessible0(flag);
 194     }
 195 
 196     @Override
 197     void checkCanSetAccessible(Class<?> caller) {
 198         checkCanSetAccessible(caller, clazz);
 199         if (clazz == Class.class) {
 200             // can we change this to InaccessibleObjectException?
 201             throw new SecurityException("Cannot make a java.lang.Class"
 202                                         + " constructor accessible");
 203         }
 204     }
 205 
 206     @Override
 207     boolean hasGenericInformation() {
 208         return (getSignature() != null);
 209     }


 463      * @exception IllegalArgumentException  if the number of actual
 464      *              and formal parameters differ; if an unwrapping
 465      *              conversion for primitive arguments fails; or if,
 466      *              after possible unwrapping, a parameter value
 467      *              cannot be converted to the corresponding formal
 468      *              parameter type by a method invocation conversion; if
 469      *              this constructor pertains to an enum type.
 470      * @exception InstantiationException    if the class that declares the
 471      *              underlying constructor represents an abstract class.
 472      * @exception InvocationTargetException if the underlying constructor
 473      *              throws an exception.
 474      * @exception ExceptionInInitializerError if the initialization provoked
 475      *              by this method fails.
 476      */
 477     @CallerSensitive
 478     @ForceInline // to ensure Reflection.getCallerClass optimization
 479     public T newInstance(Object ... initargs)
 480         throws InstantiationException, IllegalAccessException,
 481                IllegalArgumentException, InvocationTargetException
 482     {
 483         if (clazz.isValue()) {
 484             throw new IllegalAccessException(
 485                 "cannot create new instance of value class " + clazz.getName());
 486         }
 487 
 488         if (!override) {
 489             Class<?> caller = Reflection.getCallerClass();
 490             checkAccess(caller, clazz, clazz, modifiers);
 491         }
 492         if ((clazz.getModifiers() & Modifier.ENUM) != 0)
 493             throw new IllegalArgumentException("Cannot reflectively create enum objects");
 494         ConstructorAccessor ca = constructorAccessor;   // read volatile
 495         if (ca == null) {
 496             ca = acquireConstructorAccessor();
 497         }
 498         @SuppressWarnings("unchecked")
 499         T inst = (T) ca.newInstance(initargs);
 500         return inst;
 501     }
 502 
 503     /**
 504      * {@inheritDoc}
 505      * @since 1.5
 506      */
 507     @Override


< prev index next >