I'm using beanlib 3.3beta6 with hibernate and having some trouble with
abstract classes. Let's consider following example:
class A {
private B b;
public B getB() { return b; }
public void setB(B b) { this.b = b }
}
abstract class B {}
class B1 extends B {}
class B2 extends B {}
In this example, coping an instance of A that references e.g. B1 throws an InstantiationException.
Reason: BeanReplicator tries to create an instance of class B that is abstract.
After a short debug session I found that the to instantiate class is defined
by the setter method in BeanPopulator:processSetterMethod():
Class<?> paramType = setterMethod.getParameterTypes()[0];
so in the example above the abstract B class.
Is this correct?
If yes, what am I doing wrong? How can I use abstract classes?
Thanks for help.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You can implement the CustomBeanTransformerSpi interface such that
1) the isTransformable method would return true when the toClass is found to be B; and
2) inside the transform method, the proper instance would be instantiated and populated from the "in" object.
To implement (2), you can also take advantage of the "beanTransformer" object passed in to the factory method of creating an instance of CustomBeanTransformSpi.
Good question. I am thinking of making it do so - ie default to instantiate the target object using the class of the source object class instead of the target class if found abstract.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Interestingly I just checked the existing code and the default behavior already handles the abstract class properly. That is, if the from-class is assignable to the to-class, the from-class will be instantiated instead of the to-class.
In other words, it would work even without using CustomBeanTransformerSpi. So if you comment out the initCustomTransformer(...) in the sample code, it would still work.
Or am I missing something here ?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm using beanlib 3.3beta6 with hibernate and having some trouble with
abstract classes. Let's consider following example:
class A {
private B b;
public B getB() { return b; }
public void setB(B b) { this.b = b }
}
abstract class B {}
class B1 extends B {}
class B2 extends B {}
In this example, coping an instance of A that references e.g. B1 throws an InstantiationException.
Reason: BeanReplicator tries to create an instance of class B that is abstract.
After a short debug session I found that the to instantiate class is defined
by the setter method in BeanPopulator:processSetterMethod():
Class<?> paramType = setterMethod.getParameterTypes()[0];
so in the example above the abstract B class.
Is this correct?
If yes, what am I doing wrong? How can I use abstract classes?
Thanks for help.
You can implement the CustomBeanTransformerSpi interface such that
1) the isTransformable method would return true when the toClass is found to be B; and
2) inside the transform method, the proper instance would be instantiated and populated from the "in" object.
To implement (2), you can also take advantage of the "beanTransformer" object passed in to the factory method of creating an instance of CustomBeanTransformSpi.
See here for an example:
http://beanlib.svn.sourceforge.net/viewvc/beanlib/trunk/beanlib-hibernate-test/src/net/sf/beanlib/hibernate3/CustomBeanTransformerSpiTest.java?view=markup
Thanks you for the quick response and the example! It works!
Have one more question, why does BeanLib not handle this by default,
like in the example?
Good question. I am thinking of making it do so - ie default to instantiate the target object using the class of the source object class instead of the target class if found abstract.
Interestingly I just checked the existing code and the default behavior already handles the abstract class properly. That is, if the from-class is assignable to the to-class, the from-class will be instantiated instead of the to-class.
In other words, it would work even without using CustomBeanTransformerSpi. So if you comment out the initCustomTransformer(...) in the sample code, it would still work.
Or am I missing something here ?