Skip to content

Commit

Permalink
Support contributing types with Any as boundType.
Browse files Browse the repository at this point in the history
Fixes #619
  • Loading branch information
vRallev committed Jul 25, 2022
1 parent 2c5b3be commit 39fefe9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ internal val assistedInjectFqName = AssistedInject::class.fqName
internal val providerFqName = Provider::class.fqName
internal val jvmSuppressWildcardsFqName = JvmSuppressWildcards::class.fqName
internal val publishedApiFqName = PublishedApi::class.fqName
internal val anyFqName = Any::class.fqName

internal val daggerDoubleCheckFqNameString = DoubleCheck::class.java.canonicalName

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.squareup.anvil.compiler.codegen

import com.squareup.anvil.compiler.anyFqName
import com.squareup.anvil.compiler.assistedInjectFqName
import com.squareup.anvil.compiler.contributesMultibindingFqName
import com.squareup.anvil.compiler.injectFqName
Expand Down Expand Up @@ -81,6 +82,10 @@ internal fun ClassReference.checkClassExtendsBoundType(
classReference = this
)

// The boundType is declared explicitly in the annotation. Since all classes extend Any, we can
// stop here.
if (boundType.fqName == anyFqName) return

if (allSuperTypeClassReferences().none { it.fqName == boundType.fqName }) {
throw AnvilCompilationExceptionClassReference(
message = "${this.fqName} contributes a binding for ${boundType.fqName}, but doesn't " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,22 @@ class ContributesBindingGeneratorTest {
}
}

@Test fun `the contributed binding class can extend Any explicitly`() {
compile(
"""
package com.squareup.test
import com.squareup.anvil.annotations.ContributesBinding
@ContributesBinding(Int::class, boundType = Any::class)
interface ContributingInterface
"""
) {
assertThat(contributingInterface.hintBinding?.java).isEqualTo(contributingInterface)
assertThat(contributingInterface.hintBindingScope).isEqualTo(Int::class)
}
}

@Test fun `there are multiple hints for multiple contributed bindings`() {
assumeIrBackend()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,22 @@ class ContributesMultibindingGeneratorTest {
}
}

@Test fun `the contributed multibinding class can extend Any explicitly`() {
compile(
"""
package com.squareup.test
import com.squareup.anvil.annotations.ContributesMultibinding
@ContributesMultibinding(Int::class, boundType = Any::class)
interface ContributingInterface
"""
) {
assertThat(contributingInterface.hintMultibinding?.java).isEqualTo(contributingInterface)
assertThat(contributingInterface.hintMultibindingScope).isEqualTo(Int::class)
}
}

@Test fun `a contributed multibinding can be generated`() {
val codeGenerator = simpleCodeGenerator { clazz ->
clazz
Expand Down

0 comments on commit 39fefe9

Please sign in to comment.