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

javax.lang.model.element.Element.getEnclosingElement() doesn't return null for type parameter

XMLWordPrintable

        Please see the following code sample.
        If parametrized class below

        class MyClass<T> { }

        is given to annotation processor that checks spec assertion:

        @SupportedAnnotationTypes("*")
        @SupportedSourceVersion(SourceVersion.RELEASE_6)
        public class MyProcessor extends AbstractProcessor {

            public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
                Set<? extends Element> rootElements = roundEnv.getRootElements();
                if (rootElements.size() > 0) {
                    performCheck(rootElements);
                }
                return true;
            }

            private void performCheck(Set<? extends Element> rootElements) {
                TypeElement typeElement = ElementFilter.typesIn(rootElements).iterator().next();
                TypeParameterElement typeParameterElement = typeElement.getTypeParameters().get(0);
                // Element getEnclosingElement():
                // If this is a type parameter, {@code null} is returned.
                Element enclosingElement = typeParameterElement.getEnclosingElement();
                assert enclosingElement == null :
                        "As spec for getEnclosingElement() says, enclosing element must be null for type parameter";
            }
        }

        We get:
        java.lang.AssertionError: As spec for getEnclosingElement() says, enclosing element must be null for type parameter

        Please don't forget to give -ea option to VM to enable asserts.

              darcy Joe Darcy
              dbessono Dmitry Bessonov
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: