-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ltttttttttttt/dev
Dev
- Loading branch information
Showing
13 changed files
with
168 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import androidx.compose.material.TextField | |
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import com.lt.buff.Buff | ||
import com.lt.buffapp.ui.theme.Type | ||
|
||
/** | ||
* creator: lt 2022/10/19 [email protected] | ||
|
@@ -34,10 +35,10 @@ fun ColumnScope.UseBuff() { | |
@Buff | ||
class BuffBean( | ||
val id: Int? = null, | ||
val info: InfoBean? = null, | ||
) { | ||
var name: String? = null | ||
var info2: InfoBean? = null | ||
var info: InfoBean? = null | ||
var type: Type? = null | ||
} | ||
|
||
@kotlinx.serialization.Serializable | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
buff/src/main/java/com/lt/buff/options/CustomOptionsInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.lt.buff.options | ||
|
||
/** | ||
* creator: lt 2022/10/23 [email protected] | ||
* effect : 自定义代码的配置 | ||
* warning: | ||
*/ | ||
class CustomOptionsInfo( | ||
/** | ||
* 原始类名 | ||
*/ | ||
val originalClassName: String, | ||
/** | ||
* 修改后的类名 | ||
*/ | ||
val className: String, | ||
) |
2 changes: 1 addition & 1 deletion
2
...in/java/com/lt/buff/FunctionFieldsInfo.kt → ...com/lt/buff/options/FunctionFieldsInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.lt.buff | ||
package com.lt.buff.options | ||
|
||
/** | ||
* creator: lt 2022/10/22 [email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.lt.buff.options | ||
|
||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment | ||
import com.lt.buff.ifNullOfEmpty | ||
|
||
/** | ||
* creator: lt 2022/10/23 [email protected] | ||
* effect : buff的配置 | ||
* warning: | ||
*/ | ||
internal class KspOptions(environment: SymbolProcessorEnvironment) { | ||
val suffix = "WithBuff"//后缀 | ||
private val options = environment.options | ||
private val classSerializeAnnotation = "classSerializeAnnotation$suffix" | ||
private val fieldSerializeTransientAnnotation = "fieldSerializeTransientAnnotation$suffix" | ||
private val customInClass = "customInClass$suffix" | ||
private val customInFile = "customInFile$suffix" | ||
|
||
/** | ||
* 获取类序列化的注解 | ||
*/ | ||
fun getClassSerializeAnnotation(): String = | ||
options[classSerializeAnnotation].ifNullOfEmpty { "@kotlinx.serialization.Serializable" } | ||
|
||
/** | ||
* 获取表示属性不参与序列化的注解 | ||
*/ | ||
fun getFieldSerializeTransientAnnotation(): String = | ||
options[fieldSerializeTransientAnnotation].ifNullOfEmpty { "@kotlinx.serialization.Transient" } | ||
|
||
/** | ||
* 获取类中自定义的代码 | ||
*/ | ||
fun getCustomInClass(getInfo: () -> CustomOptionsInfo): String { | ||
return handlerCustomCode(options[customInClass].ifNullOfEmpty { return "" }, getInfo()) | ||
} | ||
|
||
/** | ||
* 获取文件中自定义的代码 | ||
*/ | ||
fun getCustomInFile(getInfo: () -> CustomOptionsInfo): String { | ||
return handlerCustomCode(options[customInFile].ifNullOfEmpty { return "" }, getInfo()) | ||
} | ||
|
||
/** | ||
* 处理自定义代码,将特殊字段替换为真实数据 | ||
*/ | ||
private fun handlerCustomCode(code: String, info: CustomOptionsInfo): String { | ||
return code.replace("#originalClassName#", info.originalClassName) | ||
.replace("#className#", info.className) | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...n/java/com/lt/buff/BuffSymbolProcessor.kt → ...m/lt/buff/provider/BuffSymbolProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
package com.lt.buff | ||
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.KSAnnotated | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
import com.lt.buff.Buff | ||
|
||
/** | ||
* creator: lt 2022/10/20 [email protected] | ||
|
2 changes: 1 addition & 1 deletion
2
...om/lt/buff/BuffSymbolProcessorProvider.kt → ...f/provider/BuffSymbolProcessorProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
package com.lt.buff | ||
package com.lt.buff.provider | ||
|
||
import com.google.devtools.ksp.processing.Dependencies | ||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
import com.google.devtools.ksp.symbol.KSVisitorVoid | ||
import com.google.devtools.ksp.symbol.Nullability | ||
import com.lt.buff.Buff | ||
import com.lt.buff.appendText | ||
import com.lt.buff.options.CustomOptionsInfo | ||
import com.lt.buff.options.FunctionFieldsInfo | ||
import com.lt.buff.options.KspOptions | ||
|
||
/** | ||
* creator: lt 2022/10/20 [email protected] | ||
* effect : 访问并处理相应符号 | ||
* warning: | ||
*/ | ||
internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) : KSVisitorVoid() { | ||
private val suffix = "WithBuff" | ||
private val buffName = Buff::class.simpleName | ||
private val options = KspOptions(environment) | ||
|
||
/** | ||
* 访问class的声明 | ||
|
@@ -22,7 +27,7 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) | |
//获取class信息并创建kt文件 | ||
val packageName = classDeclaration.containingFile!!.packageName.asString() | ||
val originalClassName = classDeclaration.simpleName.asString() | ||
val className = "$originalClassName$suffix" | ||
val className = "$originalClassName${options.suffix}" | ||
val file = environment.codeGenerator.createNewFile( | ||
Dependencies( | ||
true, | ||
|
@@ -36,7 +41,7 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) | |
"import androidx.compose.runtime.mutableStateOf\n\n" | ||
) | ||
file.appendText( | ||
"@kotlinx.serialization.Serializable\n" +// TODO by lt 2022/10/21 17:23 后续改为多种json解析方式支持 | ||
"${options.getClassSerializeAnnotation()}\n" + | ||
"class $className(\n" | ||
) | ||
//类内的字段(非构造内的) | ||
|
@@ -50,10 +55,11 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) | |
val isBuffBean = | ||
ksType.declaration.annotations.toList() | ||
.find { it.shortName.getShortName() == buffName } != null | ||
val type = ksType.declaration.simpleName.getShortName() | ||
val typeName = | ||
"${ksType.declaration.packageName.asString()}.${ksType.declaration.simpleName.asString()}" | ||
val nullable = if (ksType.nullability == Nullability.NULLABLE) "?" else "" | ||
//写入构造内的普通字段 | ||
file.appendText(" ${if (it.isVal) "val" else "var"} $name: ${if (isBuffBean) "$type$suffix$nullable" else "$type$nullable"},\n") | ||
file.appendText(" ${if (it.isVal) "val" else "var"} $name: ${if (isBuffBean) "$typeName${options.suffix}$nullable" else "$typeName$nullable"},\n") | ||
functionFields.add(FunctionFieldsInfo(name, true, isBuffBean)) | ||
} | ||
//遍历所有字段 | ||
|
@@ -67,12 +73,14 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) | |
val isBuffBean = | ||
ksType.declaration.annotations.toList() | ||
.find { it.shortName.getShortName() == buffName } != null | ||
val typeName = ksType.declaration.simpleName.getShortName() | ||
val typeName = | ||
"${ksType.declaration.packageName.asString()}.${ksType.declaration.simpleName.asString()}" | ||
val nullable = if (ksType.nullability == Nullability.NULLABLE) "?" else "" | ||
val stateFieldName = "_${fieldName}_state" | ||
val buffType = if (isBuffBean) "$typeName$suffix$nullable" else "$typeName$nullable" | ||
val buffType = | ||
if (isBuffBean) "$typeName${options.suffix}$nullable" else "$typeName$nullable" | ||
//写入构造内的state字段 | ||
file.appendText(" @kotlinx.serialization.Transient val $stateFieldName: MutableState<$buffType> = null!!,\n") | ||
file.appendText(" ${options.getFieldSerializeTransientAnnotation()} val $stateFieldName: MutableState<$buffType> = null!!,\n") | ||
classFields.add( | ||
" var $fieldName: $buffType = $stateFieldName.value\n" + | ||
" get() {\n" + | ||
|
@@ -88,6 +96,11 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) | |
} | ||
} | ||
file.appendText(") {\n") | ||
|
||
fun getInfo() = CustomOptionsInfo( | ||
originalClassName, className | ||
) | ||
|
||
//写入非构造内的字段 | ||
classFields.forEach(file::appendText) | ||
//写入removeBuff | ||
|
@@ -115,6 +128,7 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) | |
) | ||
} | ||
file.appendText(" }\n") | ||
file.appendText("\n${options.getCustomInClass(::getInfo)}\n\n") | ||
file.appendText("}\n\n") | ||
//写入addBuff | ||
file.appendText( | ||
|
@@ -141,7 +155,7 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment) | |
}),\n" | ||
) | ||
} | ||
file.appendText(" )\n\n") | ||
file.appendText(" )\n\n${options.getCustomInFile(::getInfo)}") | ||
file.close() | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...in/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
com.lt.buff.BuffSymbolProcessorProvider | ||
com.lt.buff.provider.BuffSymbolProcessorProvider |