Search in sources :

Example 1 with AutoValueProcessor

use of com.google.auto.value.processor.AutoValueProcessor in project auto by google.

the class GwtCompilationTest method testBasic.

/**
 * Test where the serialized properties don't include generics, so no {@code @SuppressWarnings}
 * annotation is needed. We explicitly check that one is not included anyway, because Eclipse for
 * example can be configured to complain about unnecessary warning suppression.
 */
@Test
public void testBasic() {
    JavaFileObject javaFileObject = JavaFileObjects.forSourceLines("foo.bar.Baz", "package foo.bar;", "", "import com.google.auto.value.AutoValue;", "import com.google.annotations.GwtCompatible;", "", "@AutoValue", "@GwtCompatible(serializable = true)", "public abstract class Baz {", "  public abstract int buh();", "", "  public static Baz create(int buh) {", "    return new AutoValue_Baz(buh);", "  }", "}");
    JavaFileObject expectedOutput = JavaFileObjects.forSourceLines("foo.bar.AutoValue_Baz_CustomFieldSerializer", "package foo.bar;", "", "import com.google.gwt.user.client.rpc.CustomFieldSerializer;", "import com.google.gwt.user.client.rpc.SerializationException;", "import com.google.gwt.user.client.rpc.SerializationStreamReader;", "import com.google.gwt.user.client.rpc.SerializationStreamWriter;", "import " + generatedAnnotationType() + ";", "", "@Generated(\"com.google.auto.value.processor.AutoValueProcessor\")", "public final class AutoValue_Baz_CustomFieldSerializer" + " extends CustomFieldSerializer<AutoValue_Baz> {", "", "  public static AutoValue_Baz instantiate(", "      SerializationStreamReader streamReader) throws SerializationException {", "    int buh = streamReader.readInt();", "    return new AutoValue_Baz(buh);", "  }", "", "  public static void serialize(", "      SerializationStreamWriter streamWriter,", "      AutoValue_Baz instance) throws SerializationException {", "    streamWriter.writeInt(instance.buh());", "  }", "", "  public static void deserialize(", "      @SuppressWarnings(\"unused\") SerializationStreamReader streamReader,", "      @SuppressWarnings(\"unused\") AutoValue_Baz instance) {", "  }", "", "  @SuppressWarnings(\"unused\") private int dummy_3f8e1b04;", "", "  @Override", "  public void deserializeInstance(", "      SerializationStreamReader streamReader,", "      AutoValue_Baz instance) {", "    deserialize(streamReader, instance);", "  }", "", "  @Override", "  public boolean hasCustomInstantiateInstance() {", "    return true;", "  }", "", "  @Override", "  public AutoValue_Baz instantiateInstance(", "      SerializationStreamReader streamReader) throws SerializationException {", "    return instantiate(streamReader);", "  }", "", "  @Override", "  public void serializeInstance(", "    SerializationStreamWriter streamWriter,", "    AutoValue_Baz instance) throws SerializationException {", "    serialize(streamWriter, instance);", "  }", "}");
    Compilation compilation = javac().withProcessors(new AutoValueProcessor()).compile(javaFileObject, GWT_COMPATIBLE);
    assertThat(compilation).succeededWithoutWarnings();
    assertThat(compilation).generatedSourceFile("foo.bar.AutoValue_Baz_CustomFieldSerializer").hasSourceEquivalentTo(expectedOutput);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) AutoValueProcessor(com.google.auto.value.processor.AutoValueProcessor) Compilation(com.google.testing.compile.Compilation) Test(org.junit.Test)

Example 2 with AutoValueProcessor

use of com.google.auto.value.processor.AutoValueProcessor in project auto by google.

the class MemoizedMethodSubject method hasError.

void hasError(String error) {
    JavaFileObject file = JavaFileObjects.forSourceLines("Value", "import com.google.auto.value.AutoValue;", "import com.google.auto.value.extension.memoized.Memoized;", "", "@AutoValue abstract class Value {", "  abstract String string();", actual, "}");
    Compilation compilation = javac().withProcessors(new AutoValueProcessor(ImmutableList.of(new MemoizeExtension()))).compile(file);
    assertThat(compilation).hadErrorContaining(error).inFile(file).onLineContaining(actual);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) AutoValueProcessor(com.google.auto.value.processor.AutoValueProcessor) Compilation(com.google.testing.compile.Compilation) MemoizeExtension(com.google.auto.value.extension.memoized.processor.MemoizeExtension)

