Skip to content

Commit

Permalink
Merge pull request #359 from square/ralf/state
Browse files Browse the repository at this point in the history
Look for star imports before checking imports from the Kotlin package
  • Loading branch information
vRallev authored Aug 27, 2021
2 parents 219c2f3 + 0caece0 commit 695b84a
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ public fun PsiElement.requireFqName(
}
}

containingKtFile.importDirectives
.asSequence()
.filter { it.isAllUnder }
.mapNotNull {
// This fqName is everything in front of the star, e.g. for "import java.io.*" it
// returns "java.io".
it.importPath?.fqName
}
.forEach { importFqName ->
module.resolveFqNameOrNull(importFqName, classReference)
?.let { return it }
}

// If there is no import, then try to resolve the class with the same package as this file.
module.resolveFqNameOrNull(containingKtFile.packageFqName, classReference)
?.let { return it }
Expand All @@ -342,19 +355,6 @@ public fun PsiElement.requireFqName(
findFqNameInSuperTypes(module, classReference)
?.let { return it }

containingKtFile.importDirectives
.asSequence()
.filter { it.isAllUnder }
.mapNotNull {
// This fqName is the everything in front of the star, e.g. for "import java.io.*" it
// returns "java.io".
it.importPath?.fqName
}
.forEach { importFqName ->
module.resolveFqNameOrNull(importFqName, classReference)
?.let { return it }
}

// Check if it's a named import.
containingKtFile.importDirectives
.firstOrNull { classReference == it.importPath?.importedName?.asString() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,16 @@ public fun compileAnvil(
)
)

this.sources = sources.map { content ->
this.sources = sources.mapIndexed { index, content ->
val packageDir = content.lines()
.firstOrNull { it.trim().startsWith("package ") }
?.substringAfter("package ")
?.replace('.', '/')
?.let { "$it/" }
?: ""

val name = "${this.workingDir.absolutePath}/sources/src/main/java/$packageDir/Source.kt"
val name =
"${this.workingDir.absolutePath}/sources/src/main/java/$packageDir/Source$index.kt"
with(File(name).parentFile) {
check(exists() || mkdirs())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class InterfaceMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (6, 16)")
assertThat(messages).contains("Source0.kt: (6, 16)")
}
}

Expand Down Expand Up @@ -197,7 +197,7 @@ class InterfaceMergerTest(
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class. Unfortunately, a different error is reported that the class is
// missing an @Module annotation.
assertThat(messages).contains("Source.kt: (7, 7)")
assertThat(messages).contains("Source0.kt: (7, 7)")
}
}

Expand Down Expand Up @@ -225,7 +225,7 @@ class InterfaceMergerTest(
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class. Unfortunately, a different error is reported that the class is
// missing an @Module annotation.
assertThat(messages).contains("Source.kt: (13, 11)")
assertThat(messages).contains("Source0.kt: (13, 11)")
assertThat(messages).contains(
"com.squareup.test.SecondContributingInterface with scope kotlin.Any wants to replace " +
"com.squareup.test.ContributingInterface with scope kotlin.Unit. The replacement " +
Expand Down Expand Up @@ -313,7 +313,7 @@ class InterfaceMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (18, 11)")
assertThat(messages).contains("Source0.kt: (18, 11)")
assertThat(messages).contains(
"com.squareup.test.ComponentInterface with scope kotlin.Any wants to exclude " +
"com.squareup.test.ContributingInterface with scope kotlin.Unit. The exclusion " +
Expand Down Expand Up @@ -444,7 +444,7 @@ class InterfaceMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (7, ")
assertThat(messages).contains("Source0.kt: (7, ")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class MergeModulesTest {
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (7, 7)")
assertThat(messages).contains("Source0.kt: (7, 7)")
}
}

Expand Down Expand Up @@ -156,7 +156,7 @@ class MergeModulesTest {
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (7, 16)")
assertThat(messages).contains("Source0.kt: (7, 16)")
}
}

Expand Down Expand Up @@ -279,7 +279,7 @@ class MergeModulesTest {
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (17, 16)")
assertThat(messages).contains("Source0.kt: (17, 16)")
assertThat(messages).contains(
"com.squareup.test.DaggerModule2 with scope kotlin.Any wants to replace " +
"com.squareup.test.ContributingInterface with scope kotlin.Unit. The replacement " +
Expand Down Expand Up @@ -315,7 +315,7 @@ class MergeModulesTest {
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (17, 16)")
assertThat(messages).contains("Source0.kt: (17, 16)")
assertThat(messages).contains(
"com.squareup.test.DaggerModule2 with scope kotlin.Any wants to replace " +
"com.squareup.test.ContributingInterface with scope kotlin.Unit. The replacement " +
Expand Down Expand Up @@ -411,7 +411,7 @@ class MergeModulesTest {
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (17, 11)")
assertThat(messages).contains("Source0.kt: (17, 11)")
assertThat(messages).contains(
"com.squareup.test.ContributingInterface with scope kotlin.Any wants to replace " +
"com.squareup.test.DaggerModule2 with scope kotlin.Unit. The replacement must use " +
Expand Down Expand Up @@ -447,7 +447,7 @@ class MergeModulesTest {
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (17, 11)")
assertThat(messages).contains("Source0.kt: (17, 11)")
assertThat(messages).contains(
"com.squareup.test.ContributingInterface with scope kotlin.Any wants to replace " +
"com.squareup.test.DaggerModule2 with scope kotlin.Unit. The replacement must use " +
Expand Down Expand Up @@ -479,7 +479,7 @@ class MergeModulesTest {
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (13, 16)")
assertThat(messages).contains("Source0.kt: (13, 16)")
}
}

Expand Down Expand Up @@ -508,7 +508,7 @@ class MergeModulesTest {
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (15, 16)")
assertThat(messages).contains("Source0.kt: (15, 16)")
assertThat(messages).contains(
"com.squareup.test.DaggerModule2 with scope kotlin.Any wants to replace " +
"com.squareup.test.DaggerModule3 with scope kotlin.Unit. The replacement must use " +
Expand Down Expand Up @@ -603,7 +603,7 @@ class MergeModulesTest {
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (16, 7)")
assertThat(messages).contains("Source0.kt: (16, 7)")
assertThat(messages).contains(
"com.squareup.test.DaggerModule1 with scope kotlin.Any wants to exclude " +
"com.squareup.test.DaggerModule2 with scope kotlin.Unit. The exclusion must " +
Expand Down Expand Up @@ -688,7 +688,7 @@ class MergeModulesTest {
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (17, 11)")
assertThat(messages).contains("Source0.kt: (17, 11)")
assertThat(messages).contains(
"com.squareup.test.ComponentInterface with scope kotlin.Any wants to exclude " +
"com.squareup.test.ContributingInterface with scope kotlin.Unit. The exclusion " +
Expand Down Expand Up @@ -721,7 +721,7 @@ class MergeModulesTest {
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (17, 11)")
assertThat(messages).contains("Source0.kt: (17, 11)")
assertThat(messages).contains(
"com.squareup.test.ComponentInterface with scope kotlin.Any wants to exclude " +
"com.squareup.test.ContributingInterface with scope kotlin.Unit. The exclusion " +
Expand Down Expand Up @@ -785,7 +785,7 @@ class MergeModulesTest {
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (8, ")
assertThat(messages).contains("Source0.kt: (8, ")
}
}
}
Expand Down Expand Up @@ -881,7 +881,7 @@ class MergeModulesTest {
"""
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
assertThat(messages).contains("Source.kt: (19, 11)")
assertThat(messages).contains("Source0.kt: (19, 11)")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class ModuleMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (7, 11)")
assertThat(messages).contains("Source0.kt: (7, 11)")
}
}

Expand Down Expand Up @@ -214,7 +214,7 @@ class ModuleMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (7, 16)")
assertThat(messages).contains("Source0.kt: (7, 16)")
}
}

Expand Down Expand Up @@ -337,7 +337,7 @@ class ModuleMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (17, 16)")
assertThat(messages).contains("Source0.kt: (17, 16)")
assertThat(messages).contains(
"com.squareup.test.DaggerModule1 with scope kotlin.Any wants to replace " +
"com.squareup.test.ContributingInterface with scope kotlin.Unit. The replacement " +
Expand Down Expand Up @@ -373,7 +373,7 @@ class ModuleMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (17, 16)")
assertThat(messages).contains("Source0.kt: (17, 16)")
assertThat(messages).contains(
"com.squareup.test.DaggerModule1 with scope kotlin.Any wants to replace " +
"com.squareup.test.ContributingInterface with scope kotlin.Unit. The replacement " +
Expand Down Expand Up @@ -469,7 +469,7 @@ class ModuleMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (17, 11)")
assertThat(messages).contains("Source0.kt: (17, 11)")
assertThat(messages).contains(
"com.squareup.test.ContributingInterface with scope kotlin.Any wants to replace " +
"com.squareup.test.DaggerModule1 with scope kotlin.Unit. The replacement must use " +
Expand Down Expand Up @@ -505,7 +505,7 @@ class ModuleMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (17, 11)")
assertThat(messages).contains("Source0.kt: (17, 11)")
assertThat(messages).contains(
"com.squareup.test.ContributingInterface with scope kotlin.Any wants to replace " +
"com.squareup.test.DaggerModule1 with scope kotlin.Unit. The replacement must use " +
Expand Down Expand Up @@ -537,7 +537,7 @@ class ModuleMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (13, 16)")
assertThat(messages).contains("Source0.kt: (13, 16)")
}
}

Expand Down Expand Up @@ -566,7 +566,7 @@ class ModuleMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (15, 16)")
assertThat(messages).contains("Source0.kt: (15, 16)")
assertThat(messages).contains(
"com.squareup.test.DaggerModule2 with scope kotlin.Any wants to replace " +
"com.squareup.test.DaggerModule1 with scope kotlin.Unit. The replacement must " +
Expand Down Expand Up @@ -666,7 +666,7 @@ class ModuleMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (20, 11)")
assertThat(messages).contains("Source0.kt: (20, 11)")
assertThat(messages).contains(
"com.squareup.test.ComponentInterface with scope kotlin.Any wants to exclude " +
"com.squareup.test.DaggerModule1 with scope kotlin.Unit. The exclusion must use " +
Expand Down Expand Up @@ -816,7 +816,7 @@ class ModuleMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (17, 11)")
assertThat(messages).contains("Source0.kt: (17, 11)")
assertThat(messages).contains(
"com.squareup.test.ComponentInterface with scope kotlin.Any wants to exclude " +
"com.squareup.test.ContributingInterface with scope kotlin.Unit. The exclusion " +
Expand Down Expand Up @@ -849,7 +849,7 @@ class ModuleMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (17, 11)")
assertThat(messages).contains("Source0.kt: (17, 11)")
assertThat(messages).contains(
"com.squareup.test.ComponentInterface with scope kotlin.Any wants to exclude " +
"com.squareup.test.ContributingInterface with scope kotlin.Unit. The exclusion " +
Expand Down Expand Up @@ -946,7 +946,7 @@ class ModuleMergerTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (8, ")
assertThat(messages).contains("Source0.kt: (8, ")
}
}
}
Expand Down Expand Up @@ -1067,7 +1067,7 @@ class ModuleMergerTest(
"""
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
assertThat(messages).contains("Source.kt: (19, 11)")
assertThat(messages).contains("Source0.kt: (19, 11)")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ class BindingModuleGeneratorTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)

assertThat(messages).contains("Source.kt: (6, 11)")
assertThat(messages).contains("Source0.kt: (6, 11)")
assertThat(messages).contains(
"Binding com.squareup.test.ParentInterface contains type parameters(s)" +
" <Map<String, List<Pair<String, Int>>>, SomeOtherType>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class BindingModuleMultibindingSetTest(
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)

assertThat(messages).contains("Source.kt: (6, 11)")
assertThat(messages).contains("Source0.kt: (6, 11)")
assertThat(messages).contains(
"Binding com.squareup.test.ParentInterface contains type parameters(s)" +
" <Map<String, List<Pair<String, Int>>>, SomeOtherType>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class ContributesBindingGeneratorTest {
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (8, ")
assertThat(messages).contains("Source0.kt: (8, ")
assertThat(messages).contains(
"com.squareup.test.ContributingInterface is binding a type, but the class is not " +
"public. Only public types are supported."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class ContributesMultibindingGeneratorTest {
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
// Position to the class.
assertThat(messages).contains("Source.kt: (8, ")
assertThat(messages).contains("Source0.kt: (8, ")
assertThat(messages).contains(
"com.squareup.test.ContributingInterface is binding a type, but the class is not " +
"public. Only public types are supported."
Expand Down
Loading

0 comments on commit 695b84a

Please sign in to comment.