highsource / jaxb2-basics

Useful plugins and tools for JAXB2.
BSD 2-Clause "Simplified" License
109 stars 54 forks source link

Make list setters 'null' safe #29

Closed cr0man closed 9 years ago

cr0man commented 9 years ago

From my point of view the below code is not null safe. If value is null, draftl.addAll(value) will throw a NullPointerException

public void setStrings(List<String> value) {
    this.strings = null;
    List<String> draftl = this.getStrings();
    draftl.addAll(value);
}

There are situations where I can simply do not have the chance to test the value before. Eg: setStrings() is automatically invoked by some framework (like Dozer) Could you make it look like below:

public void setStrings(List<String> value) {
    this.strings = null;
    if (value != null) {
      List<String> draftl = this.getStrings();
      draftl.addAll(value);
    }
}

One workaround is to use the direct access, but if the incoming list is unmodifiable, then we are on dangerous grounds.

highsource commented 9 years ago

Should be fixed now, please give it a try.

highsource commented 9 years ago

I assume this works.