|
34 | 34 | import java.lang.reflect.AnnotatedType;
|
35 | 35 | import java.lang.reflect.Constructor;
|
36 | 36 | import java.lang.reflect.Method;
|
| 37 | +import java.lang.reflect.TypeVariable; |
37 | 38 | import java.util.Arrays;
|
38 | 39 | import java.util.List;
|
39 | 40 | import java.util.Set;
|
@@ -546,4 +547,63 @@ public void testEqualsNullable() throws ReflectiveOperationException {
|
546 | 547 | assertThat(parameterTypes[0].isAnnotationPresent(EqualsNullable.Nullable.class))
|
547 | 548 | .isTrue();
|
548 | 549 | }
|
| 550 | + |
| 551 | + @AutoValue |
| 552 | + abstract static class AnnotatedTypeParameter<@Nullable T> { |
| 553 | + abstract @Nullable T thing(); |
| 554 | + |
| 555 | + static <@Nullable T> AnnotatedTypeParameter<T> create(T thing) { |
| 556 | + return new AutoValue_AutoValueJava8Test_AnnotatedTypeParameter<T>(thing); |
| 557 | + } |
| 558 | + } |
| 559 | + |
| 560 | + /** |
| 561 | + * Tests that an annotation on a type parameter of an {@code @AutoValue} class is copied to |
| 562 | + * the implementation class. |
| 563 | + */ |
| 564 | + @Test |
| 565 | + public void testTypeAnnotationCopiedToImplementation() throws ReflectiveOperationException { |
| 566 | + @Nullable String nullableString = "blibby"; |
| 567 | + AnnotatedTypeParameter<@Nullable String> x = AnnotatedTypeParameter.create(nullableString); |
| 568 | + Class<?> c = x.getClass(); |
| 569 | + assertThat(c.getTypeParameters()).hasLength(1); |
| 570 | + TypeVariable<?> typeParameter = c.getTypeParameters()[0]; |
| 571 | + assertThat(typeParameter.getAnnotations()) |
| 572 | + .named(typeParameter.toString()) |
| 573 | + .asList() |
| 574 | + .contains(nullable()); |
| 575 | + } |
| 576 | + |
| 577 | + @AutoValue |
| 578 | + abstract static class AnnotatedTypeParameterWithBuilder<@Nullable T> { |
| 579 | + abstract @Nullable T thing(); |
| 580 | + |
| 581 | + static <@Nullable T> Builder<T> builder() { |
| 582 | + return new AutoValue_AutoValueJava8Test_AnnotatedTypeParameterWithBuilder.Builder<T>(); |
| 583 | + } |
| 584 | + |
| 585 | + @AutoValue.Builder |
| 586 | + abstract static class Builder<@Nullable T> { |
| 587 | + abstract Builder<T> setThing(T thing); |
| 588 | + abstract AnnotatedTypeParameterWithBuilder<T> build(); |
| 589 | + } |
| 590 | + } |
| 591 | + |
| 592 | + /** |
| 593 | + * Tests that an annotation on a type parameter of an {@code @AutoValue} builder is copied to |
| 594 | + * the implementation class. |
| 595 | + */ |
| 596 | + @Test |
| 597 | + public void testTypeAnnotationOnBuilderCopiedToImplementation() |
| 598 | + throws ReflectiveOperationException { |
| 599 | + AnnotatedTypeParameterWithBuilder.Builder<@Nullable String> builder = |
| 600 | + AnnotatedTypeParameterWithBuilder.builder(); |
| 601 | + Class<?> c = builder.getClass(); |
| 602 | + assertThat(c.getTypeParameters()).hasLength(1); |
| 603 | + TypeVariable<?> typeParameter = c.getTypeParameters()[0]; |
| 604 | + assertThat(typeParameter.getAnnotations()) |
| 605 | + .named(typeParameter.toString()) |
| 606 | + .asList() |
| 607 | + .contains(nullable()); |
| 608 | + } |
549 | 609 | }
|
0 commit comments