Sunday, 15 September 2013

Why does JAXB call getter during unmarshalling -



Why does JAXB call getter during unmarshalling -

i surprised see next stack trace during jaxb unmarshalling:

[#|2013-02-05t18:59:27.551-0500|severe|glassfish3.1.2|configurationservice|_threadid=82;_threadname=thread-2;|exception processing c:\glassfish3\glassfish\domains\domain1\config\myconfig.xml : @notnull method com/foo/services/config/config.getbars must not homecoming null java.lang.illegalstateexception: @notnull method com.foo.services.config.config.getbars must not homecoming null @ com.foo.services.config.getbars(config.java:222) @ com.foo.services.config$jaxbaccessorm_getbars_setbars_java_util_list.get(methodaccessor_ref.java:56) @ com.sun.xml.bind.v2.runtime.reflect.lister$collectionlister.startpacking(lister.java:294) @ com.sun.xml.bind.v2.runtime.reflect.lister$collectionlister.startpacking(lister.java:269) @ com.sun.xml.bind.v2.runtime.unmarshaller.scope.start(scope.java:142) @ com.sun.xml.bind.v2.runtime.property.arrayerproperty$itemsloader.startelement(arrayerproperty.java:119) @ com.sun.xml.bind.v2.runtime.unmarshaller.unmarshallingcontext._startelement(unmarshallingcontext.java:501) @ com.sun.xml.bind.v2.runtime.unmarshaller.unmarshallingcontext.startelement(unmarshallingcontext.java:480) @ com.sun.xml.bind.v2.runtime.unmarshaller.validatingunmarshaller.startelement(validatingunmarshaller.java:102) @ com.sun.xml.bind.v2.runtime.unmarshaller.saxconnector.startelement(saxconnector.java:150) @ com.sun.org.apache.xerces.internal.parsers.abstractsaxparser.startelement(abstractsaxparser.java:506) @ com.sun.org.apache.xerces.internal.impl.xmlnsdocumentscannerimpl.scanstartelement(xmlnsdocumentscannerimpl.java:376) @ com.sun.org.apache.xerces.internal.impl.xmldocumentfragmentscannerimpl$fragmentcontentdriver.next(xmldocumentfragmentscannerimpl.java:2715) @ com.sun.org.apache.xerces.internal.impl.xmldocumentscannerimpl.next(xmldocumentscannerimpl.java:607) @ com.sun.org.apache.xerces.internal.impl.xmlnsdocumentscannerimpl.next(xmlnsdocumentscannerimpl.java:116) @ com.sun.org.apache.xerces.internal.impl.xmldocumentfragmentscannerimpl.scandocument(xmldocumentfragmentscannerimpl.java:488) @ com.sun.org.apache.xerces.internal.parsers.xml11configuration.parse(xml11configuration.java:835) @ com.sun.org.apache.xerces.internal.parsers.xml11configuration.parse(xml11configuration.java:764) @ com.sun.org.apache.xerces.internal.parsers.xmlparser.parse(xmlparser.java:123) @ com.sun.org.apache.xerces.internal.parsers.abstractsaxparser.parse(abstractsaxparser.java:1210) @ com.sun.org.apache.xerces.internal.jaxp.saxparserimpl$jaxpsaxparser.parse(saxparserimpl.java:568) @ com.sun.xml.bind.v2.runtime.unmarshaller.unmarshallerimpl.unmarshal0(unmarshallerimpl.java:218) @ com.sun.xml.bind.v2.runtime.unmarshaller.unmarshallerimpl.unmarshal(unmarshallerimpl.java:190) @ javax.xml.bind.helpers.abstractunmarshallerimpl.unmarshal(abstractunmarshallerimpl.java:172) @ javax.xml.bind.helpers.abstractunmarshallerimpl.unmarshal(abstractunmarshallerimpl.java:177) @ javax.xml.bind.helpers.abstractunmarshallerimpl.unmarshal(abstractunmarshallerimpl.java:186) @ javax.xml.bind.helpers.abstractunmarshallerimpl.unmarshal(abstractunmarshallerimpl.java:204)

the getter annotated org.jetbrains.annotation.notnull intent should marked not homecoming null because getter annotated @xmlelementref(required = true). bascially @notnull set there tell clients hey should never null because required element in xml file beingness unmarshalled , such either parsing fail because missing or going there. more info on @notnull can found here.

the property associated getter in case list<bar> not initialized class expected unmarshalling process so.

in case seeing if parsing fails during unmarshalling jaxb calls getter , trips @notnull generates above exception.

can shed lite on behavior? thanks,

-noah

a jaxb (jsr-222) implementation default treats public properties mapped. reason calls on list property see if value has been pre-initialized.

scenario #1

jaxb phone call getbars() see if collection has been created, homecoming null. since null returned jaxb create instance of java.util.arraylist set via setbars.

class="lang-java prettyprint-override">public class foo { private list<bar> bars; public list<bar> getbars() { homecoming bars; } public void setbars(list<bar> bars) { this.bars = bars; } }

scenario #2

jaxb phone call getbars() see if collection has been created, homecoming instance of linklist. since null not returned jaxb utilize instance of list returned method.

class="lang-java prettyprint-override">public class foo { private list<bar> bars = new linkedlist<bar>(); public list<bar> getbars() { homecoming bars; } public void setbars(list<bar> bars) { this.bars = bars; } }

scenario #3

if rather jaxb utilize fields instead of properties, can specify @xmlaccessortype(xmlaccesstype.field) on class or bundle (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).

class="lang-java prettyprint-override">@xmlaccessortype(xmlaccesstype.field) public class foo { private list<bar> bars; public list<bar> getbars() { homecoming bars; } public void setbars(list<bar> bars) { this.bars = bars; } }

jaxb

No comments:

Post a Comment