< 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             }


 421                                             ClassLoader loader,
 422                                             Class<?> caller)
 423         throws ClassNotFoundException;
 424 
 425 
 426     /**
 427      * Returns the {@code Class} with the given <a href="ClassLoader.html#binary-name">
 428      * binary name</a> in the given module.
 429      *
 430      * <p> This method attempts to locate, load, and link the class or interface.
 431      * It does not run the class initializer.  If the class is not found, this
 432      * method returns {@code null}. </p>
 433      *
 434      * <p> If the class loader of the given module defines other modules and
 435      * the given name is a class defined in a different module, this method
 436      * returns {@code null} after the class is loaded. </p>
 437      *
 438      * <p> This method does not check whether the requested class is
 439      * accessible to its caller. </p>
 440      *




 441      * @apiNote
 442      * This method returns {@code null} on failure rather than
 443      * throwing a {@link ClassNotFoundException}, as is done by
 444      * the {@link #forName(String, boolean, ClassLoader)} method.
 445      * The security check is a stack-based permission check if the caller
 446      * loads a class in another module.
 447      *
 448      * @param  module   A module
 449      * @param  name     The <a href="ClassLoader.html#binary-name">binary name</a>
 450      *                  of the class
 451      * @return {@code Class} object of the given name defined in the given module;
 452      *         {@code null} if not found.
 453      *
 454      * @throws NullPointerException if the given module or name is {@code null}
 455      *
 456      * @throws LinkageError if the linkage fails
 457      *
 458      * @throws SecurityException
 459      *         <ul>
 460      *         <li> if the caller is not the specified module and
 461      *         {@code RuntimePermission("getClassLoader")} permission is denied; or</li>
 462      *         <li> access to the module content is denied. For example,
 463      *         permission check will be performed when a class loader calls
 464      *         {@link ModuleReader#open(String)} to read the bytes of a class file
 465      *         in a module.</li>
 466      *         </ul>
 467      *


 468      * @since 9
 469      * @spec JPMS
 470      */
 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




 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             }


 425                                             ClassLoader loader,
 426                                             Class<?> caller)
 427         throws ClassNotFoundException;
 428 
 429 
 430     /**
 431      * Returns the {@code Class} with the given <a href="ClassLoader.html#binary-name">
 432      * binary name</a> in the given module.
 433      *
 434      * <p> This method attempts to locate, load, and link the class or interface.
 435      * It does not run the class initializer.  If the class is not found, this
 436      * method returns {@code null}. </p>
 437      *
 438      * <p> If the class loader of the given module defines other modules and
 439      * the given name is a class defined in a different module, this method
 440      * returns {@code null} after the class is loaded. </p>
 441      *
 442      * <p> This method does not check whether the requested class is
 443      * accessible to its caller. </p>
 444      * 
 445      * <p> Note that this method throws errors related to loading and linking as
 446      * specified in Sections 12.2 and 12.3 of <em>The Java Language
 447      * Specification</em>.
 448      * 
 449      * @apiNote
 450      * This method returns {@code null} on failure rather than
 451      * throwing a {@link ClassNotFoundException}, as is done by
 452      * the {@link #forName(String, boolean, ClassLoader)} method.
 453      * The security check is a stack-based permission check if the caller
 454      * loads a class in another module.
 455      *
 456      * @param  module   A module
 457      * @param  name     The <a href="ClassLoader.html#binary-name">binary name</a>
 458      *                  of the class
 459      * @return {@code Class} object of the given name defined in the given module;
 460      *         {@code null} if not found.
 461      *
 462      * @throws NullPointerException if the given module or name is {@code null}
 463      *
 464      * @throws LinkageError if the linkage fails
 465      *
 466      * @throws SecurityException
 467      *         <ul>
 468      *         <li> if the caller is not the specified module and
 469      *         {@code RuntimePermission("getClassLoader")} permission is denied; or</li>
 470      *         <li> access to the module content is denied. For example,
 471      *         permission check will be performed when a class loader calls
 472      *         {@link ModuleReader#open(String)} to read the bytes of a class file
 473      *         in a module.</li>
 474      *         </ul>
 475      *
 476      * @jls 12.2 Loading of Classes and Interfaces
 477      * @jls 12.3 Linking of Classes and Interfaces
 478      * @since 9
 479      * @spec JPMS
 480      */
 481     @CallerSensitive
 482     public static Class<?> forName(Module module, String name) {
 483         Objects.requireNonNull(module);
 484         Objects.requireNonNull(name);
 485 
 486         ClassLoader cl;
 487         SecurityManager sm = System.getSecurityManager();
 488         if (sm != null) {
 489             Class<?> caller = Reflection.getCallerClass();
 490             if (caller != null && caller.getModule() != module) {
 491                 // if caller is null, Class.forName is the last java frame on the stack.
 492                 // java.base has all permissions
 493                 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
 494             }
 495             PrivilegedAction<ClassLoader> pa = module::getClassLoader;
 496             cl = AccessController.doPrivileged(pa);
 497         } else {
 498             cl = module.getClassLoader();
 499         }
 500 
 501         Class<?> ret;
 502         if (cl != null) {
 503             ret = cl.loadClass(module, name);
 504         } else {
 505             ret = BootLoader.loadClass(module, name);
 506         }
 507         if (ret != null) {
 508             // The loaded class should also be linked
 509             linkClass(ret);
 510         }
 511         return ret;
 512     }
 513 
 514     private static native void linkClass(Class<?> c);
 515     
 516     /**
 517      * Creates a new instance of the class represented by this {@code Class}
 518      * object.  The class is instantiated as if by a {@code new}
 519      * expression with an empty argument list.  The class is initialized if it
 520      * has not already been initialized.
 521      *
 522      * @deprecated This method propagates any exception thrown by the
 523      * nullary constructor, including a checked exception.  Use of
 524      * this method effectively bypasses the compile-time exception
 525      * checking that would otherwise be performed by the compiler.
 526      * The {@link
 527      * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
 528      * Constructor.newInstance} method avoids this problem by wrapping
 529      * any exception thrown by the constructor in a (checked) {@link
 530      * java.lang.reflect.InvocationTargetException}.
 531      *
 532      * <p>The call
 533      *
 534      * <pre>{@code


< prev index next >