Example 3 with AutoValueProcessor

use of com.google.auto.value.processor.AutoValueProcessor in project auto by google.

the class CompileWithEclipseTest method compileWithEclipse.

private void compileWithEclipse(String version, Predicate<File> predicate) throws IOException {
    File sourceRootFile = new File(SOURCE_ROOT);
    File javaDir = new File(sourceRootFile, "src/main/java");
    File javatestsDir = new File(sourceRootFile, "src/test/java");
    Set<File> sources = new ImmutableSet.Builder<File>().addAll(filesUnderDirectory(javaDir, predicate)).addAll(filesUnderDirectory(javatestsDir, predicate)).build();
    assertThat(sources).isNotEmpty();
    JavaCompiler compiler = new EclipseCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    // This hack is only needed in a Google-internal Java 8 environment where symbolic links make it
    // hard for ecj to find the boot class path. Elsewhere it is unnecessary but harmless. Notably,
    // on Java 9+ there is no rt.jar. There, fileManager.getLocation(PLATFORM_CLASS_PATH) returns
    // null, because the relevant classes are in modules inside
    // fileManager.getLocation(SYSTEM_MODULES).
    File rtJar = new File(JAVA_HOME.value() + "/lib/rt.jar");
    if (rtJar.exists()) {
        List<File> bootClassPath = ImmutableList.<File>builder().add(rtJar).addAll(fileManager.getLocation(StandardLocation.PLATFORM_CLASS_PATH)).build();
        fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, bootClassPath);
    }
    Iterable<? extends JavaFileObject> sourceFileObjects = fileManager.getJavaFileObjectsFromFiles(sources);
    String outputDir = tmp.getRoot().toString();
    ImmutableList<String> options = ImmutableList.of("-d", outputDir, "-s", outputDir, "-source", version, "-target", version, "-warn:-warningToken,-intfAnnotation");
    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, sourceFileObjects);
    // Explicitly supply an empty list of extensions for AutoValueProcessor, because otherwise this
    // test will pick up a test one and get confused.
    AutoValueProcessor autoValueProcessor = new AutoValueProcessor(ImmutableList.of());
    ImmutableList<? extends Processor> processors = ImmutableList.of(autoValueProcessor, new AutoOneOfProcessor(), new AutoAnnotationProcessor(), new AutoBuilderProcessor());
    task.setProcessors(processors);
    assertWithMessage("Compilation should succeed").that(task.call()).isTrue();
}
Also used : AutoOneOfProcessor(com.google.auto.value.processor.AutoOneOfProcessor) AutoValueProcessor(com.google.auto.value.processor.AutoValueProcessor) JavaCompiler(javax.tools.JavaCompiler) AutoAnnotationProcessor(com.google.auto.value.processor.AutoAnnotationProcessor) EclipseCompiler(org.eclipse.jdt.internal.compiler.tool.EclipseCompiler) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) StandardJavaFileManager(javax.tools.StandardJavaFileManager) AutoBuilderProcessor(com.google.auto.value.processor.AutoBuilderProcessor) File(java.io.File)

Example 4 with AutoValueProcessor

use of com.google.auto.value.processor.AutoValueProcessor in project auto by google.

the class GwtCompilationTest method testSuppressWarnings.

/**
 * Test where the serialized properties don't include generics, so a {@code @SuppressWarnings}
 * annotation is needed.
 */
