Uploaded image for project: 'XMLBeans'
  1. XMLBeans
  2. XMLBEANS-547

Option to avoid use of primitives for nullable elements

    XMLWordPrintableJSON

    Details

    • Type: New Feature
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Binding
    • Labels:
    • Environment:
      Affects all (general design issue)

      Description

      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;

      }

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              mvglasow Michael von Glasow
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: