|
40 | 40 | import java.lang.reflect.Method;
|
41 | 41 | import java.lang.reflect.TypeVariable;
|
42 | 42 | import java.util.Arrays;
|
| 43 | +import java.util.ArrayList; |
43 | 44 | import java.util.List;
|
44 | 45 | import java.util.Optional;
|
45 | 46 | import java.util.OptionalDouble;
|
@@ -922,4 +923,45 @@ public void nestedOptionalGetter() {
|
922 | 923 | assertThat(foo.bar()).isNotNull();
|
923 | 924 | assertThat(foo.baz()).isEqualTo(0.0);
|
924 | 925 | }
|
| 926 | + |
| 927 | + // Test that we can build a property of type List<? extends Foo> using a property builder whose |
| 928 | + // build() method returns List<Foo>. The main motivation for this is Kotlin, where you can |
| 929 | + // easily run into this situation with "in" types. |
| 930 | + // This is a "Java 8" test because the generated code uses List.of (which is actually Java 9). |
| 931 | + // If we really are on Java 8 then the generated code will use `new ListBuilder<T>().build()` |
| 932 | + // instead. |
| 933 | + @AutoValue |
| 934 | + public abstract static class PropertyBuilderWildcard<T> { |
| 935 | + public abstract List<? extends T> list(); |
| 936 | + |
| 937 | + public static <T>PropertyBuilderWildcard.Builder<T> builder() { |
| 938 | + return new AutoValue_AutoValueJava8Test_PropertyBuilderWildcard.Builder<>(); |
| 939 | + } |
| 940 | + |
| 941 | + @AutoValue.Builder |
| 942 | + public interface Builder<T> { |
| 943 | + ListBuilder<T> listBuilder(); |
| 944 | + |
| 945 | + PropertyBuilderWildcard<T> build(); |
| 946 | + } |
| 947 | + |
| 948 | + public static class ListBuilder<T> { |
| 949 | + private final List<T> list = new ArrayList<>(); |
| 950 | + |
| 951 | + public void add(T value) { |
| 952 | + list.add(value); |
| 953 | + } |
| 954 | + |
| 955 | + public List<T> build() { |
| 956 | + return list; |
| 957 | + } |
| 958 | + } |
| 959 | + } |
| 960 | + |
| 961 | + @Test |
| 962 | + public void propertyBuilderWildcard() { |
| 963 | + PropertyBuilderWildcard.Builder<CharSequence> builder = PropertyBuilderWildcard.builder(); |
| 964 | + builder.listBuilder().add("foo"); |
| 965 | + assertThat(builder.build().list()).containsExactly("foo"); |
| 966 | + } |
925 | 967 | }
|
0 commit comments