I have class name in string variable model, so I want to cast an object to type of that loaded class ba reflection
Class name what I’am loading is “Notification” it is written in String variable model
String model = "Notification";
Class<?> cls = Class.forName("biznea.zredzic.pojo."+model);
So I have abstract class PojoAbstract which extends all pojo classes as it is cls or which really is Notification.class
public class Notification extends PojoAbstract{...}
And now I want to cast this to cls real class type (Notification)
WHAT_TO_PUT_HERE po = (WHAT_TO_PUT_HERE) gson.fromJson(json.toString(), cls);
I do not want to po be a PojoAbstract type I want it to be Notification type
I hope that I was clear :S
You should use the method
public T cast(Object obj)ofClass<T>.But you can’t assign directly to a static declaration since at compile time you won’t know the type. You must keep the declaration as much non-specific as you can, since you can’t assume anything at compile time.