users@jaxb.java.net

Mapping interfaces

From: Pauli Savolainen <savolainen.pauli_at_gmail.com>
Date: Sun, 18 Jan 2009 12:41:49 -0800 (PST)

Hello

I have problems using interfaces with JAXB. I have looked around quite a bit
and what I've gathered so far is that I should use the XmlJavaTypeAdapter
annotation. I do the following:

My Schema:
<xsd:element name="a" type="tns:a"></xsd:element>
<xsd:complexType name="a">
</xsd:complexType>

my binding file:
<jxb:bindings node="//xs:complexType[@name='a']">
    <jxb:class ref="org.example.A">
    </jxb:class>
</jxb:bindings>

- So in fact the xjc command I invoke creates only the ObjectFactory class:
@XmlRegistry
public class ObjectFactory {
    private final static QName _A_QNAME = new
QName("http://www.example.org/schema/", "a");
    public ObjectFactory() {
    }
    @XmlElementDecl(namespace = "http://www.example.org/schema/", name =
"a")
    public JAXBElement createA(A value) {
        return new JAXBElement (_A_QNAME, A.class, null, value);
    }
}

- My A-interface looks like this. Note that I am trying to use the
XmlJavaTypeAdapter:

@XmlJavaTypeAdapter(AImpl.Adapter.class)
public interface A {
}

My AImpl class looks like this:

public class AImpl implements A {
  public AImpl() {}

  public static class Adapter extends XmlAdapter<AImpl, A> {
    public Adapter() {
    }
    @Override
    public AImpl marshal(A v) throws Exception {
      return (AImpl) v;
    }
    @Override
    public A unmarshal(AImpl v) throws Exception {
      return v;
    }
  }
}

- In my test class I try to create a JAXBContext, but it always fails:
JAXBContext c =
JAXBContext.newInstance(ObjectFactory.class.getPackage().getName());

The exception is the infamous "JAXB can't handle interfaces"

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts
of IllegalAnnotationExceptions
org.example.A is an interface, and JAXB can't handle interfaces.
        this problem is related to the following location:
                at org.example.A
                at public javax.xml.bind.JAXBElement
org.example.ObjectFactory.createA(org.example.A)
                at org.example.ObjectFactory
org.example.A does not have a no-arg default constructor.
        this problem is related to the following location:
                at org.example.A
                at public javax.xml.bind.JAXBElement
org.example.ObjectFactory.createA(org.example.A)
                at org.example.ObjectFactory

- What I've read so far indicates that the XmlJavaTypeAdapter annotation
should use the AImpl wherever JAXB sees the A-interface. I also tried
setting the annotation to package-info.java, but it didnt work either:

@XmlJavaTypeAdapter(value=AImpl.Adapter.class,type=A.class)
package org.example;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

- I can get rid of the "does not have a no-arg default constructor" error by
setting a @XmlType(factoryClass, factoryMethod) annotation on A using a
selfmade factoryobject.
- Maybe I am missing some annotations on the AImpl and A classes like
XmlRootElement or XmlType. But then again I have tried to use them without
any success.
- I am using jdk1.6.0_07.

So how do I use the XmlJavaTypeAdapter to bind A to my AImpl?

Pauli


-- 
View this message in context: http://www.nabble.com/Mapping-interfaces-tp21532657p21532657.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.