dev@jaxb.java.net

XJC: get the narrowed class of an JClass or an JFieldVar

From: Florian Bachmann <f.bachmann_at_micromata.de>
Date: Mon, 23 Nov 2009 15:14:52 +0100

Dear XJC-Developers,

I’m writing a XJC-Plug-in (as you can easily guess) and I wonder how to access the generic information of a class. It is easy to add the generic information to a class via JClass’ narrow()-method.

For example I can create a ArrayList<String> with the followed code:

outline.getCodeModel().ref(ArrayList.class).narrow(String.class);

Assume that I have

Map<String, JFieldVar> fields = classOutline.implClass.fields();

for (JFieldVar field : cfields.values()) {

   JClass fieldClass = field.type().boxify();

   fieldClass.getNarrowdClass()????

}

Is there an easy way, to get the narrowed classes? Like getNarrowedClasses() that return in the case of ArrayList<String> the String-class?

I need this because JAXB has only the more generalized list type (e.g. List<String> list;) in field.type() and if I want to create a new list, the generated code looks like list = new List<String>(); or if create a ArrayList by hand (method.body().assign(field.ref(jFieldVar), JExpr._new(arrayList).arg());) it creates list = new ArrayList(); which generates a Raw-Type warning. So, is there a way, to get the generic type of the List (in this case String), to narrow the List class with this type?

regards Flori