If an optional property has been set, there is no way to set it back to Optional.empty() because the setter uses Optional.of() instead of Optional.ofNullable()
For example if you have this class:
import com.google.auto.value.AutoValue;
import java.util.Optional;
import javax.annotation.Nullable;
public class Test {
@AutoValue
public static abstract class Item {
public abstract Optional<String> getName();
public static Builder builder() {
return new AutoValue_Test_Item.Builder();
}
public abstract Builder toBuilder();
@AutoValue.Builder
public static abstract class Builder {
public abstract Builder setName(@Nullable String name);
public abstract Item build();
}
}
public static void main(String args[]) {
Item item = Item.builder().setName("Test").build();
item.toBuilder().setName(null).build();
}
}
This will result in a NullPointerException. Is there no way to set this property to empty in the builder? Seems like a valid use case to me.
If an optional property has been set, there is no way to set it back to
Optional.empty()because the setter usesOptional.of()instead ofOptional.ofNullable()For example if you have this class:
This will result in a NullPointerException. Is there no way to set this property to empty in the builder? Seems like a valid use case to me.