[records] A reflection API to get the primary constructor?

Brian Goetz brian.goetz at oracle.com
Wed Sep 25 16:19:32 UTC 2019


Clearly, this has to be possible.  For example, serialization will want 
to find the canonical constructor.  Currently, it does so with a stream 
expression like you present below; with the new API, the structure of 
the stream expression changes slightly, but it is basically the same game.

We surely _can_ expose some reflective call like 
Class::getCanonicalRecordConstructor().  The question is whether this is 
worth having yet another entry point in a class that is already quite 
crowded, or ask frameworks to do as you illustrate below.  It's not 
entirely clear whether it carries its weight, as opposed to putting an 
example in the spec for getRecordComponents().




On 9/25/2019 11:44 AM, Tagir Valeev wrote:
> Hello!
>
> Speaking of the reflection API, do we have a way to get a primary
> constructor of a record class? Sure we can do something like
>
> cls.getConstructor(Arrays.stream(cls.getRecordAccessors()).map(Method::getReturnType).toArray())
>
> But it looks like a bit verbose. Something like
> cls.getPrimaryConstructor() (throwing on non-records) looks better.
> What do you think?
>
> With best regards,
> Tagir Valeev.



More information about the amber-spec-experts mailing list