JDK-8233554 : The Class.forName specification should be updated to match the long-standing implementation with respect to class linking
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.lang:class_loading
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 14
  • Submitted: 2019-11-04
  • Updated: 2019-11-15
  • Resolved: 2019-11-14
Related Reports
CSR :  
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)



Comments
There is no behavioral change. I think a release note is not highly needed.
15-11-2019

Please consider a release note. Moving to Approved.
14-11-2019