Skip to content

Commit

Permalink
fix: generic class resolution errors while using the @Autowired annot…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
JailedBird committed Aug 28, 2023
1 parent 411eee1 commit 3ba1460
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cn.jailedbird.arouter.ksp

import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route

interface TestInterface1
interface TestInterface2

abstract class AbstactGenericAcitivty1<T : TestInterface1> : AppCompatActivity() {
@Autowired
var list = mutableListOf<String>()
}

abstract class AbstactGenericAcitivty2<T : TestInterface1, G:TestInterface2> : AppCompatActivity() {
@Autowired
var list = mutableListOf<String>()
}

@Route(path = "/app/TestAbstactGenericAcitivty1")
class TestAbstactGenericAcitivty1 : AbstactGenericAcitivty1<TestInterface1>(){
@Autowired
var t = mutableListOf<String>()
}

@Route(path = "/app/TestAbstactGenericAcitivty2")
class TestAbstactGenericAcitivty2 : AbstactGenericAcitivty2<TestInterface1, TestInterface2>(){
@Autowired
var t = mutableListOf<String>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.*
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ksp.toClassName
import com.squareup.kotlinpoet.ksp.toTypeName
import com.squareup.kotlinpoet.ksp.toTypeParameterResolver
import com.squareup.kotlinpoet.ksp.writeTo

class AutowiredSymbolProcessorProvider : SymbolProcessorProvider {
Expand Down Expand Up @@ -117,11 +119,17 @@ class AutowiredSymbolProcessorProvider : SymbolProcessorProvider {
JSON_SERVICE_CLASS_NAME
)
val parentClassName = parent.toClassName()
/**
* [com.google.devtools.ksp.symbol.KSClassDeclaration.asStarProjectedType]
* If this is a generic class, return the type where the type argument is applied with star projection at use-site.
* */
val parentClassNameGeneric = parent.asStarProjectedType().toTypeName(parent.typeParameters.toTypeParameterResolver())

val message =
"The target that needs to be injected must be ${parentClassName.simpleName}, please check your code!"
injectMethodBuilder.addStatement(
"val substitute = (target as? %T)?: throw IllegalStateException(\n·%S\n·)",
parentClassName, message
parentClassNameGeneric, message
)

val parentRouteType = parent.routeType
Expand Down

0 comments on commit 3ba1460

Please sign in to comment.