java - Customize JAXB (xjc) binding compiler to instantiate list member with empty list -


is possible generate code java xml binding compiler (xjc) instantiates list members empty list instead of null?

example:

xsd-file foobar.xsd:

<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:tns="http://foo.bar"     targetnamespace="http://foo.bar" version="1.0">      <xs:complextype name="foobar">         <xs:sequence>             <xs:element name="entry" type="xs:string"                 maxoccurs="unbounded" minoccurs="0" />         </xs:sequence>     </xs:complextype>  </xs:schema> 

running binding compiler file, e.g.

xjc foobar.xsd 

produces java source code this

@xmlaccessortype(xmlaccesstype.field) @xmltype(name = "foobar", proporder = {     "entry" }) public class foobar {      protected list<string> entry;      public list<string> getentry() {         if (entry == null) {             entry = new arraylist<string>();         }         return this.entry;     }  } 

the entry list instantiated null , null check done in getter method. need entry mamber instantiated empty list this:

 protected list<string> entry = new arraylist<string>(); 

is somehow possible customization? did not find how?

well, ben thurley's suggestion one. however, different approach i've been working on, not finished create plugin.

i've written short tutorial, on how create xjc plugin allows instantiate fields.

adding following line:

f.init(jexpr._new(co.parent().getcodemodel().ref(arraylist.class))); 

i able generate following code:

@xmlelement(required = true) protected list<word> word = new arraylist(); 

again, not finished, promising ;)

update:

in run() method: replace following if-statement

if (types.contains(f.type())){      //if type defined in schema     //3. instantiate     f.init(jexpr._new(f.type()));  } 

with following if-statement

if (f.type().boxify().gettypeparameters()!=null &&      f.type().boxify().gettypeparameters().size()==1){         // f.type() list         jtype inner = f.type().boxify().gettypeparameters().get(0);     f.init(jexpr._new(co.parent().getcodemodel()             .ref(arraylist.class).narrow(inner))); } 

and correctly instantiate array list parameterization.

what if-statement here saying, if type parameterized, , if number of parameters 1 (two mean map), assume list , instantiate arraylist. method narrow() parameterization.

update 2:

since creating plugin might seem lot of work. i've committed plugin i've been working on github. plugin can instantiate fields, lists (into arraylists), , convert lists set!

all need do, plugin. install mvn install. add pom file shown in readme file on github.

i think closest can "possibility configuration".

have fun it!


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -