diff options
Diffstat (limited to 'factory/src/test')
5 files changed, 182 insertions, 0 deletions
diff --git a/factory/src/test/java/com/google/auto/factory/processor/AutoFactoryProcessorTest.java b/factory/src/test/java/com/google/auto/factory/processor/AutoFactoryProcessorTest.java index 0df4c9ca..2ab0fe95 100644 --- a/factory/src/test/java/com/google/auto/factory/processor/AutoFactoryProcessorTest.java +++ b/factory/src/test/java/com/google/auto/factory/processor/AutoFactoryProcessorTest.java @@ -508,6 +508,22 @@ public class AutoFactoryProcessorTest { .hasSourceEquivalentTo(loadExpectedFile("expected/DefaultPackageFactory.java")); } + @Test + public void generics() { + JavaFileObject file = JavaFileObjects.forResource("good/Generics.java"); + Compilation compilation = javac.compile(file); + assertThat(compilation).succeededWithoutWarnings(); + assertThat(compilation) + .generatedSourceFile("tests.Generics_FooImplFactory") + .hasSourceEquivalentTo(loadExpectedFile("expected/Generics_FooImplFactory.java")); + assertThat(compilation) + .generatedSourceFile("tests.Generics_ExplicitFooImplFactory") + .hasSourceEquivalentTo(loadExpectedFile("expected/Generics_ExplicitFooImplFactory.java")); + assertThat(compilation) + .generatedSourceFile("tests.Generics_FooImplWithClassFactory") + .hasSourceEquivalentTo(loadExpectedFile("expected/Generics_FooImplWithClassFactory.java")); + } + private JavaFileObject loadExpectedFile(String resourceName) { if (isJavaxAnnotationProcessingGeneratedAvailable()) { return JavaFileObjects.forResource(resourceName); diff --git a/factory/src/test/resources/expected/Generics_ExplicitFooImplFactory.java b/factory/src/test/resources/expected/Generics_ExplicitFooImplFactory.java new file mode 100644 index 00000000..00c6d92c --- /dev/null +++ b/factory/src/test/resources/expected/Generics_ExplicitFooImplFactory.java @@ -0,0 +1,48 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package tests; + +import javax.annotation.processing.Generated; +import javax.inject.Inject; +import javax.inject.Provider; + +@Generated( + value = "com.google.auto.factory.processor.AutoFactoryProcessor", + comments = "https://github.com/google/auto/tree/master/factory" + ) +final class Generics_ExplicitFooImplFactory<M extends Generics.Bar> + implements Generics.FooFactory<M> { + private final Provider<M> unusedProvider; + + @Inject + Generics_ExplicitFooImplFactory(Provider<M> unusedProvider) { + this.unusedProvider = checkNotNull(unusedProvider, 1); + } + + @Override + public Generics.ExplicitFooImpl<M> create() { + return new Generics.ExplicitFooImpl<M>(checkNotNull(unusedProvider.get(), 1)); + } + + private static <T> T checkNotNull(T reference, int argumentIndex) { + if (reference == null) { + throw new NullPointerException( + "@AutoFactory method argument is null but is not marked @Nullable. Argument index: " + + argumentIndex); + } + return reference; + } +} diff --git a/factory/src/test/resources/expected/Generics_FooImplFactory.java b/factory/src/test/resources/expected/Generics_FooImplFactory.java new file mode 100644 index 00000000..2fb560a7 --- /dev/null +++ b/factory/src/test/resources/expected/Generics_FooImplFactory.java @@ -0,0 +1,34 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package tests; + +import javax.annotation.processing.Generated; +import javax.inject.Inject; + +@Generated( + value = "com.google.auto.factory.processor.AutoFactoryProcessor", + comments = "https://github.com/google/auto/tree/master/factory" + ) +final class Generics_FooImplFactory<M extends Generics.Bar> implements Generics.FooFactory<M> { + @Inject + Generics_FooImplFactory() { + } + + @Override + public Generics.FooImpl<M> create() { + return new Generics.FooImpl<M>(); + } +} diff --git a/factory/src/test/resources/expected/Generics_FooImplWithClassFactory.java b/factory/src/test/resources/expected/Generics_FooImplWithClassFactory.java new file mode 100644 index 00000000..b338454f --- /dev/null +++ b/factory/src/test/resources/expected/Generics_FooImplWithClassFactory.java @@ -0,0 +1,34 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package tests; + +import javax.annotation.processing.Generated; +import javax.inject.Inject; + +@Generated( + value = "com.google.auto.factory.processor.AutoFactoryProcessor", + comments = "https://github.com/google/auto/tree/master/factory" + ) +final class Generics_FooImplWithClassFactory<M extends Generics.Bar> extends Generics.FooFactoryClass<M> { + @Inject + Generics_FooImplWithClassFactory() { + } + + @Override + public Generics.FooImplWithClass<M> create() { + return new Generics.FooImplWithClass<M>(); + } +} diff --git a/factory/src/test/resources/good/Generics.java b/factory/src/test/resources/good/Generics.java new file mode 100644 index 00000000..638302fe --- /dev/null +++ b/factory/src/test/resources/good/Generics.java @@ -0,0 +1,50 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package tests; + +import com.google.auto.factory.AutoFactory; +import com.google.auto.factory.Provided; + +class Generics { + interface Bar {} + + interface Foo<M extends Bar> {} + + interface FooFactory<M extends Bar> { + Foo<M> create(); + } + + // The generated FooImplFactory should also have an <M extends Bar> type parameter, so we can + // have FooImplFactory<M extends Bar> implements FooFactory<M>. + @AutoFactory(implementing = FooFactory.class) + static final class FooImpl<M extends Bar> implements Foo<M> { + FooImpl() {} + } + + // The generated ExplicitFooImplFactory should have an <M extends Bar> type parameter, which + // serves both for FooFactory<M> and for Provider<M> in the constructor. + @AutoFactory(implementing = FooFactory.class) + static final class ExplicitFooImpl<M extends Bar> implements Foo<M> { + ExplicitFooImpl(@Provided M unused) {} + } + + abstract static class FooFactoryClass<M extends Bar> { + abstract Foo<M> create(); + } + + @AutoFactory(extending = FooFactoryClass.class) + static final class FooImplWithClass<M extends Bar> implements Foo<M> {} +} |