Skip to content

Commit 6f167a3

Browse files
committed
完善ksp2判断逻辑
1 parent 0c88fae commit 6f167a3

2 files changed

Lines changed: 43 additions & 29 deletions

File tree

android-aop-apt/src/main/java/com/flyjingfish/android_aop_ksp/AndroidAopSymbolProcessor.kt

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import com.google.devtools.ksp.symbol.KSFunctionDeclaration
3232
import com.google.devtools.ksp.symbol.KSType
3333
import com.google.devtools.ksp.symbol.Modifier
3434
import com.google.devtools.ksp.symbol.Origin
35+
import com.google.devtools.ksp.symbol.Variance
3536
import com.google.devtools.ksp.validate
3637
import com.google.gson.Gson
3738
import com.squareup.kotlinpoet.AnnotationSpec
@@ -594,7 +595,7 @@ class AndroidAopSymbolProcessor(private val codeGenerator: CodeGenerator,
594595
var collectClassName = if (isClazz){
595596
if (symbol.origin == Origin.KOTLIN){
596597
val checkType = "${parameter.type.resolve()}"
597-
logger.error("=====$checkType")
598+
// logger.error("=====$checkType")
598599
if (regexIsEmpty){
599600
if (!checkKotlinType(checkType)){
600601
throw IllegalArgumentException("$exceptionHintPreText 的 参数的泛型设置的不对")
@@ -606,37 +607,50 @@ class AndroidAopSymbolProcessor(private val codeGenerator: CodeGenerator,
606607
}
607608
}else if (symbol.origin == Origin.JAVA){
608609
// logger.error("$exceptionHintPreText---${parameter.type}")
609-
val checkType = "${parameter.type}"
610-
logger.error("=====$checkType ==${parameter.type.resolve()}")
611-
val paramName = parameter.name?.asString() ?: "<anonymous>"
610+
// val checkType = "${parameter.type}"
612611
val typeRef = parameter.type
613612
val resolvedType = typeRef.resolve()
614613

615-
// 打印参数名和类型全名
616-
logger.error("Parameter: $paramName, type: ${resolvedType.declaration.qualifiedName?.asString()}")
617-
618-
// 获取泛型参数列表
619-
val typeArgs = resolvedType.arguments
620-
if (typeArgs.isEmpty()) {
621-
logger.error(" No generic parameters")
622-
} else {
623-
logger.error(" Generic parameters:")
624-
for ((index, arg) in typeArgs.withIndex()) {
625-
// arg 是 KSTypeArgument
626-
val argType = arg.type?.resolve()
627-
val argStr = argType?.declaration?.qualifiedName?.asString() ?: "StarProjection or Unknown"
628-
logger.error(" [$index]: $argStr")
629-
}
630-
}
631-
if (regexIsEmpty){
632-
if (!checkJavaType(checkType)){
633-
throw IllegalArgumentException("$exceptionJavaHintPreText 的 参数的泛型设置的不对")
614+
// logger.error("Parameter: $paramName, type: ${resolvedType.declaration.qualifiedName?.asString()}")
615+
if(resolvedType.arguments.size == 1){
616+
val arg = resolvedType.arguments[0]
617+
val variance = arg.variance
618+
// val type = arg.type?.resolve()
619+
620+
// val typeName = type?.declaration?.qualifiedName?.asString() ?: ""
621+
//
622+
// val varianceStr = when (variance) {
623+
// Variance.INVARIANT -> "invariant (T)"
624+
// Variance.COVARIANT -> "covariant (out T) -> ? extends"
625+
// Variance.CONTRAVARIANT -> "contravariant (in T) -> ? super"
626+
// else -> "unknown"
627+
// }
628+
// logger.error("$typeName, variance: $varianceStr")
629+
630+
if (regexIsEmpty){
631+
if (variance != Variance.COVARIANT){
632+
throw IllegalArgumentException("$exceptionJavaHintPreText 的 参数的泛型设置的不对")
633+
}
634+
}else{
635+
if (variance == Variance.CONTRAVARIANT){
636+
throw IllegalArgumentException("$exceptionJavaHintPreText 的 参数的泛型设置的不对")
637+
}
634638
}
639+
640+
635641
}else{
636-
if (checkJavaType1(checkType)){
637-
throw IllegalArgumentException("$exceptionJavaHintPreText 的 参数的泛型设置的不对")
638-
}
642+
throw IllegalArgumentException("$exceptionJavaHintPreText 的 参数的泛型设置的不对,必须设置1个泛型")
639643
}
644+
645+
// if (regexIsEmpty){
646+
// if (!checkJavaType(checkType)){
647+
// throw IllegalArgumentException("$exceptionJavaHintPreText 的 参数的泛型设置的不对")
648+
// }
649+
// }else{
650+
// if (checkJavaType1(checkType)){
651+
// throw IllegalArgumentException("$exceptionJavaHintPreText 的 参数的泛型设置的不对")
652+
// }
653+
// }
640654
}
641655
val element = parameter.type.element
642656
if (element != null){

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ buildscript {
99
plugins {
1010
id 'com.android.application' version '8.2.2' apply false
1111
id 'com.android.library' version '8.2.2' apply false
12-
id 'org.jetbrains.kotlin.android' version '2.1.20' apply false
13-
id 'org.jetbrains.kotlin.jvm' version '2.1.20' apply false
12+
id 'org.jetbrains.kotlin.android' version '1.9.23' apply false
13+
id 'org.jetbrains.kotlin.jvm' version '1.9.23' apply false
1414
id "com.vanniktech.maven.publish" version "0.33.0" apply false
15-
id 'com.google.devtools.ksp' version '2.1.20-2.0.0' apply false
15+
id 'com.google.devtools.ksp' version '1.9.23-1.0.20' apply false
1616
id 'io.github.flyjingfish.easyregister' version '1.1.0' apply false
1717
id 'com.gradle.plugin-publish' version '1.2.1' apply false
1818
// id "kotlinx-serialization" version "1.5.0"

0 commit comments

Comments
 (0)