@Test
public void testSuppressWarnings() {
    JavaFileObject javaFileObject = JavaFileObjects.forSourceLines("foo.bar.Baz", "package foo.bar;", "", "import com.google.auto.value.AutoValue;", "import com.google.annotations.GwtCompatible;", "", "import java.util.List;", "", "@AutoValue", "@GwtCompatible(serializable = true)", "public abstract class Baz {", "  public abstract List<String> buh();", "", "  public static Baz create(List<String> buh) {", "    return new AutoValue_Baz(buh);", "  }", "}");
    JavaFileObject expectedOutput = JavaFileObjects.forSourceLines("foo.bar.AutoValue_Baz_CustomFieldSerializer", "package foo.bar;", "", "import com.google.gwt.user.client.rpc.CustomFieldSerializer;", "import com.google.gwt.user.client.rpc.SerializationException;", "import com.google.gwt.user.client.rpc.SerializationStreamReader;", "import com.google.gwt.user.client.rpc.SerializationStreamWriter;", "import java.util.List;", "import " + generatedAnnotationType() + ";", "", "@Generated(\"com.google.auto.value.processor.AutoValueProcessor\")", "public final class AutoValue_Baz_CustomFieldSerializer" + " extends CustomFieldSerializer<AutoValue_Baz> {", "", "  public static AutoValue_Baz instantiate(", "      SerializationStreamReader streamReader) throws SerializationException {", "    @SuppressWarnings(\"unchecked\")", "    List<String> buh = (List<String>) streamReader.readObject();", "    return new AutoValue_Baz(buh);", "  }", "", "  public static void serialize(", "      SerializationStreamWriter streamWriter,", "      AutoValue_Baz instance) throws SerializationException {", "    streamWriter.writeObject(instance.buh());", "  }", "", "  public static void deserialize(", "      @SuppressWarnings(\"unused\") SerializationStreamReader streamReader,", "      @SuppressWarnings(\"unused\") AutoValue_Baz instance) {", "  }", "", "  @SuppressWarnings(\"unused\") private int dummy_949e312e;", "", "  @Override", "  public void deserializeInstance(", "      SerializationStreamReader streamReader,", "      AutoValue_Baz instance) {", "    deserialize(streamReader, instance);", "  }", "", "  @Override", "  public boolean hasCustomInstantiateInstance() {", "    return true;", "  }", "", "  @Override", "  public AutoValue_Baz instantiateInstance(", "      SerializationStreamReader streamReader) throws SerializationException {", "    return instantiate(streamReader);", "  }", "", "  @Override", "  public void serializeInstance(", "    SerializationStreamWriter streamWriter,", "    AutoValue_Baz instance) throws SerializationException {", "    serialize(streamWriter, instance);", "  }", "}");
    Compilation compilation = javac().withProcessors(new AutoValueProcessor()).compile(javaFileObject, GWT_COMPATIBLE);
    assertThat(compilation).succeededWithoutWarnings();
    assertThat(compilation).generatedSourceFile("foo.bar.AutoValue_Baz_CustomFieldSerializer").hasSourceEquivalentTo(expectedOutput);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) AutoValueProcessor(com.google.auto.value.processor.AutoValueProcessor) Compilation(com.google.testing.compile.Compilation) Test(org.junit.Test)

Example 5 with AutoValueProcessor

use of com.google.auto.value.processor.AutoValueProcessor in project auto by google.

the class GwtCompilationTest method testBuildersAndGenerics.

/**
 * Test builders and classes that are generic (as opposed to just containing properties with
 * generics).
 */
