<div dir="ltr"><div>Should `<span style="color:rgb(0,0,0)">sAsType.getAnnotations()` be `</span>sAsType.getAnnotationMirrors()`?</div><div><br></div><div>It works for me if B is being compiled from source as part of the same compilation doing the annotation processing, but not if B is loaded from the classpath, which is consistent with this known issue: <a href="https://bugs.openjdk.org/browse/JDK-8225377" target="_blank">https://bugs.openjdk.org/browse/JDK-8225377</a></div><br>$ javac -processor P -implicit:none  -sourcepath : -parameters A.java B.java<br>Note: B.c(java.lang.@A String).s has annotations [@A]<br>Note: B.c(java.lang.@A String).s has annotations [@A]<br>$ javac -processor P -implicit:none  -sourcepath : -parameters A.java<br>Note: B.c(java.lang.String).s has annotations []<br>Note: B.c(java.lang.String).s has annotations []<br><br>=== A.java ===<br>import java.lang.annotation.ElementType;<br>import java.lang.annotation.Retention;<br>import java.lang.annotation.RetentionPolicy;<br>import java.lang.annotation.Target;<br><br>@Retention(RetentionPolicy.RUNTIME)<br>@Target({ElementType.TYPE_USE})<br>public @interface A {}<br>=== B.java ===<br>public final class B {<br>  public static final void c(@A String s) {}<br>}<br>=== P.java ===<br>import java.util.Set;<br>import javax.annotation.processing.AbstractProcessor;<br>import javax.annotation.processing.RoundEnvironment;<br>import javax.annotation.processing.SupportedAnnotationTypes;<br>import javax.lang.model.SourceVersion;<br>import javax.lang.model.element.Element;<br>import javax.lang.model.element.ExecutableElement;<br>import javax.lang.model.element.TypeElement;<br>import javax.lang.model.element.VariableElement;<br>import javax.lang.model.type.TypeMirror;<br>import javax.lang.model.util.Elements;<br>import javax.tools.Diagnostic;<br><br>@SupportedAnnotationTypes("*")<br>public class P extends AbstractProcessor {<br>  @Override<br>  public SourceVersion getSupportedSourceVersion() {<br>    return SourceVersion.latestSupported();<br>  }<br><br>  @Override<br>  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {<br>    Elements elements = processingEnv.getElementUtils();<br>    Element e = elements.getTypeElement("B");<br>    ExecutableElement c = (ExecutableElement) e.getEnclosedElements().get(1);<br>    VariableElement s = c.getParameters().get(0);<br>    TypeMirror sAsType = s.asType();<br>    processingEnv<br>        .getMessager()<br>        .printMessage(<br>            Diagnostic.Kind.NOTE,<br>            String.format(<br>                "%s.%s.%s has annotations [%s]", e, c, s, sAsType.getAnnotationMirrors()));<br>    return false;<br>  }<br>}<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 15, 2023 at 6:00 PM Laird Nelson <<a href="mailto:ljnelson@gmail.com" target="_blank">ljnelson@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr">On Wed, Feb 15, 2023 at 5:07 PM Alex Buckley <<a href="mailto:alex.buckley@oracle.com" target="_blank">alex.buckley@oracle.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Via ExecutableElement::getParameters, the VariableElement which <br>
represents the declaration of formal parameter `s` should expose a <br>
TypeMirror for the type `@A String`.<br></blockquote><div><br></div><div>Thank you. That is exactly what I expected. It does not. Should I file a bug?</div><div><br></div><div>Test code excerpted below:</div><div><br></div><div>Element e = elements.getTypeElement("B");</div><div>ExecutableElement c = (ExecutableElement)e.getEnclosedElements().get(1); // 0 is the implicit constructor; 1 is the c method<br>assert "c".equals(c.getSimpleName().toString()); // yep, got the right method</div><div>VariableElement s = (VariableElement)c.getParameters().get(0);<br>assert "s".equals(s.getSimpleName().toString()); // yep, got the right parameter</div><div>DeclaredType sAsType = (DeclaredType)s.asType();</div><div>assert !sAsType.getAnnotations().isEmpty(); // fails; it is empty</div><div><br></div><div>Thanks,</div><div>Laird</div></div></div>
</blockquote></div>