Search in sources :

Example 1 with BuilderContext

use of com.google.auto.value.extension.AutoValueExtension.BuilderContext in project auto by google.

the class ExtensionTest method oddBuilderContext.

@Test
public void oddBuilderContext() {
    JavaFileObject autoValueClass = JavaFileObjects.forSourceLines("foo.bar.Baz", "package foo.bar;", "", "import com.google.auto.value.AutoValue;", "import com.google.common.collect.ImmutableList;", "", "@AutoValue", "abstract class Baz {", "  abstract String string();", "", "  @AutoValue.Builder", "  abstract static class Builder {", "    abstract Builder setString(String x);", "    abstract Baz oddBuild();", "    Baz build(int butNotReallyBecauseOfThisParameter) {", "      return null;", "    }", "  }", "}");
    ContextChecker checker = context -> {
        assertThat(context.builder()).isPresent();
        BuilderContext builderContext = context.builder().get();
        assertThat(builderContext.builderMethods()).isEmpty();
        assertThat(builderContext.toBuilderMethods()).isEmpty();
        assertThat(builderContext.buildMethod()).isEmpty();
        assertThat(builderContext.autoBuildMethod().getSimpleName().toString()).isEqualTo("oddBuild");
        Map<String, Set<ExecutableElement>> setters = builderContext.setters();
        assertThat(setters.keySet()).containsExactly("string");
        Set<ExecutableElement> thingSetters = setters.get("string");
        assertThat(thingSetters).hasSize(1);
        ExecutableElement thingSetter = Iterables.getOnlyElement(thingSetters);
        assertThat(thingSetter.getSimpleName().toString()).isEqualTo("setString");
        assertThat(builderContext.propertyBuilders()).isEmpty();
    };
    ContextCheckingExtension extension = new ContextCheckingExtension(checker);
    Compilation compilation = javac().withProcessors(new AutoValueProcessor(ImmutableList.of(extension))).compile(autoValueClass);
    assertThat(compilation).succeededWithoutWarnings();
}
Also used : Iterables(com.google.common.collect.Iterables) MoreTypes(com.google.auto.common.MoreTypes) SupportedOptions(javax.annotation.processing.SupportedOptions) Modifier(javax.lang.model.element.Modifier) URL(java.net.URL) Compiler.javac(com.google.testing.compile.Compiler.javac) VariableElement(javax.lang.model.element.VariableElement) RunWith(org.junit.runner.RunWith) BuilderContext(com.google.auto.value.extension.AutoValueExtension.BuilderContext) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) Truth(com.google.common.truth.Truth) URLClassLoader(java.net.URLClassLoader) ImmutableList(com.google.common.collect.ImmutableList) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) ServiceConfigurationError(java.util.ServiceConfigurationError) ElementFilter(javax.lang.model.util.ElementFilter) Truth8.assertThat(com.google.common.truth.Truth8.assertThat) JarOutputStream(java.util.jar.JarOutputStream) ZipEntry(java.util.zip.ZipEntry) AutoValueExtension(com.google.auto.value.extension.AutoValueExtension) JavaFileObjects(com.google.testing.compile.JavaFileObjects) StandardLocation(javax.tools.StandardLocation) ImmutableSet(com.google.common.collect.ImmutableSet) ExecutableElement(javax.lang.model.element.ExecutableElement) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) File(java.io.File) Compilation(com.google.testing.compile.Compilation) TypeKind(javax.lang.model.type.TypeKind) Consumer(java.util.function.Consumer) JavaFileObject(javax.tools.JavaFileObject) CompilationSubject.assertThat(com.google.testing.compile.CompilationSubject.assertThat) TypeMirror(javax.lang.model.type.TypeMirror) Filer(javax.annotation.processing.Filer) Writer(java.io.Writer) Optional(java.util.Optional) Collections(java.util.Collections) Joiner(com.google.common.base.Joiner) JavaFileObject(javax.tools.JavaFileObject) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Compilation(com.google.testing.compile.Compilation) ExecutableElement(javax.lang.model.element.ExecutableElement) BuilderContext(com.google.auto.value.extension.AutoValueExtension.BuilderContext) Map(java.util.Map) Test(org.junit.Test)

Example 2 with BuilderContext

use of com.google.auto.value.extension.AutoValueExtension.BuilderContext in project auto by google.

