Records to/from JSON

Alex Buckley alex.buckley at oracle.com
Thu May 7 22:13:25 UTC 2020


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