dev@jaxb.java.net

Re: Parent Pointer XJC Plugin

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Thu, 25 Jan 2007 08:40:54 +0100

Hi.

> I'm working on an xjc plugin that would automatically generate parent
> pointers similar to the example (
> https://jaxb.dev.java.net/guide/Mapping_cyclic_references_to_XML.html
> ) in the jaxb guide. Obviously, since I'm trying to do it in xjc, I'm
> not trying to avoid cycles, but rather just hold on to a reference to
> the parent in order to make traversing the object tree a little
> easier.

You may take a look at te "tree" add-on from jaxbcommons (JAXB1) it
allowed representing the wole JAXB structure as a generic tree. I use
this to traverse structures of schema-derived classes in a generic way
(for instance, to find objects of a certain type etc.).

> I currently have a pretty simple little plugin that uses a field of
> type Object and just assigns the parent parameter of afterUnmarshal()
> to it. (By the way, doesn't there need to be a cast in the example?)
> This works pretty well, but relies on a lot of instanceof and casting.
>
> Ideally, I would like something a little more type-safe. In a lot of
> cases, there will only be one type for the parent so something like
> "private Department parent;" would work. In the cases with more than
> one type of parent, maybe have multiple fields and the ones that
> aren't used just stay null?

Ehm, this should be your design decision.

> Anyway, the question at hand is how would I figure out what the
> possible parent types are from a plugin? The whole xjc
> model/codemodel thing isn't hugely documented and I can't figure it
> out.

Figure out all children and revert the mapping. ;)

Just analyze the ClassOuline/FieldOutline relationsip to figure out
which field types have certain class types. Ater that you'll have all
possible parent/child relationships. Then simply revert the map to see
which parent types are possible for the given child type.

Bye.
/lexi