Skip to content

Commit b8a230f

Browse files
Merge pull request #13 from ltttttttttttt/dev
增加对Compose可变性注解的支持,如果原Bean带有 @stable@immutable 注解,则生成的Buff类也带有相应注解
2 parents 7b0bf45 + 257578e commit b8a230f

File tree

11 files changed

+57
-13
lines changed

11 files changed

+57
-13
lines changed

Buff-lib/build.gradle.kts

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ kotlin {
1818
jvm {
1919
compilations.all {
2020
kotlinOptions {
21-
jvmTarget = "11"
21+
jvmTarget = "17"
2222
}
2323
}
2424
}
@@ -78,7 +78,7 @@ android {
7878
sourceSets["main"].res.srcDir("resources")
7979
}
8080
compileOptions {
81-
sourceCompatibility = JavaVersion.VERSION_1_8
82-
targetCompatibility = JavaVersion.VERSION_1_8
81+
sourceCompatibility = JavaVersion.VERSION_17
82+
targetCompatibility = JavaVersion.VERSION_17
8383
}
8484
}

Buff/src/jvmMain/kotlin/com/lt/buff/Util.kt

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.lt.buff
22

33
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
4+
import com.google.devtools.ksp.symbol.KSAnnotation
45
import com.google.devtools.ksp.symbol.KSTypeReference
56
import com.google.devtools.ksp.symbol.Nullability
67
import com.lt.buff.options.KSTypeInfo
@@ -41,7 +42,11 @@ internal fun String?.w(environment: SymbolProcessorEnvironment) {
4142
* [options] 用户的配置
4243
* [isFirstFloor] 是否是最外层,用于判断泛型
4344
*/
44-
internal fun getKSTypeInfo(ks: KSTypeReference, options: KspOptions, isFirstFloor: Boolean = true): KSTypeInfo {
45+
internal fun getKSTypeInfo(
46+
ks: KSTypeReference,
47+
options: KspOptions,
48+
isFirstFloor: Boolean = true
49+
): KSTypeInfo {
4550
//type对象
4651
val ksType = ks.resolve()
4752
//类是否有Buff注解
@@ -96,4 +101,22 @@ internal fun getKSTypeInfo(ks: KSTypeReference, options: KspOptions, isFirstFloo
96101
typeString,
97102
isList
98103
)
104+
}
105+
106+
/**
107+
* 获取注解的全类名
108+
*/
109+
internal fun getAnnotationFullClassName(ksa: KSAnnotation): String {
110+
val ksType = ksa.annotationType.resolve()
111+
//完整type字符串
112+
return ksType.declaration.let {
113+
val name = it.qualifiedName?.asString()
114+
if (name != null)
115+
return@let name
116+
val packageName = it.packageName.asString()
117+
return@let if (packageName.isEmpty())
118+
ksa.shortName.asString()
119+
else
120+
"$packageName.${it.simpleName.asString()}"
121+
}
99122
}

Buff/src/jvmMain/kotlin/com/lt/buff/provider/BuffVisitor.kt

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
55
import com.google.devtools.ksp.symbol.KSClassDeclaration
66
import com.google.devtools.ksp.symbol.KSVisitorVoid
77
import com.lt.buff.appendText
8+
import com.lt.buff.getAnnotationFullClassName
89
import com.lt.buff.getKSTypeInfo
910
import com.lt.buff.options.CustomOptionsInfo
1011
import com.lt.buff.options.FunctionFieldsInfo
@@ -28,6 +29,13 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment)
2829
val fullName = classDeclaration.qualifiedName?.asString()
2930
?: (classDeclaration.packageName.asString() + classDeclaration.simpleName.asString())
3031
val className = "$originalClassName${options.suffix}"
32+
val haveStable = classDeclaration.annotations.find {
33+
getAnnotationFullClassName(it) == "androidx.compose.runtime.Stable"
34+
} != null
35+
val haveImmutable = classDeclaration.annotations.find {
36+
getAnnotationFullClassName(it) == "androidx.compose.runtime.Immutable"
37+
} != null
38+
3139
val file = environment.codeGenerator.createNewFile(
3240
Dependencies(
3341
true,
@@ -40,9 +48,15 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment)
4048
"import androidx.compose.runtime.MutableState\n" +
4149
"import androidx.compose.runtime.mutableStateListOf\n" +
4250
"import androidx.compose.runtime.mutableStateOf\n" +
51+
"import androidx.compose.runtime.Stable\n" +
52+
"import androidx.compose.runtime.Immutable\n" +
4353
"import androidx.compose.runtime.snapshots.SnapshotStateList\n" +
4454
"import androidx.compose.runtime.toMutableStateList\n\n"
4555
)
56+
if (haveStable)
57+
file.appendText("@Stable\n")
58+
if (haveImmutable)
59+
file.appendText("@Immutable\n")
4660
file.appendText(
4761
"${options.getClassSerializeAnnotation()}\n" +
4862
"class $className @Deprecated(\"Do not directly call the constructor, instead use addBuff()\") constructor(\n"

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">Add status to beans in Jetpack(jb) Compose, Fields in beans can be directly used as the MutableState&lt;T&gt;</p>
44

5-
<p>⚠️Non mainstream warning:This item violates 'Unique trusted data source' and 'FP',If the content on this page causes your discomfort,please press Ctrl+W</p>
5+
<p align="center">⚠️Non mainstream warning:If the content on this page causes your discomfort,please press Ctrl+W</p>
66

77
<p align="center">
88
<img src="https://img.shields.io/badge/Kotlin-Multiplatform-%237f52ff?logo=kotlin">
@@ -109,3 +109,5 @@ ksp {
109109
arg("customInFileWithBuff", "//File end")//in file
110110
}
111111
```
112+
113+
The project provides support for Compose variability annotations. If the original bean has @Stable or @Immutable annotations, the generated Buff class also has corresponding annotations

README_CN.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">将beans中的某些字段转换为可以直接使用的MutableState&lt;T&gt;,适用于Jetpack(jb) Compose</p>
44

5-
<p>⚠️非主流警告:此项目违背了'唯一可信数据源'和'函数式编程思想',若此页面所述内容引起了您的不适,请按下Ctrl+W</p>
5+
<p align="center">⚠️非主流警告:若此页面所述内容引起了您的不适,请按下Ctrl+W</p>
66

77
<p align="center">
88
<img src="https://img.shields.io/badge/Kotlin-Multiplatform-%237f52ff?logo=kotlin">
@@ -107,3 +107,5 @@ ksp {
107107
arg("customInFileWithBuff", "//File end")//类外,kt文件内
108108
}
109109
```
110+
111+
项目提供了对Compose可变性注解的支持,如果原Bean带有 @Stable@Immutable 注解,则生成的Buff类也带有相应注解

app/build.gradle.kts

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ android {
5353
}
5454
}
5555
compileOptions {
56-
sourceCompatibility = JavaVersion.VERSION_1_8
57-
targetCompatibility = JavaVersion.VERSION_1_8
56+
sourceCompatibility = JavaVersion.VERSION_17
57+
targetCompatibility = JavaVersion.VERSION_17
5858
}
5959
kotlinOptions {
60-
jvmTarget = "1.8"
60+
jvmTarget = "17"
6161
}
6262
buildFeatures {
6363
compose = true

app/src/main/java/com/lt/buffapp/UseBuff.kt

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import androidx.compose.material.Button
55
import androidx.compose.material.Text
66
import androidx.compose.material.TextField
77
import androidx.compose.runtime.Composable
8+
import androidx.compose.runtime.Immutable
9+
import androidx.compose.runtime.Stable
810
import androidx.compose.runtime.remember
911
import com.lt.buff.Buff
1012
import com.lt.buffapp.ui.theme.Type
@@ -34,6 +36,7 @@ fun ColumnScope.UseBuff() {
3436

3537
@kotlinx.serialization.Serializable
3638
@Buff
39+
@Stable
3740
class BuffBean(
3841
val id: Int? = null,
3942
var info2: InfoBean? = null,

buildSrc/src/main/kotlin/Versions.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ const val composeVersion = "1.4.0"//compose版本
33
const val composeCompilerVersion = "1.4.5"//compose编译版本
44
const val kspVersion = "$kotlinVersion-1.0.10"//ksp版本
55

6-
const val mVersion = "1.0.1"//此库的版本
6+
const val mVersion = "1.0.2"//此库的版本
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Wed Oct 19 11:47:39 CST 2022
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

gradlew

100644100755
File mode changed.

ios_shared/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ android {
3333
targetSdk = 31
3434
}
3535
compileOptions {
36-
sourceCompatibility = JavaVersion.VERSION_1_8
37-
targetCompatibility = JavaVersion.VERSION_1_8
36+
sourceCompatibility = JavaVersion.VERSION_17
37+
targetCompatibility = JavaVersion.VERSION_17
3838
}
3939
lint {
4040
checkDependencies = true//开启 lint 性能优化

0 commit comments

Comments
 (0)