Repository: square/anvil (kotlin)

Issue:
Generated factories use wrong function name for calling setter members
Given this case:

```kotlin
class MemberInjectedClass {
  var inputs: Inputs
    get() = ...
    @Inject
    set(inputs) { ... }
}

// anvil generated
class MemberInjectedClass_Factory {
  override fun get(...): MemberInjectedClass {
    val instance = ...
    MemberInjectedClass_MembersInjector.injectInputs(instance, ...)
  }
}
```

This is wrong though and should use `MemberInjectedClass_MembersInjector.injectSetInputs(instance, ...)`, and thus won't compile. I'll send a PR

Pull request description:
Resolves #362

Patch:
diff --git a/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/AssistedInjectGenerator.kt b/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/AssistedInjectGenerator.kt
index e5dfb6609..c3e7c5e70 100644
--- a/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/AssistedInjectGenerator.kt
+++ b/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/AssistedInjectGenerator.kt
@@ -141,7 +141,8 @@ internal class AssistedInjectGenerator : PrivateCodeGenerator() {
                   clazz,
                   memberInjectParameters,
                   memberInjectProperties,
-                  instanceName
+                  instanceName,
+                  module
                 )
                 addStatement("return $instanceName")
               }
diff --git a/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/InjectConstructorFactoryGenerator.kt b/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/InjectConstructorFactoryGenerator.kt
index 6de5afae2..dcc136d13 100644
--- a/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/InjectConstructorFactoryGenerator.kt
+++ b/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/InjectConstructorFactoryGenerator.kt
@@ -136,7 +136,8 @@ internal class InjectConstructorFactoryGenerator : PrivateCodeGenerator() {
                   clazz,
                   memberInjectParameters,
                   memberInjectProperties,
-                  instanceName
+                  instanceName,
+                  module
                 )
                 addStatement("return $instanceName")
               }
diff --git a/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/MembersInjectionUtils.kt b/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/MembersInjectionUtils.kt
index b61d650f6..e29250df8 100644
--- a/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/MembersInjectionUtils.kt
+++ b/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/MembersInjectionUtils.kt
@@ -43,15 +43,15 @@ internal fun FunSpec.Builder.addMemberInjection(
   clazz: KtClassOrObject,
   memberInjectParameters: List<Parameter>,
   memberInjectProperties: List<KtProperty>,
-  instanceName: String
+  instanceName: String,
+  module: ModuleDescriptor
 ) {
   val memberInjectorClassName = "${clazz.generateClassName()}_MembersInjector"
   val memberInjectorClass = ClassName(packageName, memberInjectorClassName)
 
   memberInjectParameters.forEachIndexed { index, parameter ->
     val property = memberInjectProperties[index]
-    val propertyName = property.nameAsSafeName.asString()
-    val functionName = "inject${propertyName.capitalize()}"
+    val functionName = "inject${property.memberInjectionAccessName(module).capitalize()}"
 
     val param = when {
       parameter.isWrappedInProvider -> parameter.name
@@ -63,3 +63,13 @@ internal fun FunSpec.Builder.addMemberInjection(
     addStatement("%T.$functionName($instanceName, $param)", memberInjectorClass)
   }
 }
+
+/** Returns the access name for a property, which is prefixed with "set" if it's a setter. */
+internal fun KtProperty.memberInjectionAccessName(module: ModuleDescriptor): String {
+  val propertyName = nameAsSafeName.asString()
+  return if (isSetterInjected(module)) {
+    "set${propertyName.capitalize()}"
+  } else {
+    propertyName
+  }
+}
diff --git a/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/MembersInjectorGenerator.kt b/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/MembersInjectorGenerator.kt
index 726a1c3fd..c62cfb0b2 100644
--- a/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/MembersInjectorGenerator.kt
+++ b/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/MembersInjectorGenerator.kt
@@ -137,7 +137,6 @@ internal class MembersInjectorGenerator : PrivateCodeGenerator() {
               .apply {
                 parameters.forEachIndexed { index, parameter ->
                   val property = injectProperties[index]
-                  val propertyName = property.nameAsSafeName.asString()
 
                   val parameterString = when {
                     parameter.isWrappedInProvider -> parameter.name
@@ -145,11 +144,7 @@ internal class MembersInjectorGenerator : PrivateCodeGenerator() {
                       "$daggerDoubleCheckFqNameString.lazy(${parameter.name})"
                     else -> parameter.name + ".get()"
                   }
-                  val propertyRef = if (property.isSetterInjected(module)) {
-                    "set${propertyName.capitalize()}"
-                  } else {
-                    propertyName
-                  }
+                  val propertyRef = property.memberInjectionAccessName(module)
                   addStatement("inject${propertyRef.capitalize()}(instance, $parameterString)")
                 }
               }


Test patch:
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/MembersInjectorGeneratorTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/MembersInjectorGeneratorTest.kt
index d9c1a0cca..e862d8296 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/MembersInjectorGeneratorTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/MembersInjectorGeneratorTest.kt
@@ -166,7 +166,8 @@ public final class InjectClass_MembersInjector implements MembersInjector<Inject
       
       typealias StringList = List<String>
       
-      class InjectClass {
+      // Generate a factory too to cover for https://github.com/square/anvil/issues/362
+      class InjectClass @Inject constructor() {
         @Inject lateinit var string: String
         @Named("qualified") @Inject lateinit var qualifiedString: String
         @Inject lateinit var charSequence: CharSequence