Question regarding type use annotation on method parameter type

Liam Miller-Cushon cushon at
Thu Feb 16 03:57:16 UTC 2023

Should `sAsType.getAnnotations()` be `sAsType.getAnnotationMirrors()`?

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:

$ javac -processor P -implicit:none  -sourcepath : -parameters
Note: B.c(java.lang. at A String).s has annotations [@A]
Note: B.c(java.lang. at A String).s has annotations [@A]
$ javac -processor P -implicit:none  -sourcepath : -parameters
Note: B.c(java.lang.String).s has annotations []
Note: B.c(java.lang.String).s has annotations []

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

public @interface A {}
=== ===
public final class B {
  public static final void c(@A String s) {}
=== ===
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;

public class P extends AbstractProcessor {
  public SourceVersion getSupportedSourceVersion() {
    return SourceVersion.latestSupported();

  public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
    Elements elements = processingEnv.getElementUtils();
    Element e = elements.getTypeElement("B");
    ExecutableElement c = (ExecutableElement)
    VariableElement s = c.getParameters().get(0);
    TypeMirror sAsType = s.asType();
                "%s.%s.%s has annotations [%s]", e, c, s,
    return false;

On Wed, Feb 15, 2023 at 6:00 PM Laird Nelson <ljnelson at> wrote:

> On Wed, Feb 15, 2023 at 5:07 PM Alex Buckley <alex.buckley at>
> wrote:
>> Via ExecutableElement::getParameters, the VariableElement which
>> represents the declaration of formal parameter `s` should expose a
>> TypeMirror for the type `@A String`.
> Thank you. That is exactly what I expected. It does not. Should I file a
> bug?
> Test code excerpted below:
> Element e = elements.getTypeElement("B");
> ExecutableElement c = (ExecutableElement)e.getEnclosedElements().get(1);
> // 0 is the implicit constructor; 1 is the c method
> assert "c".equals(c.getSimpleName().toString()); // yep, got the right
> method
> VariableElement s = (VariableElement)c.getParameters().get(0);
> assert "s".equals(s.getSimpleName().toString()); // yep, got the right
> parameter
> DeclaredType sAsType = (DeclaredType)s.asType();
> assert !sAsType.getAnnotations().isEmpty(); // fails; it is empty
> Thanks,
> Laird
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the compiler-dev mailing list