@Override on a record component compiles but does not work

forax at univ-mlv.fr forax at univ-mlv.fr
Tue Jul 20 17:26:08 UTC 2021


> From: "Gavin Bierman" <gavin.bierman at oracle.com>
> To: "Vicente Romero" <vicente.romero at oracle.com>
> Cc: "Remi Forax" <forax at univ-mlv.fr>, "compiler-dev"
> <compiler-dev at openjdk.java.net>
> Sent: Mardi 20 Juillet 2021 19:04:47
> Subject: Re: @Override on a record component compiles but does not work

> Hi Remi,

> Just to back up Vicente’s reply.

> You’ll recall that the intent of the implicit accessor declaration is that these
> two record declarations are, in essence, identical :

> record R(@Override int x){}

> and

> record R(int x){
> @Override
> public int x(){
> return this.x;
> }
> }

> So, given that assumption, and the snippet that Vicente included from JLS
> 16.9.6.4.4 that explains that @Override has a special meaning for accessor
> methods, then you get the behaviour that you have observed.

Thanks to both of you, 
I think i miss the fact that allowing @Override on an accessor also means that it makes @Override useless on components. 

Now, i wonder if it something that we should fix or not, only allows @Override on a component if it implements a method of an interface or it does not worth to have a special case and it's already too late. 

> Thanks,
> Gavin

regards, 
Rémi 

>> On 19 Jul 2021, at 20:54, Vicente Romero < [ mailto:vicente.romero at oracle.com |
>> vicente.romero at oracle.com ] > wrote:

>> Hi Remi,

>> According to JLS 16 9.6.4.4:

>> ...
>> The clause about a record class is due to the special meaning of @Override in a
>> record
>> declaration. Namely, it can be used to specify that a method declaration is an
>> accessor
>> method for a record component. Consider the following record declaration:

>> record Roo(int x) {
>> @Override
>> public int x() {
>> return Math.abs(x);
>> }
>> }
>> }

>> The use of @Override on the accessor method int x() ensures that if the record
>> component x is modified or removed, then the corresponding accessor method must
>> be
>> modified or removed too.
>> ...

>> So it is OK to annotate a record component with the @Override annotation without
>> the need for a superinterface defining an override equivalent method to exist,

>> Thanks,
>> Vicente

>> On 7/19/21 12:49 PM, Remi Forax wrote:

>>> It does not seems that @Override correctly compiles on a record but the
>>> "override" check is not done.

>>> By example,
>>>   interface Named {
>>>     String name();
>>>   }
>>>   record Foo(@Override String name) implements Named { }

>>> actually compiles but
>>>   record Foo(String name, @Override String bar) implements Named { }

>>> also compiles ??

>>> It seems that the logic that checks if a method "overrides" another (in the
>>> general sense i.e replace another method) is not implemented if @Override is
>>> declared on a record component.

>>> regards,
>>> Rémi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20210720/9bd34b98/attachment-0001.htm>


More information about the compiler-dev mailing list