Can a @Target-less (runtime-retained) annotation be applied on a type parameter declaration?
Alex Buckley
alex.buckley at oracle.com
Wed Mar 8 18:00:14 UTC 2023
Thanks Laird and Liam.
In Java 19, this is a javac bug, because JLS19 is clear that a
@Target-less annotation can be applied to a type parameter declaration:
"If an annotation of type java.lang.annotation.Target is not present on
the declaration of an annotation interface A, then A is applicable in
all declaration contexts and in no type contexts."
-
https://docs.oracle.com/javase/specs/jls/se19/html/jls-9.html#jls-9.6.4.1-400
As Liam implies, there has been some shimmer in this rule. When type
annotations were introduced in Java 8, an @Target-less annotation could
not be applied to a type parameter declaration, but this changed in Java
14. Compare JLS13 with JLS14:
-
https://docs.oracle.com/javase/specs/jls/se13/html/jls-9.html#jls-9.6.4.1-400
-
https://docs.oracle.com/javase/specs/jls/se14/html/jls-9.html#jls-9.6.4.1-400
Alex
On 3/7/2023 4:06 PM, Liam Miller-Cushon wrote:
> Hi Laird,
>
> Nice find. I'm curious whether anyone on the list is remembering details
> I'm not, but this looks like a bug to me too.
>
> I went ahead and filed: https://bugs.openjdk.org/browse/JDK-8303784
> <https://bugs.openjdk.org/browse/JDK-8303784>
>
> On Tue, Mar 7, 2023 at 3:53 PM Laird Nelson <ljnelson at gmail.com
> <mailto:ljnelson at gmail.com>> wrote:
>
> Using Java 19:
>
> @Retention(RetentionPolicy.RUNTIME)
> public @interface A {}
>
> public class B<@A C> {}
>
> Why does javac say that A is "not applicable in this type context"?
> (I understand that things have changed here in various versions of
> the JLS and related @Target javadocs over the years so I'm probably
> misreading something.)
>
> (Originally asked on StackOverflow with more detail:
> https://stackoverflow.com/questions/75667924/a-target-less-annotation-cannot-be-applied-to-a-type-parameter-declaration-in-j <https://stackoverflow.com/questions/75667924/a-target-less-annotation-cannot-be-applied-to-a-type-parameter-declaration-in-j>
>
> Thanks,
> Laird
>
More information about the compiler-dev
mailing list