Skip to content

Commit

Permalink
Handle cases of using class with generics for @BindingAdapter in bi…
Browse files Browse the repository at this point in the history
…nding-adapter-bridge

Support cases where `@BindingAdapter` is statically declared in a class that uses generic in its type params. This change fix code generation to not generate generic information in generated code generics for static access is invalid in Java/Kotlin.

Fixes #16
  • Loading branch information
arunsampathkumar-grabtaxi committed Jan 25, 2022
1 parent f94294d commit d6f8409
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ class BindingAdapterProcessor : BasicAnnotationProcessor(),
.addParameters(params.map { variableElement ->
ParameterSpec.get(variableElement)
})
.addStatement(buildStatement(methodName, params, returns), parentClass)
.addTypeVariables(typeParams.map { TypeVariableName.get(it) })
.addStatement(
buildStatement(methodName, params, returns),
buildParentClassName(parentClass)
).addTypeVariables(typeParams.map { TypeVariableName.get(it) })
.build()
} else null
}
Expand All @@ -99,6 +101,14 @@ class BindingAdapterProcessor : BasicAnnotationProcessor(),
return packageName.replace(".", "_") + GeneratedSuffix
}

private fun buildParentClassName(parentClass: Element): ClassName {
val packageElement = processingEnv.elementUtils.getPackageOf(parentClass)
return ClassName.get(
packageElement.qualifiedName.toString(),
parentClass.simpleName.toString() // Strip type params since we only access the class statically
)
}

private fun buildStatement(
methodName: String,
params: List<VariableElement>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ fun <T : Node> Node.genericsUpdateNode(node: T, value: Int) {
updateNode(node, value)
}

class CompanionTest<T> {
companion object Databinding {
@BindingAdapter(TEST_BIND)
@JvmStatic
fun <T : Node> companionUpdateNode(node: T, value: Int) {
updateNode(node, value)
}
}
}

class BindingAdapterProcessorTest {

@Test
Expand Down Expand Up @@ -158,6 +168,11 @@ class BindingAdapterProcessorTest {
assertNodeUpdate(com_grab_pax_binding_processor_Binding_Adapter_Stub::getUpdateNode)
}

@Test
fun `when binding adapters in named companion object, assert proxy forwards to Kotlin implementation`() {
assertNodeUpdate(com_grab_pax_binding_processor_Binding_Adapter_Stub::companionUpdateNode)
}

private fun assertNodeUpdate(updateFunction: (Node, Int) -> Unit) {
val node = Node()
val value = 10
Expand Down

0 comments on commit d6f8409

Please sign in to comment.