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

java.lang.classfile.TypeKind improvements

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 23
    • core-libs
    • None
    • source, binary, behavioral
    • minimal
    • Hide
      The Class-File API is preview, users should anticipate the API to have breaking changes.
      1. The newarrayCode APIs are only used in 2 files in CF implementations and not used by tests, impact should be low.
      2. Users generally don't pass an empty String to fromDescriptor, so the migration cost should be low.
      Show
      The Class-File API is preview, users should anticipate the API to have breaking changes. 1. The newarrayCode APIs are only used in 2 files in CF implementations and not used by tests, impact should be low. 2. Users generally don't pass an empty String to fromDescriptor, so the migration cost should be low.
    • Java API
    • SE

      Summary

      Update TypeKind's method names and API specification to address issues with TypeKind.

      Problem

      1. TypeKind::newarraycode and TypeKind::fromNewArrayCode have name discrepancies.
      2. TypeKind::fromDescriptor(String) throws undocumented IndexOutOfBoundsException instead of IllegalArgumentException.

      Solution

      1. Rename the methods to newarrayCode and fromNewarrayCode. newarray is a whole word as it's the name of an instruction.
      2. Specify that TypeKind::fromDescriptor may throw IllegalArgumentException if the descriptor string is invalid (but is not required to do so)

      Specification

      --- a/src/java.base/share/classes/java/lang/classfile/TypeKind.java
      +++ b/src/java.base/share/classes/java/lang/classfile/TypeKind.java
      @@ -66,9 +66,12 @@ public enum TypeKind {
           /** {@return the field descriptor character corresponding to this type} */
           public String descriptor() { return descriptor; }
      
      -    /** {@return the code used by the {@code newarray} opcode corresponding to this type} */
      -    public int newarraycode() {
      -        return newarraycode;
      +    /**
      +     * {@return the code used by the {@code newarray} opcode corresponding to this type}
      +     * @since 23
      +     */
      +    public int newarrayCode() {
      +        return newarrayCode;
           }
      
           /**
      @@ -94,19 +97,21 @@ public TypeKind asLoadable() {
           /**
            * {@return the type kind associated with the array type described by the
            * array code used as an operand to {@code newarray}}
      -     * @param newarraycode the operand of the {@code newarray} instruction
      +     * @param newarrayCode the operand of the {@code newarray} instruction
      +     * @throws IllegalArgumentException if the code is invalid
      +     * @since 23
            */
      -    public static TypeKind fromNewArrayCode(int newarraycode) {
      -        return switch (newarraycode) {
      +    public static TypeKind fromNewarrayCode(int newarrayCode) {
      +        return switch (newarrayCode) {
                   case 4 -> TypeKind.BooleanType;
                   case 5 -> TypeKind.CharType;
                   case 6 -> TypeKind.FloatType;
      @@ -115,15 +120,19 @@ public static TypeKind fromNewArrayCode(int newarraycode) {
           /**
            * {@return the type kind associated with the specified field descriptor}
            * @param s the field descriptor
      +     * @throws IllegalArgumentException only if the descriptor is not valid
            */
           public static TypeKind fromDescriptor(CharSequence s) {
      +        if (s.isEmpty()) { // implicit null check
      +            throw new IllegalArgumentException("Empty descriptor");
      +        }
               return switch (s.charAt(0)) {
                   case '[', 'L' -> TypeKind.ReferenceType;
                   case 'B' -> TypeKind.ByteType;

            liach Chen Liang
            liach Chen Liang
            Adam Sotona
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: