Skip to content

Commit 97105d9

Browse files
authored
Merge pull request #54 from ForteScarlet/support-include-property-annotation
Support for configuring `includeProperty` in the IncludeAnnotation
2 parents 1ced505 + 4fe0079 commit 97105d9

File tree

7 files changed

+103
-24
lines changed

7 files changed

+103
-24
lines changed

buildSrc/src/main/kotlin/IProject.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import love.forte.gradle.common.core.project.ProjectDetail
22
import love.forte.gradle.common.core.project.Version
3-
import love.forte.gradle.common.core.project.minus
43
import love.forte.gradle.common.core.project.version
54

65
object IProject : ProjectDetail() {
@@ -9,7 +8,7 @@ object IProject : ProjectDetail() {
98
const val DESCRIPTION = "Generate platform-compatible functions for Kotlin suspend functions"
109
const val HOMEPAGE = "https://github.com/ForteScarlet/kotlin-suspend-transform-compiler-plugin"
1110

12-
override val version: Version = version(0, 8, 0) - version("beta1")
11+
override val version: Version = version(0, 9, 0)
1312

1413
override val homepage: String get() = HOMEPAGE
1514

compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/SuspendTransformConfiguration.kt

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,28 @@ data class Transformer(
7777
val originFunctionIncludeAnnotations: List<IncludeAnnotation>,
7878

7979
/**
80-
* 需要在生成出来的函数上追截的注解信息。(不需要指定 `@Generated`)
80+
* 需要在生成出来的函数上追加的注解信息。(不需要指定 `@Generated`)
8181
*/
8282
val syntheticFunctionIncludeAnnotations: List<IncludeAnnotation>,
8383

8484
/**
8585
* 是否复制源函数上的注解到新的函数上。
86+
* 如果生成的是属性类型,则表示是否复制到 `getter` 上。
8687
*/
8788
val copyAnnotationsToSyntheticFunction: Boolean,
8889

8990
/**
9091
* 复制原函数上注解时需要排除掉的注解。
9192
*/
9293
val copyAnnotationExcludes: List<ClassInfo>
93-
)
94+
) {
95+
/**
96+
* 如果是生成属性的话,是否复制源函数上的注解到新的属性上
97+
*
98+
* @since 0.9.0
99+
*/
100+
var copyAnnotationsToSyntheticProperty: Boolean = false
101+
}
94102

95103
/**
96104
* 用于标记的注解信息.
@@ -131,7 +139,14 @@ data class MarkAnnotation @JvmOverloads constructor(
131139
@Serializable
132140
data class IncludeAnnotation(
133141
val classInfo: ClassInfo, val repeatable: Boolean = false
134-
)
142+
) {
143+
/**
144+
* 如果是追加,是否追加到property上
145+
*
146+
* @since 0.9.0
147+
*/
148+
var includeProperty: Boolean = false
149+
}
135150

