Reflection APIs for records

Oliver Drotbohm odrotbohm at pivotal.io
Sat Jan 18 14:54:42 UTC 2020


Hi all,

the Spring team is currently investigating integration with records in various parts of the framework like form binding in Spring WebMVC/WebFlux or object mapping in Spring Data. Historically those features have worked with the PropertyDescriptor APIs adding a few tweaks here and there.

I was wondering whether you'd be willing to consider to expose PropertyDescriptors for record types so that they're effectively equivalents to a read-only property. I gave the Spring Framework abstraction around this a quick spin, plugging in a component that would create a synthetic PropertyDescriptor instance like this:

private static PropertyDescriptor toPropertyDescriptor(RecordComponent component) {

  try {
    return new PropertyDescriptor(component.getName(), component.getAccessor(), null);
  } catch (IntrospectionException o_O) {
    throw new RuntimeException("Invalid record definition!", o_O);
  }
}

This makes Spring Data object mapping work with records without any further changes. It also nicely mitigates between towards the different naming convention of the accessor methods in records.

If those PropertyDescriptors were exposed by the reflection APIs in the JDK in the first place,  other libraries could automatically work with records out of the box and would not need similar - or at least less - additional code. Jackson, Hibernate, template engines, EL implementations etc.

Slightly less important but still very welcome would be explicit exposure of the record constructor so that we don't have to scan and potentially disambiguate the one to be used to create instances of those records.

Cheers,
Ollie


More information about the amber-dev mailing list