Details
-
Bug
-
Resolution: Fixed
-
P3
-
21, 22, 23
-
b25
-
generic
-
generic
-
Verified
Backports
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8334608 | 21.0.6-oracle | Weibing Xiao | P3 | Open | Unresolved |
Description
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
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
Attachments
Issue Links
- backported by
-
JDK-8334608 generic type information lost on mandated parameters of record's compact constructors
-
- Open
-
- csr for
-
JDK-8321819 generic type information lost on mandated parameters of record's compact constructors
-
- Closed
-
- duplicates
-
JDK-8331026 Compact record constructor is missing generic type info on parameters
-
- Resolved
-
- relates to
-
JDK-8292275 javac does not emit SYNTHETIC and MANDATED flags for parameters by default
-
- Resolved
-
(1 links to)