Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8222240

Annotation processors have no access to CLASS_TYPE_PARAMETER_BOUND annotations

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      Given an annotation processor processing an ElementKind.CLASS. The class being processed has an annotation on a type parameter bound. The processor tries to get all type parameter annotations via calling getAnnotationMirrors() on each element returned by TypeElement#getTypeParameters().getTypeParameters(), however, CLASS_TYPE_PARAMETER_BOUND annotations are not returned.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Write a trivial annotation processor and corresponding trivial annotation. The annotation processor need only output the number of entries returned by getTypeParameters(). Create another annotation that has a target of (at least) ElementType.TYPE_USE and use it to annotate a class that has type bound parameter.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Calling getAnnotationMirrors() on each element returned by getTypeParameters() should return the type bound parameter annotation mirror
      ACTUAL -
      They are not returned

      ---------- BEGIN SOURCE ----------
      // a trivial annotation processor
      @SupportedAnnotationTypes("name here")
      @SupportedSourceVersion(SourceVersion.RELEASE_8)
      public class TestProcessor extends AbstractProcessor {
          @Override
          public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment environment) {
              annotations.forEach(annotation -> {
                  Set<? extends Element> elementsAnnotatedWith = environment.getElementsAnnotatedWith(annotation);
                  elementsAnnotatedWith.forEach(element -> {
                      if (element.getKind() == ElementKind.CLASS) {
                          TypeElement typeElement = (TypeElement) element;
                          List<? extends TypeParameterElement> typeParameters = typeElement.getTypeParameters();
                          typeParameters.forEach(p -> processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Found: " + p.getAnnotationMirrors().size()));
                      }
                  });
              });
              return true;
          }
      }

      // the annotation to use for processing
      @Retention(RetentionPolicy.SOURCE)
      @Target(ElementType.TYPE)
      public @interface TestProcessorAnnotation {
      }

      // the annotation to use on the class
      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.TYPE_USE)
      public @interface TestAnnotation {
      }

      // the class
      @TestProcessorAnnotation
      public class Test<T extends @TestAnnotation List> {
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: