Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8286927

Class::forName(String) defaults to system class loader if the caller is null

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 19
    • core-libs
    • None
    • behavioral
    • minimal
    • Hide
      The compatibility risk is minimal. `Class::forName(String)` will use the system class loader instead of the boot loader when there is no caller frame, and brings compatibility with the JNI FindClass method. `Class::forName(String)` will continue to return a class defined to the boot loader if the class exists. If a class cannot be found by the boot loader but can be found by the system class loader, the method will return the class defined to the system loader instead of getting `ClassNotFoundException` thrown by JDK 18.
      Show
      The compatibility risk is minimal. `Class::forName(String)` will use the system class loader instead of the boot loader when there is no caller frame, and brings compatibility with the JNI FindClass method. `Class::forName(String)` will continue to return a class defined to the boot loader if the class exists. If a class cannot be found by the boot loader but can be found by the system class loader, the method will return the class defined to the system loader instead of getting `ClassNotFoundException` thrown by JDK 18.
    • Java API
    • SE

      Summary

      Specify Class::forName(String) to use the system class loader if invoked with no caller frame.

      Problem

      The Class::forName(String) method is a caller sensitive method but does not specify what should happen when there are no stack frames to determine the caller. Currently the boot loader is used when there is no caller.

      JNI FindClass uses the system class loader instead if invoked via JNI invocation interface when there is no associated class loader. Class::forName when invoked by JNI code with no caller frame should be consistent with JNI FindClass and use the system class loader if caller is null.

      Solution

      When there is no caller frame on the stack, simply use the system class loader.

      Specification

      The spec of java.lang.Class.forName(String) is updated as follows:

      @@ -357,6 +357,11 @@ public final class Class<T> implements java.io.Serializable,
            * A call to {@code forName("X")} causes the class named
            * {@code X} to be initialized.
            *
      +     * <p>
      +     * In cases where this method is called from a context where there is no
      +     * caller frame on the stack (e.g. when called directly from a JNI
      +     * attached thread), the system class loader is used.
      +     *
            * @param      className   the fully qualified name of the desired class.
            * @return     the {@code Class} object for the class with the
            *             specified name.
      

            tprinzing Tim Prinzing (Inactive)
            mchung Mandy Chung (Inactive)
            Mandy Chung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: