XmlJavaTypeAdapters lets you list a bunch of XmlJavaTypeAdapter annotations, each of which governs how a non-bindable type is mapped to a bindable type by JAXB.

You can use this annotation at the package level. When you do so, every XmlJavaTypeAdapter annotation needs its type() attribute fully specified.

There does not appear to be a requirement that the package that is being annotated have anything to do with the package of the non-bindable types being adapted. That is convenient and nice.

That, however, leads me to my next question: if there is no relationship between the annotated package and the package of the type being adapted, how does JAXB discover package-level XmlJavaTypeAdapters annotations? How, in other words, does it know which packages to consult for potential XmlJavaTypeAdapters annotations? May I make a random package in, say, a .jar file in my .ear file's lib directory that contains a single, ginormous package-info class that is annotated with all the adapters for all of my non-bindable types?

2 Answers
2

When the JAXB runtime loads a JAXB-annotated class, it looks for a package-info.java in the same package as that class, and checks that to look for package-level annotations. So while XmlJavaTypeAdapters doesn't have to reside in the same package as the "non-bindable" types, it does have to reside in the same package as the "bindable" types.

For example, say I have a JAXB-annotated class A, in package X, which has a property of type B in package Y. In order to bind the B property, let's say a type adapter is required. That adapter can be specified in A itself, or it can be specified in the package-info.java in package X. Package Y is pretty much arbitrary, and is of no interest to the JAXB runtime.

Very helpful; thanks. Does the graph get walked from there? That is, suppose I have a single bindable type that I feed to JAXB. Suppose further that his package is annotated with 647 XmlJavaTypeAdapter annotations, each of which specifies a non-bindable type and an adapter for that type. Would my JAXBContext now be able to handle all 647 now-adapted types?
–
Laird NelsonJan 5 '12 at 0:11

@Laird: Yes, as long as (a) you had the RAM to handle it, and (b) All of the bindable types were in that package.
–
skaffmanJan 5 '12 at 7:50

Interesting. Seems odd that I'd have to bootstrap the whole process, though. I mean, if I can set up a pile of adapters by simply feeding JAXB a single type, then I would hope it should be possible to improve JAXB so that that single type wouldn't be necessary either. Like have JAXB look by default for a "root" package-info somewhere so that the initial bootstrapping isn't necessary. Thanks again for your help.
–
Laird NelsonJan 5 '12 at 13:34

There does not appear to be a requirement that the package that is
being annotated have anything to do with the package of the
non-bindable types being adapted. That is convenient and nice.

This is correct. When @XmlJavaTypeAdapter is used at the package level it means apply this adapter to all properties of the specified type for classes that reside in this package. I'll demonstrate below with an example.

forum8735737.bar.package-info

For this package we will specify an XmlAdapter that will be applied to all fields/properties of type String within this package.