< prev index next >

src/java.base/share/classes/java/lang/Class.java

Print this page




 453         final Class<?> c;
 454         if (cl != null) {
 455             c = cl.loadLocalClass(name);
 456         } else {
 457             c = BootLoader.loadClassOrNull(name);
 458         }
 459 
 460         if (c != null && c.getModule() == module) {
 461             return c;
 462         } else {
 463             return null;
 464         }
 465     }
 466 
 467     /**
 468      * Creates a new instance of the class represented by this {@code Class}
 469      * object.  The class is instantiated as if by a {@code new}
 470      * expression with an empty argument list.  The class is initialized if it
 471      * has not already been initialized.
 472      *
 473      * <p>Note that this method propagates any exception thrown by the
 474      * nullary constructor, including a checked exception.  Use of
 475      * this method effectively bypasses the compile-time exception
 476      * checking that would otherwise be performed by the compiler.
 477      * The {@link
 478      * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
 479      * Constructor.newInstance} method avoids this problem by wrapping
 480      * any exception thrown by the constructor in a (checked) {@link
 481      * java.lang.reflect.InvocationTargetException}.
 482      *
 483      * @return  a newly allocated instance of the class represented by this
 484      *          object.
 485      * @throws  IllegalAccessException  if the class or its nullary
 486      *          constructor is not accessible.
 487      * @throws  InstantiationException
 488      *          if this {@code Class} represents an abstract class,
 489      *          an interface, an array class, a primitive type, or void;
 490      *          or if the class has no nullary constructor;
 491      *          or if the instantiation fails for some other reason.
 492      * @throws  ExceptionInInitializerError if the initialization
 493      *          provoked by this method fails.
 494      * @throws  SecurityException
 495      *          If a security manager, <i>s</i>, is present and
 496      *          the caller's class loader is not the same as or an
 497      *          ancestor of the class loader for the current class and
 498      *          invocation of {@link SecurityManager#checkPackageAccess
 499      *          s.checkPackageAccess()} denies access to the package
 500      *          of this class.
 501      */
 502     @CallerSensitive

 503     public T newInstance()
 504         throws InstantiationException, IllegalAccessException
 505     {
 506         if (System.getSecurityManager() != null) {
 507             checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false);
 508         }
 509 
 510         // NOTE: the following code may not be strictly correct under
 511         // the current Java memory model.
 512 
 513         // Constructor lookup
 514         if (cachedConstructor == null) {
 515             if (this == Class.class) {
 516                 throw new IllegalAccessException(
 517                     "Can not call newInstance() on the Class for java.lang.Class"
 518                 );
 519             }
 520             try {
 521                 Class<?>[] empty = {};
 522                 final Constructor<T> c = getConstructor0(empty, Member.DECLARED);




 453         final Class<?> c;
 454         if (cl != null) {
 455             c = cl.loadLocalClass(name);
 456         } else {
 457             c = BootLoader.loadClassOrNull(name);
 458         }
 459 
 460         if (c != null && c.getModule() == module) {
 461             return c;
 462         } else {
 463             return null;
 464         }
 465     }
 466 
 467     /**
 468      * Creates a new instance of the class represented by this {@code Class}
 469      * object.  The class is instantiated as if by a {@code new}
 470      * expression with an empty argument list.  The class is initialized if it
 471      * has not already been initialized.
 472      *
 473      * @deprecated This method propagates any exception thrown by the
 474      * nullary constructor, including a checked exception.  Use of
 475      * this method effectively bypasses the compile-time exception
 476      * checking that would otherwise be performed by the compiler.
 477      * The {@link
 478      * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
 479      * Constructor.newInstance} method avoids this problem by wrapping
 480      * any exception thrown by the constructor in a (checked) {@link
 481      * java.lang.reflect.InvocationTargetException}.
 482      *
 483      * @return  a newly allocated instance of the class represented by this
 484      *          object.
 485      * @throws  IllegalAccessException  if the class or its nullary
 486      *          constructor is not accessible.
 487      * @throws  InstantiationException
 488      *          if this {@code Class} represents an abstract class,
 489      *          an interface, an array class, a primitive type, or void;
 490      *          or if the class has no nullary constructor;
 491      *          or if the instantiation fails for some other reason.
 492      * @throws  ExceptionInInitializerError if the initialization
 493      *          provoked by this method fails.
 494      * @throws  SecurityException
 495      *          If a security manager, <i>s</i>, is present and
 496      *          the caller's class loader is not the same as or an
 497      *          ancestor of the class loader for the current class and
 498      *          invocation of {@link SecurityManager#checkPackageAccess
 499      *          s.checkPackageAccess()} denies access to the package
 500      *          of this class.
 501      */
 502     @CallerSensitive
 503     @Deprecated(since="9")
 504     public T newInstance()
 505         throws InstantiationException, IllegalAccessException
 506     {
 507         if (System.getSecurityManager() != null) {
 508             checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false);
 509         }
 510 
 511         // NOTE: the following code may not be strictly correct under
 512         // the current Java memory model.
 513 
 514         // Constructor lookup
 515         if (cachedConstructor == null) {
 516             if (this == Class.class) {
 517                 throw new IllegalAccessException(
 518                     "Can not call newInstance() on the Class for java.lang.Class"
 519                 );
 520             }
 521             try {
 522                 Class<?>[] empty = {};
 523                 final Constructor<T> c = getConstructor0(empty, Member.DECLARED);


< prev index next >