title: JAXB XML Adapters work via annotations but not via setAdapter
body: <p>I understand all about how to use <a href="http://download.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html" rel="noreferrer">XMLAdapters</a> to <a href="http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html" rel="noreferrer">convert unmappable types</a>, or just to change how certain objects are serialized/deserialized to XML.  Everything works great if I use annotations (either package level or otherwise).  The problem is that I am attempting to change the representations of third party objects which I cannot change the source code to (i.e. in order to inject annotations).</p>
  
  <p>That shouldn't be a problem, considering that the Marshaller object has a method for <a href="http://download.oracle.com/javase/6/docs/api/javax/xml/bind/Marshaller.html#setAdapter%28javax.xml.bind.annotation.adapters.XmlAdapter%29" rel="noreferrer">manually adding adapters</a>. Unfortunately, no matter what I do, I can't get the adapters set in this way to "kick in".  For instance, I have a class representing a point in XYZ space (geocentric coordinates).  In the XML I produce, I want this to be converted into lat/long/altitude (geodetic coordinates).  Here are my classes:</p>
  
  <p><strong>Geocentric</strong></p>
  
  <pre><code>package testJaxb;
  
  import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement
  public class GeocentricCoordinate {
      // Units are in meters; see http://en.wikipedia.org/wiki/Geocentric_coordinates
      private double x;
      private double y;
      private double z;
  
      @XmlAttribute
      public double getX() {
          return x;
      }
      public void setX(double x) {
          this.x = x;
      }
      @XmlAttribute
      public double getY() {
          return y;
      }
      public void setY(double y) {
          this.y = y;
      }
      @XmlAttribute
      public double getZ() {
          return z;
      }
      public void setZ(double z) {
          this.z = z;
      }
  }
  </code></pre>
  
  <p><strong>Geodetic</strong></p>
  
  <pre><code>package testJaxb;
  import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlRootElement;
  /**
   * @see http://en.wikipedia.org/wiki/Geodetic_system
   */
  @XmlRootElement
  public class GeodeticCoordinate {
  
      private double latitude;
      private double longitude;
      // Meters
      private double altitude;
  
      public GeodeticCoordinate() {
          this(0,0,0);
      }
  
      public GeodeticCoordinate(double latitude, double longitude, double altitude) {
          super();
          this.latitude = latitude;
          this.longitude = longitude;
          this.altitude = altitude;
      }
  
      @XmlAttribute
      public double getLatitude() {
          return latitude;
      }
      public void setLatitude(double latitude) {
          this.latitude = latitude;
      }
  
      @XmlAttribute
      public double getLongitude() {
          return longitude;
      }
  
      public void setLongitude(double longitude) {
          this.longitude = longitude;
      }
  
      @XmlAttribute
      public double getAltitude() {
          return altitude;
      }
      public void setAltitude(double altitude) {
          this.altitude = altitude;
      }
  
  
  
  }
  </code></pre>
  
  <p><strong>GeocentricToGeodeticLocationAdapter</strong></p>
  
  <pre><code>package testJaxb;
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.Marshaller;
  import javax.xml.bind.annotation.adapters.XmlAdapter;
  
  
  /**
   * One of our systems uses xyz coordinates to represent locations. Consumers of our XML would much
   * prefer lat/lon/altitude.  This handles converting between xyz and lat lon alt.  
   * 
   * @author ndunn
   *
   */
  public class GeocentricToGeodeticLocationAdapter extends XmlAdapter&lt;GeodeticCoordinate,GeocentricCoordinate&gt; {
  
      @Override
      public GeodeticCoordinate marshal(GeocentricCoordinate arg0) throws Exception {
          // TODO: do a real coordinate transformation
          GeodeticCoordinate coordinate = new GeodeticCoordinate();
          coordinate.setLatitude(45);
          coordinate.setLongitude(45);
          coordinate.setAltitude(1000);
          return coordinate;
      }
  
      @Override
      public GeocentricCoordinate unmarshal(GeodeticCoordinate arg0) throws Exception {
          // TODO do a real coordinate transformation
          GeocentricCoordinate gcc = new GeocentricCoordinate();
          gcc.setX(100);
          gcc.setY(200);
          gcc.setZ(300);
          return gcc;
      }
  }
  </code></pre>
  
  <p><strong>ObjectWithLocation field</strong></p>
  
  <pre><code>package testJaxb; 
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.Marshaller;
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement
  public class ObjectWithLocation {
  
      private GeocentricCoordinate location = new GeocentricCoordinate();
  
      public GeocentricCoordinate getLocation() {
          return location;
      }
  
      public void setLocation(GeocentricCoordinate location) {
          this.location = location;
      }
  
  
      public static void main(String[] args) {
  
          ObjectWithLocation object = new ObjectWithLocation();
  
          try {
              JAXBContext context = JAXBContext.newInstance(ObjectWithLocation.class, GeodeticCoordinate.class, GeocentricCoordinate.class);
              Marshaller marshaller = context.createMarshaller();
  
              marshaller.setAdapter(new GeocentricToGeodeticLocationAdapter());
              marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  
              marshaller.marshal(object, System.out);
  
          }
          catch (JAXBException jaxb) {
              jaxb.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;objectWithLocation&gt;
      &lt;location z="0.0" y="0.0" x="0.0"/&gt;
  &lt;/objectWithLocation&gt;
  </code></pre>
  
  <p>By using an annotation (in my <code>package-info.java</code> file):</p>
  
  <pre><code>@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters
  ({
  @javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value=GeocentricToGeodeticLocationAdapter.class,type=GeocentricCoordinate.class),
  })
  
  package package testJaxb;
  </code></pre>
  
  <p>I get the following (desired) xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;objectWithLocation&gt;
      &lt;location longitude="45.0" latitude="45.0" altitude="1000.0"/&gt;
  &lt;/objectWithLocation&gt;
  </code></pre>
  
  <p>So my question is twofold.</p>
  
  <ol>
  <li>Why does the adapter work when annotated, but not when explicitly set via the <code>setAdapter</code> method?</li>
  <li>How do I work around this problem when I have classes that I cannot annotate and whose package-info.java I cannot modify in order to add the annotations?</li>
  </ol>