samedi 28 février 2015

Java.lang.Class.cast doesn't return a casted object



I am trying to cast an object to its superclass using Java.lang.Class.cast but I get the same object. What can be the reason?


This is the code I'm running:



public static void parse(Object obj)
{
// parse all super classes
Class<?> clazz = obj.getClass().getSuperclass();
if (!clazz.equals(prevClass))
{
prevClass = clazz;
Object castedObj = clazz.cast(obj);
parse(castedObj);
}
fillObject(obj);
}


but when passing to parse an object of dynamic type B, where B extends A, castedObj is equal to obj. But I want castedObj to be a new object of dynamic type A because the parse method relies on that fact (iterates on the fields of the dynamic type class).




Aucun commentaire:

Enregistrer un commentaire