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

Add type parameter to Lookup::accessClass and Lookup::ensureInitialized

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 21
    • core-libs
    • None
    • source
    • minimal
    • Adding a type parameter to a method is binary compatible as the method signature in the classfile is unchanged. Existing source files can compile against this change and this is source compatible.
    • Java API
    • SE

      Summary

      Generically parameterize Lookup::accessClass and Lookup::ensureInitialized, and document this type parameter.

      Problem

      When a class instance is passed to these two methods, the return value loses its type argument, render it infeasible to use in chained calls like Objects::requireNonNull.

      Solution

      Add a type parameter to these two methods; and use this type parameter as the type argument to Class passed and returned. Indicate in the specification that the targetClass argument passed is returned.

      Specification

      @@ -2810,6 +2810,7 @@ assertEquals("[x, y, z]", pb.command().toString());
                * This method returns when {@code targetClass} is fully initialized, or
                * when {@code targetClass} is being initialized by the current thread.
                *
      +         * @param <T> the type of the class to be initialized
                * @param targetClass the class to be initialized
                * @return {@code targetClass} that has been initialized, or that is being
                *         initialized by the current thread.
      @@ -2825,7 +2826,7 @@ assertEquals("[x, y, z]", pb.command().toString());
                * @since 15
                * @jvms 5.5 Initialization
                */
      -        public Class<?> ensureInitialized(Class<?> targetClass) throws IllegalAccessException {
      +        public <T> Class<T> ensureInitialized(Class<T> targetClass) throws IllegalAccessException {
                   if (targetClass.isPrimitive())
                       throw new IllegalArgumentException(targetClass + " is a primitive class");
                   if (targetClass.isArray())
      @@ -2925,8 +2926,9 @@ assertEquals("[x, y, z]", pb.command().toString());
                * <p>
                * Otherwise, {@code targetClass} is not accessible.
                *
      +         * @param <T> the type of the class to be access-checked
                * @param targetClass the class to be access-checked
      -         * @return the class that has been access-checked
      +         * @return {@code targetClass} that has been access-checked
                * @throws IllegalAccessException if the class is not accessible from the lookup class
                * and previous lookup class, if present, using the allowed access modes.
                * @throws SecurityException if a security manager is present and it
      @@ -2935,7 +2937,7 @@ assertEquals("[x, y, z]", pb.command().toString());
                * @since 9
                * @see <a href="#cross-module-lookup">Cross-module lookups</a>
                */
      -        public Class<?> accessClass(Class<?> targetClass) throws IllegalAccessException {
      +        public <T> Class<T> accessClass(Class<T> targetClass) throws IllegalAccessException {
                   if (!isClassAccessible(targetClass)) {
                       throw makeAccessException(targetClass);
                   }

            liach Chen Liang
            webbuggrp Webbug Group
            Mandy Chung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: