JDK-8331745 : java.lang.classfile.TypeKind improvements
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.lang.classfile
  • Priority: P4
  • Status: Draft
  • Resolution: Unresolved
  • Fix Versions: 23
  • Submitted: 2024-05-06
  • Updated: 2024-05-07
Related Reports
CSR :  
Relates :  
Description
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;