the class ExtensionTest method builderContext.

@Test
public void builderContext() {
    JavaFileObject parent = JavaFileObjects.forSourceLines("foo.bar.Parent", "package foo.bar;", "", "import com.google.common.collect.ImmutableList;", "", "interface Parent<T> {", "  T thing();", "  ImmutableList<T> list();", "}");
    JavaFileObject autoValueClass = JavaFileObjects.forSourceLines("foo.bar.Baz", "package foo.bar;", "", "import com.google.auto.value.AutoValue;", "import com.google.common.collect.ImmutableList;", "", "@AutoValue", "abstract class Baz implements Parent<String> {", "  static Builder builder() {", "    return new AutoValue_Baz.Builder();", "  }", "", "  abstract Builder toBuilder();", "", "  @AutoValue.Builder", "  abstract static class Builder {", "    abstract Builder setThing(String x);", "    abstract Builder setList(Iterable<String> x);", "    abstract Builder setList(ImmutableList<String> x);", "    abstract ImmutableList.Builder<String> listBuilder();", "    abstract Baz autoBuild();", "    Baz build() {", "      return autoBuild();", "    }", "  }", "}");
    ContextChecker checker = context -> {
        assertThat(context.builder()).isPresent();
        BuilderContext builderContext = context.builder().get();
        assertThat(builderContext.builderType().getQualifiedName().toString()).isEqualTo("foo.bar.Baz.Builder");
        Set<ExecutableElement> builderMethods = builderContext.builderMethods();
        assertThat(builderMethods).hasSize(1);
        ExecutableElement builderMethod = Iterables.getOnlyElement(builderMethods);
        assertThat(builderMethod.getSimpleName().toString()).isEqualTo("builder");
        Set<ExecutableElement> toBuilderMethods = builderContext.toBuilderMethods();
        assertThat(toBuilderMethods).hasSize(1);
        ExecutableElement toBuilderMethod = Iterables.getOnlyElement(toBuilderMethods);
        assertThat(toBuilderMethod.getSimpleName().toString()).isEqualTo("toBuilder");
        Optional<ExecutableElement> buildMethod = builderContext.buildMethod();
        assertThat(buildMethod).isPresent();
        assertThat(buildMethod.get().getSimpleName().toString()).isEqualTo("build");
        assertThat(buildMethod.get().getParameters()).isEmpty();
        assertThat(buildMethod.get().getReturnType().toString()).isEqualTo("foo.bar.Baz");
        ExecutableElement autoBuildMethod = builderContext.autoBuildMethod();
        assertThat(autoBuildMethod.getSimpleName().toString()).isEqualTo("autoBuild");
        assertThat(autoBuildMethod.getModifiers()).contains(Modifier.ABSTRACT);
        assertThat(autoBuildMethod.getParameters()).isEmpty();
        assertThat(autoBuildMethod.getReturnType().toString()).isEqualTo("foo.bar.Baz");
        Map<String, Set<ExecutableElement>> setters = builderContext.setters();
        assertThat(setters.keySet()).containsExactly("thing", "list");
        Set<ExecutableElement> thingSetters = setters.get("thing");
        assertThat(thingSetters).hasSize(1);
        ExecutableElement thingSetter = Iterables.getOnlyElement(thingSetters);
        assertThat(thingSetter.getSimpleName().toString()).isEqualTo("setThing");
        Set<ExecutableElement> listSetters = setters.get("list");
        assertThat(listSetters).hasSize(2);
        for (ExecutableElement listSetter : listSetters) {
            assertThat(listSetter.getSimpleName().toString()).isEqualTo("setList");
        }
        Map<String, ExecutableElement> propertyBuilders = builderContext.propertyBuilders();
        assertThat(propertyBuilders.keySet()).containsExactly("list");
        assertThat(propertyBuilders.get("list").getSimpleName().toString()).isEqualTo("listBuilder");
    };
    ContextCheckingExtension extension = new ContextCheckingExtension(checker);
    Compilation compilation = javac().withProcessors(new AutoValueProcessor(ImmutableList.of(extension))).compile(autoValueClass, parent);
    assertThat(compilation).succeededWithoutWarnings();
}
Also used : Iterables(com.google.common.collect.Iterables) MoreTypes(com.google.auto.common.MoreTypes) SupportedOptions(javax.annotation.processing.SupportedOptions) Modifier(javax.lang.model.element.Modifier) URL(java.net.URL) Compiler.javac(com.google.testing.compile.Compiler.javac) VariableElement(javax.lang.model.element.VariableElement) RunWith(org.junit.runner.RunWith) BuilderContext(com.google.auto.value.extension.AutoValueExtension.BuilderContext) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) Truth(com.google.common.truth.Truth) URLClassLoader(java.net.URLClassLoader) ImmutableList(com.google.common.collect.ImmutableList) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) ServiceConfigurationError(java.util.ServiceConfigurationError) ElementFilter(javax.lang.model.util.ElementFilter) Truth8.assertThat(com.google.common.truth.Truth8.assertThat) JarOutputStream(java.util.jar.JarOutputStream) ZipEntry(java.util.zip.ZipEntry) AutoValueExtension(com.google.auto.value.extension.AutoValueExtension) JavaFileObjects(com.google.testing.compile.JavaFileObjects) StandardLocation(javax.tools.StandardLocation) ImmutableSet(com.google.common.collect.ImmutableSet) ExecutableElement(javax.lang.model.element.ExecutableElement) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) File(java.io.File) Compilation(com.google.testing.compile.Compilation) TypeKind(javax.lang.model.type.TypeKind) Consumer(java.util.function.Consumer) JavaFileObject(javax.tools.JavaFileObject) CompilationSubject.assertThat(com.google.testing.compile.CompilationSubject.assertThat) TypeMirror(javax.lang.model.type.TypeMirror) Filer(javax.annotation.processing.Filer) Writer(java.io.Writer) Optional(java.util.Optional) Collections(java.util.Collections) Joiner(com.google.common.base.Joiner) JavaFileObject(javax.tools.JavaFileObject) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Compilation(com.google.testing.compile.Compilation) Optional(java.util.Optional) ExecutableElement(javax.lang.model.element.ExecutableElement) BuilderContext(com.google.auto.value.extension.AutoValueExtension.BuilderContext) Map(java.util.Map) Test(org.junit.Test)

