< prev index next >

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

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

@@ -25,12 +25,16 @@
 package java.lang.constant;
 
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
+import java.util.stream.Stream;
+
+import sun.invoke.util.Wrapper;
 
 import static java.util.Objects.requireNonNull;
+import static java.util.stream.Collectors.joining;
 
 /**
  * Helper methods for the implementation of {@code java.lang.constant}.
  */
 class ConstantUtils {

@@ -233,6 +237,41 @@
                 index ++;
             }
         }
         return true;
     }
+
+    static String getClassDisplayName(ClassDesc cd, boolean detail) {
+        String descriptor = cd.descriptorString();
+        if (cd.isPrimitive()) {
+           return Wrapper.forBasicType(descriptor
+                         .charAt(0))
+                         .primitiveSimpleName();
+        } else if (cd.isClassOrInterface()) {
+            String internalClassName = ConstantUtils.dropFirstAndLastChar(descriptor);
+            String binaryClassName = internalToBinary(internalClassName);
+            if (detail) {
+                return binaryClassName;
+
+            }
+            int index = binaryClassName.lastIndexOf('.');
+            return index == -1 ? binaryClassName : binaryClassName.substring(index + 1);
+        } else if (cd.isArray()) {
+            int depth = ConstantUtils.arrayDepth(descriptor);
+            ClassDesc c = cd;
+            for (int i=0; i<depth; i++)
+                c = c.componentType();
+            return getClassDisplayName(c, detail) + "[]".repeat(depth);
+        } else {
+            throw new IllegalStateException(descriptor);
+        }
+    }
+
+    static String getDisplayDescriptor(MethodTypeDesc mtd, boolean detail) {
+        return String.format("(%s)%s",
+                Stream.of(mtd.parameterArray())
+                        .map(cd -> getClassDisplayName(cd, detail))
+                        .collect(joining(",")),
+                getClassDisplayName(mtd.returnType(), detail));
+    }
+
 }
< prev index next >