diff --git a/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypeNames.kt b/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypeNames.kt index 3ba211c8819..f4451c0b202 100644 --- a/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypeNames.kt +++ b/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypeNames.kt @@ -23,11 +23,9 @@ import androidx.room3.compiler.codegen.compat.XConverters.toJavaPoet import androidx.room3.compiler.codegen.compat.XConverters.toKotlinPoet import androidx.room3.compiler.codegen.compat.XConverters.toXPoet import androidx.room3.compiler.processing.XType -import com.squareup.javapoet.ArrayTypeName as JArrayTypeName import com.squareup.javapoet.ClassName as JClassName import com.squareup.javapoet.ParameterizedTypeName as JParameterizedTypeName import com.squareup.javapoet.TypeName as JTypeName -import com.squareup.javapoet.TypeVariableName as JTypeVariableName import com.squareup.javapoet.WildcardTypeName as JWildcardTypeName import com.squareup.kotlinpoet.ClassName as KClassName import com.squareup.kotlinpoet.ParameterizedTypeName as KParameterizedTypeName @@ -375,43 +373,6 @@ object XTypeNames { @JvmStatic fun XTypeName.isJavaWildcard(): Boolean = toJavaPoet() is JWildcardTypeName - @JvmStatic - fun XTypeName.boundsHasSelfReference(): Boolean = - // The result should be the same for both JavaPoet and KotlinPoet, so we only need to check one. - toJavaPoet().boundsHasSelfReference(mutableMapOf()) - - private fun JTypeName.boundsHasSelfReference( - typeParametersMap: MutableMap - ): Boolean { - if (isPrimitive) { - return false - } - return when (this) { - is JClassName -> false - is JParameterizedTypeName -> - typeArguments.any { it.boundsHasSelfReference(typeParametersMap) } - is JWildcardTypeName -> - if (!lowerBounds.isEmpty()) { - lowerBounds.first().boundsHasSelfReference(typeParametersMap) - } else { - upperBounds.first().boundsHasSelfReference(typeParametersMap) - } - is JArrayTypeName -> componentType.boundsHasSelfReference(typeParametersMap) - is JTypeVariableName -> { - if (bounds.isEmpty()) { - return false - } - if (!typeParametersMap.contains(this)) { - // Initialize the map to true so that we can catch cycles before a real value is assigned. - typeParametersMap.put(this, true) - typeParametersMap.put(this, bounds.any { it.boundsHasSelfReference(typeParametersMap) }) - } - typeParametersMap.get(this)!! - } - else -> error("Unexpected type name: $this") - } - } - // TODO(bcorso): Remove this workaround once @JvmStatic is added to XTypeName#getTypeVariableName @JvmStatic fun getTypeVariableName(name: String) = XTypeName.getTypeVariableName(name) } diff --git a/javatests/dagger/functional/kotlinsrc/multipackage/BoundedGenericsMultipackageTest.kt b/javatests/dagger/functional/kotlinsrc/multipackage/BoundedGenericsMultipackageTest.kt new file mode 100644 index 00000000000..5ab337ac9b8 --- /dev/null +++ b/javatests/dagger/functional/kotlinsrc/multipackage/BoundedGenericsMultipackageTest.kt @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2026 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 dagger.functional.kotlinsrc.multipackage + +import com.google.common.truth.Truth.assertThat +import dagger.Component +import dagger.functional.kotlinsrc.multipackage.a.BoundedSubcomponent +import dagger.functional.kotlinsrc.multipackage.a.UsesInaccessibleBoundedGenerics +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(JUnit4::class) +class BoundedGenericsMultipackageTest { + @Component + internal interface TestComponent { + fun usesBoundedGenerics(): UsesInaccessibleBoundedGenerics + + fun boundedSubcomponent(): BoundedSubcomponent + } + + @Test + fun internalCyclicAndIntersectionBoundsWorkAcrossModules() { + val component = DaggerBoundedGenericsMultipackageTest_TestComponent.create() + assertThat(component.usesBoundedGenerics().summary()).isEqualTo("bound:other:bound") + assertThat(component.boundedSubcomponent().boundedModuleValue()).isEqualTo("bound:other") + } +} diff --git a/javatests/dagger/functional/kotlinsrc/multipackage/a/InaccessibleBoundedGenerics.kt b/javatests/dagger/functional/kotlinsrc/multipackage/a/InaccessibleBoundedGenerics.kt new file mode 100644 index 00000000000..19d3563058b --- /dev/null +++ b/javatests/dagger/functional/kotlinsrc/multipackage/a/InaccessibleBoundedGenerics.kt @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2026 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 dagger.functional.kotlinsrc.multipackage.a + +import dagger.Module +import dagger.Provides +import dagger.Subcomponent +import javax.inject.Inject +import javax.inject.Qualifier + +internal interface InaccessibleBound> { + fun boundValue(): String +} + +internal interface OtherInaccessibleBound { + fun otherValue(): String +} + +internal class InaccessibleBoundImpl @Inject constructor() : + InaccessibleBound, OtherInaccessibleBound { + override fun boundValue(): String = "bound" + + override fun otherValue(): String = "other" +} + +// Intentionally combines constructor and member injection to test both _Factory and +// _MembersInjector generation on an internal generic class. +@Suppress("BadInject") +internal class InternalGenericInjectClass @Inject constructor(private val constructorDep: T) + where T : InaccessibleBound, T : OtherInaccessibleBound { + @Inject lateinit var fieldDep: T + private lateinit var methodDep: T + + @Inject + fun injectMethod(methodDep: T) { + this.methodDep = methodDep + } + + fun summary(): String = + "${constructorDep.boundValue()}:${fieldDep.otherValue()}:${methodDep.boundValue()}" +} + +@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class BoundedModuleValue + +@Module +internal abstract class InternalGenericBoundedModule + where T : InaccessibleBound, T : OtherInaccessibleBound { + @Provides + @BoundedModuleValue + fun provideBoundedValue(t: T): String = "${t.boundValue()}:${t.otherValue()}" +} + +@Module +internal class InternalConcreteBoundedModule : InternalGenericBoundedModule() + +@Subcomponent(modules = [InternalConcreteBoundedModule::class]) +interface BoundedSubcomponent { + @BoundedModuleValue fun boundedModuleValue(): String +} + +class UsesInaccessibleBoundedGenerics +@Inject +internal constructor(private val genericInject: InternalGenericInjectClass) { + fun summary(): String = genericInject.summary() +} diff --git a/javatests/dagger/functional/multipackage/BoundedGenericsMultipackageTest.java b/javatests/dagger/functional/multipackage/BoundedGenericsMultipackageTest.java new file mode 100644 index 00000000000..1845eb57704 --- /dev/null +++ b/javatests/dagger/functional/multipackage/BoundedGenericsMultipackageTest.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2026 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 dagger.functional.multipackage; + +import static com.google.common.truth.Truth.assertThat; + +import dagger.Component; +import dagger.functional.multipackage.a.InaccessibleBoundedGenerics.BoundedModuleValue; +import dagger.functional.multipackage.a.InaccessibleBoundedGenerics.ConcreteBoundedModule; +import dagger.functional.multipackage.a.InaccessibleBoundedGenerics.ConcreteMembersInjected; +import dagger.functional.multipackage.a.InaccessibleBoundedGenerics.UsesInaccessibleBoundedGenerics; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class BoundedGenericsMultipackageTest { + @Component(modules = ConcreteBoundedModule.class) + interface TestComponent { + UsesInaccessibleBoundedGenerics usesBoundedGenerics(); + + @BoundedModuleValue + String boundedModuleValue(); + + void inject(ConcreteMembersInjected target); + } + + @Test + public void inaccessibleCyclicAndIntersectionBoundsWorkAcrossPackages() { + TestComponent component = DaggerBoundedGenericsMultipackageTest_TestComponent.create(); + UsesInaccessibleBoundedGenerics usage = component.usesBoundedGenerics(); + assertThat(usage.publicGenericInject.summary()).isEqualTo("bound:other"); + assertThat(usage.packagePrivateSummary()).isEqualTo("bound:other:bound"); + assertThat(usage.moduleProvidedValue).isEqualTo("bound:other"); + assertThat(component.boundedModuleValue()).isEqualTo("bound:other"); + + ConcreteMembersInjected target = new ConcreteMembersInjected(); + component.inject(target); + assertThat(target.baseSummary()).isEqualTo("bound:other"); + } +} diff --git a/javatests/dagger/functional/multipackage/a/InaccessibleBoundedGenerics.java b/javatests/dagger/functional/multipackage/a/InaccessibleBoundedGenerics.java new file mode 100644 index 00000000000..ffc93facd1a --- /dev/null +++ b/javatests/dagger/functional/multipackage/a/InaccessibleBoundedGenerics.java @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2026 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 dagger.functional.multipackage.a; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import dagger.Module; +import dagger.Provides; +import java.lang.annotation.Retention; +import javax.inject.Inject; +import javax.inject.Qualifier; + +/** Types with package-private cyclic and intersection bounds for cross-package testing. */ +public final class InaccessibleBoundedGenerics { + interface InaccessibleBound> { + String boundValue(); + } + + interface OtherInaccessibleBound { + String otherValue(); + } + + static final class InaccessibleBoundImpl + implements InaccessibleBound, OtherInaccessibleBound { + @Inject + InaccessibleBoundImpl() {} + + @Override + public String boundValue() { + return "bound"; + } + + @Override + public String otherValue() { + return "other"; + } + } + + public static class GenericConstructorInjectClass< + T extends InaccessibleBound & OtherInaccessibleBound> { + final T constructorDep; + + @Inject + GenericConstructorInjectClass(T constructorDep) { + this.constructorDep = constructorDep; + } + + public String summary() { + return constructorDep.boundValue() + ":" + constructorDep.otherValue(); + } + } + + // Intentionally combines constructor and member injection to test both _Factory and + // _MembersInjector generation on a package-private generic class. + @SuppressWarnings("BadInject") + static class PackagePrivateGenericInjectClass< + T extends InaccessibleBound & OtherInaccessibleBound> { + final T constructorDep; + @Inject T fieldDep; + T methodDep; + + @Inject + PackagePrivateGenericInjectClass(T constructorDep) { + this.constructorDep = constructorDep; + } + + @Inject + void injectMethod(T methodDep) { + this.methodDep = methodDep; + } + + String summary() { + return constructorDep.boundValue() + + ":" + + fieldDep.otherValue() + + ":" + + methodDep.boundValue(); + } + } + + public static class GenericMembersInjectedBase< + T extends InaccessibleBound & OtherInaccessibleBound> { + @Inject T baseField; + T baseMethodDep; + + @Inject + void injectBaseMethod(T baseMethodDep) { + this.baseMethodDep = baseMethodDep; + } + + public String baseSummary() { + return baseField.boundValue() + ":" + baseMethodDep.otherValue(); + } + } + + public static final class ConcreteMembersInjected + extends GenericMembersInjectedBase {} + + @Qualifier + @Retention(RUNTIME) + public @interface BoundedModuleValue {} + + @Module + abstract static class GenericBoundedModule< + T extends InaccessibleBound & OtherInaccessibleBound> { + @Provides + @BoundedModuleValue + String provideBoundedValue(T t) { + return t.boundValue() + ":" + t.otherValue(); + } + } + + @Module + public static final class ConcreteBoundedModule + extends GenericBoundedModule {} + + public static final class UsesInaccessibleBoundedGenerics { + public final GenericConstructorInjectClass publicGenericInject; + private final PackagePrivateGenericInjectClass packagePrivateGenericInject; + public final String moduleProvidedValue; + + @Inject + UsesInaccessibleBoundedGenerics( + GenericConstructorInjectClass publicGenericInject, + PackagePrivateGenericInjectClass packagePrivateGenericInject, + @BoundedModuleValue String moduleProvidedValue) { + this.publicGenericInject = publicGenericInject; + this.packagePrivateGenericInject = packagePrivateGenericInject; + this.moduleProvidedValue = moduleProvidedValue; + } + + public String packagePrivateSummary() { + return packagePrivateGenericInject.summary(); + } + } + + private InaccessibleBoundedGenerics() {} +} diff --git a/javatests/dagger/functional/producers/GenericComponentTest.java b/javatests/dagger/functional/producers/GenericComponentTest.java index efc5bcf6cb1..91ce810704e 100644 --- a/javatests/dagger/functional/producers/GenericComponentTest.java +++ b/javatests/dagger/functional/producers/GenericComponentTest.java @@ -30,6 +30,7 @@ import java.util.Arrays; import java.util.List; import java.util.concurrent.Executor; +import javax.inject.Named; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -38,11 +39,19 @@ @RunWith(JUnit4.class) public final class GenericComponentTest { @ProductionComponent( - modules = {ExecutorModule.class, NongenericModule.class, ConcreteBoundedModule.class}) + modules = { + ExecutorModule.class, + NongenericModule.class, + ConcreteBoundedModule.class, + ConcretePackagePrivateBoundedModule.class + }) interface GenericComponent { ListenableFuture> list(); ListenableFuture> boundedList(); + + @Named("packagePrivateBounded") + ListenableFuture> packagePrivateBoundedList(); } public interface PublicBar { @@ -56,6 +65,27 @@ public String value() { } } + interface PackagePrivateBar> { + String value(); + } + + interface PackagePrivateBaz { + String otherValue(); + } + + static final class PackagePrivateFoo + implements PackagePrivateBar, PackagePrivateBaz { + @Override + public String value() { + return "inaccessible"; + } + + @Override + public String otherValue() { + return "intersection"; + } + } + @Module static final class ExecutorModule { @Provides @@ -100,6 +130,27 @@ List boundedList(T t) { } } + // Tests a generic base module with package-private intersection bounds (T extends + // PackagePrivateBar & PackagePrivateBaz). + @ProducerModule + static final class ConcretePackagePrivateBoundedModule + extends PackagePrivateBoundedGenericModule { + @Produces + static PackagePrivateFoo packagePrivateFoo() { + return new PackagePrivateFoo(); + } + } + + @ProducerModule + abstract static class PackagePrivateBoundedGenericModule< + T extends PackagePrivateBar & PackagePrivateBaz> { + @Produces + @Named("packagePrivateBounded") + List packagePrivateBoundedList(T t) { + return Arrays.asList(t.value() + ":" + t.otherValue()); + } + } + @Test public void compileTest() throws Exception { GenericComponent component = DaggerGenericComponentTest_GenericComponent.create(); @@ -108,5 +159,8 @@ public void compileTest() throws Exception { assertThat(component.boundedList().isDone()).isTrue(); assertThat(Futures.getDone(component.boundedList())).hasSize(1); assertThat(Futures.getDone(component.boundedList()).get(0).value()).isEqualTo("bounded"); + assertThat(component.packagePrivateBoundedList().isDone()).isTrue(); + assertThat(Futures.getDone(component.packagePrivateBoundedList())) + .containsExactly("inaccessible:intersection"); } } diff --git a/javatests/dagger/internal/codegen/InjectConstructorFactoryGeneratorTest.java b/javatests/dagger/internal/codegen/InjectConstructorFactoryGeneratorTest.java index 60afeb7dfd6..0a6871a130e 100644 --- a/javatests/dagger/internal/codegen/InjectConstructorFactoryGeneratorTest.java +++ b/javatests/dagger/internal/codegen/InjectConstructorFactoryGeneratorTest.java @@ -330,26 +330,11 @@ public void boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds() { "", "interface Bar {}"); daggerCompiler(component, usage, genericClass, foo, packagePrivateBar) - .compile( - subject -> { - if (compilerMode.isKotlinCodegenEnabled()) { - // TODO(b/438765237): Currently, this fails at the declaration of the factory - // (rather than the call site) because the internal Bar is exposed in the public - // factory declaration: "class GenericClass_Factory". - // See b/438765237 for details on how we can support this case in the future. - subject.hasErrorCount(1); - subject.hasErrorContaining( - "Bounds for type parameter, A, in class GenericClass must be " - + "publicly accessible."); - } else { - // Note: In this case, when calling the factory the component will use the requested - // type, Foo, e.g. "GenericClass_Factory.create()" since Foo is publicly - // accessible. It doesn't matter that the bound type, Bar, is package-private. - subject.hasErrorCount(0); - assertSourceMatchesGolden(subject, "test/GenericClass_Factory"); - subject.generatedSource(goldenFileRule.goldenSource("other/DaggerMyComponent")); - } - }); + .compile(subject -> { + subject.hasErrorCount(0); + assertSourceMatchesGolden(subject, "test/GenericClass_Factory"); + subject.generatedSource(goldenFileRule.goldenSource("other/DaggerMyComponent")); + }); } @Test @@ -405,9 +390,6 @@ public void boundedGenerics_withPackagePrivateTypeArgumentAndPublicBounds() { daggerCompiler(component, usage, genericClass, packagePrivateFoo, bar) .compile( subject -> { - // Note: In this case, the requested type is GenericClass, but when calling the - // factory with Kotlin codegen, the component will use the bound type, Bar, e.g. - // "GenericClass_Factory.create()" since Foo is not publicly accessible. subject.hasErrorCount(0); assertSourceMatchesGolden(subject, "test/GenericClass_Factory"); subject.generatedSource(goldenFileRule.goldenSource("other/DaggerMyComponent")); @@ -480,10 +462,6 @@ public void boundedGenerics_withPackagePrivateTypeArgumentAndNonCyclicRecursiveB daggerCompiler(component, usage, genericClass, packagePrivateFoo, bar, barModule) .compile( subject -> { - // Note: In this case, the requested type is GenericClass, but when - // calling the factory with Kotlin codegen, the component will use the type, - // "GenericClass_Factory.>create()" since Foo is not publicly - // accessible. subject.hasErrorCount(0); assertSourceMatchesGolden(subject, "test/GenericClass_Factory"); subject.generatedSource(goldenFileRule.goldenSource("other/DaggerMyComponent")); @@ -541,7 +519,7 @@ public void boundedGenerics_withPackagePrivateTypeArgumentAndPackagePrivateBound "", "interface Bar {}"); daggerCompiler(component, usage, genericClass, packagePrivateFoo, packagePrivateBar) - .compile(subject -> subject.hasErrorCount(0)); + .compile(subject -> subject.hasErrorCount(0)); } @Test @@ -601,7 +579,7 @@ public void boundedGenerics_withPackagePrivateTypeArgumentAndIntersectionBounds( "", "public interface Baz {}"); daggerCompiler(component, usage, genericClass, packagePrivateFoo, bar, baz) - .compile(subject -> subject.hasErrorCount(0)); + .compile(subject -> subject.hasErrorCount(0)); } @Test @@ -655,7 +633,7 @@ public void boundedGenerics_withPackagePrivateTypeArgumentAndCyclicRecursiveBoun "", "public interface Bar {}"); daggerCompiler(component, usage, genericClass, packagePrivateFoo, bar) - .compile(subject -> subject.hasErrorCount(0)); + .compile(subject -> subject.hasErrorCount(0)); } @Test diff --git a/javatests/dagger/internal/codegen/ModuleFactoryGeneratorTest.java b/javatests/dagger/internal/codegen/ModuleFactoryGeneratorTest.java index 0c02d2d7ce4..cb3e26676d6 100644 --- a/javatests/dagger/internal/codegen/ModuleFactoryGeneratorTest.java +++ b/javatests/dagger/internal/codegen/ModuleFactoryGeneratorTest.java @@ -1006,27 +1006,11 @@ public void parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds() "", "interface Bar {}"); daggerCompiler(component, usage, concreteModule, parameterizedModule, foo, packagePrivateBar) - .compile( - subject -> { - if (compilerMode.isKotlinCodegenEnabled()) { - // TODO(b/438765237): Currently, this fails at the declaration of the factory - // (rather than the call site) because the internal Bar is exposed in the public - // factory declaration: "class ParameterizedModule_ProvideListTFactory". - // See b/438765237 for details on how we can support this case in the future. - subject.hasErrorCount(1); - subject.hasErrorContaining( - "Bounds for type parameter, T, in class ParameterizedModule " - + "must be publicly accessible."); - } else { - // Note: In this case, when calling the factory the component will use the requested - // type, Foo, e.g. "ParameterizedModule_ProvideListTFactory.create()" since Foo - // is publicly accessible. It doesn't matter that the bound type, Bar, is - // package-private. - subject.hasErrorCount(0); - assertSourceMatchesGolden(subject, "test/ParameterizedModule_ProvideListTFactory"); - subject.generatedSource(goldenFileRule.goldenSource("other/DaggerMyComponent")); - } - }); + .compile(subject -> { + subject.hasErrorCount(0); + assertSourceMatchesGolden(subject, "test/ParameterizedModule_ProvideListTFactory"); + subject.generatedSource(goldenFileRule.goldenSource("other/DaggerMyComponent")); + }); } @Test @@ -1097,10 +1081,6 @@ public void parameterizedModule_withPackagePrivateTypeArgumentAndPublicBounds() daggerCompiler(component, usage, concreteModule, parameterizedModule, packagePrivateFoo, bar) .compile( subject -> { - // Note: In this case, the requested type is List, but when calling the factory - // with Kotlin codegen, the component will use the bound type, Bar, e.g. - // "ParameterizedModule_ProvideListTFactory.create()" since Foo is not publicly - // accessible. subject.hasErrorCount(0); assertSourceMatchesGolden(subject, "test/ParameterizedModule_ProvideListTFactory"); subject.generatedSource(goldenFileRule.goldenSource("other/DaggerMyComponent")); @@ -1175,10 +1155,6 @@ public void parameterizedModule_withPackagePrivateTypeArgumentAndNonCyclicRecurs daggerCompiler(component, usage, concreteModule, parameterizedModule, packagePrivateFoo, bar) .compile( subject -> { - // Note: In this case, the requested type is Map, but when calling the - // factory with Kotlin codegen, the component will use the type, - // "ParameterizedModule_ProvideMapFactory.>create()" since Foo is - // not publicly accessible. subject.hasErrorCount(0); assertSourceMatchesGolden(subject, "test/ParameterizedModule_ProvideMapFactory"); subject.generatedSource(goldenFileRule.goldenSource("other/DaggerMyComponent")); @@ -1250,8 +1226,14 @@ public void parameterizedModule_withPackagePrivateTypeArgumentAndPackagePrivateB "package test;", "", "interface Bar {}"); - daggerCompiler(component, usage, concreteModule, parameterizedModule, packagePrivateFoo, packagePrivateBar) - .compile(subject -> subject.hasErrorCount(0)); + daggerCompiler( + component, + usage, + concreteModule, + parameterizedModule, + packagePrivateFoo, + packagePrivateBar) + .compile(subject -> subject.hasErrorCount(0)); } @Test @@ -1327,7 +1309,7 @@ public void parameterizedModule_withPackagePrivateTypeArgumentAndIntersectionBou "public interface Baz {}"); daggerCompiler( component, usage, concreteModule, parameterizedModule, packagePrivateFoo, bar, baz) - .compile(subject -> subject.hasErrorCount(0)); + .compile(subject -> subject.hasErrorCount(0)); } @Test @@ -1396,7 +1378,7 @@ public void parameterizedModule_withPackagePrivateTypeArgumentAndCyclicRecursive "", "public interface Bar {}"); daggerCompiler(component, usage, concreteModule, parameterizedModule, packagePrivateFoo, bar) - .compile(subject -> subject.hasErrorCount(0)); + .compile(subject -> subject.hasErrorCount(0)); } @Test diff --git a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_DEFAULT_MODE b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_DEFAULT_MODE index c6204edfbab..b9c21f72097 100644 --- a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_DEFAULT_MODE +++ b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_DEFAULT_MODE @@ -2,7 +2,6 @@ package other; import dagger.internal.DaggerGenerated; import javax.annotation.processing.Generated; -import test.Foo; import test.Foo_Factory; import test.GenericClass_Factory; import test.Usage; @@ -39,20 +38,20 @@ final class DaggerMyComponent { } public MyComponent build() { - return new MyComponentImpl(); + return new ComponentImpl(); } } - private static final class MyComponentImpl implements MyComponent { - private final MyComponentImpl myComponentImpl = this; + private static final class ComponentImpl implements MyComponent { + private final ComponentImpl componentImpl = this; - MyComponentImpl() { + ComponentImpl() { } Object genericClassOfFoo() { - return GenericClass_Factory.newInstance(Foo_Factory.newInstance()); + return GenericClass_Factory.newInstance(Foo_Factory.newInstance()); } @Override diff --git a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_FAST_INIT_MODE b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_FAST_INIT_MODE index c6204edfbab..b9c21f72097 100644 --- a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_FAST_INIT_MODE +++ b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_FAST_INIT_MODE @@ -2,7 +2,6 @@ package other; import dagger.internal.DaggerGenerated; import javax.annotation.processing.Generated; -import test.Foo; import test.Foo_Factory; import test.GenericClass_Factory; import test.Usage; @@ -39,20 +38,20 @@ final class DaggerMyComponent { } public MyComponent build() { - return new MyComponentImpl(); + return new ComponentImpl(); } } - private static final class MyComponentImpl implements MyComponent { - private final MyComponentImpl myComponentImpl = this; + private static final class ComponentImpl implements MyComponent { + private final ComponentImpl componentImpl = this; - MyComponentImpl() { + ComponentImpl() { } Object genericClassOfFoo() { - return GenericClass_Factory.newInstance(Foo_Factory.newInstance()); + return GenericClass_Factory.newInstance(Foo_Factory.newInstance()); } @Override diff --git a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/test.GenericClass_Factory_KT_DEFAULT_MODE b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/test.GenericClass_Factory_KT_DEFAULT_MODE index da872b19b70..7b3bcd6a3b3 100644 --- a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/test.GenericClass_Factory_KT_DEFAULT_MODE +++ b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/test.GenericClass_Factory_KT_DEFAULT_MODE @@ -1,45 +1,44 @@ -package test; +package test -import dagger.internal.DaggerGenerated; -import dagger.internal.Factory; -import dagger.internal.Provider; -import dagger.internal.QualifierMetadata; -import dagger.internal.ScopeMetadata; -import javax.annotation.processing.Generated; +import dagger.`internal`.DaggerGenerated +import dagger.`internal`.Factory +import dagger.`internal`.Provider +import dagger.`internal`.QualifierMetadata +import dagger.`internal`.ScopeMetadata +import javax.`annotation`.processing.Generated +import kotlin.Any +import kotlin.Nothing +import kotlin.Suppress +import kotlin.jvm.JvmStatic @ScopeMetadata @QualifierMetadata @DaggerGenerated @Generated( - value = "dagger.internal.codegen.ComponentProcessor", - comments = "https://dagger.dev" + value = ["dagger.internal.codegen.ComponentProcessor"], + comments = "https://dagger.dev", ) -@SuppressWarnings({ - "unchecked", - "rawtypes", - "KotlinInternal", - "KotlinInternalInJava", - "cast", - "deprecation", - "nullness:initialization.field.uninitialized" -}) -public final class GenericClass_Factory implements Factory { - private final Provider aProvider; +@Suppress(names = [ + "UNCHECKED_CAST", + "USELESS_CAST", + "DEPRECATION" +]) +public class GenericClass_Factory : Factory { + private val aProvider: Provider<*> - private GenericClass_Factory(Provider aProvider) { - this.aProvider = aProvider; + private constructor(aProvider: Provider<*>) { + this.aProvider = aProvider } - @Override - public Object get() { - return GenericClass_Factory.newInstance(aProvider.get()); - } + public override fun `get`(): Any? = newInstance(aProvider.get()) - public static GenericClass_Factory create(Provider aProvider) { - return new GenericClass_Factory((Provider) (aProvider)); - } + public companion object { + @JvmStatic + public fun create(aProvider: Provider<*>): GenericClass_Factory = GenericClass_Factory((aProvider) as Provider<*>) + + @JvmStatic + public fun newInstance(a: Any?): Any? = newInstanceInternal(a) - public static Object newInstance(Object a) { - return new GenericClass((A) (a)); + private fun newInstanceInternal(a: Any?): Any? = GenericClass((a) as A) } } diff --git a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/test.GenericClass_Factory_KT_FAST_INIT_MODE b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/test.GenericClass_Factory_KT_FAST_INIT_MODE index da872b19b70..7b3bcd6a3b3 100644 --- a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/test.GenericClass_Factory_KT_FAST_INIT_MODE +++ b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest/boundedGenerics_withPublicTypeArgumentAndPackagePrivateBounds/test.GenericClass_Factory_KT_FAST_INIT_MODE @@ -1,45 +1,44 @@ -package test; +package test -import dagger.internal.DaggerGenerated; -import dagger.internal.Factory; -import dagger.internal.Provider; -import dagger.internal.QualifierMetadata; -import dagger.internal.ScopeMetadata; -import javax.annotation.processing.Generated; +import dagger.`internal`.DaggerGenerated +import dagger.`internal`.Factory +import dagger.`internal`.Provider +import dagger.`internal`.QualifierMetadata +import dagger.`internal`.ScopeMetadata +import javax.`annotation`.processing.Generated +import kotlin.Any +import kotlin.Nothing +import kotlin.Suppress +import kotlin.jvm.JvmStatic @ScopeMetadata @QualifierMetadata @DaggerGenerated @Generated( - value = "dagger.internal.codegen.ComponentProcessor", - comments = "https://dagger.dev" + value = ["dagger.internal.codegen.ComponentProcessor"], + comments = "https://dagger.dev", ) -@SuppressWarnings({ - "unchecked", - "rawtypes", - "KotlinInternal", - "KotlinInternalInJava", - "cast", - "deprecation", - "nullness:initialization.field.uninitialized" -}) -public final class GenericClass_Factory implements Factory { - private final Provider aProvider; +@Suppress(names = [ + "UNCHECKED_CAST", + "USELESS_CAST", + "DEPRECATION" +]) +public class GenericClass_Factory : Factory { + private val aProvider: Provider<*> - private GenericClass_Factory(Provider aProvider) { - this.aProvider = aProvider; + private constructor(aProvider: Provider<*>) { + this.aProvider = aProvider } - @Override - public Object get() { - return GenericClass_Factory.newInstance(aProvider.get()); - } + public override fun `get`(): Any? = newInstance(aProvider.get()) - public static GenericClass_Factory create(Provider aProvider) { - return new GenericClass_Factory((Provider) (aProvider)); - } + public companion object { + @JvmStatic + public fun create(aProvider: Provider<*>): GenericClass_Factory = GenericClass_Factory((aProvider) as Provider<*>) + + @JvmStatic + public fun newInstance(a: Any?): Any? = newInstanceInternal(a) - public static Object newInstance(Object a) { - return new GenericClass((A) (a)); + private fun newInstanceInternal(a: Any?): Any? = GenericClass((a) as A) } } diff --git a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_DEFAULT_MODE b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_DEFAULT_MODE index 6085cf83437..07d57c94c44 100644 --- a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_DEFAULT_MODE +++ b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_DEFAULT_MODE @@ -4,7 +4,6 @@ import dagger.internal.DaggerGenerated; import dagger.internal.Preconditions; import javax.annotation.processing.Generated; import test.ConcreteModule; -import test.Foo; import test.Foo_Factory; import test.ParameterizedModule_ProvideListTFactory; import test.Usage; @@ -51,22 +50,22 @@ final class DaggerMyComponent { if (concreteModule == null) { this.concreteModule = new ConcreteModule(); } - return new MyComponentImpl(concreteModule); + return new ComponentImpl(concreteModule); } } - private static final class MyComponentImpl implements MyComponent { + private static final class ComponentImpl implements MyComponent { private final ConcreteModule concreteModule; - private final MyComponentImpl myComponentImpl = this; + private final ComponentImpl componentImpl = this; - MyComponentImpl(ConcreteModule concreteModuleParam) { + ComponentImpl(ConcreteModule concreteModuleParam) { this.concreteModule = concreteModuleParam; } Object listOfFoo() { - return ParameterizedModule_ProvideListTFactory.provideListT(concreteModule, Foo_Factory.newInstance()); + return ParameterizedModule_ProvideListTFactory.provideListT(concreteModule, Foo_Factory.newInstance()); } @Override diff --git a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_FAST_INIT_MODE b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_FAST_INIT_MODE index 6085cf83437..07d57c94c44 100644 --- a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_FAST_INIT_MODE +++ b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/other.DaggerMyComponent_KT_FAST_INIT_MODE @@ -4,7 +4,6 @@ import dagger.internal.DaggerGenerated; import dagger.internal.Preconditions; import javax.annotation.processing.Generated; import test.ConcreteModule; -import test.Foo; import test.Foo_Factory; import test.ParameterizedModule_ProvideListTFactory; import test.Usage; @@ -51,22 +50,22 @@ final class DaggerMyComponent { if (concreteModule == null) { this.concreteModule = new ConcreteModule(); } - return new MyComponentImpl(concreteModule); + return new ComponentImpl(concreteModule); } } - private static final class MyComponentImpl implements MyComponent { + private static final class ComponentImpl implements MyComponent { private final ConcreteModule concreteModule; - private final MyComponentImpl myComponentImpl = this; + private final ComponentImpl componentImpl = this; - MyComponentImpl(ConcreteModule concreteModuleParam) { + ComponentImpl(ConcreteModule concreteModuleParam) { this.concreteModule = concreteModuleParam; } Object listOfFoo() { - return ParameterizedModule_ProvideListTFactory.provideListT(concreteModule, Foo_Factory.newInstance()); + return ParameterizedModule_ProvideListTFactory.provideListT(concreteModule, Foo_Factory.newInstance()); } @Override diff --git a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/test.ParameterizedModule_ProvideListTFactory_KT_DEFAULT_MODE b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/test.ParameterizedModule_ProvideListTFactory_KT_DEFAULT_MODE index 998612ffb83..038e63b5aa4 100644 --- a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/test.ParameterizedModule_ProvideListTFactory_KT_DEFAULT_MODE +++ b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/test.ParameterizedModule_ProvideListTFactory_KT_DEFAULT_MODE @@ -1,51 +1,48 @@ -package test; +package test -import dagger.internal.DaggerGenerated; -import dagger.internal.Factory; -import dagger.internal.Preconditions; -import dagger.internal.Provider; -import dagger.internal.QualifierMetadata; -import dagger.internal.ScopeMetadata; -import javax.annotation.processing.Generated; +import dagger.`internal`.DaggerGenerated +import dagger.`internal`.Factory +import dagger.`internal`.Preconditions +import dagger.`internal`.Provider +import dagger.`internal`.QualifierMetadata +import dagger.`internal`.ScopeMetadata +import javax.`annotation`.processing.Generated +import kotlin.Any +import kotlin.Nothing +import kotlin.Suppress +import kotlin.jvm.JvmStatic @ScopeMetadata @QualifierMetadata @DaggerGenerated @Generated( - value = "dagger.internal.codegen.ComponentProcessor", - comments = "https://dagger.dev" + value = ["dagger.internal.codegen.ComponentProcessor"], + comments = "https://dagger.dev", ) -@SuppressWarnings({ - "unchecked", - "rawtypes", - "KotlinInternal", - "KotlinInternalInJava", - "cast", - "deprecation", - "nullness:initialization.field.uninitialized" -}) -public final class ParameterizedModule_ProvideListTFactory implements Factory { - private final ParameterizedModule module; - - private final Provider tProvider; - - private ParameterizedModule_ProvideListTFactory(ParameterizedModule module, - Provider tProvider) { - this.module = module; - this.tProvider = tProvider; - } +@Suppress(names = [ + "UNCHECKED_CAST", + "USELESS_CAST", + "DEPRECATION" +]) +public class ParameterizedModule_ProvideListTFactory : Factory { + private val module: Any - @Override - public Object get() { - return ParameterizedModule_ProvideListTFactory.provideListT(module, tProvider.get()); - } + private val tProvider: Provider<*> - public static ParameterizedModule_ProvideListTFactory create(Object module, - Provider tProvider) { - return new ParameterizedModule_ProvideListTFactory((ParameterizedModule) (module), (Provider) (tProvider)); + private constructor(module: Any, tProvider: Provider<*>) { + this.module = module + this.tProvider = tProvider } - public static Object provideListT(Object instance, Object t) { - return Preconditions.checkNotNullFromProvides(((ParameterizedModule) (instance)).provideListT((T) (t))); + public override fun `get`(): Any? = provideListT(module, tProvider.get()) + + public companion object { + @JvmStatic + public fun create(module: Any?, tProvider: Provider<*>): ParameterizedModule_ProvideListTFactory = ParameterizedModule_ProvideListTFactory((module) as Any, (tProvider) as Provider<*>) + + @JvmStatic + public fun provideListT(instance: Any?, t: Any?): Any? = provideListTInternal(instance, t) + + private fun provideListTInternal(instance: Any?, t: Any?): Any? = Preconditions.checkNotNullFromProvides(((instance) as ParameterizedModule).provideListT((t) as T)) } } diff --git a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/test.ParameterizedModule_ProvideListTFactory_KT_FAST_INIT_MODE b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/test.ParameterizedModule_ProvideListTFactory_KT_FAST_INIT_MODE index 998612ffb83..038e63b5aa4 100644 --- a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/test.ParameterizedModule_ProvideListTFactory_KT_FAST_INIT_MODE +++ b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest/parameterizedModule_withPublicTypeArgumentAndPackagePrivateBounds/test.ParameterizedModule_ProvideListTFactory_KT_FAST_INIT_MODE @@ -1,51 +1,48 @@ -package test; +package test -import dagger.internal.DaggerGenerated; -import dagger.internal.Factory; -import dagger.internal.Preconditions; -import dagger.internal.Provider; -import dagger.internal.QualifierMetadata; -import dagger.internal.ScopeMetadata; -import javax.annotation.processing.Generated; +import dagger.`internal`.DaggerGenerated +import dagger.`internal`.Factory +import dagger.`internal`.Preconditions +import dagger.`internal`.Provider +import dagger.`internal`.QualifierMetadata +import dagger.`internal`.ScopeMetadata +import javax.`annotation`.processing.Generated +import kotlin.Any +import kotlin.Nothing +import kotlin.Suppress +import kotlin.jvm.JvmStatic @ScopeMetadata @QualifierMetadata @DaggerGenerated @Generated( - value = "dagger.internal.codegen.ComponentProcessor", - comments = "https://dagger.dev" + value = ["dagger.internal.codegen.ComponentProcessor"], + comments = "https://dagger.dev", ) -@SuppressWarnings({ - "unchecked", - "rawtypes", - "KotlinInternal", - "KotlinInternalInJava", - "cast", - "deprecation", - "nullness:initialization.field.uninitialized" -}) -public final class ParameterizedModule_ProvideListTFactory implements Factory { - private final ParameterizedModule module; - - private final Provider tProvider; - - private ParameterizedModule_ProvideListTFactory(ParameterizedModule module, - Provider tProvider) { - this.module = module; - this.tProvider = tProvider; - } +@Suppress(names = [ + "UNCHECKED_CAST", + "USELESS_CAST", + "DEPRECATION" +]) +public class ParameterizedModule_ProvideListTFactory : Factory { + private val module: Any - @Override - public Object get() { - return ParameterizedModule_ProvideListTFactory.provideListT(module, tProvider.get()); - } + private val tProvider: Provider<*> - public static ParameterizedModule_ProvideListTFactory create(Object module, - Provider tProvider) { - return new ParameterizedModule_ProvideListTFactory((ParameterizedModule) (module), (Provider) (tProvider)); + private constructor(module: Any, tProvider: Provider<*>) { + this.module = module + this.tProvider = tProvider } - public static Object provideListT(Object instance, Object t) { - return Preconditions.checkNotNullFromProvides(((ParameterizedModule) (instance)).provideListT((T) (t))); + public override fun `get`(): Any? = provideListT(module, tProvider.get()) + + public companion object { + @JvmStatic + public fun create(module: Any?, tProvider: Provider<*>): ParameterizedModule_ProvideListTFactory = ParameterizedModule_ProvideListTFactory((module) as Any, (tProvider) as Provider<*>) + + @JvmStatic + public fun provideListT(instance: Any?, t: Any?): Any? = provideListTInternal(instance, t) + + private fun provideListTInternal(instance: Any?, t: Any?): Any? = Preconditions.checkNotNullFromProvides(((instance) as ParameterizedModule).provideListT((t) as T)) } }