vendredi 8 février 2019

Why, in generics, does T.getClass() return a Class> and not a Class

This often leaves us having to do 'unchecked' casts back to Class<T> when using Guava's TypeToken:

public <T> T doSomething(T dest) {
    @SuppressWarnings("unchecked")
    final Class<T> destClass = (Class<T>) dest.getClass();

    final TypeToken<T> destType = TypeToken.of(destClass);

    ...

    return dest;
}

Why was this choice made? Is this due to type erasure or something?





Aucun commentaire:

Enregistrer un commentaire