Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an issue with builder getters. #1387

Merged
merged 1 commit into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class ${builderName}${builderFormalTypes} ##
if ($noValueToGetCondition) {
return $builderGetter.optional.empty;
}
#elseif ($p.nullable)
#else
if ($p == null) {
return $builderGetter.optional.empty;
}
Expand Down