I know this was asked a long time ago, but I also tried to find the answer. I realized that AutoBeans, since they are basically just fancy wrappers for JSON, still contain all the data for the fields of the child object for which you want to disable it. So I wrote this method:
public <A, B> B cast( A sourceObject, Class<B> targetClass ) { AutoBean<A> sourceBean = AutoBeanUtils.getAutoBean( sourceObject );
- Where typeFactory extends AutoBeanFactory, as you can see.
It worked well enough for me. The hardest bit was passed to HasSplittable, since AutoBean does not extend this interface, but AbstractAutoBean (which implements AutoBean) does, and a subclass of this is what is returned by getAutoBean () calls.
You also need to copy Splittable, otherwise AutoBeanCodex will think: "Hey, I already have AutoBean for this Splittable! That's it!" - and just gives you the original .;)
In any case, you can throw it down, up ... sideways !: P
Later editing: Having stumbled upon this a few months later, I thought I'd add a little warning about something that Jonathan mentioned below. The method described here is intended for use in AutoBean, which has not been modified since it was deserialized. This is because (AFAIK) there is no guarantee that any setters you call will actually update JSON (necessary for casting). This is probably not a big deal, as you will usually use this when you have an inbound DTO and want to convert it to a real ASAP type before doing anything else with it. In our case, none of our AutoBeans even had setters, so this was not a problem.;)
After you deposit it, you can do whatever you want with the result of the bean, which is fresh from the factory in the end!