I have different kinds of serializable ObjectState‘s that store state data for important application objects. I do this so I can persist these important objects arcoss application instatances. The user will sometimes need to group certain objects together into containers, so some of the ObjectState‘s are placed into an array of ObjectState‘s and passed to a ContainerState (just a serializable class that holds an array of generic types serializables).
My question is, how do I de-serialize the ContianerState and keep type integrity? I thought I could try something like:
public static ContainerState<V> inflateState(V v, Serializable state) throws ClassCastException {
return (ContainerState<V>)state;
}
But that won’t even get past Eclipses’ warning system. I also tried V.class to the same result. Is it even possible for me to achieve this?
EDIT 1:
I tried this, it seems to work, but I’m not sure if it’s the best way. Any thoughts?
public static <cls extends Serializable> ContainerState<cls> inflateState(Class<? extends Serializable> cls, Serializable state) throws ClassCastException {
return (ContainerState<cls>)state;
}
This should at least compile: