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

ClassDesc should have a full name method

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P3
    • Resolution: Withdrawn
    • Fix Version/s: tbd
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      binary
    • Compatibility Risk:
      low
    • Compatibility Risk Description:
      Introducing a new method to a recently added interface. Possibility of name conflict.
    • Interface Kind:
      Java API
    • Scope:
      SE

      Description

      Summary

      Add the method ClassDesc::displayFullName to return the fully qualified class name from a ClassDesc. Add a second method MethodTypeDesc::displayFullDescriptor which returns the MethodTypeDesc descriptor using fully qualified class names.

      Problem

      After the release of the constants interface, ClassDesc became quite useful to the internals of the java compiler APIs (see JEP 348.) One common use pattern is using the fully qualified class name to locate the class or to create a class symbol. An example of this code pattern is as follows;

          ClassDesc cd = ...
          String packageName = cd.packageName();
          String name = packageName.isEmpty() ? cd.displayName() :
                                                packageName + "." + cd.displayName();
          Symbol classSym = names.fromString(name);

      This is overly complicated, especially considering the ClassDesc already holds the full qualified class name as part of the type descriptor.

      Solution

      Introduce a new method ClassDesc::displayFullName which returns the fully qualified class name.

          String.class.describeConstable().displayName() -> "String"
      
          String.class.describeConstable().displayFullName() -> "java.lang.String"

      Thus use case from above then becomes;

          ClassDesc cd = ...
          Symbol classSym = names.fromString(cd.displayFullName());

      Secondarily, this change should propagate to MethodTypeDesc::displayDescriptor, introducing a new method MethodTypeDesc::displayFullDescriptor which, like MethodTypeDesc::displayDescriptor, returns a MethodType descriptor except with fully qualified class names.

          MethodType mt = MethodType.methodType(String.class, Integer.class);
          MethodTypeDesc mtd = mt.describeConstable().get();
      
          mtd.displayDescriptor() -> "(Integer)String"
          mtd.displayFullDescriptor() -> "(java.lang.Integer)java.lang.String"
      

      Specification

      In ClassDesc.java

           /**
           * Returns a fully qualified human-readable name for the type described
           * by this descriptor.
           *
           * @implSpec
           * <p>The default implementation returns the name
           * (e.g., {@code int}) for primitive types, the qualified class name
           * for class or interface types (e.g., {@code java.lang.String}),
           * or the name of the component type with the appropriate
           * number of {@code []} pairs for array types
           * (e.g., {@code java.lang.String[]}).
           *
           * @return the fully qualified human-readable name
           */
          default String displayFullName() {
      
      
      in MethodTypeDesc.java
      
          /**
           * Returns a human-readable descriptor for this method type, using the
           * fully qualified canonical names for parameter and return types.
           *
           * @return the human-readable descriptor for this method type
           */
          default String displayFullDescriptor() {

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              jlaskey Jim Laskey
              Reporter:
              jlaskey Jim Laskey
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: