Skip to content

Commit c830cf2

Browse files
eamonnmcmanusronshapiro
authored andcommitted
In @autovalue class Foo<@bar T>, copy @bar to the implementing subclass.
RELNOTES=In @autovalue class Foo<@bar T>, copy @bar to the implementing subclass. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=193530939
1 parent c4bfbef commit c830cf2

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

value/src/it/functional/src/test/java/com/google/auto/value/AutoValueJava8Test.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.lang.reflect.AnnotatedType;
3535
import java.lang.reflect.Constructor;
3636
import java.lang.reflect.Method;
37+
import java.lang.reflect.TypeVariable;
3738
import java.util.Arrays;
3839
import java.util.List;
3940
import java.util.Set;
@@ -546,4 +547,63 @@ public void testEqualsNullable() throws ReflectiveOperationException {
546547
assertThat(parameterTypes[0].isAnnotationPresent(EqualsNullable.Nullable.class))
547548
.isTrue();
548549
}
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+
}
549609
}

value/src/main/java/com/google/auto/value/processor/TypeEncoder.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,15 @@ static String formalTypeParametersString(TypeElement type) {
162162
for (TypeParameterElement typeParameter : typeParameters) {
163163
sb.append(sep);
164164
sep = ", ";
165-
appendTypeParameterWithBounds(sb, typeParameter);
165+
appendTypeParameterWithBounds(typeParameter, sb);
166166
}
167167
return sb.append(">").toString();
168168
}
169169
}
170170

171171
private static void appendTypeParameterWithBounds(
172-
StringBuilder sb, TypeParameterElement typeParameter) {
172+
TypeParameterElement typeParameter, StringBuilder sb) {
173+
appendAnnotations(typeParameter.getAnnotationMirrors(), sb);
173174
sb.append(typeParameter.getSimpleName());
174175
String sep = " extends ";
175176
for (TypeMirror bound : typeParameter.getBounds()) {
@@ -181,6 +182,13 @@ private static void appendTypeParameterWithBounds(
181182
}
182183
}
183184

185+
private static void appendAnnotations(
186+
List<? extends AnnotationMirror> annotationMirrors, StringBuilder sb) {
187+
for (AnnotationMirror annotationMirror : annotationMirrors) {
188+
sb.append(AnnotationOutput.sourceFormForAnnotation(annotationMirror)).append(" ");
189+
}
190+
}
191+
184192
/**
185193
* Converts a type into a string, using standard Java syntax, except that every class name
186194
* is wrapped in backquotes, like {@code `java.util.List`}.
@@ -308,13 +316,6 @@ private static class AnnotatedEncodingTypeVisitor extends EncodingTypeVisitor {
308316
appendTypeArguments(type, sb);
309317
return sb;
310318
}
311-
312-
private void appendAnnotations(
313-
List<? extends AnnotationMirror> annotationMirrors, StringBuilder sb) {
314-
for (AnnotationMirror annotationMirror : annotationMirrors) {
315-
sb.append(AnnotationOutput.sourceFormForAnnotation(annotationMirror)).append(" ");
316-
}
317-
}
318319
}
319320

320321
private static class TypeRewriter {

0 commit comments

Comments
 (0)