2

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)
jit
  • 452
  • 1
  • 7
  • 26
  • 1
    Why would you need a cast? The above code, not using reflection, doesn't make any cast. So the same code using reflection shouldn't do any cast either. That said, that's really not a good reason to use reflection. – JB Nizet Feb 04 '15 at 14:55

2 Answers2

3

You can use Apache Commons BeanUtils to do this. Using its BeanUtils class you have access to a lot of utility methods for populating JavaBeans properties via reflection.

To copy all the common/inherited properties from a base class object to a derived class object you can use its static copyProperties() method as

BeanUtils.copyProperties(baseClassObj, derivedClassObj);

From the BeanUtils.copyProperties() docs

Copy property values from the origin bean to the destination bean for all cases where the property names are the same.

If you don't want to use a third-party library, your next best option would be to provide a utility method on the derived class that initializes itself with all the properties of an instance of its parent class.

public void initFromParent(BaseClass obj) {
    this.propOne = obj.getPropOne();
    // ... and so on
}
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • _You definitely answered his question_, but so that everyone coming later understands, building an object via a bean piecemeal should be a last-resort kind of thing -- using a constructor or composition would be far safer as you could then preserve class contracts/invariants. – Rich Henry Feb 04 '15 at 15:20
  • Yeah, constructor makes sense too but then you're restricting the copy to one time initialization. With a method you can copy the state anytime. It would depend on case to case basis. And I won't suggest composition as OP's already using inheritance and we don't know enough about the program entities to proclaim which one would fit better. – Ravi K Thapliyal Feb 04 '15 at 15:28
  • A single argument method with the object to clone would be superior to copying properties, as the object is in an inconsistent state until you call all setters. The solution to a long copy method is not to use beans+reflection, but rather to break the object down into manageable parts. Again, I understand that this isn't your idea. – Rich Henry Feb 04 '15 at 15:45
  • Yes, if the object is shared then a composition with the passed in parent object simply cloned and set as a member is definitely a better idea. I see where you coming from now. In a multi-threaded environment I would have to synchronize while the properties are being copied. – Ravi K Thapliyal Feb 04 '15 at 18:08
0

You can downcast if the cast is valid:

BaseClass instance = new DerivedClass();
if(DerivedClass.class.isAssignableFrom(instance.getClass()) {
    DerivedClass dc = DerivedClass.class.cast(instance);
}

But normally one would implement a constructor with the same arguments and call super().

Or even better, use composition, where instead of inheritance you have DerivedClass hold an instance of BaseClass and delegate calls for the fields to that object.

Rich Henry
  • 1,837
  • 15
  • 25