Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] MOXy/JAXB question regarding package-info

Hi Laird,

Below I've attempted to address the main points from your email.  Please let me know if I missed the mark.

"My larger context is that I'd like to simply have a package somewhere on the classpath--call it com.foobar.adapters--that has a package-info.java on it that "hosts" all known XmlJavaTypeAdapters for my enterprise application.  Some of these govern types that would obviously not be part of the com.foobar.adapters package.  I haven't yet tried this but I would think this might pose a problem."

When you specify @XmlJavaTypeAdapters at the package level, the adapters only apply to properties of those that type that belong to the domain classes that exist within that package.  This means that you won't be able to have on package that contains all the XmlAdapters that you will every need that can be leveraged by domain models in different packages.  I have added a detailed example as an answer to a similar question that you posed on Stack Overflow:
"More precisely: Given an @XmlJavaTypeAdapter annotation found (somehow) on a package-info class (inside a wrapping @XmlJavaTypeAdapters annotation) with a correctly-specified type() attribute, is there any requirement that the type so specified in the annotation belong to the same package that is being annotated?"

The package-info class is the standard Java SE mechanism for providing package level annotations.  So these annotations are picked up as the annotations for the domain classes are processed.  As I stated above there is no requirement that the type specified belong to the same package that is being annotated.  Normally this mechanism is introduced to handle third party classes:
If you have a domain class that you always want to be handled by an XmlAdapter you are better of specifying the XmlAdapter at the type level.  Below is an example where this is used to handle an immutable class:
-Blaise
 
PS:  I've been following your tweets about the work your doing with generating XmlAdapters to support interfaces.  I'm very curious to try it out.  Let us know if we can be of any help.


On 04/01/2012 6:00 PM, Laird Nelson wrote:
@XmlJavaTypeAdapter annotations can be used to specify XmlAdapter instances that can be used to map non-bindable classes to bindable JAXB classes.  What's even more convenient is that these annotations can be applied at the package level by placing them on the package-info.java file.  My question concerns: which package-info.java file does MOXy/JAXB consult, and how does it know which package-info.class files to hunt through for such annotations?

More precisely: Given an @XmlJavaTypeAdapter annotation found (somehow) on a package-info class (inside a wrapping @XmlJavaTypeAdapters annotation) with a correctly-specified type() attribute, is there any requirement that the type so specified in the annotation belong to the same package that is being annotated?

In other words, is this valid:

// package-info.java for com.whizbang.whatever:

@XmlJavaTypeAdapters({
  @XmlJavaTypeAdapter(type = com.foobar.bozo.Type.class,
                      value = com.fizbaw.adapters.SomeXmlAdapterSubclass.class)
})

package com.whizbang.whatever; // look; it's not com.foobar.bozo

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;

// end package-info.java

...?

Note that the package being annotated is com.whizbang.whatever, the package of the class specified as the @XmlJavaTypeAdapter's type() attribute is com.foobar.bozo, and the adapter class package (probably irrelevantly) is com.fizbaw.adapters.

I'm curious how JAXB implementations like MOXy go about locating this package-info to find the potential list of adapted classes.  I could see two conceptual approaches:
  1. Some sort of scanning for package-info.class resources.  I'm guessing this is not what happens?
  2. "Backing into" the package: JAXB encounters an object that needs to be marshaled, discovers it's not in the JAXBContext of the moment, and so investigates its package (Class#getPackage()) to see if that package has any helpful adapter annotations on it.  In this case, it would be an effective requirement that an @XmlJavaTypeAdapter annotation applied to a package-info.java file would need to specify a Class for its type() attribute that belonged to the same package as the one being annotated.
My larger context is that I'd like to simply have a package somewhere on the classpath--call it com.foobar.adapters--that has a package-info.java on it that "hosts" all known XmlJavaTypeAdapters for my enterprise application.  Some of these govern types that would obviously not be part of the com.foobar.adapters package.  I haven't yet tried this but I would think this might pose a problem.

This is obviously quite hard to articulate; I hope I've been clear in my questions.  Thanks for any help!

Best,
Laird

--
http://about.me/lairdnelson



_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Back to the top