Type-variable parameter with type annotation.

Alex Buckley alex.buckley at oracle.com
Mon Jun 17 18:38:40 UTC 2019


On 6/17/2019 10:54 AM, Antonio Cortes Perez wrote:
> I am not able to retrieve the annotation from the parameter param or its
> type in the following program when using the javac API (JDK 1.8.0_212).
>
> === InputFile.java ===
> import java.lang.annotation.ElementType;
> import java.lang.annotation.Target;
>
> @Target({ ElementType.TYPE_USE, ElementType.TYPE_PARAMETER })
> @interface TypeAnnotation {
> }
>
> public class InputFile<T> {
>    void test(@TypeAnnotation T param) {}
> }
> === end ===

This test case could be simplified by losing TYPE_PARAMETER (a 
declaration-site construct) from the @Target.

There is one type parameter declared in the above code -- the T in 
between < > after InputFile -- and that declaration has no annotations.

> The program does not print anything when using JDK 1.8.
>
> Using JDK 9 produces the expected output.

Did you search https://bugs.openjdk.java.net/ for bugs in the 'tools' 
component with fixversion=9 ?

> I also inspected the element, but I did not find the annotation.
>        Element element = trees.getElement(getCurrentPath());
>        Set<Modifier> modifiers = element.getModifiers();
>        List<? extends AnnotationMirror> annotations =
>   element.getAnnotationMirrors();
>
> I noticed that the list metadata.type_attributes in the underlying
> VarSymbol contains the annotation. But I did not find a way to retrieve
> it via the public API. Any tips are appreciated.

You're confusing TYPE_PARAMETER and PARAMETER.

Alex


More information about the compiler-dev mailing list