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

Assertion check for TypeVariable.getUpperBound() fails

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 8
    • 6, 8
    • core-libs

    Description

      Documentation for method javax.lang.model.type.TypeVariable.getUpperBound() says that:

        * If it was declared with multiple upper bounds,
        * the result is an intersection type (modeled as a
        * {@link DeclaredType}).
        * Individual bounds can be found by examining the result's
        * {@linkplain Types#directSupertypes(TypeMirror) supertypes}.

      It's not clear enough what the word "examining" means here, because direct supertypes also contain java.lang.Object. I would like to have possibility to compare results of supertypes examination with the results of TypeParameterElement.getBounds() method to check assertion above.

      Please see examples below.

      class MyClass<S extends List & Serializable> {
           void method(S s) { }
      }

      and processor for this source file:

      import javax.annotation.processing.*;
      import javax.lang.model.SourceVersion;
      import javax.lang.model.type.*;
      import javax.lang.model.util.ElementFilter;
      import javax.lang.model.element.*;
      import java.util.*;
      import junit.framework.Assert;

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

          private Set<? extends Element> rootElements;

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

          private void performCheck() {
              TypeElement typeElement = (TypeElement) rootElements.iterator().next();
              ExecutableElement method = ElementFilter.methodsIn(typeElement.getEnclosedElements()).get(0);
              TypeVariable typeVariable = (TypeVariable) method.getParameters().get(0).asType();

              final TypeMirror upperBound = typeVariable.getUpperBound();

              TypeParameterElement typeParameterElement = ((TypeParameterElement) typeVariable.asElement());
              final List<? extends TypeMirror> bounds = typeParameterElement.getBounds();
              final HashSet actual = new HashSet<TypeMirror>(
                      processingEnv.getTypeUtils().directSupertypes(upperBound));
              final HashSet expected = new HashSet<TypeMirror>(bounds);
              Assert.assertEquals("Sets of type mirrors are expected to be equal", expected, actual);
          }

      }

      Output will contain:

      junit.framework.AssertionFailedError: Sets of type mirrors are expected to be equal expected:<[java.util.List, java.io.Serializable]> but was:<[java.lang.Object, java.util.List, java.io.Serializable]>

      Attachments

        Issue Links

          Activity

            People

              emc Eric Mccorkle
              dbessono Dmitry Bessonov
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: