From fcf69f0eafabd04e5418e9d438a1591da4a88791 Mon Sep 17 00:00:00 2001 From: lt Date: Mon, 15 May 2023 14:36:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dtypealias=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E7=9A=84=E6=B3=9B=E5=9E=8Bbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LazyPeopleHttp/build.gradle.kts | 1 + .../kotlin/com/lt/lazy_people_http/Util.kt | 25 ++++++++++++++----- buildSrc/src/main/kotlin/Versions.kt | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/LazyPeopleHttp/build.gradle.kts b/LazyPeopleHttp/build.gradle.kts index c24b197..d1dd999 100644 --- a/LazyPeopleHttp/build.gradle.kts +++ b/LazyPeopleHttp/build.gradle.kts @@ -24,6 +24,7 @@ kotlin { dependencies { implementation("com.google.devtools.ksp:symbol-processing-api:$kspVersion") implementation(project(":LazyPeopleHttp-lib")) + implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlinVersion") } } val jvmTest by getting diff --git a/LazyPeopleHttp/src/jvmMain/kotlin/com/lt/lazy_people_http/Util.kt b/LazyPeopleHttp/src/jvmMain/kotlin/com/lt/lazy_people_http/Util.kt index 66f85e4..55146c0 100644 --- a/LazyPeopleHttp/src/jvmMain/kotlin/com/lt/lazy_people_http/Util.kt +++ b/LazyPeopleHttp/src/jvmMain/kotlin/com/lt/lazy_people_http/Util.kt @@ -6,6 +6,8 @@ import com.google.devtools.ksp.symbol.KSTypeAlias import com.google.devtools.ksp.symbol.KSTypeReference import com.google.devtools.ksp.symbol.Nullability import com.lt.lazy_people_http.options.KSTypeInfo +import org.jetbrains.kotlin.js.descriptorUtils.getKotlinTypeFqName +import org.jetbrains.kotlin.types.KotlinType import java.io.OutputStream /** @@ -62,7 +64,6 @@ internal fun getKSTypeInfo(ks: KSTypeReference): KSTypeInfo { private val ksTypeImplClass = Class.forName("com.google.devtools.ksp.symbol.impl.kotlin.KSTypeImpl") -private val kotlinTypeClass = Class.forName("org.jetbrains.kotlin.types.KotlinType") /** * 获取ksType的完整子泛型信息列表,返回可直接使用的String @@ -75,9 +76,9 @@ internal fun getKSTypeArguments(ks: KSTypeReference): List { val ksType = ks.resolve() //如果是typealias类型 return if (ksType.declaration is KSTypeAlias) { - val kotlinType = ksTypeImplClass.getMethod("getKotlinType").invoke(ksType) - (kotlinTypeClass.getMethod("getArguments").invoke(kotlinType) as List<*>).map { - it.toString() + val kotlinType = ksTypeImplClass.getMethod("getKotlinType").invoke(ksType) as KotlinType + kotlinType.arguments.map { + getKotlinTypeInfo(it.type) } } else { ks.element!!.typeArguments.map { @@ -87,9 +88,9 @@ internal fun getKSTypeArguments(ks: KSTypeReference): List { } /** - * 通过[KSAnnotation]获取还原这个注解的String + * 通过[KSAnnotation]获取还原(构造)这个注解的String */ -fun getNewAnnotationString(ksa: KSAnnotation): String { +internal fun getNewAnnotationString(ksa: KSAnnotation): String { val ksType = ksa.annotationType.resolve() //完整type字符串 val typeName = @@ -104,4 +105,16 @@ fun getNewAnnotationString(ksa: KSAnnotation): String { .append("\", ") } return "$typeName($args)" +} + +//通过[KotlinType]获取完整的泛型信息 +private fun getKotlinTypeInfo(type: KotlinType): String { + val typeString = type.getKotlinTypeFqName(false) + val arguments = type.arguments + if (arguments.isEmpty()) + return typeString + val argTypeString = arguments.joinToString(prefix = "<", postfix = ">") { + getKotlinTypeInfo(it.type) + } + return typeString + argTypeString } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 9d42e86..be6e959 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -20,4 +20,4 @@ const val ktorVersion = "2.2.4"//ktor版本 const val serializationJsonVersion = "1.5.0"//json序列号版本 const val kspVersion = "$kotlinVersion-1.0.10"//ksp版本 -const val mVersion = "1.0.7"//此库的版本 \ No newline at end of file +const val mVersion = "1.0.8"//此库的版本 \ No newline at end of file