Skip to content

Commit

Permalink
In @autovalue class Foo<@bar T>, copy @bar to the implementing subclass.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eamonnmcmanus authored and ronshapiro committed Apr 20, 2018
1 parent c4bfbef commit c830cf2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -546,4 +547,63 @@ public void testEqualsNullable() throws ReflectiveOperationException {
assertThat(parameterTypes[0].isAnnotationPresent(EqualsNullable.Nullable.class))
.isTrue();
}

@AutoValue
abstract static class AnnotatedTypeParameter<@Nullable T> {
abstract @Nullable T thing();

static <@Nullable T> AnnotatedTypeParameter<T> create(T thing) {
return new AutoValue_AutoValueJava8Test_AnnotatedTypeParameter<T>(thing);
}
}

/**
* Tests that an annotation on a type parameter of an {@code @AutoValue} class is copied to
* the implementation class.
*/
@Test
public void testTypeAnnotationCopiedToImplementation() throws ReflectiveOperationException {
@Nullable String nullableString = "blibby";
AnnotatedTypeParameter<@Nullable String> x = AnnotatedTypeParameter.create(nullableString);
Class<?> c = x.getClass();
assertThat(c.getTypeParameters()).hasLength(1);
TypeVariable<?> typeParameter = c.getTypeParameters()[0];
assertThat(typeParameter.getAnnotations())
.named(typeParameter.toString())
.asList()
.contains(nullable());
}

@AutoValue
abstract static class AnnotatedTypeParameterWithBuilder<@Nullable T> {
abstract @Nullable T thing();

static <@Nullable T> Builder<T> builder() {
return new AutoValue_AutoValueJava8Test_AnnotatedTypeParameterWithBuilder.Builder<T>();
}

@AutoValue.Builder
abstract static class Builder<@Nullable T> {
abstract Builder<T> setThing(T thing);
abstract AnnotatedTypeParameterWithBuilder<T> build();
}
}

/**
* Tests that an annotation on a type parameter of an {@code @AutoValue} builder is copied to
* the implementation class.
*/
@Test
public void testTypeAnnotationOnBuilderCopiedToImplementation()
throws ReflectiveOperationException {
AnnotatedTypeParameterWithBuilder.Builder<@Nullable String> builder =
AnnotatedTypeParameterWithBuilder.builder();
Class<?> c = builder.getClass();
assertThat(c.getTypeParameters()).hasLength(1);
TypeVariable<?> typeParameter = c.getTypeParameters()[0];
assertThat(typeParameter.getAnnotations())
.named(typeParameter.toString())
.asList()
.contains(nullable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,15 @@ static String formalTypeParametersString(TypeElement type) {
for (TypeParameterElement typeParameter : typeParameters) {
sb.append(sep);
sep = ", ";
appendTypeParameterWithBounds(sb, typeParameter);
appendTypeParameterWithBounds(typeParameter, sb);
}
return sb.append(">").toString();
}
}

private static void appendTypeParameterWithBounds(
StringBuilder sb, TypeParameterElement typeParameter) {
TypeParameterElement typeParameter, StringBuilder sb) {
appendAnnotations(typeParameter.getAnnotationMirrors(), sb);
sb.append(typeParameter.getSimpleName());
String sep = " extends ";
for (TypeMirror bound : typeParameter.getBounds()) {
Expand All @@ -181,6 +182,13 @@ private static void appendTypeParameterWithBounds(
}
}

private static void appendAnnotations(
List<? extends AnnotationMirror> annotationMirrors, StringBuilder sb) {
for (AnnotationMirror annotationMirror : annotationMirrors) {
sb.append(AnnotationOutput.sourceFormForAnnotation(annotationMirror)).append(" ");
}
}

/**
* Converts a type into a string, using standard Java syntax, except that every class name
* is wrapped in backquotes, like {@code `java.util.List`}.
Expand Down Expand Up @@ -308,13 +316,6 @@ private static class AnnotatedEncodingTypeVisitor extends EncodingTypeVisitor {
appendTypeArguments(type, sb);
return sb;
}

private void appendAnnotations(
List<? extends AnnotationMirror> annotationMirrors, StringBuilder sb) {
for (AnnotationMirror annotationMirror : annotationMirrors) {
sb.append(AnnotationOutput.sourceFormForAnnotation(annotationMirror)).append(" ");
}
}
}

private static class TypeRewriter {
Expand Down

0 comments on commit c830cf2

Please sign in to comment.