Question regarding type use annotation on method parameter type

Vicente Romero vicente.romero at oracle.com
Thu Feb 16 12:04:46 UTC 2023


Hi,

I have added the test case below to [1], will do some research on it,

Thanks,
Vicente

[1] https://bugs.openjdk.org/browse/JDK-8225377

On 2/15/23 22:57, Liam Miller-Cushon wrote:
> 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: 
> https://bugs.openjdk.org/browse/JDK-8225377
>
> $ javac -processor P -implicit:none  -sourcepath : -parameters A.java 
> B.java
> 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 A.java
> Note: B.c(java.lang.String).s has annotations []
> Note: B.c(java.lang.String).s has annotations []
>
> === A.java ===
> import java.lang.annotation.ElementType;
> import java.lang.annotation.Retention;
> import java.lang.annotation.RetentionPolicy;
> import java.lang.annotation.Target;
>
> @Retention(RetentionPolicy.RUNTIME)
> @Target({ElementType.TYPE_USE})
> public @interface A {}
> === B.java ===
> public final class B {
>   public static final void c(@A String s) {}
> }
> === P.java ===
> 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;
> import javax.tools.Diagnostic;
>
> @SupportedAnnotationTypes("*")
> public class P extends AbstractProcessor {
>   @Override
>   public SourceVersion getSupportedSourceVersion() {
>     return SourceVersion.latestSupported();
>   }
>
>   @Override
>   public boolean process(Set<? extends TypeElement> annotations, 
> RoundEnvironment roundEnv) {
>     Elements elements = processingEnv.getElementUtils();
>     Element e = elements.getTypeElement("B");
>     ExecutableElement c = (ExecutableElement) 
> e.getEnclosedElements().get(1);
>     VariableElement s = c.getParameters().get(0);
>     TypeMirror sAsType = s.asType();
>     processingEnv
>         .getMessager()
>         .printMessage(
>             Diagnostic.Kind.NOTE,
>             String.format(
>                 "%s.%s.%s has annotations [%s]", e, c, s, 
> sAsType.getAnnotationMirrors()));
>     return false;
>   }
> }
>
> On Wed, Feb 15, 2023 at 6:00 PM Laird Nelson <ljnelson at gmail.com> wrote:
>
>     On Wed, Feb 15, 2023 at 5:07 PM Alex Buckley
>     <alex.buckley at oracle.com> 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: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20230216/51ccfec4/attachment-0001.htm>


More information about the compiler-dev mailing list