Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<JTypeName, Boolean>
): 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)
}
Original file line number Diff line number Diff line change
@@ -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")
}
}
Original file line number Diff line number Diff line change
@@ -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<T : InaccessibleBound<T>> {
fun boundValue(): String
}

internal interface OtherInaccessibleBound {
fun otherValue(): String
}

internal class InaccessibleBoundImpl @Inject constructor() :
InaccessibleBound<InaccessibleBoundImpl>, 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<T> @Inject constructor(private val constructorDep: T)
where T : InaccessibleBound<T>, 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<T>
where T : InaccessibleBound<T>, T : OtherInaccessibleBound {
@Provides
@BoundedModuleValue
fun provideBoundedValue(t: T): String = "${t.boundValue()}:${t.otherValue()}"
}

@Module
internal class InternalConcreteBoundedModule : InternalGenericBoundedModule<InaccessibleBoundImpl>()

@Subcomponent(modules = [InternalConcreteBoundedModule::class])
interface BoundedSubcomponent {
@BoundedModuleValue fun boundedModuleValue(): String
}

class UsesInaccessibleBoundedGenerics
@Inject
internal constructor(private val genericInject: InternalGenericInjectClass<InaccessibleBoundImpl>) {
fun summary(): String = genericInject.summary()
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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<T extends InaccessibleBound<T>> {
String boundValue();
}

interface OtherInaccessibleBound {
String otherValue();
}

static final class InaccessibleBoundImpl
implements InaccessibleBound<InaccessibleBoundImpl>, OtherInaccessibleBound {
@Inject
InaccessibleBoundImpl() {}

@Override
public String boundValue() {
return "bound";
}

@Override
public String otherValue() {
return "other";
}
}

public static class GenericConstructorInjectClass<
T extends InaccessibleBound<T> & 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<T> & 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<T> & 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<InaccessibleBoundImpl> {}

@Qualifier
@Retention(RUNTIME)
public @interface BoundedModuleValue {}

@Module
abstract static class GenericBoundedModule<
T extends InaccessibleBound<T> & OtherInaccessibleBound> {
@Provides
@BoundedModuleValue
String provideBoundedValue(T t) {
return t.boundValue() + ":" + t.otherValue();
}
}

@Module
public static final class ConcreteBoundedModule
extends GenericBoundedModule<InaccessibleBoundImpl> {}

public static final class UsesInaccessibleBoundedGenerics {
public final GenericConstructorInjectClass<?> publicGenericInject;
private final PackagePrivateGenericInjectClass<?> packagePrivateGenericInject;
public final String moduleProvidedValue;

@Inject
UsesInaccessibleBoundedGenerics(
GenericConstructorInjectClass<InaccessibleBoundImpl> publicGenericInject,
PackagePrivateGenericInjectClass<InaccessibleBoundImpl> packagePrivateGenericInject,
@BoundedModuleValue String moduleProvidedValue) {
this.publicGenericInject = publicGenericInject;
this.packagePrivateGenericInject = packagePrivateGenericInject;
this.moduleProvidedValue = moduleProvidedValue;
}

public String packagePrivateSummary() {
return packagePrivateGenericInject.summary();
}
}

private InaccessibleBoundedGenerics() {}
}
Loading
Loading