< prev index next >

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

Print this page




 308      * the current class.
 309      *
 310      * <p> For example, the following code fragment returns the
 311      * runtime {@code Class} descriptor for the class named
 312      * {@code java.lang.Thread}:
 313      *
 314      * <blockquote>
 315      *   {@code Class t = Class.forName("java.lang.Thread")}
 316      * </blockquote>
 317      * <p>
 318      * A call to {@code forName("X")} causes the class named
 319      * {@code X} to be initialized.
 320      *
 321      * @param      className   the fully qualified name of the desired class.
 322      * @return     the {@code Class} object for the class with the
 323      *             specified name.
 324      * @throws    LinkageError if the linkage fails
 325      * @throws    ExceptionInInitializerError if the initialization provoked
 326      *            by this method fails
 327      * @throws    ClassNotFoundException if the class cannot be located




 328      */
 329     @CallerSensitive
 330     public static Class<?> forName(String className)
 331                 throws ClassNotFoundException {
 332         Class<?> caller = Reflection.getCallerClass();
 333         return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
 334     }
 335 
 336 
 337     /**
 338      * Returns the {@code Class} object associated with the class or
 339      * interface with the given string name, using the given class loader.
 340      * Given the fully qualified name for a class or interface (in the same
 341      * format returned by {@code getName}) this method attempts to
 342      * locate, load, and link the class or interface.  The specified class
 343      * loader is used to load the class or interface.  If the parameter
 344      * {@code loader} is null, the class is loaded through the bootstrap
 345      * class loader.  The class is initialized only if the
 346      * {@code initialize} parameter is {@code true} and if it has
 347      * not been initialized earlier.
 348      *
 349      * <p> If {@code name} denotes a primitive type or void, an attempt
 350      * will be made to locate a user-defined class in the unnamed package whose
 351      * name is {@code name}. Therefore, this method cannot be used to
 352      * obtain any of the {@code Class} objects representing primitive
 353      * types or void.
 354      *
 355      * <p> If {@code name} denotes an array class, the component type of
 356      * the array class is loaded but not initialized.
 357      *
 358      * <p> For example, in an instance method the expression:
 359      *
 360      * <blockquote>
 361      *  {@code Class.forName("Foo")}
 362      * </blockquote>
 363      *
 364      * is equivalent to:
 365      *
 366      * <blockquote>
 367      *  {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
 368      * </blockquote>
 369      *
 370      * Note that this method throws errors related to loading, linking or
 371      * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
 372      * Java Language Specification</em>.
 373      * Note that this method does not check whether the requested class
 374      * is accessible to its caller.
 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      * @throws    LinkageError if the linkage fails
 383      * @throws    ExceptionInInitializerError if the initialization provoked
 384      *            by this method fails
 385      * @throws    ClassNotFoundException if the class cannot be located by
 386      *            the specified class loader
 387      * @throws    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             }
 415         }
 416         return forName0(name, initialize, loader, caller);
 417     }
 418 
 419     /** Called after security check for system loader access checks have been made. */
 420     private static native Class<?> forName0(String name, boolean initialize,
 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 {




 308      * the current class.
 309      *
 310      * <p> For example, the following code fragment returns the
 311      * runtime {@code Class} descriptor for the class named
 312      * {@code java.lang.Thread}:
 313      *
 314      * <blockquote>
 315      *   {@code Class t = Class.forName("java.lang.Thread")}
 316      * </blockquote>
 317      * <p>
 318      * A call to {@code forName("X")} causes the class named
 319      * {@code X} to be initialized.
 320      *
 321      * @param      className   the fully qualified name of the desired class.
 322      * @return     the {@code Class} object for the class with the
 323      *             specified name.
 324      * @throws    LinkageError if the linkage fails
 325      * @throws    ExceptionInInitializerError if the initialization provoked
 326      *            by this method fails
 327      * @throws    ClassNotFoundException if the class cannot be located
 328      * 
 329      * @jls 12.2 Loading of Classes and Interfaces
 330      * @jls 12.3 Linking of Classes and Interfaces
 331      * @jls 12.4 Initialization of Classes and Interfaces
 332      */
 333     @CallerSensitive
 334     public static Class<?> forName(String className)
 335                 throws ClassNotFoundException {
 336         Class<?> caller = Reflection.getCallerClass();
 337         return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
 338     }
 339 
 340 
 341     /**
 342      * Returns the {@code Class} object associated with the class or
 343      * interface with the given string name, using the given class loader.
 344      * Given the fully qualified name for a class or interface (in the same
 345      * format returned by {@code getName}) this method attempts to
 346      * locate and load the class or interface.  The specified class
 347      * loader is used to load the class or interface.  If the parameter
 348      * {@code loader} is null, the class is loaded through the bootstrap
 349      * class loader.  The class is initialized only if the
 350      * {@code initialize} parameter is {@code true} and if it has
 351      * not been initialized earlier.
 352      *
 353      * <p> If {@code name} denotes a primitive type or void, an attempt
 354      * will be made to locate a user-defined class in the unnamed package whose
 355      * name is {@code name}. Therefore, this method cannot be used to
 356      * obtain any of the {@code Class} objects representing primitive
 357      * types or void.
 358      *
 359      * <p> If {@code name} denotes an array class, the component type of
 360      * the array class is loaded but not initialized.
 361      *
 362      * <p> For example, in an instance method the expression:
 363      *
 364      * <blockquote>
 365      *  {@code Class.forName("Foo")}
 366      * </blockquote>
 367      *
 368      * is equivalent to:
 369      *
 370      * <blockquote>
 371      *  {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
 372      * </blockquote>
 373      *
 374      * Note that this method throws errors related to loading, linking or
 375      * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
 376      * Java Language Specification</em>.
 377      * Note that this method does not check whether the requested class
 378      * is accessible to its caller.
 379      *
 380      * @param name       fully qualified name of the desired class
 381      * @param initialize if {@code true} the class will be initialized (which implies linking).
 382      *                   See Section 12.4 of <em>The Java Language Specification</em>.
 383      * @param loader     class loader from which the class must be loaded
 384      * @return           class object representing the desired class
 385      *
 386      * @throws    LinkageError if the linkage fails
 387      * @throws    ExceptionInInitializerError if the initialization provoked
 388      *            by this method fails
 389      * @throws    ClassNotFoundException if the class cannot be located by
 390      *            the specified class loader
 391      * @throws    SecurityException
 392      *            if a security manager is present, and the {@code loader} is
 393      *            {@code null}, and the caller's class loader is not
 394      *            {@code null}, and the caller does not have the
 395      *            {@link RuntimePermission}{@code ("getClassLoader")}
 396      *
 397      * @see       java.lang.Class#forName(String)
 398      * @see       java.lang.ClassLoader
 399      *
 400      * @jls 12.2 Loading of Classes and Interfaces
 401      * @jls 12.3 Linking of Classes and Interfaces
 402      * @jls 12.4 Initialization of Classes and Interfaces
 403      * @since     1.2
 404      */
 405     @CallerSensitive
 406     public static Class<?> forName(String name, boolean initialize,
 407                                    ClassLoader loader)
 408         throws ClassNotFoundException
 409     {
 410         Class<?> caller = null;
 411         SecurityManager sm = System.getSecurityManager();
 412         if (sm != null) {
 413             // Reflective call to get caller class is only needed if a security manager
 414             // is present.  Avoid the overhead of making this call otherwise.
 415             caller = Reflection.getCallerClass();
 416             if (loader == null) {
 417                 ClassLoader ccl = ClassLoader.getClassLoader(caller);
 418                 if (ccl != null) {
 419                     sm.checkPermission(
 420                         SecurityConstants.GET_CLASSLOADER_PERMISSION);
 421                 }
 422             }
 423         }
 424         return forName0(name, initialize, loader, caller);
 425     }
 426 
 427     /** Called after security check for system loader access checks have been made. */
 428     private static native Class<?> forName0(String name, boolean initialize,
 429                                             ClassLoader loader,
 430                                             Class<?> caller)
 431         throws ClassNotFoundException;
 432 
 433 
 434     /**
 435      * Returns the {@code Class} with the given <a href="ClassLoader.html#binary-name">
 436      * binary name</a> in the given module.
 437      *
 438      * <p> This method attempts to locate and load the class or interface.
 439      * It does not link the class, and does not run the class initializer.
 440      * If the class is not found, this method returns {@code null}. </p>
 441      *
 442      * <p> If the class loader of the given module defines other modules and
 443      * the given name is a class defined in a different module, this method
 444      * returns {@code null} after the class is loaded. </p>
 445      *
 446      * <p> This method does not check whether the requested class is
 447      * accessible to its caller. </p>
 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 {


< prev index next >