diff options
author | grahamrogers <grahamrogers@google.com> | 2020-02-10 07:37:00 -0800 |
---|---|---|
committer | Chris Povirk <beigetangerine@gmail.com> | 2020-02-10 16:19:17 -0800 |
commit | 4ab1b53b0ae9a6dbaae3cbc54c270e90fc7daa84 (patch) | |
tree | d10cea1c444c4095323730a6a8a4dd08f41b2efd /value/src/test/java/com/google | |
parent | 8b17dd84476423262f69cae7e5de7f12c4124166 (diff) | |
download | auto-4ab1b53b0ae9a6dbaae3cbc54c270e90fc7daa84.tar.gz |
Add type parameters to void AutoOneOf values.
This allows users of AutoOneOf to not have to manually cast the value.
Before:
@SuppressWarnings("unchecked")
public static <T> MyOneOf<T> empty() {
return (MyOneOf<T>) AutoOneOf_MyOneOf.empty();
}
After:
public static <T> MyOneOf<T> empty() {
return AutoOneOf_MyOneOf.empty();
}
RELNOTES=Add type parameters to factory methods for void AutoOneOf values
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=294216002
Diffstat (limited to 'value/src/test/java/com/google')
-rw-r--r-- | value/src/test/java/com/google/auto/value/processor/AutoOneOfCompilationTest.java | 90 |
1 files changed, 87 insertions, 3 deletions
diff --git a/value/src/test/java/com/google/auto/value/processor/AutoOneOfCompilationTest.java b/value/src/test/java/com/google/auto/value/processor/AutoOneOfCompilationTest.java index cf5a75e9..63e84199 100644 --- a/value/src/test/java/com/google/auto/value/processor/AutoOneOfCompilationTest.java +++ b/value/src/test/java/com/google/auto/value/processor/AutoOneOfCompilationTest.java @@ -84,8 +84,9 @@ public class AutoOneOfCompilationTest { " return new Impl_exception<V, T>(exception);", " }", "", - " static TaskResult<?, ?> empty() {", - " return Impl_empty.INSTANCE;", + " @SuppressWarnings(\"unchecked\") // type parameters are unused in void instances", + " static <V, T extends Throwable> TaskResult<V, T> empty() {", + " return (TaskResult<V, T>) Impl_empty.INSTANCE;", " }", "", " // Parent class that each implementation will inherit from.", @@ -231,6 +232,90 @@ public class AutoOneOfCompilationTest { } @Test + public void voidInstanceWithoutGenericTypeParameters() { + JavaFileObject javaFileObject = + JavaFileObjects.forSourceLines( + "foo.bar.Nothing", + "package foo.bar;", + "", + "import com.google.auto.value.AutoOneOf;", + "import java.io.Serializable;", + "", + "@AutoOneOf(Nothing.Kind.class)", + "abstract class Nothing {", + "", + " enum Kind {NOTHING}", + "", + " abstract Kind kind();", + "", + " abstract void nothing();", + "}"); + JavaFileObject expectedOutput = + JavaFileObjects.forSourceLines( + "foo.bar.Nothing", + "package foo.bar;", + "", + GeneratedImport.importGeneratedAnnotationType(), + "", + "@Generated(\"com.google.auto.value.processor.AutoOneOfProcessor\")", + "final class AutoOneOf_Nothing {", + " private AutoOneOf_Nothing() {} // There are no instances of this type.", + "", + " static Nothing nothing() {", + " return Impl_nothing.INSTANCE;", + " }", + "", + " // Parent class that each implementation will inherit from.", + " private abstract static class Parent_ extends Nothing {", + " @Override", + " void nothing() {", + " throw new UnsupportedOperationException(kind().toString());", + " }", + " }", + "", + " // Implementation when the contained property is \"nothing\".", + " private static final class Impl_nothing extends Parent_ {", + " // There is only one instance of this class.", + " static final Impl_nothing INSTANCE = new Impl_nothing();", + "", + " private Impl_nothing() {}", + "", + " @Override", + " public void nothing() {}", + "", + " @Override", + " public String toString() {", + " return \"Nothing{nothing}\";", + " }", + "", + " @Override", + " public boolean equals(Object x) {", + " return x == this;", + " }", + "", + " @Override", + " public int hashCode() {", + " return System.identityHashCode(this);", + " }", + "", + " @Override", + " public Nothing.Kind kind() {", + " return Nothing.Kind.NOTHING;", + " }", + " }", + "}"); + Compilation compilation = + javac() + .withProcessors(new AutoOneOfProcessor()) + .withOptions("-Xlint:-processing", "-implicit:none") + .compile(javaFileObject); + assertThat(compilation).succeededWithoutWarnings(); + assertThat(compilation) + .generatedSourceFile("foo.bar.AutoOneOf_Nothing") + .hasSourceEquivalentTo(expectedOutput); + } + + @Test public void noKindGetter() { JavaFileObject javaFileObject = JavaFileObjects.forSourceLines( @@ -388,6 +473,5 @@ public class AutoOneOfCompilationTest { .hadErrorContaining("@AutoOneOf properties cannot be @Nullable") .inFile(javaFileObject) .onLineContaining("@Nullable String dog()"); - } } |