I have an abstract class:
public abstract class RootProcessor<T> {
Class<T> clazz;
}
I need to fill ClassT clazz; with the children of RootProcessor - every child has its own T
I found only one solution, but it needs compiler argument -Xlint:unchecked
public RootProcessor(){
this.clazz = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
Is this the best solution? Can we do the same without -Xlint:unchecked ?
-Xlint:unchecked, use@SuppressWarningsas locally as possible if you need unsafe casts.Class<T>safely at runtime if the compiler can't track the type ofTfrom beginning to end. The typesafe but boilerplatey approach here is to simply introduce a protected constructor that takesclazzas a parameter.key = gson.fromJson(json, key.getClass().getGenericSuperclass());fixeskey = gson.fromJson(json,HashMap.class);