How does JAXB guess the type of list schema?

Here is an example of a jaxb class that I have defined from scratch. When I try to create a schema for this class using JAXB it correctly guesses that the itemType of the list is as "int"

Doesn't the JVM throw away generics at runtime? By doing this, the type of the List container is lost at runtime, but still jaxb knows the ItemType is Integer. I am very curious how this is achieved by the jaxb framework. Any ideas?

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "int-list")
public class IntegerList{
    @XmlValue
    private List<Integer> values;

    public List<Integer> getValues() {
        return values;
    }

    public void setValues(List<Integer> values) {
        this.values = values;
    }
}

      

Relevant schemas generated using JAXBContext.generateSchema:

  <xs:simpleType name="int-list">
    <xs:list itemType="xs:int"/>
  </xs:simpleType>

      

+3


source to share


1 answer


Doesn't the JVM throw away generics at runtime?

Yes, in relation to the actual bytecode (well, that's the compiler that throws them away). But general information about the types of classes and some variables can still be obtained at runtime using reflection. For example:



Field field = IntegerList.class.getDeclaredField("values");
Type fieldType = field.getGenericType();
Type typeArg = ((ParameterizedType)fieldType).getActualTypeArguments()[0];
System.out.println(typeArg); //class java.lang.Integer

      

Easy to guess, but JAXB will most likely do something like this and then map Integer.class

against int.class

.

+2


source







All Articles