Skip to content

Commit c773222

Browse files
committed
完善路由
1 parent 2e77a35 commit c773222

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

lib-user/src/main/java/com/flyjingfish/user/DetailActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public class DetailActivity extends AppCompatActivity {
2727
private User user;
2828
@RouteParams(name = "userIds")
2929
private int[] userIds;
30-
// @RouteParams(name = "userList")
31-
// private ArrayList<User> userList;
30+
@RouteParams(name = "userList")
31+
private ArrayList<User> userList;
3232

3333
@Override
3434
protected void onCreate(@Nullable Bundle savedInstanceState) {

module-communication-ksp/src/main/java/com/flyjingfish/module_communication_ksp/CommunicationKspSymbolProcessor.kt

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class CommunicationKspSymbolProcessor(
248248
val classMapTypeNames = paramsClazz.toTypedArray()
249249
val paramListStr = "val paramsInfoList = mutableListOf<%T>()"
250250
// logger.error("paramsInfoStringBuilder=$paramsInfoStringBuilder")
251-
if (isSubtype(symbol,"android.app.Activity")){
251+
if (symbol.isSubtype("android.app.Activity")){
252252
val classFunName = "get${classKey}Class"
253253
val whatsMyName1 = whatsMyName("go$routeClassName")
254254
if (!emptyRoute){
@@ -344,7 +344,7 @@ class CommunicationKspSymbolProcessor(
344344
}
345345

346346
routeBuilder.addFunction(whatsMyName1.build())
347-
}else if (isSubtype(symbol,"androidx.fragment.app.Fragment") || isSubtype(symbol,"android.app.Fragment")){
347+
}else if (symbol.isSubtype("androidx.fragment.app.Fragment") || symbol.isSubtype("android.app.Fragment")){
348348
val classFunName = "new${classKey}"
349349
val anyClassName = ClassName.bestGuess(Any::class.qualifiedName!!)
350350
val whatsMyName2 = whatsMyName("new$routeClassName")
@@ -364,7 +364,7 @@ class CommunicationKspSymbolProcessor(
364364
symbol.toString()
365365
)
366366

367-
if (isSubtype(symbol,"androidx.fragment.app.Fragment")){
367+
if (symbol.isSubtype("androidx.fragment.app.Fragment")){
368368
activityBuilder.superclass(ClassName.bestGuess("androidx.fragment.app.Fragment"))
369369
}else{
370370
activityBuilder.superclass(ClassName.bestGuess("android.app.Fragment"))
@@ -395,7 +395,7 @@ class CommunicationKspSymbolProcessor(
395395
}
396396
}
397397

398-
if (isSubtype(symbol,"androidx.fragment.app.Fragment")){
398+
if (symbol.isSubtype("androidx.fragment.app.Fragment")){
399399
whatsMyName2.addStatement(
400400
"if (instance is %T) {"
401401
,ClassName.bestGuess(
@@ -476,17 +476,6 @@ class CommunicationKspSymbolProcessor(
476476
return symbols.filter { !it.validate() }.toList()
477477
}
478478

479-
private fun isSubtype(symbol: KSClassDeclaration,superType :String):Boolean{
480-
symbol.getAllSuperTypes().toList().forEach {
481-
val className = "${it.declaration.packageName.asString()}.${it}"
482-
if (className == superType){
483-
return true
484-
}
485-
// logger.error("symbol=${symbol},superTypes= ${it.declaration.packageName.asString()+"."+it}")
486-
}
487-
return false
488-
}
489-
490479
private val routeParamsMap = mutableMapOf<String,MutableMap<String,RouteParamsConfig>>()
491480
private fun processRouteParams(resolver: Resolver): List<KSAnnotated> {
492481
val symbols =
@@ -513,7 +502,7 @@ class CommunicationKspSymbolProcessor(
513502
map[key] = config
514503
// logger.error("annotationMap=$annotationMap")
515504
// logger.error("symbolLocation=$className${symbol}")
516-
// logger.error("symbolType=${symbol.type.resolve().declaration.packageName.asString()}.${symbol.type}")
505+
// logger.error("symbolType=${(symbol.type.resolve().declaration as KSClassDeclaration).superTypes}")
517506

518507
}
519508
return symbols.filter { !it.validate() }.toList()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.flyjingfish.module_communication_ksp
2+
3+
import com.google.devtools.ksp.getAllSuperTypes
4+
import com.google.devtools.ksp.symbol.KSClassDeclaration
5+
6+
fun KSClassDeclaration.isSubtype(superType :String):Boolean{
7+
getAllSuperTypes().toList().forEach {
8+
val className = "${it.declaration.packageName.asString()}.${it}"
9+
if (className == superType){
10+
return true
11+
}
12+
}
13+
return false
14+
}

module-communication-ksp/src/main/java/com/flyjingfish/module_communication_ksp/RouteParamsConfig.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.flyjingfish.module_communication_ksp
22

3+
import com.google.devtools.ksp.symbol.KSClassDeclaration
34
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
45
import com.squareup.kotlinpoet.ClassName
56
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
@@ -36,7 +37,15 @@ data class RouteParamsConfig(val className:String,val realClassName :String, val
3637
val subClazzName = type.resolve().declaration.toString()
3738
val typeClazzName = "$subPackageName.$subClazzName"
3839
val typeName = ClassName.bestGuess(this.realClassName)
39-
typeName.parameterizedBy(ClassName.bestGuess(typeClazzName))
40+
if (typeClazzName == "android.os.Parcelable"
41+
||typeClazzName == "kotlin.CharSequence"
42+
||typeClazzName == "kotlin.String"
43+
||typeClazzName == "java.lang.CharSequence"
44+
||typeClazzName == "java.lang.String"){
45+
typeName.parameterizedBy(ClassName.bestGuess(typeClazzName))
46+
}else{
47+
null
48+
}
4049
}else{
4150
null
4251
}
@@ -46,8 +55,15 @@ data class RouteParamsConfig(val className:String,val realClassName :String, val
4655

4756
}
4857
else ->{
58+
val isSubtype = (symbol.type.resolve().declaration as KSClassDeclaration).isSubtype("java.io.Serializable")
59+
||(symbol.type.resolve().declaration as KSClassDeclaration).isSubtype("android.os.Parcelable")
60+
4961
if (className == realClassName){
50-
ClassName.bestGuess(this.realClassName)
62+
if (isSubtype){
63+
ClassName.bestGuess(this.realClassName)
64+
}else{
65+
throw IllegalArgumentException("$realClassName 至少需要实现 java.io.Serializable 或 android.os.Parcelable 这两个接口中的一个")
66+
}
5167
}else{
5268
null
5369
}

0 commit comments

Comments
 (0)