< prev index next >

src/java.base/share/classes/java/lang/constant/ClassDesc.java

Print this page
rev 54588 : 8212975: ClassDesc should have a full name method
Reviewed-by: vromero

@@ -289,26 +289,57 @@
      * suffixed with the appropriate number of {@code []} pairs for array types.
      *
      * @return the human-readable name
      */
     default String displayName() {
-        if (isPrimitive())
-            return Wrapper.forBasicType(descriptorString().charAt(0)).primitiveSimpleName();
-        else if (isClassOrInterface()) {
-            return descriptorString().substring(Math.max(1, descriptorString().lastIndexOf('/') + 1),
-                                                descriptorString().length() - 1);
+        return displayName(false);
         }
-        else if (isArray()) {
+
+    /**
+     * Returns a 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 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[]}).
+     * <p>The actual class name returned will be full qualified if
+     * {@code detail} is {@code true}, otherwise the simple class
+     * name is returned.
+     *
+     * @param detail - if true returns full class name,
+     *                 else the simple class name
+     *
+     * @return the full human-readable name
+     */
+    default String displayName(boolean detail) {
+        if (isPrimitive()) {
+            return Wrapper.forBasicType(descriptorString()
+                          .charAt(0))
+                          .primitiveSimpleName();
+        } else if (isClassOrInterface()) {
+            String internalClassName = ConstantUtils.dropFirstAndLastChar(descriptorString());
+            String binaryClassName = internalToBinary(internalClassName);
+            if (detail) {
+                return binaryClassName;
+
+            }
+            int index = binaryClassName.lastIndexOf('.');
+            return index == -1 ? binaryClassName : binaryClassName.substring(index + 1);
+        } else if (isArray()) {
             int depth = ConstantUtils.arrayDepth(descriptorString());
             ClassDesc c = this;
             for (int i=0; i<depth; i++)
                 c = c.componentType();
-            return c.displayName() + "[]".repeat(depth);
-        }
-        else
+            return c.displayName(detail) + "[]".repeat(depth);
+        } else {
             throw new IllegalStateException(descriptorString());
     }
+    }
 
     /**
      * Returns a field type descriptor string for this type
      *
      * @return the descriptor string
< prev index next >