136151
/**
137152
*
@@ -234,7 +249,10 @@ open class SuspendTransformConfiguration {
234249
originFunctionIncludeAnnotations = listOf(IncludeAnnotation(jvmSyntheticClassInfo)),
235250
copyAnnotationsToSyntheticFunction = true,
236251
copyAnnotationExcludes = listOf(jvmSyntheticClassInfo, jvmBlockingAnnotationInfo.classInfo),
237-
syntheticFunctionIncludeAnnotations = listOf(IncludeAnnotation(jvmApi4JAnnotationClassInfo))
252+
syntheticFunctionIncludeAnnotations = listOf(
253+
IncludeAnnotation(jvmApi4JAnnotationClassInfo)
254+
.apply { includeProperty = true }
255+
)
238256
)
239257
//endregion
240258

@@ -261,7 +279,11 @@ open class SuspendTransformConfiguration {
261279
originFunctionIncludeAnnotations = listOf(IncludeAnnotation(jvmSyntheticClassInfo)),
262280
copyAnnotationsToSyntheticFunction = true,
263281
copyAnnotationExcludes = listOf(jvmSyntheticClassInfo, jvmAsyncAnnotationInfo.classInfo),
264-
syntheticFunctionIncludeAnnotations = listOf(IncludeAnnotation(jvmApi4JAnnotationClassInfo))
282+
syntheticFunctionIncludeAnnotations = listOf(
283+
IncludeAnnotation(jvmApi4JAnnotationClassInfo).apply {
284+
includeProperty = true
285+
}
286+
)
265287
)
266288
//endregion
267289

@@ -291,7 +313,11 @@ open class SuspendTransformConfiguration {
291313
originFunctionIncludeAnnotations = listOf(),
292314
copyAnnotationsToSyntheticFunction = true,
293315
copyAnnotationExcludes = listOf(jsAsyncAnnotationInfo.classInfo),
294-
syntheticFunctionIncludeAnnotations = listOf(IncludeAnnotation(jsApi4JsAnnotationInfo))
316+
syntheticFunctionIncludeAnnotations = listOf(
317+
IncludeAnnotation(jsApi4JsAnnotationInfo).apply {
318+
includeProperty = true
319+
}
320+
)
295321
)
296322
//endregion
297323

compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/fir/SuspendTransformFirTransformer.kt

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,14 @@ class SuspendTransformFirTransformer(
401401
): Pair<List<FirAnnotation>, List<FirAnnotation>> {
402402
val transformer = funData.transformer
403403

404-
val (copyFunction, excludes, includes) = CopyAnnotationsData(
404+
val (copyFunction, copyProperty, excludes, includes) = CopyAnnotationsData(
405405
transformer.copyAnnotationsToSyntheticFunction,
406+
transformer.copyAnnotationsToSyntheticProperty,
406407
transformer.copyAnnotationExcludes.map { it.toClassId() },
407408
transformer.syntheticFunctionIncludeAnnotations.map { it.toInfo() }
408409
)
409410

410-
val annotationList = mutableListOf<FirAnnotation>()
411-
412-
with(annotationList) {
411+
val functionAnnotationList = buildList<FirAnnotation> {
413412
if (copyFunction) {
414413
val notCompileAnnotationsCopied = original.annotations.filterNot {
415414
val annotationClassId = it.toAnnotationClassId(session) ?: return@filterNot true
@@ -448,7 +447,32 @@ class SuspendTransformFirTransformer(
448447
}
449448
}
450449

451-
return annotationList to emptyList()
450+
val propertyAnnotationList = buildList<FirAnnotation> {
451+
if (copyProperty) {
452+
val notCompileAnnotationsCopied = original.annotations.filterNot {
453+
val annotationClassId = it.toAnnotationClassId(session) ?: return@filterNot true
454+
excludes.any { ex -> annotationClassId == ex }
455+
}
456+
457+
addAll(notCompileAnnotationsCopied)
458+
}
459+
460+
// add includes
461+
includes
462+
.filter { it.includeProperty }
463+
.forEach { include ->
464+
val classId = include.classId
465+
val includeAnnotation = buildAnnotation {
466+
argumentMapping = buildAnnotationArgumentMapping()
467+
annotationTypeRef = buildResolvedTypeRef {
468+
type = classId.createConeType(session)
469+
}
470+
}
471+
add(includeAnnotation)
472+
}
473+
}
474+
475+
return functionAnnotationList to propertyAnnotationList
452476
}
453477

454478

compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/symbol/AbstractSuspendTransformFunctionDescriptor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ sealed class AbstractSuspendTransformFunctionDescriptor(
2828
private val originFunction: SimpleFunctionDescriptor,
2929
functionName: Name,
3030
annotations: Annotations,
31-
internal val propertyAnnotations: Annotations,
31+
val propertyAnnotations: Annotations,
3232
private val userData: SuspendTransformUserData,
3333
val transformer: Transformer
3434
) : SimpleFunctionDescriptorImpl(
@@ -92,7 +92,7 @@ sealed class AbstractSuspendTransformFunctionDescriptor(
9292
return SimpleSuspendTransformPropertyDescriptor(
9393
classDescriptor,
9494
this,
95-
annotations,
95+
propertyAnnotations,
9696
)
9797
}
9898

compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/symbol/SuspendTransformSyntheticResolveExtension.kt

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,14 @@ private fun copyAnnotations(
235235
return AnnotationDescriptorImpl(type, valueArguments, descriptor.source)
236236
}
237237

238-
val (copyFunction, excludes, includes) = CopyAnnotationsData(
238+
val (copyFunction, copyProperty, excludes, includes) = CopyAnnotationsData(
239239
transformer.copyAnnotationsToSyntheticFunction,
240+
transformer.copyAnnotationsToSyntheticProperty,
240241
transformer.copyAnnotationExcludes.map { it.toClassId() },
241242
transformer.syntheticFunctionIncludeAnnotations.map { it.toInfo() }
242243
)
243244

244-
val annotationsList = mutableListOf<AnnotationDescriptor>()
245-
246-
annotationsList.apply {
245+
val functionAnnotationsList = buildList<AnnotationDescriptor> {
247246
if (copyFunction) {
248247
val notCompileAnnotationsCopied = originFunction.annotations.filterNotCompileAnnotations().filterNot {
249248
val annotationClassId = it.annotationClass?.classId ?: return@filterNot true
@@ -269,7 +268,35 @@ private fun copyAnnotations(
269268
}
270269
}
271270

272-
return Annotations.create(annotationsList) to Annotations.EMPTY
271+
val propertyAnnotationsList = buildList<AnnotationDescriptor> {
272+
if (copyProperty) {
273+
val notCompileAnnotationsCopied = originFunction.annotations.filterNotCompileAnnotations().filterNot {
274+
val annotationClassId = it.annotationClass?.classId ?: return@filterNot true
275+
excludes.any { ex -> annotationClassId == ex }
276+
}
277+
addAll(notCompileAnnotationsCopied)
278+
}
279+
280+
// try add @Generated(by = ...)
281+
findAnnotation(
282+
generatedAnnotationClassId,
283+
mutableMapOf(Name.identifier("by") to StringArrayValue(originFunction.toGeneratedByDescriptorInfo()))
284+
)?.also(::add)
285+
286+
// add includes
287+
includes
288+
.filter { it.includeProperty }
289+
.forEach { include ->
290+
val classId = include.classId
291+
val unsafeFqName = classId.asSingleFqName().toUnsafe()
292+
if (!include.repeatable && this.any { it.fqName?.toUnsafe() == unsafeFqName }) {
293+
return@forEach
294+
}
295+
findAnnotation(classId)?.also(::add)
296+
}
297+
}
298+
299+
return Annotations.create(functionAnnotationsList) to Annotations.create(propertyAnnotationsList)
273300
}
274301

275302
private class StringArrayValue(values: List<StringValue>) : ArrayValue(values, { module ->

compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/utils/CopyAnnotationUtils.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import org.jetbrains.kotlin.name.ClassId
55

66
data class CopyAnnotationsData(
77
val copyFunction: Boolean,
8+
val copyProperty: Boolean,
89
val excludes: List<ClassId>,
910
val includes: List<IncludeAnnotationInfo>
1011
)
1112

1213
data class IncludeAnnotationInfo(
1314
val classId: ClassId,
14-
val repeatable: Boolean
15+
val repeatable: Boolean,
16+
val includeProperty: Boolean,
1517
)
1618

17-
fun IncludeAnnotation.toInfo(): love.forte.plugin.suspendtrans.utils.IncludeAnnotationInfo {
18-
return IncludeAnnotationInfo(classInfo.toClassId(), repeatable)
19+
fun IncludeAnnotation.toInfo(): IncludeAnnotationInfo {
20+
return IncludeAnnotationInfo(classInfo.toClassId(), repeatable, includeProperty)
1921
}

samples/sample-jvm/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import love.forte.plugin.suspendtrans.gradle.SuspendTransformGradleExtension
2+
import org.jetbrains.kotlin.js.translate.context.Namer.kotlin
23

34
plugins {
45
`java-library`
@@ -16,7 +17,7 @@ buildscript {
1617
}
1718
dependencies {
1819
//this.implementation()
19-
classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:0.8.0-beta1")
20+
classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:0.9.0-beta1")
2021
}
2122
}
2223

0 commit comments

Comments
 (0)