Skip to content

Commit

Permalink
Merge pull request #1 from ltttttttttttt/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ltttttttttttt authored Oct 23, 2022
2 parents bc0545f + 5aec766 commit db71965
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 28 deletions.
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ plugins {

dependencies {
...
implementation("com.github.ltttttttttttt:Buff:$version")//this, such as 0.0.2
ksp("com.github.ltttttttttttt:Buff:$version")//this, such as 0.0.2
implementation("com.github.ltttttttttttt:Buff:$version")//this, such as 0.0.3
ksp("com.github.ltttttttttttt:Buff:$version")//this, such as 0.0.3
}
```

Expand All @@ -79,7 +79,7 @@ bean.name//The name's getter and setter have the effect of MutableState<T>
bean.removeBuff()//Fallback to BuffBean(optional)
```

Step 4.Add ksp dir to the srcDir
Step 4.Add ksp dir to the srcDir

Your app dir, build.gradle.kts add:

Expand Down Expand Up @@ -141,4 +141,27 @@ kotlin {
kotlin.srcDir("build/generated/ksp/test/kotlin")
}
}
```

Step 5.Config

Serialize of this project uses kotlinx-serialization by default, To modify, Your app dir,
build.gradle.kts add:

```kotlin
ksp {
//Set the Annotation of the class, Usually as follows
arg("classSerializeAnnotationWithBuff", "//Not have")
//Set the Annotation of the field to transient, Usually as follows
arg("fieldSerializeTransientAnnotationWithBuff", "@kotlin.jvm.Transient")
}
```

Add custom code, reference [KspOptions.handlerCustomCode], Your app dir, build.gradle.kts add:

```kotlin
ksp {
arg("customInClassWithBuff", "//Class end")//in class
arg("customInFileWithBuff", "//File end")//in file
}
```
29 changes: 24 additions & 5 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
1. 适用于多平台
2. 对List的支持(可能)
3. 对嵌套类和嵌套List的支持(可能)
4. 对其他json解析框架的支持
5. 支持自定义增加代码
6. (可能需要支持外部包)

## 使用方式

Expand Down Expand Up @@ -56,8 +53,8 @@ plugins {

dependencies {
...
implementation("com.github.ltttttttttttt:Buff:$version")//this,比如0.0.2
ksp("com.github.ltttttttttttt:Buff:$version")//this,比如0.0.2
implementation("com.github.ltttttttttttt:Buff:$version")//this,比如0.0.3
ksp("com.github.ltttttttttttt:Buff:$version")//this,比如0.0.3
}
```

Expand Down Expand Up @@ -145,4 +142,26 @@ kotlin {
kotlin.srcDir("build/generated/ksp/test/kotlin")
}
}
```

Step 5.配置

本项目对序列化的默认支持为:kotlinx-serialization,如需修改,在app模块目录内的build.gradle.kts内添加:

```kotlin
ksp {
//设置类序列化所需要的注解,其他序列化库一般不需要,所以我们放一个注释即可
arg("classSerializeAnnotationWithBuff", "//Not have")
//设置属性无需被序列化的注解,一般使用jvm中的transient关键字
arg("fieldSerializeTransientAnnotationWithBuff", "@kotlin.jvm.Transient")
}
```

支持自定义增加代码,属性参考[KspOptions.handlerCustomCode],在app模块目录内的build.gradle.kts内添加:

```kotlin
ksp {
arg("customInClassWithBuff", "//Class end")//类内
arg("customInFileWithBuff", "//File end")//类外,kt文件内
}
```
8 changes: 5 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
//}
}
buildTypes {
debug{
debug {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
Expand Down Expand Up @@ -72,8 +72,10 @@ android {
//}
//ksp配置
//ksp {
// arg("option1", "value1")
// arg("option2", "value2")
// arg("customInClassWithBuff", "//Class end")
// arg("customInFileWithBuff", "//File end")
// arg("classSerializeAnnotationWithBuff", "//Not have")
// arg("fieldSerializeTransientAnnotationWithBuff", "@kotlin.jvm.Transient")
//}
}

Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/lt/buffapp/UseBuff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/lt/buffapp/ui/theme/Type.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ val Typography = Typography(
fontSize = 12.sp
)
*/
)

@kotlinx.serialization.Serializable
class Type(
val type: Int = 0
)
8 changes: 7 additions & 1 deletion buff/src/main/java/com/lt/buff/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ import java.io.OutputStream
*/
internal fun OutputStream.appendText(str: String) {
this.write(str.toByteArray())
}
}

/**
* 如果字符串为空或长度为0,就使用lambda中的字符串
*/
internal inline fun String?.ifNullOfEmpty(defaultValue: () -> String): String =
if (this.isNullOrEmpty()) defaultValue() else this
17 changes: 17 additions & 0 deletions buff/src/main/java/com/lt/buff/options/CustomOptionsInfo.kt
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,
)
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]
Expand Down
52 changes: 52 additions & 0 deletions buff/src/main/java/com/lt/buff/options/KspOptions.kt
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)
}
}
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]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.lt.buff
package com.lt.buff.provider

import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
Expand Down
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的声明
Expand All @@ -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,
Expand All @@ -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"
)
//类内的字段(非构造内的)
Expand All @@ -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))
}
//遍历所有字段
Expand All @@ -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" +
Expand All @@ -88,6 +96,11 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment)
}
}
file.appendText(") {\n")

fun getInfo() = CustomOptionsInfo(
originalClassName, className
)

//写入非构造内的字段
classFields.forEach(file::appendText)
//写入removeBuff
Expand Down Expand Up @@ -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(
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
com.lt.buff.BuffSymbolProcessorProvider
com.lt.buff.provider.BuffSymbolProcessorProvider

0 comments on commit db71965

Please sign in to comment.