mercredi 18 décembre 2019

TypeMirror isSubtype and isAssignable for ImmutableSet and java.lang.Iterable

I can't understand the difference between subtype and assignable.

Given parameters is an ImmutableSet type, I have following code to check if an ImmutableSet is a subtype or assignable:

      boolean isAssignable =
          types.isAssignable(
              types.erasure(parameters.asType()),
              elements.getTypeElement(Iterable.class.getCanonicalName()).asType());

      boolean isSubtype =
          types.isSubtype(
              types.erasure(parameters.asType()),
              elements.getTypeElement(Iterable.class.getCanonicalName()).asType());

      processingEnv.getMessager().printMessage(Kind.NOTE, "Type " + types.erasure(parameters.asType())
          + " is " + (isSubtype ? "" : " not ") + " a subtype of " + Iterable.class.getCanonicalName());

      processingEnv.getMessager().printMessage(Kind.NOTE, "Type " + types.erasure(parameters.asType())
          + " is " + (isAssignable ? "" : " not ") + " a assignable of " + Iterable.class.getCanonicalName());

And the output is as the following:

Type com.google.common.collect.ImmutableSet is not a subtype of java.lang.Iterable
Type com.google.common.collect.ImmutableSet is a assignable of java.lang.Iterable

I can understand why ImmutableSet is assignable to Iterable interface, this is very clear. However, I can't understand why ImmutableSet is not a subtype of Iterable.

I checked other similar questions but none of them has explained this. Can anyone help?





Aucun commentaire:

Enregistrer un commentaire