I have three classes in play here:
public class MyClass1<T, ID> {
@Getter
private static class IdHolder<ID> {
private ID id;
}
}
public class MyClass2<T> {
public MyClass2(Class<? extends T> type) {
}
public List<T> fetch() {
}
}
Inside of MyClass1, I need to create an instance of MyClass2 and call the fetch() method:
List<IdHolder<ID>> previous = new MyClass2<IdHolder<ID>>(???).fetch();
If I use raw types like this:
List<IdHolder> previous = new MyClass2<>(IdHolder.class).fetch();
It does work, but then IntelliJ complains about me using raw types (obviously).
I have tried:
TypeReference<IdHolder<ID>> t = new TypeReference<IdHolder<ID>>() {};
What can I put in ??? to get the class type for IdHolder<ID> or is that just not possible due to type erasure and I have to use raw types? I thought that was the whole point of TypeReference.
>Solution :
is that just not possible due to type erasure and I have to use raw types?
That’s correct, it is not possible. You will have to use raw types.