Skip to content

Commit

Permalink
优化拦截器的构造器参数判断. 修复拦截器有参数无法构造的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojinzi123 committed Jan 1, 2025
1 parent 8bc1666 commit b24331a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ object RouterDegradeCache {
// 这里为什么使用 for 循环而不是直接获取空参数的构造函数或者以下有某个参数的构造函数
// 是因为你获取的时候会有异常抛出,三种情况你得 try{}catch{}三次
for (constructor in constructors) {
val parameterTypes = constructor.typeParameters
if (parameterTypes.isEmpty()) {
val parameterClassifiers = constructor.parameters.map { it.type.classifier }
if (parameterClassifiers.isEmpty()) {
return constructor.call()
}
if (parameterTypes.size == 1 && parameterTypes[0] == Application::class) {
return constructor.call(getApplication())
}
if (parameterTypes.size == 1 && parameterTypes[0] == Context::class.java) {
if (parameterClassifiers.size == 1 && (
parameterClassifiers[0] == Application::class ||
parameterClassifiers[0] == Context::class
)
) {
return constructor.call(getApplication())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ object RouterInterceptorCache {
// 这里为什么使用 for 循环而不是直接获取空参数的构造函数或者以下有某个参数的构造函数
// 是因为你获取的时候会有异常抛出,三种情况你得 try{}catch{}三次
for (constructor in constructors) {
val parameterTypes = constructor.typeParameters
if (parameterTypes.isEmpty()) {
val parameterClassifiers = constructor.parameters.map { it.type.classifier }
if (parameterClassifiers.isEmpty()) {
return constructor.call()
}
if (parameterTypes.size == 1 && parameterTypes[0] == Application::class) {
return constructor.call(getApplication())
}
if (parameterTypes.size == 1 && parameterTypes[0] == Context::class.java) {
if (parameterClassifiers.size == 1 && (
parameterClassifiers[0] == Application::class ||
parameterClassifiers[0] == Context::class
)
) {
return constructor.call(getApplication())
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ version-code = "1000"
version-name = "1.0"
agp = "8.7.2"
kotlin = "2.0.21"
kotlin-ksp = "2.0.21-1.0.26"
kotlin-ksp = "2.0.21-1.0.28"
kotlin-coroutines = "1.9.0"
rxjava = "2.2.21"
rxandroid = "2.1.1"
Expand Down

0 comments on commit b24331a

Please sign in to comment.