Example 3 with BuilderContext

use of com.google.auto.value.extension.AutoValueExtension.BuilderContext in project auto by google.

the class ExtensionTest method builderContextWithInheritance.

@Test
public void builderContextWithInheritance() {
    JavaFileObject parent = JavaFileObjects.forSourceLines("foo.bar.Parent", "package foo.bar;", "", "interface Parent<BuilderT> {", "  BuilderT toBuilder();", "  interface Builder<T, BuilderT, BuiltT> {", "    BuilderT setThing(T x);", "    BuiltT build();", "  }", "}");
    JavaFileObject autoValueClass = JavaFileObjects.forSourceLines("foo.bar.Baz", "package foo.bar;", "", "import com.google.auto.value.AutoValue;", "", "@AutoValue", "abstract class Baz<T> implements Parent<Baz.Builder<T>> {", "  abstract T thing();", "  static <T> Builder<T> builder() {", "    return new AutoValue_Baz.Builder<>();", "  }", "", "  @AutoValue.Builder", "  abstract static class Builder<T> implements Parent.Builder<T, Builder<T>, Baz<T>> {", "  }", "}");
    ContextChecker checker = context -> {
        assertThat(context.builder()).isPresent();
        BuilderContext builderContext = context.builder().get();
        assertThat(builderContext.builderType().getQualifiedName().toString()).isEqualTo("foo.bar.Baz.Builder");
        Set<ExecutableElement> builderMethods = builderContext.builderMethods();
        assertThat(builderMethods).hasSize(1);
        ExecutableElement builderMethod = Iterables.getOnlyElement(builderMethods);
        assertThat(builderMethod.getSimpleName().toString()).isEqualTo("builder");
        Set<ExecutableElement> toBuilderMethods = builderContext.toBuilderMethods();
        assertThat(toBuilderMethods).hasSize(1);
        ExecutableElement toBuilderMethod = Iterables.getOnlyElement(toBuilderMethods);
        assertThat(toBuilderMethod.getSimpleName().toString()).isEqualTo("toBuilder");
        Optional<ExecutableElement> buildMethod = builderContext.buildMethod();
        assertThat(buildMethod).isPresent();
        assertThat(buildMethod.get().getSimpleName().toString()).isEqualTo("build");
        assertThat(buildMethod.get().getParameters()).isEmpty();
        assertThat(buildMethod.get().getReturnType().toString()).isEqualTo("BuiltT");
        ExecutableElement autoBuildMethod = builderContext.autoBuildMethod();
        assertThat(autoBuildMethod).isEqualTo(buildMethod.get());
        Map<String, Set<ExecutableElement>> setters = builderContext.setters();
        assertThat(setters.keySet()).containsExactly("thing");
        Set<ExecutableElement> thingSetters = setters.get("thing");
        assertThat(thingSetters).hasSize(1);
        ExecutableElement thingSetter = Iterables.getOnlyElement(thingSetters);
        assertThat(thingSetter.getSimpleName().toString()).isEqualTo("setThing");
    };
    ContextCheckingExtension extension = new ContextCheckingExtension(checker);
    Compilation compilation = javac().withProcessors(new AutoValueProcessor(ImmutableList.of(extension))).compile(autoValueClass, parent);
    assertThat(compilation).succeededWithoutWarnings();
}
Also used : Iterables(com.google.common.collect.Iterables) MoreTypes(com.google.auto.common.MoreTypes) SupportedOptions(javax.annotation.processing.SupportedOptions) Modifier(javax.lang.model.element.Modifier) URL(java.net.URL) Compiler.javac(com.google.testing.compile.Compiler.javac) VariableElement(javax.lang.model.element.VariableElement) RunWith(org.junit.runner.RunWith) BuilderContext(com.google.auto.value.extension.AutoValueExtension.BuilderContext) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) Truth(com.google.common.truth.Truth) URLClassLoader(java.net.URLClassLoader) ImmutableList(com.google.common.collect.ImmutableList) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) ServiceConfigurationError(java.util.ServiceConfigurationError) ElementFilter(javax.lang.model.util.ElementFilter) Truth8.assertThat(com.google.common.truth.Truth8.assertThat) JarOutputStream(java.util.jar.JarOutputStream) ZipEntry(java.util.zip.ZipEntry) AutoValueExtension(com.google.auto.value.extension.AutoValueExtension) JavaFileObjects(com.google.testing.compile.JavaFileObjects) StandardLocation(javax.tools.StandardLocation) ImmutableSet(com.google.common.collect.ImmutableSet) ExecutableElement(javax.lang.model.element.ExecutableElement) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) File(java.io.File) Compilation(com.google.testing.compile.Compilation) TypeKind(javax.lang.model.type.TypeKind) Consumer(java.util.function.Consumer) JavaFileObject(javax.tools.JavaFileObject) CompilationSubject.assertThat(com.google.testing.compile.CompilationSubject.assertThat) TypeMirror(javax.lang.model.type.TypeMirror) Filer(javax.annotation.processing.Filer) Writer(java.io.Writer) Optional(java.util.Optional) Collections(java.util.Collections) Joiner(com.google.common.base.Joiner) JavaFileObject(javax.tools.JavaFileObject) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Compilation(com.google.testing.compile.Compilation) Optional(java.util.Optional) ExecutableElement(javax.lang.model.element.ExecutableElement) BuilderContext(com.google.auto.value.extension.AutoValueExtension.BuilderContext) Map(java.util.Map) Test(org.junit.Test)

Aggregations

MoreTypes (com.google.auto.common.MoreTypes)3 AutoValueExtension (com.google.auto.value.extension.AutoValueExtension)3 BuilderContext (com.google.auto.value.extension.AutoValueExtension.BuilderContext)3 Joiner (com.google.common.base.Joiner)3 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 Iterables (com.google.common.collect.Iterables)3 Truth (com.google.common.truth.Truth)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)3 Truth8.assertThat (com.google.common.truth.Truth8.assertThat)3 Compilation (com.google.testing.compile.Compilation)3 CompilationSubject.assertThat (com.google.testing.compile.CompilationSubject.assertThat)3 Compiler.javac (com.google.testing.compile.Compiler.javac)3 JavaFileObjects (com.google.testing.compile.JavaFileObjects)3 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 Writer (java.io.Writer)3 URL (java.net.URL)3 URLClassLoader (java.net.URLClassLoader)3