[jira] [Created] (XMLBEANS-547) Option to avoid use of primitives for nullable elements

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[jira] [Created] (XMLBEANS-547) Option to avoid use of primitives for nullable elements

PJ Fanning (Jira)
Michael von Glasow created XMLBEANS-547:

             Summary: Option to avoid use of primitives for nullable elements
                 Key: XMLBEANS-547
                 URL: https://issues.apache.org/jira/browse/XMLBEANS-547
             Project: XMLBeans
          Issue Type: New Feature
          Components: Binding
         Environment: Affects all (general design issue)
            Reporter: Michael von Glasow

When generating Java classes from XSD, primitive Java types (such as `int` or `boolean`) are used for the corresponding XML types.

When an element is nullable, an additional `isNilFoo()` method allows the user to query that.

As I understand it, the corresponding `getFoo()` method always returns a value, even when the `foo` element is null. That makes it very easy to accidentally read bogus data from a null element.

Several other XML data binding frameworks use objects in that case (e.g. `Integer` instead of `int`), which has the advantage that accidentally reading a null value will throw an NPE rather than let the code continue to run on bogus data.

My suggestion is to follow that example. Since backward compatibility is likely a requirement, this behavior could be made configurable via a command-line switch in `scomp`.

If for whatever reason this is not a feasible solution, an alternate approach would be to generate getter methods like this:

{{public int getFoo() {}}

{{    if (isNilFoo())}}

{{        throw new IllegalStateException("attempt to read from null member: foo");}}

{{    return foo;}}


This message was sent by Atlassian Jira

To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]