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

The Class.forName specification should be updated to match the long-standing implementation with respect to class linking

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P3
    • Resolution: Approved
    • Fix Version/s: 14
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      behavioral
    • Compatibility Risk:
      minimal
    • Compatibility Risk Description:
      This changes the spec to match current behavior.
    • Interface Kind:
      Java API
    • Scope:
      SE

      Description

      Summary

      Class.forName() should clarify that it does not perform linking.

      Problem

      There has long been a mismatch between the specification for Class.forName() and its behavior with respect to class linking. Class.forName() is stated to "locate, load, and link" the class, however the method does not proceed beyond the loading phase (except in cases where initialization is performed).

      An attempt was made (JDK-8212117) for Class.forName() to perform the prescribed linking, but the resulting incompatibility turned out to be too disruptive, so the change was backed out (JDK-8233091).

      Solution

      A better solution is to update the Class.forName() spec to accurately reflect the long-standing behavior of locating and loading, but not linking.

      Specification

      java.lang.Class

            * @throws    ClassNotFoundException if the class cannot be located
      +     * 
      +     * @jls 12.2 Loading of Classes and Interfaces
      +     * @jls 12.3 Linking of Classes and Interfaces
      +     * @jls 12.4 Initialization of Classes and Interfaces
            */
           @CallerSensitive
           public static Class<?> forName(String className)
                       throws ClassNotFoundException

      ...

            * Given the fully qualified name for a class or interface (in the same
            * format returned by {@code getName}) this method attempts to
      -     * locate, load, and link the class or interface.  The specified class
      +     * locate and load the class or interface.  The specified class
            * loader is used to load the class or interface.  If the parameter
            * {@code loader} is null, the class is loaded through the bootstrap
            * class loader.  The class is initialized only if the
            * {@code initialize} parameter is {@code true} and if it has
            * not been initialized earlier.
      ...
            * @param name       fully qualified name of the desired class
      -     * @param initialize if {@code true} the class will be initialized.
      +     * @param initialize if {@code true} the class will be initialized (which implies linking).
            *                   See Section 12.4 of <em>The Java Language Specification</em>.
            * @param loader     class loader from which the class must be loaded
      ...
            * @see       java.lang.ClassLoader
      +     *
      +     * @jls 12.2 Loading of Classes and Interfaces
      +     * @jls 12.3 Linking of Classes and Interfaces
      +     * @jls 12.4 Initialization of Classes and Interfaces
            * @since     1.2
            */
      
          @CallerSensitive
          public static Class<?> forName(String name, boolean initialize,
                                         ClassLoader loader)
              throws ClassNotFoundException

      ...

           /**
            * Returns the {@code Class} with the given <a href="ClassLoader.html#binary-name">
            * binary name</a> in the given module.
            *
      -     * <p> This method attempts to locate, load, and link the class or interface.
      -     * It does not run the class initializer.  If the class is not found, this
      -     * method returns {@code null}. </p>
      +     * <p> This method attempts to locate and load the class or interface.
      +     * It does not link the class, and does not run the class initializer.
      +     * If the class is not found, this method returns {@code null}. </p>
            *
            * <p> If the class loader of the given module defines other modules and
      ...
            *         </ul>
            *
      +     * @jls 12.2 Loading of Classes and Interfaces
      +     * @jls 12.3 Linking of Classes and Interfaces
            * @since 9
            * @spec JPMS
            */
           @CallerSensitive
           public static Class<?> forName(Module module, String name)

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              bchristi Brent Christian
              Reporter:
              bchristi Brent Christian
              Reviewed By:
              Mandy Chung
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: