[records] Specify use of @Override and @SafeVarargs on record components

Gavin Bierman gavin.bierman at oracle.com
Mon Sep 21 20:25:26 UTC 2020



> On 19 Sep 2020, at 10:23, Tagir Valeev <amaembo at gmail.com> wrote:
> 
> Hello!
> 
> On Fri, Sep 18, 2020 at 10:40 PM Gavin Bierman <gavin.bierman at oracle.com> wrote:
> 
>> - It is annotated with the annotations, if any, that appear on the corresponding
>>  record component and whose annotation types are applicable in the method
>>  declaration context, or in type contexts, or both. **The rules for these
>>  annotation modifiers, if any, on the accessor method are the same as for a
>>  method declaration, and are specified in [9.7.4] and [9.7.5].**
>> 
>> The last sentence is new, and mirrors what is said about normal method declarations in §8.4.3.
>> 
>> What do you think?
> 
> I'm not very good in English but I'm fine if this statement
> unambiguously imply that
> if the annotation is not applicable to the method, then this is a
> compilation error to put it on the record accessor.

Well it’s as unambiguous as the existing text around annotations on method declarations :-) 

I think to further emphasise this point, I will add the following text after the list of properties of the implicitly declared accessor method:

An implicitly declared accessor method must satisfy all the rules for a
method in a normal class declaration ([8.4]).

That makes it crystal clear (I hope). 

> 
> Btw, we allowed Override on explicit accessors, so now it might be
> unclear whether it's allowed on implicit ones.
> 
> Also what if we mark the record component with `@SafeVarargs` and
> define an explicit accessor without this annotation? Like:
> 
> record X(@SafeVarargs int y) {
>  @Override
>  public int y() {
>    return y;
>  }
> }
> 
> Is this acceptable? Javac 15 happily compiles this.

Under the current rules this is acceptable; as the @SafeVarargs annotation is not propagated to the explicitly declared accessor method. 

> 
> This brings more general question: is it allowed to put an annotation
> that targeted to METHOD only if we define explicit accessor? In this
> case, the annotation is not propagated anywhere and completely
> disappears from the bytecode, even if it has CLASS/RUNTIME retention.
> So probably this code should be rejected as mistaken? 

That’s a very good question - well spotted. I think there is a good case to call that out as a compile-time error. Opinions everyone?

Gavin

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20200921/8c166204/attachment.htm>


More information about the amber-spec-experts mailing list