I have a superclass of type Parent
, it's subclasses retains an arbitrary amount of items let them be known as children
. When I say "arbitrary" I mean they are held in multiple different data structures in the classes that extend Parent
, in Arrays, Maps, and Collections. Currently, the parent's know their children as they maintain the data structures but the children hold no reference to the parents.
We need to create a way for the children to know the reference to their parents. We understand that passing in a reference to the child in the constructor can solve this for all new data. The challenge is existing serialized data. We need a way to give the existing children a reference to their parent.
Right now our solution is to override the readObject(ObjectInputStream in)
method in the Parent
and set a static value that will then be called on by all the Children
who are being read in, in their readObject(ObjectInputStream in)
. The Children
will use the value to set their reference if their reference is null. This is with the understanding that the Parent
class's readObject() invocation call precedes readObject() invocation of its children.
We thought about hard-coding a solution for each class but there are too many subclasses of parent
for that to be efficient.
I thought that maybe using java.reflections
to create a function that would recursively look through an object's fields for objects of a particular type then sets the value of the field in question would be a better solution. I am having trouble when it comes to recursively looking through the fields of generic classes such as Collections
and Maps
. Has anyone implemented or read about something that performs a recursive field value injection? Does anyone have a better solution to solve this?
Thanks for your time.
Aucun commentaire:
Enregistrer un commentaire