@Test
public void testBuildersAndGenerics() {
    JavaFileObject javaFileObject = JavaFileObjects.forSourceLines("foo.bar.Baz", "package foo.bar;", "", "import com.google.auto.value.AutoValue;", "import com.google.annotations.GwtCompatible;", "import com.google.common.collect.ImmutableMap;", "import java.util.Map;", "", "@AutoValue", "@GwtCompatible(serializable = true)", "public abstract class Baz<K extends Comparable<K>, V extends K> {", "  public abstract Map<K, V> map();", "  public abstract ImmutableMap<K, V> immutableMap();", "", "  public static <K extends Comparable<K>, V extends K> Builder<K, V> builder() {", "    return new AutoValue_Baz.Builder<K, V>();", "  }", "", "  @AutoValue.Builder", "  public interface Builder<K extends Comparable<K>, V extends K> {", "    Builder<K, V> map(Map<K, V> map);", "    ImmutableMap.Builder<K, V> immutableMapBuilder();", "    Baz<K, V> build();", "  }", "}");
    JavaFileObject expectedOutput = JavaFileObjects.forSourceLines("foo.bar.AutoValue_Baz_CustomFieldSerializer", "package foo.bar;", "", "import com.google.common.collect.ImmutableMap;", "import com.google.gwt.user.client.rpc.CustomFieldSerializer;", "import com.google.gwt.user.client.rpc.SerializationException;", "import com.google.gwt.user.client.rpc.SerializationStreamReader;", "import com.google.gwt.user.client.rpc.SerializationStreamWriter;", "import java.util.Map;", "import " + generatedAnnotationType() + ";", "", "@Generated(\"com.google.auto.value.processor.AutoValueProcessor\")", "public final class AutoValue_Baz_CustomFieldSerializer" + "<K extends Comparable<K>, V extends K>" + " extends CustomFieldSerializer<AutoValue_Baz<K, V>> {", "", "  public static <K extends Comparable<K>, V extends K> AutoValue_Baz<K, V>" + " instantiate(", "      SerializationStreamReader streamReader) throws SerializationException {", "    @SuppressWarnings(\"unchecked\")", "    Map<K, V> map = (Map<K, V>) streamReader.readObject();", "    @SuppressWarnings(\"unchecked\")", "    ImmutableMap<K, V> immutableMap = (ImmutableMap<K, V>) streamReader.readObject();", "    AutoValue_Baz.Builder<K, V> builder$ = new AutoValue_Baz.Builder<K, V>();", "    builder$.map(map);", "    builder$.immutableMapBuilder().putAll(immutableMap);", "    return (AutoValue_Baz<K, V>) builder$.build();", "  }", "", "  public static <K extends Comparable<K>, V extends K> void serialize(", "      SerializationStreamWriter streamWriter,", "      AutoValue_Baz<K, V> instance) throws SerializationException {", "    streamWriter.writeObject(instance.map());", "    streamWriter.writeObject(instance.immutableMap());", "  }", "", "  public static <K extends Comparable<K>, V extends K> void deserialize(", "      @SuppressWarnings(\"unused\") SerializationStreamReader streamReader,", "      @SuppressWarnings(\"unused\") AutoValue_Baz<K, V> instance) {", "  }", "", "  @SuppressWarnings(\"unused\")", "  private int dummy_2865d9ec;", "", "  @Override", "  public void deserializeInstance(", "      SerializationStreamReader streamReader,", "      AutoValue_Baz<K, V> instance) {", "    deserialize(streamReader, instance);", "  }", "", "  @Override", "  public boolean hasCustomInstantiateInstance() {", "    return true;", "  }", "", "  @Override", "  public AutoValue_Baz<K, V> instantiateInstance(", "      SerializationStreamReader streamReader) throws SerializationException {", "    return instantiate(streamReader);", "  }", "", "  @Override", "  public void serializeInstance(", "    SerializationStreamWriter streamWriter,", "    AutoValue_Baz<K, V> instance) throws SerializationException {", "    serialize(streamWriter, instance);", "  }", "}");
    Compilation compilation = javac().withProcessors(new AutoValueProcessor()).compile(javaFileObject, GWT_COMPATIBLE);
    assertThat(compilation).succeededWithoutWarnings();
    assertThat(compilation).generatedSourceFile("foo.bar.AutoValue_Baz_CustomFieldSerializer").hasSourceEquivalentTo(expectedOutput);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) AutoValueProcessor(com.google.auto.value.processor.AutoValueProcessor) Compilation(com.google.testing.compile.Compilation) Test(org.junit.Test)

Aggregations

AutoValueProcessor (com.google.auto.value.processor.AutoValueProcessor)5 Compilation (com.google.testing.compile.Compilation)4 JavaFileObject (javax.tools.JavaFileObject)4 Test (org.junit.Test)3 MemoizeExtension (com.google.auto.value.extension.memoized.processor.MemoizeExtension)1 AutoAnnotationProcessor (com.google.auto.value.processor.AutoAnnotationProcessor)1 AutoBuilderProcessor (com.google.auto.value.processor.AutoBuilderProcessor)1 AutoOneOfProcessor (com.google.auto.value.processor.AutoOneOfProcessor)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 File (java.io.File)1 JavaCompiler (javax.tools.JavaCompiler)1 StandardJavaFileManager (javax.tools.StandardJavaFileManager)1 EclipseCompiler (org.eclipse.jdt.internal.compiler.tool.EclipseCompiler)1