Type annotation on record component doesn't seem to be supported by javac ?

Vicente Romero vicente.romero at oracle.com
Wed Oct 9 15:30:12 UTC 2019


Hi Remi,

This error message can be reproduced without records:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE_USE)
@Retention(RetentionPolicy.RUNTIME)
@interface TypeAnn {}

class TypeAnnosErrorMsg {
     public TypeAnnosErrorMsg (@TypeAnn java.lang.String annotated) {}
}

This seems to be supported by the spec section: 9.7.4 Where Annotations 
May Appear where it says:

A type annotation is admissible if both of the following are true:
• The simple name to which the annotation is closest is classified as a 
TypeName,
not a PackageName.
• If the simple name to which the annotation is closest is followed by " 
. " and
another TypeName - that is, the annotation appears as @Foo T.U - then U 
denotes
an inner class of T .

Of course the user of the record you wrote would be bugged as he wrote 
the type of the record component as: `String` not `java.lang.String`. 
The record constructor is automatically generated by the compiler, and 
for the generation of its arguments the full qualified type was being 
used. I have fixed this in order to use the same type as written by the 
user. If you update your repo your test case should compile,

Thanks,
Vicente

On 10/9/19 4:16 AM, forax at univ-mlv.fr wrote:
> It's a test with sealed + record.
>
> package jdk14;
>
> public sealed interface AllStructures {
>    final class ClassSubType implements AllStructures { }
>    record RecordSubType(@Ann int component1, @TypeAnn String component2) implements AllStructures { }
> }
>
> with:
> package jdk14;
>
> import java.lang.annotation.ElementType;
> import java.lang.annotation.Retention;
> import java.lang.annotation.RetentionPolicy;
> import java.lang.annotation.Target;
>
> @Target(ElementType.RECORD_COMPONENT)
> @Retention(RetentionPolicy.CLASS)
> public @interface Ann {
>
> }
>
> and:
> package jdk14;
>
> import java.lang.annotation.ElementType;
> import java.lang.annotation.Retention;
> import java.lang.annotation.RetentionPolicy;
> import java.lang.annotation.Target;
>
> @Target(ElementType.TYPE_USE)
> @Retention(RetentionPolicy.RUNTIME)
> public @interface TypeAnn {
>
> }
>
> and i'm using the branch "records-and-sealed", so it can be a bad merge ?
>
> cheers,
> Rémi
>
>
> ----- Mail original -----
>> De: "Vicente Romero" <vicente.romero at oracle.com>
>> À: "Remi Forax" <forax at univ-mlv.fr>, "amber-dev" <amber-dev at openjdk.java.net>
>> Envoyé: Mercredi 9 Octobre 2019 06:07:47
>> Objet: Re: Type annotation on record component doesn't seem to be supported by javac ?
>> Hi Remi,
>>
>> Getting that error messages looks weird, what's the full test case?
>>
>> Thanks,
>> Vicente
>>
>> On 10/8/19 6:26 PM, Remi Forax wrote:
>>> Answering to myself,
>>> I use TYPE_PARAMETER instead of TYPE_USE when declaring the annotation
>>> (facepalm)
>>>
>>> but anyway, with TYPE_USE
>>>     @Target(ElementType.TYPE_USE)
>>>     @Retention(RetentionPolicy.RUNTIME)
>>>     public @interface TypeAnn { }
>>>
>>> I get:
>>>     /src/main/java/fr.umlv.record/jdk14/AllStructures.java:9: error: scoping
>>>     construct cannot be annotated with type-use annotation: @jdk14.TypeAnn
>>>       record RecordSubType(@Ann int component1, @TypeAnn String component2) {
>>>
>>> I have no idea what a scoping construct is ?
>>>
>>> Rémi
>>>
>>> ----- Mail original -----
>>>> De: "Remi Forax" <forax at univ-mlv.fr>
>>>> À: "amber-dev" <amber-dev at openjdk.java.net>
>>>> Envoyé: Mardi 8 Octobre 2019 23:54:00
>>>> Objet: Type annotation on record component doesn't seem to be supported by javac
>>>> ?
>>>> Hi all,
>>>> with a type annnotation on a record component defined like this
>>>>    
>>>>    @Target(ElementType.TYPE_PARAMETER)
>>>>    @Retention(RetentionPolicy.RUNTIME)
>>>>    public @interface TypeAnn { }
>>>>
>>>> I get:
>>>>    ./src/main/java/fr.umlv.record/jdk14/AllStructures.java:11: error: annotation
>>>>    type not applicable to this kind of declaration
>>>>      record RecordSubType(@Ann int component1, @TypeAnn String component2) {
>>>>
>>>> Note: i'm using jdk 14-amber+0-df2722d9f9d0, i will test with the current source
>>>> asap.
>>>>
>>>> regards,
>>>> Rémi



More information about the amber-dev mailing list