Skip to content

Commit

Permalink
Merge pull request #2 from ltttttttttttt/dev
Browse files Browse the repository at this point in the history
修复typealias类型的泛型bug
  • Loading branch information
ltttttttttttt authored May 15, 2023
2 parents e3aeb5a + fcf69f0 commit 4241e01
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions LazyPeopleHttp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 19 additions & 6 deletions LazyPeopleHttp/src/jvmMain/kotlin/com/lt/lazy_people_http/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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
Expand All @@ -75,9 +76,9 @@ internal fun getKSTypeArguments(ks: KSTypeReference): List<String> {
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 {
Expand All @@ -87,9 +88,9 @@ internal fun getKSTypeArguments(ks: KSTypeReference): List<String> {
}

/**
* 通过[KSAnnotation]获取还原这个注解的String
* 通过[KSAnnotation]获取还原(构造)这个注解的String
*/
fun getNewAnnotationString(ksa: KSAnnotation): String {
internal fun getNewAnnotationString(ksa: KSAnnotation): String {
val ksType = ksa.annotationType.resolve()
//完整type字符串
val typeName =
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"//此库的版本
const val mVersion = "1.0.8"//此库的版本

0 comments on commit 4241e01

Please sign in to comment.