Annotation and compact constructor

Vicente Romero vicente.romero at oracle.com
Tue Aug 25 13:15:32 UTC 2020


Hi Tagir,

Given that the annotation is applicable to fields, it should be accepted 
and propagated to the field. We didn't visualize annotations targeted to 
constructors as applicable to record components. We propagate the 
annotation in the record component to the corresponding parameter in the 
canonical constructor (if targeted to parameters only). But still as I 
said the compiler should accept the code as the annotation is applicable 
to fields. I will file a bug,

Thanks,
Vicente

On 8/25/20 1:49 AM, Tagir Valeev wrote:
> Hello!
>
> The following code cannot be compiled by javac (tried build 15-ea+33-1546):
>
> import java.lang.annotation.*;
>
> @Target({ElementType.FIELD, ElementType.CONSTRUCTOR})
> @Retention(RetentionPolicy.RUNTIME)
> @interface Ann {
> }
> record R(@Ann String str) {
>    R {}
> }
>
> Error message:
> R.java:7: error: annotation type not applicable to this kind of declaration
> record R(@Ann String str) {
>           ^
> 1 error
> 1 warning
>
> I would expect that the annotation is silently propagated to the
> compact constructor declaration. Is this correct behavior?
>
> With best regards,
> Tagir Valeev.



More information about the amber-dev mailing list