diff --git a/value/src/it/functional/src/test/java/com/google/auto/value/AutoValueJava8Test.java b/value/src/it/functional/src/test/java/com/google/auto/value/AutoValueJava8Test.java index 4a4f1b3ff7..c9585cf1ed 100644 --- a/value/src/it/functional/src/test/java/com/google/auto/value/AutoValueJava8Test.java +++ b/value/src/it/functional/src/test/java/com/google/auto/value/AutoValueJava8Test.java @@ -42,6 +42,7 @@ import java.util.Arrays; import java.util.List; import java.util.Optional; +import java.util.OptionalDouble; import java.util.Set; import java.util.function.Predicate; import javax.annotation.processing.AbstractProcessor; @@ -862,4 +863,63 @@ public void testOptionalExtends() { OptionalExtends t = OptionalExtends.builder().setPredicate(predicate).build(); assertThat(t.predicate()).hasValue(predicate); } + + @AutoValue + public abstract static class Foo { + public abstract Bar bar(); + + public abstract double baz(); + + public static Foo.Builder builder() { + return new AutoValue_AutoValueJava8Test_Foo.Builder(); + } + + @AutoValue.Builder + public abstract static class Builder { + // https://github.com/google/auto/blob/master/value/userguide/builders-howto.md#normalize + abstract Optional bar(); + + public abstract Builder bar(Bar bar); + + // https://github.com/google/auto/blob/master/value/userguide/builders-howto.md#nested_builders + public abstract Bar.Builder barBuilder(); + + abstract OptionalDouble baz(); + + public abstract Builder baz(double baz); + + abstract Foo autoBuild(); + + public Foo build() { + if (!bar().isPresent()) { + bar(Bar.builder().build()); + } + if (!baz().isPresent()) { + baz(0.0); + } + return autoBuild(); + } + } + } + + @AutoValue + public abstract static class Bar { + public abstract Bar.Builder toBuilder(); + + public static Bar.Builder builder() { + return new AutoValue_AutoValueJava8Test_Bar.Builder(); + } + + @AutoValue.Builder + public abstract static class Builder { + public abstract Bar build(); + } + } + + @Test + public void nestedOptionalGetter() { + Foo foo = Foo.builder().build(); + assertThat(foo.bar()).isNotNull(); + assertThat(foo.baz()).isEqualTo(0.0); + } } diff --git a/value/src/main/java/com/google/auto/value/processor/builder.vm b/value/src/main/java/com/google/auto/value/processor/builder.vm index 5c4f4d5dbd..d032cda98f 100644 --- a/value/src/main/java/com/google/auto/value/processor/builder.vm +++ b/value/src/main/java/com/google/auto/value/processor/builder.vm @@ -192,7 +192,7 @@ class ${builderName}${builderFormalTypes} ## if ($noValueToGetCondition) { return $builderGetter.optional.empty; } - #elseif ($p.nullable) + #else if ($p == null) { return $builderGetter.optional.empty; }