< prev index next >

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

Print this page




 375      *
 376      * @param name       fully qualified name of the desired class
 377      * @param initialize if {@code true} the class will be initialized.
 378      *                   See Section 12.4 of <em>The Java Language Specification</em>.
 379      * @param loader     class loader from which the class must be loaded
 380      * @return           class object representing the desired class
 381      *
 382      * @exception LinkageError if the linkage fails
 383      * @exception ExceptionInInitializerError if the initialization provoked
 384      *            by this method fails
 385      * @exception ClassNotFoundException if the class cannot be located by
 386      *            the specified class loader
 387      * @exception SecurityException
 388      *            if a security manager is present, and the {@code loader} is
 389      *            {@code null}, and the caller's class loader is not
 390      *            {@code null}, and the caller does not have the
 391      *            {@link RuntimePermission}{@code ("getClassLoader")}
 392      *
 393      * @see       java.lang.Class#forName(String)
 394      * @see       java.lang.ClassLoader




 395      * @since     1.2
 396      */
 397     @CallerSensitive
 398     public static Class<?> forName(String name, boolean initialize,
 399                                    ClassLoader loader)
 400         throws ClassNotFoundException
 401     {
 402         Class<?> caller = null;
 403         SecurityManager sm = System.getSecurityManager();
 404         if (sm != null) {
 405             // Reflective call to get caller class is only needed if a security manager
 406             // is present.  Avoid the overhead of making this call otherwise.
 407             caller = Reflection.getCallerClass();
 408             if (loader == null) {
 409                 ClassLoader ccl = ClassLoader.getClassLoader(caller);
 410                 if (ccl != null) {
 411                     sm.checkPermission(
 412                         SecurityConstants.GET_CLASSLOADER_PERMISSION);
 413                 }
 414             }


 471     @CallerSensitive
 472     public static Class<?> forName(Module module, String name) {
 473         Objects.requireNonNull(module);
 474         Objects.requireNonNull(name);
 475 
 476         ClassLoader cl;
 477         SecurityManager sm = System.getSecurityManager();
 478         if (sm != null) {
 479             Class<?> caller = Reflection.getCallerClass();
 480             if (caller != null && caller.getModule() != module) {
 481                 // if caller is null, Class.forName is the last java frame on the stack.
 482                 // java.base has all permissions
 483                 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
 484             }
 485             PrivilegedAction<ClassLoader> pa = module::getClassLoader;
 486             cl = AccessController.doPrivileged(pa);
 487         } else {
 488             cl = module.getClassLoader();
 489         }
 490 

 491         if (cl != null) {
 492             return cl.loadClass(module, name);
 493         } else {
 494             return BootLoader.loadClass(module, name);




 495         }

 496     }
 497 


 498     /**
 499      * Creates a new instance of the class represented by this {@code Class}
 500      * object.  The class is instantiated as if by a {@code new}
 501      * expression with an empty argument list.  The class is initialized if it
 502      * has not already been initialized.
 503      *
 504      * @deprecated This method propagates any exception thrown by the
 505      * nullary constructor, including a checked exception.  Use of
 506      * this method effectively bypasses the compile-time exception
 507      * checking that would otherwise be performed by the compiler.
 508      * The {@link
 509      * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
 510      * Constructor.newInstance} method avoids this problem by wrapping
 511      * any exception thrown by the constructor in a (checked) {@link
 512      * java.lang.reflect.InvocationTargetException}.
 513      *
 514      * <p>The call
 515      *
 516      * <pre>{@code
 517      * clazz.newInstance()




 375      *
 376      * @param name       fully qualified name of the desired class
 377      * @param initialize if {@code true} the class will be initialized.
 378      *                   See Section 12.4 of <em>The Java Language Specification</em>.
 379      * @param loader     class loader from which the class must be loaded
 380      * @return           class object representing the desired class
 381      *
 382      * @exception LinkageError if the linkage fails
 383      * @exception ExceptionInInitializerError if the initialization provoked
 384      *            by this method fails
 385      * @exception ClassNotFoundException if the class cannot be located by
 386      *            the specified class loader
 387      * @exception SecurityException
 388      *            if a security manager is present, and the {@code loader} is
 389      *            {@code null}, and the caller's class loader is not
 390      *            {@code null}, and the caller does not have the
 391      *            {@link RuntimePermission}{@code ("getClassLoader")}
 392      *
 393      * @see       java.lang.Class#forName(String)
 394      * @see       java.lang.ClassLoader
 395      *
 396      * @jls 12.2 Loading of Classes and Interfaces
 397      * @jls 12.3 Linking of Classes and Interfaces
 398      * @jls 12.4 Initialization of Classes and Interfaces
 399      * @since     1.2
 400      */
 401     @CallerSensitive
 402     public static Class<?> forName(String name, boolean initialize,
 403                                    ClassLoader loader)
 404         throws ClassNotFoundException
 405     {
 406         Class<?> caller = null;
 407         SecurityManager sm = System.getSecurityManager();
 408         if (sm != null) {
 409             // Reflective call to get caller class is only needed if a security manager
 410             // is present.  Avoid the overhead of making this call otherwise.
 411             caller = Reflection.getCallerClass();
 412             if (loader == null) {
 413                 ClassLoader ccl = ClassLoader.getClassLoader(caller);
 414                 if (ccl != null) {
 415                     sm.checkPermission(
 416                         SecurityConstants.GET_CLASSLOADER_PERMISSION);
 417                 }
 418             }


 475     @CallerSensitive
 476     public static Class<?> forName(Module module, String name) {
 477         Objects.requireNonNull(module);
 478         Objects.requireNonNull(name);
 479 
 480         ClassLoader cl;
 481         SecurityManager sm = System.getSecurityManager();
 482         if (sm != null) {
 483             Class<?> caller = Reflection.getCallerClass();
 484             if (caller != null && caller.getModule() != module) {
 485                 // if caller is null, Class.forName is the last java frame on the stack.
 486                 // java.base has all permissions
 487                 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
 488             }
 489             PrivilegedAction<ClassLoader> pa = module::getClassLoader;
 490             cl = AccessController.doPrivileged(pa);
 491         } else {
 492             cl = module.getClassLoader();
 493         }
 494 
 495         Class<?> ret;
 496         if (cl != null) {
 497             ret = cl.loadClass(module, name);
 498         } else {
 499             ret = BootLoader.loadClass(module, name);
 500         }
 501         if (ret != null) {
 502             // The loaded class should also be linked
 503             linkClass(ret);
 504         }
 505         return ret;
 506     }
 507 
 508     private static native void linkClass(Class<?> c);
 509     
 510     /**
 511      * Creates a new instance of the class represented by this {@code Class}
 512      * object.  The class is instantiated as if by a {@code new}
 513      * expression with an empty argument list.  The class is initialized if it
 514      * has not already been initialized.
 515      *
 516      * @deprecated This method propagates any exception thrown by the
 517      * nullary constructor, including a checked exception.  Use of
 518      * this method effectively bypasses the compile-time exception
 519      * checking that would otherwise be performed by the compiler.
 520      * The {@link
 521      * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
 522      * Constructor.newInstance} method avoids this problem by wrapping
 523      * any exception thrown by the constructor in a (checked) {@link
 524      * java.lang.reflect.InvocationTargetException}.
 525      *
 526      * <p>The call
 527      *
 528      * <pre>{@code
 529      * clazz.newInstance()


< prev index next >