-
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #226 from simple-robot/dev/safer-dispatch-serializ…
…er-resolve 更安全的 Signal.Dispatch 序列化方案
- Loading branch information
Showing
22 changed files
with
403 additions
and
35 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
47 changes: 47 additions & 0 deletions
47
internal-processors/dispatch-serializer-processor/build.gradle.kts
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,47 @@ | ||
/* | ||
* Copyright (c) 2024. ForteScarlet. | ||
* | ||
* This file is part of simbot-component-qq-guild. | ||
* | ||
* simbot-component-qq-guild is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software Foundation, | ||
* either version 3 of the License, or (at your option) any later version. | ||
* | ||
* simbot-component-qq-guild is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-qq-guild. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget | ||
|
||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
kotlin { | ||
jvmToolchain(JVMConstants.KT_JVM_TARGET_VALUE) | ||
compilerOptions { | ||
javaParameters = true | ||
jvmTarget.set(JvmTarget.fromTarget(JVMConstants.KT_JVM_TARGET_VALUE.toString())) | ||
} | ||
} | ||
|
||
configJavaCompileWithModule() | ||
|
||
dependencies { | ||
api(libs.ksp) | ||
api(libs.kotlinPoet.ksp) | ||
testImplementation(kotlin("test-junit5")) | ||
} | ||
|
||
tasks.getByName<Test>("test") { | ||
useJUnitPlatform() | ||
} | ||
|
171 changes: 171 additions & 0 deletions
171
...rc/main/kotlin/qg/internal/processors/dispatcherserializer/DispatchSerializerProcessor.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,171 @@ | ||
/* | ||
* Copyright (c) 2024. ForteScarlet. | ||
* | ||
* This file is part of simbot-component-qq-guild. | ||
* | ||
* simbot-component-qq-guild is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software Foundation, | ||
* either version 3 of the License, or (at your option) any later version. | ||
* | ||
* simbot-component-qq-guild is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-qq-guild. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package qg.internal.processors.dispatcherserializer | ||
|
||
import com.google.devtools.ksp.getClassDeclarationByName | ||
import com.google.devtools.ksp.isAbstract | ||
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.google.devtools.ksp.symbol.KSFile | ||
import com.google.devtools.ksp.symbol.Modifier | ||
import com.squareup.kotlinpoet.* | ||
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy | ||
import com.squareup.kotlinpoet.ksp.toClassName | ||
import com.squareup.kotlinpoet.ksp.writeTo | ||
import java.time.Instant | ||
import java.time.ZoneOffset | ||
|
||
private const val SIGNAL_DISPATCH_PKG = "love.forte.simbot.qguild.event" | ||
private const val SIGNAL_DISPATCH_SIMPLE_NAME = "Signal.Dispatch" | ||
private const val SIGNAL_DISPATCH_NAME = "$SIGNAL_DISPATCH_PKG.$SIGNAL_DISPATCH_SIMPLE_NAME" | ||
|
||
/** | ||
* | ||
* @author ForteScarlet | ||
*/ | ||
internal class DispatchSerializerProcessor( | ||
private val environment: SymbolProcessorEnvironment, | ||
) : SymbolProcessor { | ||
private data class SerializableDispatch( | ||
val declaration: KSClassDeclaration, | ||
val typeName: String | ||
) | ||
|
||
private lateinit var signalDispatchDeclaration: KSClassDeclaration | ||
|
||
private val dispatchSerializableDeclarations = sortedSetOf<SerializableDispatch>( | ||
Comparator.comparing { it.typeName } | ||
) | ||
|
||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
signalDispatchDeclaration = resolver.getClassDeclarationByName(SIGNAL_DISPATCH_NAME) | ||
?: error("Cannot find class declaration $SIGNAL_DISPATCH_NAME") | ||
|
||
// find all subtypes, | ||
resolver.getAllFiles() | ||
.flatMap { it.declarations } | ||
.filterIsInstance<KSClassDeclaration>() | ||
.filter { | ||
signalDispatchDeclaration.asStarProjectedType().isAssignableFrom(it.asStarProjectedType()) | ||
} | ||
// not abstract | ||
.filter { | ||
!it.isAbstract() && !it.modifiers.contains(Modifier.SEALED) | ||
} | ||
// marked @kotlinx.serialization.Serializable | ||
.filter { | ||
it.annotations.any { anno -> | ||
with(anno.annotationType.resolve().declaration) { | ||
packageName.asString() == "kotlinx.serialization" && | ||
simpleName.asString() == "Serializable" | ||
} | ||
} | ||
} | ||
.mapNotNull { | ||
// find @love.forte.simbot.qguild.event.DispatchTypeName | ||
val typeNameAnnotation = it.annotations.find { anno -> | ||
with(anno.annotationType.resolve().declaration) { | ||
packageName.asString() == "love.forte.simbot.qguild.event" && | ||
simpleName.asString() == "DispatchTypeName" | ||
} | ||
} ?: return@mapNotNull null | ||
|
||
val argument = typeNameAnnotation.arguments.find { a -> a.name?.asString() == "value" } | ||
?: return@mapNotNull null | ||
|
||
val typeName = argument.value as String | ||
|
||
SerializableDispatch(it, typeName) | ||
} | ||
.toCollection(dispatchSerializableDeclarations) | ||
|
||
return emptyList() | ||
} | ||
|
||
override fun finish() { | ||
genResolver() | ||
} | ||
|
||
private fun genResolver() { | ||
val orgFiles = mutableListOf<KSFile>() | ||
signalDispatchDeclaration.containingFile?.also(orgFiles::add) | ||
|
||
val fileSpecBuilder = FileSpec.builder("love.forte.simbot.qguild.event", "SignalDispatchResolvers.generated") | ||
|
||
val funBuilder = FunSpec.builder("resolveDispatchSerializer").apply { | ||
addAnnotation(ClassName("love.forte.simbot.qguild", "Generated")) | ||
addParameter("eventName", String::class) | ||
returns( | ||
ClassName("kotlinx.serialization", "KSerializer") | ||
.parameterizedBy( | ||
WildcardTypeName | ||
.producerOf( | ||
ClassName(SIGNAL_DISPATCH_PKG, SIGNAL_DISPATCH_SIMPLE_NAME) | ||
) | ||
) | ||
.copy(nullable = true) | ||
) | ||
|
||
addCode( | ||
CodeBlock.builder().apply { | ||
beginControlFlow("return when(eventName)") | ||
// "" -> Type.serializer() | ||
for (dispatchSerializableDeclaration in dispatchSerializableDeclarations) { | ||
dispatchSerializableDeclaration.declaration.containingFile?.also(orgFiles::add) | ||
|
||
addStatement( | ||
"%S -> %T.serializer()", | ||
dispatchSerializableDeclaration.typeName, | ||
dispatchSerializableDeclaration.declaration.toClassName() | ||
) | ||
} | ||
addStatement("else -> null") | ||
endControlFlow() | ||
}.build() | ||
) | ||
|
||
addKdoc( | ||
CodeBlock.builder().apply { | ||
addStatement("| event name | target |") | ||
addStatement("| --- | --- |") | ||
for (dispatchSerializableDeclaration in dispatchSerializableDeclarations) { | ||
addStatement( | ||
"| `%S` | [%T] |", | ||
dispatchSerializableDeclaration.typeName, | ||
dispatchSerializableDeclaration.declaration.toClassName() | ||
) | ||
} | ||
addStatement("") | ||
addStatement("@since 4.1.0") | ||
}.build() | ||
) | ||
} | ||
|
||
fileSpecBuilder.addFileComment("Auto generated at ${Instant.now().atOffset(ZoneOffset.ofHours(8))}") | ||
fileSpecBuilder.addFunction(funBuilder.build()) | ||
|
||
fileSpecBuilder.build().writeTo( | ||
environment.codeGenerator, | ||
aggregating = true, | ||
originatingKSFiles = orgFiles | ||
) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...kotlin/qg/internal/processors/dispatcherserializer/DispatchSerializerProcessorProvider.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,31 @@ | ||
/* | ||
* Copyright (c) 2024. ForteScarlet. | ||
* | ||
* This file is part of simbot-component-qq-guild. | ||
* | ||
* simbot-component-qq-guild is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software Foundation, | ||
* either version 3 of the License, or (at your option) any later version. | ||
* | ||
* simbot-component-qq-guild is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-qq-guild. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package qg.internal.processors.dispatcherserializer | ||
|
||
import com.google.devtools.ksp.processing.SymbolProcessor | ||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment | ||
import com.google.devtools.ksp.processing.SymbolProcessorProvider | ||
|
||
/** | ||
* | ||
* @author ForteScarlet | ||
*/ | ||
class DispatchSerializerProcessorProvider : SymbolProcessorProvider { | ||
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = | ||
DispatchSerializerProcessor(environment) | ||
} |
1 change: 1 addition & 0 deletions
1
...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 |
---|---|---|
@@ -0,0 +1 @@ | ||
qg.internal.processors.dispatcherserializer.DispatchSerializerProcessorProvider |
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
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
Oops, something went wrong.