diff --git a/Buff/src/jvmMain/kotlin/com/lt/buff/options/FunctionFieldsInfo.kt b/Buff/src/jvmMain/kotlin/com/lt/buff/options/FunctionFieldsInfo.kt index d6384cc..9951361 100644 --- a/Buff/src/jvmMain/kotlin/com/lt/buff/options/FunctionFieldsInfo.kt +++ b/Buff/src/jvmMain/kotlin/com/lt/buff/options/FunctionFieldsInfo.kt @@ -10,5 +10,11 @@ internal class FunctionFieldsInfo( val isInTheConstructor: Boolean, val isBuffBean: Boolean, val nullable: String, - val isList: Boolean = false, -) \ No newline at end of file + val isList: Boolean, + val typeString: String, +) { + /** + * 获取addBuff或removeBuff的后缀,用于区分type是否可空 + */ + fun getBuffSuffix(): String = if (isList && typeString.endsWith("?>")) "WithNull" else "" +} \ No newline at end of file diff --git a/Buff/src/jvmMain/kotlin/com/lt/buff/provider/BuffSymbolProcessor.kt b/Buff/src/jvmMain/kotlin/com/lt/buff/provider/BuffSymbolProcessor.kt index 7c2a98c..ae0ff93 100644 --- a/Buff/src/jvmMain/kotlin/com/lt/buff/provider/BuffSymbolProcessor.kt +++ b/Buff/src/jvmMain/kotlin/com/lt/buff/provider/BuffSymbolProcessor.kt @@ -3,6 +3,7 @@ package com.lt.buff.provider import com.google.devtools.ksp.processing.Resolver import com.google.devtools.ksp.processing.SymbolProcessor import com.google.devtools.ksp.processing.SymbolProcessorEnvironment +import com.google.devtools.ksp.symbol.ClassKind import com.google.devtools.ksp.symbol.KSAnnotated import com.google.devtools.ksp.symbol.KSClassDeclaration import com.google.devtools.ksp.validate @@ -18,9 +19,8 @@ internal class BuffSymbolProcessor(private val environment: SymbolProcessorEnvir override fun process(resolver: Resolver): List { val ret = mutableListOf() resolver.getSymbolsWithAnnotation(Buff::class.qualifiedName!!) - .toList() .forEach { - if (it is KSClassDeclaration && it.classKind.type == "class") { + if (it is KSClassDeclaration && it.classKind == ClassKind.CLASS) { if (!it.validate()) ret.add(it) else it.accept(BuffVisitor(environment), Unit)//处理符号 } @@ -28,4 +28,4 @@ internal class BuffSymbolProcessor(private val environment: SymbolProcessorEnvir //返回无法处理的符号 return ret } -} \ No newline at end of file +} diff --git a/Buff/src/jvmMain/kotlin/com/lt/buff/provider/BuffVisitor.kt b/Buff/src/jvmMain/kotlin/com/lt/buff/provider/BuffVisitor.kt index 47f9ceb..13d0f50 100644 --- a/Buff/src/jvmMain/kotlin/com/lt/buff/provider/BuffVisitor.kt +++ b/Buff/src/jvmMain/kotlin/com/lt/buff/provider/BuffVisitor.kt @@ -54,10 +54,19 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) //遍历构造内的字段 classDeclaration.primaryConstructor?.parameters?.forEach { val name = it.name?.getShortName() ?: "" - val (ksType, isBuffBean, typeName, nullable, finallyTypeName) = getKSTypeInfo(it.type, options) + val ksTypeInfo = getKSTypeInfo(it.type, options) //写入构造内的普通字段 - file.appendText(" ${if (it.isVal) "val" else "var"} $name: $finallyTypeName,\n") - functionFields.add(FunctionFieldsInfo(name, true, isBuffBean, nullable)) + file.appendText(" ${if (it.isVal) "val" else "var"} $name: ${ksTypeInfo.finallyTypeName},\n") + functionFields.add( + FunctionFieldsInfo( + name, + true, + ksTypeInfo.isBuffBean, + ksTypeInfo.nullable, + ksTypeInfo.isList, + ksTypeInfo.typeString, + ) + ) } //遍历所有字段 classDeclaration.getAllProperties().forEach { @@ -89,7 +98,16 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) " val $fieldName: $typeName${info.typeString} = $stateFieldName\n" ) } - functionFields.add(FunctionFieldsInfo(fieldName, false, isBuffBean, nullable, info.isList)) + functionFields.add( + FunctionFieldsInfo( + fieldName, + false, + isBuffBean, + nullable, + info.isList, + info.typeString, + ) + ) } } file.appendText(") {\n") @@ -103,24 +121,23 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) file.appendText( "\n fun removeBuff(): $fullName =\n" + " $fullName(${ - functionFields.filter { it.isInTheConstructor } - .map { - if (it.isBuffBean) - "${it.fieldName}${it.nullable}.removeBuff()" - else - it.fieldName - } - .joinToString() + functionFields.filter { it.isInTheConstructor }.joinToString { + if (it.isBuffBean) + "${it.fieldName}${it.nullable}.removeBuff${it.getBuffSuffix()}()" + else + it.fieldName + } }).also {\n" ) functionFields.filter { !it.isInTheConstructor }.forEach { - val isList = if (it.isList) it.nullable + ".toList()" else "" file.appendText( " it.${it.fieldName} = ${ if (it.isBuffBean) - "${it.fieldName}$isList${it.nullable}.removeBuff()" + "${it.fieldName}${ + if (it.isList) "" else it.nullable + }.removeBuff${it.getBuffSuffix()}()" else - it.fieldName + isList + it.fieldName }\n" ) } @@ -137,7 +154,7 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) file.appendText( " ${ if (it.isBuffBean) - "${it.fieldName}${it.nullable}.addBuff()" + "${it.fieldName}${it.nullable}.addBuff${it.getBuffSuffix()}()" else it.fieldName },\n" @@ -147,7 +164,7 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) file.appendText( " mutableStateOf(${ if (it.isBuffBean) - "${it.fieldName}${it.nullable}.addBuff()" + "${it.fieldName}${it.nullable}.addBuff${it.getBuffSuffix()}()" else it.fieldName }),\n" @@ -156,7 +173,7 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) file.appendText( " ${ if (it.isBuffBean) - "${it.fieldName}${it.nullable}.addBuff()" + "${it.fieldName}${it.nullable}.addBuff${it.getBuffSuffix()}()" else it.fieldName }${it.nullable}.toMutableStateList() ?: mutableStateListOf(),\n" @@ -167,13 +184,21 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) file.appendText(" )\n\n${options.getCustomInFile(::getInfo)}") //写入Collection file.appendText( - "\n\nfun Collection<$fullName?>.addBuff() =\n" + - " map { it?.addBuff() } as List<$className>" + "\n\nfun Collection<$fullName?>.addBuffWithNull() =\n" + + " map { it?.addBuff() }" + ) + file.appendText( + "\nfun Collection<$fullName>.addBuff() =\n" + + " map { it.addBuff() }" ) //写入Collection file.appendText( - "\n\nfun Collection<$className?>.removeBuff() =\n" + - " map { it?.removeBuff() } as List<$fullName>" + "\n\nfun Collection<$className?>.removeBuffWithNull() =\n" + + " map { it?.removeBuff() }" + ) + file.appendText( + "\nfun Collection<$className>.removeBuff() =\n" + + " map { it.removeBuff() }" ) file.close() } diff --git a/README.md b/README.md index 8cf884d..ab2386b 100644 --- a/README.md +++ b/README.md @@ -16,10 +16,6 @@ 1. Convert some fields in beans to MutableState<T> that can be used directly -## Capability to be achieved - -see this - ## How to use Step 1 and 2.add dependencies: diff --git a/README_CN.md b/README_CN.md index 576504e..9818b85 100644 --- a/README_CN.md +++ b/README_CN.md @@ -16,10 +16,6 @@ 1. 将beans中的某些字段转换为可以直接使用的MutableState<T> -## 待实现功能 - -1. 支持MutableList - ## 使用方式 Step 1和2.添加依赖: @@ -47,7 +43,7 @@ dependencies { ```kotlin plugins { ... - id("com.google.devtools.ksp") version "1.8.20-1.0.10"//this,前面的1.7.10对应你的kotlin版本,更多版本参考: https://github.com/google/ksp/releases + id("com.google.devtools.ksp") version "1.8.20-1.0.10"//this,前面的1.8.20对应你的kotlin版本,更多版本参考: https://github.com/google/ksp/releases } ... diff --git a/app/src/main/java/com/lt/buffapp/UseBuff.kt b/app/src/main/java/com/lt/buffapp/UseBuff.kt index bbbfa52..a436902 100644 --- a/app/src/main/java/com/lt/buffapp/UseBuff.kt +++ b/app/src/main/java/com/lt/buffapp/UseBuff.kt @@ -38,12 +38,14 @@ class BuffBean( val id: Int? = null, var info2: InfoBean? = null, var infoList2: List? = null, + var infoList4: List? = null, ) { var name: String? = null var info: InfoBean? = null var infoBean: InfoBean = InfoBean() var type: Type? = null var infoList: List? = null + var infoList3: List? = null var list: List? = null var infoListList: List>? = null var map: Map? = null diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 915e9de..17b61f2 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -3,4 +3,4 @@ const val composeVersion = "1.4.0"//compose版本 const val composeCompilerVersion = "1.4.5"//compose编译版本 const val kspVersion = "$kotlinVersion-1.0.10"//ksp版本 -const val mVersion = "0.1.2"//此库的版本 \ No newline at end of file +const val mVersion = "1.0.0"//此库的版本 \ No newline at end of file