From 0b9398e1addd28552e2ba82e6c15acd23fc2a552 Mon Sep 17 00:00:00 2001 From: Brad Corso Date: Tue, 15 Feb 2022 08:40:57 -0800 Subject: [PATCH] Add Gradle tests that use transitive types and annotation on base components, base component factories, and component dependencies in the transitive-annotation-app. RELNOTES=N/A PiperOrigin-RevId: 428791214 --- .../main/java/library1/MyBaseComponent.java | 133 ++++++++++++++++++ .../java/library1/MyComponentDependency.java | 88 ++++++++++++ .../MyComponentDependencyBinding.java | 20 +++ .../library1/MySubcomponentWithFactory.java | 19 ++- .../src/main/java/app/MyComponent.java | 42 +++--- .../src/test/java/app/MyComponentTest.java | 10 +- .../app/MySubcomponentWithBuilderTest.java | 4 +- .../app/MySubcomponentWithFactoryTest.java | 4 +- 8 files changed, 295 insertions(+), 25 deletions(-) create mode 100644 javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MyBaseComponent.java create mode 100644 javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MyComponentDependency.java create mode 100644 javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MyComponentDependencyBinding.java diff --git a/javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MyBaseComponent.java b/javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MyBaseComponent.java new file mode 100644 index 00000000000..d647f54f64b --- /dev/null +++ b/javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MyBaseComponent.java @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2022 The Dagger Authors. + * + * 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 library1; + +import library2.MyTransitiveAnnotation; +import library2.MyTransitiveType; + +/** + * A class used to test that Dagger won't fail on unresolvable transitive types used in non-dagger + * related elements and annotations. + */ +// @MyTransitiveAnnotation: Not yet supported +@MyAnnotation(MyTransitiveType.VALUE) +@MyOtherAnnotation(MyTransitiveType.class) +public abstract class MyBaseComponent { + @MyQualifier + // @MyTransitiveAnnotation: Not yet supported + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public abstract MyComponentModule.UnscopedQualifiedBindsType unscopedQualifiedBindsTypeBase(); + + // @MyTransitiveAnnotation: Not yet supported + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public abstract MyComponentModule.UnscopedUnqualifiedBindsType unscopedUnqualifiedBindsTypeBase(); + + // @MyTransitiveAnnotation: Not yet supported + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public abstract void injectFooBase( + // @MyTransitiveAnnotation: Not yet supported + @MyAnnotation(MyTransitiveType.VALUE) @MyOtherAnnotation(MyTransitiveType.class) Foo binding); + + // @MyTransitiveAnnotation: Not yet supported + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public abstract static class Factory { + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public abstract MyBaseComponent create( + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + MyComponentModule myComponentModule, + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + MyComponentDependency myComponentDependency); + + // Non-dagger factory code + + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public MyTransitiveType nonDaggerField = null; + + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public static MyTransitiveType nonDaggerStaticField = null; + + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public MyTransitiveType nonDaggerMethod( + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + MyTransitiveType nonDaggerParameter) { + return nonDaggerParameter; + } + + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public static MyTransitiveType nonDaggerStaticMethod( + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + MyTransitiveType nonDaggerParameter) { + return nonDaggerParameter; + } + } + + // Non-dagger code + + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public MyTransitiveType nonDaggerField = null; + + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public static MyTransitiveType nonDaggerStaticField = null; + + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public MyTransitiveType nonDaggerMethod( + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + MyTransitiveType nonDaggerParameter) { + return nonDaggerParameter; + } + + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public static MyTransitiveType nonDaggerStaticMethod( + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + MyTransitiveType nonDaggerParameter) { + return nonDaggerParameter; + } +} diff --git a/javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MyComponentDependency.java b/javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MyComponentDependency.java new file mode 100644 index 00000000000..bb17021b722 --- /dev/null +++ b/javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MyComponentDependency.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2022 The Dagger Authors. + * + * 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 library1; + +import library2.MyTransitiveAnnotation; +import library2.MyTransitiveType; + +/** + * A class used to test that Dagger won't fail on unresolvable transitive types used in non-dagger + * related elements and annotations. + */ +// @MyTransitiveAnnotation: Not yet supported +@MyAnnotation(MyTransitiveType.VALUE) +@MyOtherAnnotation(MyTransitiveType.class) +public final class MyComponentDependency { + private final MyComponentDependencyBinding qualifiedMyComponentDependencyBinding = + new MyComponentDependencyBinding(); + private final MyComponentDependencyBinding unqualifiedMyComponentDependencyBinding = + new MyComponentDependencyBinding(); + + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public MyComponentDependency() {} + + @MyQualifier + // @MyTransitiveAnnotation: Not yet supported + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public MyComponentDependencyBinding qualifiedMyComponentDependencyBinding() { + return qualifiedMyComponentDependencyBinding; + } + + // @MyTransitiveAnnotation: Not yet supported + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public MyComponentDependencyBinding unqualifiedMyComponentDependencyBinding() { + return unqualifiedMyComponentDependencyBinding; + } + + // Non-dagger code + + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public MyTransitiveType nonDaggerField = null; + + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public static MyTransitiveType nonDaggerStaticField = null; + + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public MyTransitiveType nonDaggerMethod( + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + MyTransitiveType nonDaggerParameter) { + return nonDaggerParameter; + } + + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + public static MyTransitiveType nonDaggerStaticMethod( + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) + MyTransitiveType nonDaggerParameter) { + return nonDaggerParameter; + } +} diff --git a/javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MyComponentDependencyBinding.java b/javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MyComponentDependencyBinding.java new file mode 100644 index 00000000000..c5d6bc917be --- /dev/null +++ b/javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MyComponentDependencyBinding.java @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2022 The Dagger Authors. + * + * 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 library1; + +/** Used as a binding in {@link MyComponentDependency}. */ +public final class MyComponentDependencyBinding {} diff --git a/javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MySubcomponentWithFactory.java b/javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MySubcomponentWithFactory.java index 87d95163783..f3c6f930aab 100644 --- a/javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MySubcomponentWithFactory.java +++ b/javatests/artifacts/dagger/transitive-annotation-app/library1/src/main/java/library1/MySubcomponentWithFactory.java @@ -57,6 +57,9 @@ public abstract void injectFoo( @MyOtherAnnotation(MyTransitiveType.class) @Subcomponent.Factory public abstract static class Factory { + @MyTransitiveAnnotation + @MyAnnotation(MyTransitiveType.VALUE) + @MyOtherAnnotation(MyTransitiveType.class) public abstract MySubcomponentWithFactory create( @MyTransitiveAnnotation @MyAnnotation(MyTransitiveType.VALUE) @@ -79,30 +82,34 @@ public abstract MySubcomponentWithFactory create( @MyTransitiveAnnotation @MyAnnotation(MyTransitiveType.VALUE) @MyOtherAnnotation(MyTransitiveType.class) - public String nonDaggerField = ""; + public MyTransitiveType nonDaggerField = null; @MyTransitiveAnnotation @MyAnnotation(MyTransitiveType.VALUE) @MyOtherAnnotation(MyTransitiveType.class) - public static String nonDaggerStaticField = ""; + public static MyTransitiveType nonDaggerStaticField = null; @MyTransitiveAnnotation @MyAnnotation(MyTransitiveType.VALUE) @MyOtherAnnotation(MyTransitiveType.class) - public void nonDaggerMethod( + public MyTransitiveType nonDaggerMethod( @MyTransitiveAnnotation @MyAnnotation(MyTransitiveType.VALUE) @MyOtherAnnotation(MyTransitiveType.class) - String str) {} + MyTransitiveType nonDaggerParameter) { + return nonDaggerParameter; + } @MyTransitiveAnnotation @MyAnnotation(MyTransitiveType.VALUE) @MyOtherAnnotation(MyTransitiveType.class) - public static void nonDaggerStaticMethod( + public static MyTransitiveType nonDaggerStaticMethod( @MyTransitiveAnnotation @MyAnnotation(MyTransitiveType.VALUE) @MyOtherAnnotation(MyTransitiveType.class) - String str) {} + MyTransitiveType nonDaggerParameter) { + return nonDaggerParameter; + } } // Non-dagger code diff --git a/javatests/artifacts/dagger/transitive-annotation-app/src/main/java/app/MyComponent.java b/javatests/artifacts/dagger/transitive-annotation-app/src/main/java/app/MyComponent.java index 68af51ce306..ee55d3a486c 100644 --- a/javatests/artifacts/dagger/transitive-annotation-app/src/main/java/app/MyComponent.java +++ b/javatests/artifacts/dagger/transitive-annotation-app/src/main/java/app/MyComponent.java @@ -20,44 +20,54 @@ import javax.inject.Singleton; import library1.AssistedFoo; import library1.Foo; +import library1.MyBaseComponent; +import library1.MyComponentDependency; +import library1.MyComponentDependencyBinding; import library1.MyComponentModule; import library1.MyQualifier; import library1.MySubcomponentWithBuilder; import library1.MySubcomponentWithFactory; @Singleton -@Component(modules = MyComponentModule.class) -interface MyComponent { - Foo foo(); +@Component(dependencies = MyComponentDependency.class, modules = MyComponentModule.class) +abstract class MyComponent extends MyBaseComponent { + abstract Foo foo(); - AssistedFoo.Factory assistedFooFactory(); + abstract AssistedFoo.Factory assistedFooFactory(); @MyQualifier - MyComponentModule.ScopedQualifiedBindsType scopedQualifiedBindsType(); + abstract MyComponentModule.ScopedQualifiedBindsType scopedQualifiedBindsType(); - MyComponentModule.ScopedUnqualifiedBindsType scopedUnqualifiedBindsType(); + abstract MyComponentModule.ScopedUnqualifiedBindsType scopedUnqualifiedBindsType(); @MyQualifier - MyComponentModule.UnscopedQualifiedBindsType unscopedQualifiedBindsType(); + abstract MyComponentModule.UnscopedQualifiedBindsType unscopedQualifiedBindsType(); - MyComponentModule.UnscopedUnqualifiedBindsType unscopedUnqualifiedBindsType(); + abstract MyComponentModule.UnscopedUnqualifiedBindsType unscopedUnqualifiedBindsType(); @MyQualifier - MyComponentModule.ScopedQualifiedProvidesType scopedQualifiedProvidesType(); + abstract MyComponentModule.ScopedQualifiedProvidesType scopedQualifiedProvidesType(); - MyComponentModule.ScopedUnqualifiedProvidesType scopedUnqualifiedProvidesType(); + abstract MyComponentModule.ScopedUnqualifiedProvidesType scopedUnqualifiedProvidesType(); @MyQualifier - MyComponentModule.UnscopedQualifiedProvidesType unscopedQualifiedProvidesType(); + abstract MyComponentModule.UnscopedQualifiedProvidesType unscopedQualifiedProvidesType(); - MyComponentModule.UnscopedUnqualifiedProvidesType unscopedUnqualifiedProvidesType(); + abstract MyComponentModule.UnscopedUnqualifiedProvidesType unscopedUnqualifiedProvidesType(); - MySubcomponentWithFactory.Factory mySubcomponentWithFactory(); + abstract MySubcomponentWithFactory.Factory mySubcomponentWithFactory(); - MySubcomponentWithBuilder.Builder mySubcomponentWithBuilder(); + abstract MySubcomponentWithBuilder.Builder mySubcomponentWithBuilder(); + + @MyQualifier + abstract MyComponentDependencyBinding qualifiedMyComponentDependencyBinding(); + + abstract MyComponentDependencyBinding unqualifiedMyComponentDependencyBinding(); @Component.Factory - interface Factory { - MyComponent create(MyComponentModule myComponentModule); + abstract static class Factory extends MyBaseComponent.Factory { + public abstract MyComponent create( + MyComponentModule myComponentModule, + MyComponentDependency myComponentDependency); } } diff --git a/javatests/artifacts/dagger/transitive-annotation-app/src/test/java/app/MyComponentTest.java b/javatests/artifacts/dagger/transitive-annotation-app/src/test/java/app/MyComponentTest.java index 99dba464faa..438d5db6038 100644 --- a/javatests/artifacts/dagger/transitive-annotation-app/src/test/java/app/MyComponentTest.java +++ b/javatests/artifacts/dagger/transitive-annotation-app/src/test/java/app/MyComponentTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import library1.Dep; +import library1.MyComponentDependency; import library1.MyComponentModule; import org.junit.Before; import org.junit.Test; @@ -31,7 +32,8 @@ public final class MyComponentTest { @Before public void setup() { - component = DaggerMyComponent.factory().create(new MyComponentModule(new Dep())); + component = DaggerMyComponent.factory() + .create(new MyComponentModule(new Dep()), new MyComponentDependency()); } @Test @@ -91,4 +93,10 @@ public void testUnscopedUnqualifiedProvidesTypeIsNotScoped() { assertThat(component.unscopedUnqualifiedProvidesType()) .isNotEqualTo(component.unscopedUnqualifiedProvidesType()); } + + @Test + public void testMyComponentDependencyBinding() { + assertThat(component.qualifiedMyComponentDependencyBinding()) + .isNotEqualTo(component.unqualifiedMyComponentDependencyBinding()); + } } diff --git a/javatests/artifacts/dagger/transitive-annotation-app/src/test/java/app/MySubcomponentWithBuilderTest.java b/javatests/artifacts/dagger/transitive-annotation-app/src/test/java/app/MySubcomponentWithBuilderTest.java index 9d2e931ae9a..3db241b4398 100644 --- a/javatests/artifacts/dagger/transitive-annotation-app/src/test/java/app/MySubcomponentWithBuilderTest.java +++ b/javatests/artifacts/dagger/transitive-annotation-app/src/test/java/app/MySubcomponentWithBuilderTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import library1.Dep; +import library1.MyComponentDependency; import library1.MyComponentModule; import library1.MySubcomponentBinding; import library1.MySubcomponentModule; @@ -35,7 +36,8 @@ public final class MySubcomponentWithBuilderTest { @Before public void setup() { subcomponentWithBuilder = - DaggerMyComponent.factory().create(new MyComponentModule(new Dep())) + DaggerMyComponent.factory() + .create(new MyComponentModule(new Dep()), new MyComponentDependency()) .mySubcomponentWithBuilder() .mySubcomponentModule(new MySubcomponentModule(3)) .qualifiedMySubcomponentBinding(new MySubcomponentBinding()) diff --git a/javatests/artifacts/dagger/transitive-annotation-app/src/test/java/app/MySubcomponentWithFactoryTest.java b/javatests/artifacts/dagger/transitive-annotation-app/src/test/java/app/MySubcomponentWithFactoryTest.java index 0b3a7e70089..a45dcc75faf 100644 --- a/javatests/artifacts/dagger/transitive-annotation-app/src/test/java/app/MySubcomponentWithFactoryTest.java +++ b/javatests/artifacts/dagger/transitive-annotation-app/src/test/java/app/MySubcomponentWithFactoryTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import library1.Dep; +import library1.MyComponentDependency; import library1.MyComponentModule; import library1.MySubcomponentBinding; import library1.MySubcomponentModule; @@ -35,7 +36,8 @@ public final class MySubcomponentWithFactoryTest { @Before public void setup() { subcomponentWithFactory = - DaggerMyComponent.factory().create(new MyComponentModule(new Dep())) + DaggerMyComponent.factory() + .create(new MyComponentModule(new Dep()), new MyComponentDependency()) .mySubcomponentWithFactory() .create( new MySubcomponentModule(1),