Question regarding type use annotation on method parameter type

Alex Buckley alex.buckley at oracle.com
Thu Feb 16 01:07:07 UTC 2023


On 2/15/2023 4:37 PM, Laird Nelson wrote:
> Consider the following contrived example, which compiles (if I've typed 
> it right):
> 
>     @Retention(RetentionPolicy.RUNTIME)
>     @Target({ ElementType.TYPE_USE }) // note: deliberately not PARAMETER
>     public @interface A {}
> 
>     public final class B {
>          public static final void c(@A String s) {}
>     }
> 
> As far as I can tell, a representation of @A does not appear in any list 
> of AnnotationMirrors "reachable" from an ExecutableElement, including 
> its backing ExecutableType and the TypeMirrors representing the parameter 
> type usages and so on, representing B#c(String). Should it?

Via ExecutableElement::getParameters, the VariableElement which 
represents the declaration of formal parameter `s` should expose a 
TypeMirror for the type `@A String`.

That TypeMirror should expose `@A` via getAnnotation, 
getAnnotationMirrors, or getAnnotationsByType -- all of which say "Note 
that any annotations returned by this method are type annotations."

Alex


More information about the compiler-dev mailing list