Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1700

TypeUtils.isAssignable returns wrong result for ParameterizedType

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 3.12.0
    • None
    • lang.reflect.*
    • None
    • Important

    Description

      TypeUtils.isAssignable returns a wrong result when checkng wheter a ParameterziedType is assignable to another ParamterizedType.

      Given class definitions as following, for example, 

      class MyNonTransientException extends MyException { ... }
      class MyException extends Exception implements Iterable<Throwable> { ... }
      class LexOrdering<T> extends MyOrdering<Iterable<T>> implements Serializable { ... }
      class MyOrdering<T> implements MyComparator<T> { ... }
      interface MyComparator<T> { ... }

      In this case,

      ParameterizedType from = TypeUtils.parameterize(LexOrdering.class, MyNonTransientException.class);
      ParameterizedType to = TypeUtils.parameterize(MyComparator.class, new WildcardTypeImpl(new Type[]{Object.class}, new Type[]{MyNonTransientException.class}));
      TypeUtils.isAssignable(from, to)

      returns true, but this should be false. First line translates to "MyComparator<Iterable<MyNonTransientException>>" and second one is "MyComparator<? super MyNonTransientException>". First type cannot be assigned to second one (Javac complains it)

       

      It seems that the problem is the method:

      private static Map<TypeVariable<?>, Type> getTypeArguments(final Type type, final Class<?> toClass, final Map<TypeVariable<?>, Type> subtypeVarAssigns)

      which is called in 

      private static boolean isAssignable(final Type type, final ParameterizedType toParameterizedType, final Map<TypeVariable<?>, Type> typeVarAssigns)

      getTypeArguments returns the map "T -> java.lang.iterable<T>, T -> MyNonTransientException, T -> java.lang.Iterable<T>".
      I think this should be something like "T -> java.lang.iterable<MyNonTransientException>, T -> MyNonTransientException, T -> java.lang.Iterable<MyNonTransientException>".

      Attachments

        Activity

          People

            Unassigned Unassigned
            cinemamoon seokhyeon moon
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: