Search code examples
javacoding-stylecomplexity-theoryxmlbeans

Null property in XML Beans optional field


I am in a situation where I get some fields from a dto to insert them in an xmlBean, for example:

xmlBeanItem.setProperty(dtoItem.getproperty());

The problem is in that if for any reason dtoItem.getproperty() returned null, the xmlBean Property generates an empty tag (for instance ).

Is there a way to avoid such situation without using the famous if condition? So far I have used it on the form:

if (dtoItem.getJustification() != null) {
     xmlBeanItem.setProperty(dtoItem.getproperty());
}

which would increase the cyclomatic complexity to an astronomical number !!.

Thanks


Solution

  • Nope, there is no other way. In XmlSchema, null and absence are two distinct concepts. That is why there are xmlbean.isSetXXX() and xmlbean.isNilXXX() getters for each property.