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

generic type information lost on mandated parameters of record's compact constructors

XMLWordPrintable

    • 21
    • b25
    • generic
    • generic
    • Verified

        ADDITIONAL SYSTEM INFORMATION :
        Tested on MacBook Pro M2, Ventura 13.4
        Java 21.0.1

        A DESCRIPTION OF THE PROBLEM :
        When, via reflection, trying to determine the generic type of an argument of the default record constructor,
        the generic type cannot be determined in Java 21.
        This did work in pre 21 versions. Tested in 17.0.9.

        REGRESSION : Last worked in version 17.0.9

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run provided test case.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Expect generic type to be available in the default record constructor.
        ACTUAL -
        No generic type information available.

        ---------- BEGIN SOURCE ----------
        import java.util.Optional;

        public class Reproducer {
            interface NoConstructorDeclarations {
                record Person(Optional<String> name, Optional<Integer> age) {}
            }

            interface AnnotatedCompactConstructor {
                record Person(Optional<String> name, Optional<Integer> age) {
                    @Deprecated public Person {}
                }
            }

            interface AnotatedExplicitCanonicalConstructor {
                record Person(Optional<String> name, Optional<Integer> age) {
                    @Deprecated
                    public Person(Optional<String> name, Optional<Integer> age) {
                        this.name = name;
                        this.age = age;
                    }
                }
            }

            public static void main(String args[]) {
                for(var approach: Reproducer.class.getDeclaredClasses()) {
                    Class<?> recordClass = approach.getClasses()[0];
                    System.out.println(approach.getSimpleName());
          
                    var constructor = recordClass.getConstructors()[0];
                    System.out.println(constructor.isAnnotationPresent(Deprecated.class));
                    for(var p: constructor.getParameters()) {
                        System.out.println(p);
                    }
                    System.out.println();
                }
            }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Explicitly add a canonical constructor with all record properties.

        FREQUENCY : always


              vromero Vicente Arturo Romero Zaldivar
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              9 Start watching this issue

                Created:
                Updated:
                Resolved: