< 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


 274     default String packageName() {
 275         if (!isClassOrInterface())
 276             return "";
 277         String className = internalToBinary(ConstantUtils.dropFirstAndLastChar(descriptorString()));
 278         int index = className.lastIndexOf('.');
 279         return (index == -1) ? "" : className.substring(0, index);
 280     }
 281 
 282     /**
 283      * Returns a human-readable name for the type described by this descriptor.
 284      *
 285      * @implSpec
 286      * <p>The default implementation returns the simple name
 287      * (e.g., {@code int}) for primitive types, the unqualified class name
 288      * for class or interface types, or the display name of the component type
 289      * suffixed with the appropriate number of {@code []} pairs for array types.
 290      *
 291      * @return the human-readable name
 292      */
 293     default String displayName() {
 294         if (isPrimitive())
 295             return Wrapper.forBasicType(descriptorString().charAt(0)).primitiveSimpleName();
 296         else if (isClassOrInterface()) {
 297             return descriptorString().substring(Math.max(1, descriptorString().lastIndexOf('/') + 1),
 298                                                 descriptorString().length() - 1);
 299         }
 300         else if (isArray()) {



































 301             int depth = ConstantUtils.arrayDepth(descriptorString());
 302             ClassDesc c = this;
 303             for (int i=0; i<depth; i++)
 304                 c = c.componentType();
 305             return c.displayName() + "[]".repeat(depth);
 306         }
 307         else
 308             throw new IllegalStateException(descriptorString());
 309     }

 310 
 311     /**
 312      * Returns a field type descriptor string for this type
 313      *
 314      * @return the descriptor string
 315      * @jvms 4.3.2 Field Descriptors
 316      */
 317     String descriptorString();
 318 
 319     /**
 320      * Compare the specified object with this descriptor for equality.  Returns
 321      * {@code true} if and only if the specified object is also a
 322      * {@linkplain ClassDesc} and both describe the same type.
 323      *
 324      * @param o the other object
 325      * @return whether this descriptor is equal to the other object
 326      */
 327     boolean equals(Object o);
 328 }


 274     default String packageName() {
 275         if (!isClassOrInterface())
 276             return "";
 277         String className = internalToBinary(ConstantUtils.dropFirstAndLastChar(descriptorString()));
 278         int index = className.lastIndexOf('.');
 279         return (index == -1) ? "" : className.substring(0, index);
 280     }
 281 
 282     /**
 283      * Returns a human-readable name for the type described by this descriptor.
 284      *
 285      * @implSpec
 286      * <p>The default implementation returns the simple name
 287      * (e.g., {@code int}) for primitive types, the unqualified class name
 288      * for class or interface types, or the display name of the component type
 289      * suffixed with the appropriate number of {@code []} pairs for array types.
 290      *
 291      * @return the human-readable name
 292      */
 293     default String displayName() {
 294         return displayName(false);




 295     }
 296 
 297     /**
 298      * Returns a human-readable name for the type described by this
 299      * descriptor.
 300      *
 301      * @implSpec
 302      * <p>The default implementation returns the name
 303      * (e.g., {@code int}) for primitive types, the class name for
 304      * class or interface types (e.g., {@code java.lang.String}),
 305      * or the name of the component type with the appropriate
 306      * number of {@code []} pairs for array types
 307      * (e.g., {@code java.lang.String[]}).
 308      * <p>The actual class name returned will be full qualified if
 309      * {@code detail} is {@code true}, otherwise the simple class
 310      * name is returned.
 311      *
 312      * @param detail - if true returns full class name,
 313      *                 else the simple class name
 314      *
 315      * @return the full human-readable name
 316      */
 317     default String displayName(boolean detail) {
 318         if (isPrimitive()) {
 319             return Wrapper.forBasicType(descriptorString()
 320                           .charAt(0))
 321                           .primitiveSimpleName();
 322         } else if (isClassOrInterface()) {
 323             String internalClassName = ConstantUtils.dropFirstAndLastChar(descriptorString());
 324             String binaryClassName = internalToBinary(internalClassName);
 325             if (detail) {
 326                 return binaryClassName;
 327 
 328             }
 329             int index = binaryClassName.lastIndexOf('.');
 330             return index == -1 ? binaryClassName : binaryClassName.substring(index + 1);
 331         } else if (isArray()) {
 332             int depth = ConstantUtils.arrayDepth(descriptorString());
 333             ClassDesc c = this;
 334             for (int i=0; i<depth; i++)
 335                 c = c.componentType();
 336             return c.displayName(detail) + "[]".repeat(depth);
 337         } else {

 338             throw new IllegalStateException(descriptorString());
 339         }
 340     }
 341 
 342     /**
 343      * Returns a field type descriptor string for this type
 344      *
 345      * @return the descriptor string
 346      * @jvms 4.3.2 Field Descriptors
 347      */
 348     String descriptorString();
 349 
 350     /**
 351      * Compare the specified object with this descriptor for equality.  Returns
 352      * {@code true} if and only if the specified object is also a
 353      * {@linkplain ClassDesc} and both describe the same type.
 354      *
 355      * @param o the other object
 356      * @return whether this descriptor is equal to the other object
 357      */
 358     boolean equals(Object o);
 359 }
< prev index next >