guevara
@guevara
Comandante

Как получить информацию о generic типе в Java во время выполнения?

Работаю с библиотекой jackson под Android и столкнулся с непониманием работы с Generic'ами через рефлекцию.

В библиотеке есть костыль TypeReference:

package com.fasterxml.jackson.core.type;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * This generic abstract class is used for obtaining full generics type information
 * by sub-classing; it must be converted to {@link ResolvedType} implementation
 * (implemented by <code>JavaType</code> from "databind" bundle) to be used.
 * Class is based on ideas from
 * <a href="http://gafter.blogspot.com/2006/12/super-type-tokens.html"
 * >http://gafter.blogspot.com/2006/12/super-type-tokens.html</a>,
 * Additional idea (from a suggestion made in comments of the article)
 * is to require bogus implementation of <code>Comparable</code>
 * (any such generic interface would do, as long as it forces a method
 * with generic type to be implemented).
 * to ensure that a Type argument is indeed given.
 *<p>
 * Usage is by sub-classing: here is one way to instantiate reference
 * to generic type <code>List&lt;Integer></code>:
 *<pre>
 *  TypeReference ref = new TypeReference&lt;List&lt;Integer>>() { };
 *</pre>
 * which can be passed to methods that accept TypeReference, or resolved
 * using <code>TypeFactory</code> to obtain {@link ResolvedType}.
 */
public abstract class TypeReference<T> implements Comparable<TypeReference<T>>
{
    protected final Type _type;
    
    protected TypeReference()
    {
        Type superClass = getClass().getGenericSuperclass();
        if (superClass instanceof Class<?>) { // sanity check, should never happen
            throw new IllegalArgumentException("Internal error: TypeReference constructed without actual type information");
        }
        /* 22-Dec-2008, tatu: Not sure if this case is safe -- I suspect
         *   it is possible to make it fail?
         *   But let's deal with specific
         *   case when we know an actual use case, and thereby suitable
         *   workarounds for valid case(s) and/or error to throw
         *   on invalid one(s).
         */
        _type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
    }

    public Type getType() { return _type; }
    
    /**
     * The only reason we define this method (and require implementation
     * of <code>Comparable</code>) is to prevent constructing a
     * reference without type information.
     */
    @Override
    public int compareTo(TypeReference<T> o) { return 0; }
    // just need an implementation, not a good one... hence ^^^
}


Когда я делаю так, все норм:
TypeReference typeReference = new TypeReference<List<Foo>> { };


Так не работает, теряется информация о типе:
class Bar<T> {
      public Bar() {
             TypeReference typeReference = new TypeReference<List<T>> { };
      }
}

Bar<Foo> br = new Bar<Foo>;


Я в курсе про очистку и все такое. Но если посмотреть в отладчике, то внутри TypeReference в первом случае будет такое:
_type = "java.util.List<Foo>"

Во втором:
_type = "T"

Не "Object", а "T".

Кто-нибудь может мне объяснить, что происходит в данном примере?
  • Вопрос задан
  • 1011 просмотров
Решения вопроса 1
targetjump
@targetjump
Type superClass = getClass().getGenericSuperclass();

Читаем доку getGenericSuperclass():
If the superclass is a parameterized type, the {@code Type}
object returned must accurately reflect the actual type
parameters used in the source code.
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
Losted
@Losted
Software Architect
Генерики не существуют после компиляции.

Для интересна можете почитать тут. Возможно как-ниубдь поможет.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы