users@jaxb.java.net

XmlAdapter and type casts

From: Mark Hansen <marklists_at_javector.com>
Date: Sat, 24 Mar 2007 11:59:15 -0400

I'm getting an IllegalAnnotationException with JAXB 2.1 that I did not
used to get with JAXB 2.0. Basically, I am trying to use an XmlAdapter
of type <Integer, String> with an *int*. In JAXB 2.0 the conversion
from int to Integer happened automatically - and I assumed that was the
way the JAXB 2.0 spec was written. But, now I'm getting this error at
runtime (not at compiler time). Did the JAXB 2.1 spec and/or
implementation change this behavior? Details below:

Runtime error:
================
     [java] com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1
counts of IllegalAnnotationExceptions
     [java] Adapter samples.IntToStringAdapter is not applicable to the
field type int.
     [java] this problem is related to the following location:
     [java] at
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(type=class
javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter$DEFAULT,
value=class samples.IntToStringAdapter)
     [java] at protected int samples.AddressXML.zip
     [java] at samples.AddressXML
     [java] at protected samples.Address samples.Order.addr
     [java] at samples.Order

I have the following simple XmlAdapter to convert between Integer and
String:
=========================================================
public class IntToStringAdapter extends XmlAdapter<String, Integer> {
 
  public Integer unmarshal(String value) {
    return new Integer(value); }
 
  public String marshal(Integer value) {
    return value.toString(); }
 
}

I am attempting to use it to convert an int to a String as follows:
=============================================
public class AddressXML {
 
  protected String addrLine1;
  protected String addrLine2;
  protected String city;
  protected State state;
  @XmlJavaTypeAdapter(IntToStringAdapter.class)
  protected int zip;
  protected Phone phone;
 
}