[Records] Component annotations not propagated when explicit canonical constructor is given
Brian Goetz
brian.goetz at oracle.com
Mon Jan 20 00:32:23 UTC 2020
This sounds like incorrect behavior to me. The way it should work is:
- A declaration annotation is applicable to a record component if it
has no @Target meta-anno, or its target includes one or more of
PARAMETER, FIELD, METHOD, RECORD_COMPONENT.
- For _each_ of the applicable types present, the anno is pushed down
to the corresponding _implicit_ { ctor parameter, field, accessor
method, record component }.
- If multiple applicable types are present, it is pushed down to all
of them.
- If a canonical ctor / accessor is explicitly specified, the annos on
the declaration are used instead.
On 1/19/2020 4:14 PM, Gunnar Morling wrote:
> Hi,
>
> I'm observing the following behaviour around annotations on record
> components, using JDK 14 b32. I'm putting an annotation to a record
> component:
>
> public record Person(@NotNull String name) {}
>
> In this case the @NotNull annotation is propagated to the
> corresponding parameter of the generated constructor, from where I can
> obtain it using reflection. This is not the case though when I
> explicitly declare the canonical constructor:
>
> public record Person(@NotNull String name) {
> public Person {
> // ...
> }
> }
>
> In this case the annotation isn't propagated, and it won't be
> retrievable from that constructor's parameter via reflection.
>
> Is this behaviour intended or is it a bug actually? I lean towards the
> latter, as I don't explicitly define the parameter in the constructor,
> so I'd expect the annotations given on the component to still be
> propagated.
>
> If it *is* intended, it'd make my use case a bit more complex, as I'd
> want to be able to put other annotations to the canonical constructor
> *itself*, while still getting all the component annotations propagated
> to its parameters.
>
> Thanks a lot,
>
> --Gunnar
More information about the amber-dev
mailing list