Records to/from JSON

Oliver Drotbohm odrotbohm at pivotal.io
Thu May 7 22:32:11 UTC 2020


I brought this up in another thread: most frameworks could work with records out of the box if Introspector.getBeanInfo() returned a BeanInfo that exposes a PropertyDescriptor that treats record components like as read-only properties.

Here's [0] a quick prototype that effectively simulates that hooking into Spring Framework's BeanInfo lookup and it makes Spring and all its ecosystem projects work with records anywhere we do bean style binding. Even the creation via constructors works.

I already suggested to tweak Introspector accordingly to avoid most Java libraries out there having to ship similar code and Brian got back to me to help me work on that but I unfortunately currently don't have any time I could spend on this, mostly because of the process required. Happy to give input though (I know, input doesn't compile).

Cheers,
Ollie

[0] https://github.com/odrotbohm/spring-record-support/blob/master/spring-record-support/src/main/java/de/odrotbohm/experiments/jdk14/Jdk14BeanInfoFactory.java

> Am 08.05.2020 um 00:13 schrieb Alex Buckley <alex.buckley at oracle.com>:
> 
> On 5/7/2020 2:55 PM, Brian Goetz wrote:
>> This is certainly one of the cases that MR Jars were designed for, but `Class::isRecord` is so trivial that I would probably just use reflection:
>> ```
>> static final Predicate<Class> isRecord;
>> static {
>>     try {
>>         Method m = Class.class.getDeclaredMethod("isRecord", ...);
>>         isRecord = c -> { try { return m.invoke(c); }
>>                           catch (ROE e) { return false; } };
>>     }
>>     catch (ROE e) {
>>         isRecord = c -> false;
>>     }
>> }
>> ```
> 
> Much better to invoke Class::isRecord in this fashion, than to inspect its source code and end up assuming that `java.lang.Record` is always the direct superclass of a record class.
> 
> Alex



More information about the amber-dev mailing list