Repository: square/anvil (kotlin)

Issue:
Support named import
The following class is processed successfully by Dagger compiler but not by Anvil
```
package com.squareup.test

import dagger.Module
import dagger.Provides
import java.lang.Runnable as NamedRunnable
        
@Module
object DaggerModule1 {
    @Provides fun provideRunner(): NamedRunnable = NamedRunnable {}
}
```

Pull request description:
Fixes #115

Patch:
diff --git a/compiler/src/main/java/com/squareup/anvil/compiler/codegen/PsiUtils.kt b/compiler/src/main/java/com/squareup/anvil/compiler/codegen/PsiUtils.kt
index dd590faaf..cbe7b68a2 100644
--- a/compiler/src/main/java/com/squareup/anvil/compiler/codegen/PsiUtils.kt
+++ b/compiler/src/main/java/com/squareup/anvil/compiler/codegen/PsiUtils.kt
@@ -278,6 +278,12 @@ internal fun PsiElement.requireFqName(
         module.findClassOrTypeAlias(importFqName, classReference)?.let { return it.fqNameSafe }
       }
 
+  // Check if it's a named import.
+  containingKtFile.importDirectives
+    .firstOrNull { classReference == it.importPath?.importedName?.asString() }
+    ?.importedFqName
+    ?.let { return it }
+
   // Everything else isn't supported.
   throw AnvilCompilationException(
       "Couldn't resolve FqName $classReference for Psi element: $text",


Test patch:
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/ProvidesMethodFactoryGeneratorTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/ProvidesMethodFactoryGeneratorTest.kt
index fe31178db..b0b8ca615 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/ProvidesMethodFactoryGeneratorTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/ProvidesMethodFactoryGeneratorTest.kt
@@ -2226,6 +2226,29 @@ public final class DaggerComponentInterface implements ComponentInterface {
     }
   }
 
+  @Test
+  fun `a factory class is generated for a method returning a class with a named import`() {
+    compile(
+      """
+        package com.squareup.test
+        
+        import dagger.Module
+        import dagger.Provides
+        import java.lang.Runnable as NamedRunnable
+        
+        @Module
+        object DaggerModule1 {
+          @Provides fun provideRunner(): NamedRunnable = NamedRunnable {}
+        }
+        """
+    ) {
+      val factoryClass = daggerModule1.moduleFactoryClass("provideRunner")
+
+      val constructor = factoryClass.declaredConstructors.single()
+      assertThat(constructor.parameterTypes.toList()).isEmpty()
+    }
+  }
+
   private fun compile(
     source: String,
     block: Result.() -> Unit = { }