mercredi 4 février 2015

casting base class to derived class via reflection

Hi I have to construct an object from an object. Since the base class has more than 50 fields i dont want to do things like



//obj1 is an instance of BaseClass
DerivedClass obj2 = new DerivedClass();
obj2.setField1(obj1.getField1());
obj2.setField2(obj1.getField2())
.... so on


As you see from the title i want to downcast my object but java does not allow this. Is there a utility library or smth that provides a method like



Object convert(BaseClass obj1, DerivedClass obj2)





Aucun commentaire:

Enregistrer un commentaire