mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 18:40:15 +08:00
Support kotlinx-serialization builtin serializers
This commit is contained in:
parent
52eaf56e5a
commit
ff9c493e4b
@ -14,8 +14,8 @@ import java.time.Instant
|
|||||||
|
|
||||||
internal object MiraiConsoleBuildConstants { // auto-filled on build (task :mirai-console:fillBuildConstants)
|
internal object MiraiConsoleBuildConstants { // auto-filled on build (task :mirai-console:fillBuildConstants)
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
val buildDate: Instant = Instant.ofEpochSecond(1604041264)
|
val buildDate: Instant = Instant.ofEpochSecond(1605147625)
|
||||||
const val versionConst: String = "1.0-RC-1"
|
const val versionConst: String = "1.0-RC2-dev-4"
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
val version: SemVersion = SemVersion(versionConst)
|
val version: SemVersion = SemVersion(versionConst)
|
||||||
|
@ -2,6 +2,7 @@ package org.example.myplugin
|
|||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import net.mamoe.mirai.console.data.AutoSavePluginConfig
|
import net.mamoe.mirai.console.data.AutoSavePluginConfig
|
||||||
|
import net.mamoe.mirai.console.data.AutoSavePluginData
|
||||||
import net.mamoe.mirai.console.data.value
|
import net.mamoe.mirai.console.data.value
|
||||||
import net.mamoe.mirai.console.permission.PermissionId
|
import net.mamoe.mirai.console.permission.PermissionId
|
||||||
import net.mamoe.mirai.console.permission.PermissionService
|
import net.mamoe.mirai.console.permission.PermissionService
|
||||||
@ -29,3 +30,9 @@ object MyPluginMain : KotlinPlugin(
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
object MyData : AutoSavePluginData("") {
|
||||||
|
val value by value("")
|
||||||
|
val value2 by value<Map<String, String>>()
|
||||||
|
}
|
@ -18,10 +18,12 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
|||||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||||
import org.jetbrains.kotlin.idea.inspections.collections.isCalling
|
import org.jetbrains.kotlin.idea.inspections.collections.isCalling
|
||||||
import org.jetbrains.kotlin.idea.refactoring.fqName.fqName
|
import org.jetbrains.kotlin.idea.refactoring.fqName.fqName
|
||||||
|
import org.jetbrains.kotlin.js.descriptorUtils.getJetTypeFqName
|
||||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||||
import org.jetbrains.kotlin.resolve.checkers.DeclarationChecker
|
import org.jetbrains.kotlin.resolve.checkers.DeclarationChecker
|
||||||
import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext
|
import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext
|
||||||
import org.jetbrains.kotlin.types.SimpleType
|
import org.jetbrains.kotlin.types.SimpleType
|
||||||
|
import org.jetbrains.kotlinx.serialization.compiler.resolve.*
|
||||||
|
|
||||||
|
|
||||||
class PluginDataValuesChecker : DeclarationChecker {
|
class PluginDataValuesChecker : DeclarationChecker {
|
||||||
@ -39,22 +41,23 @@ class PluginDataValuesChecker : DeclarationChecker {
|
|||||||
&& t is SimpleType
|
&& t is SimpleType
|
||||||
}.forEach { (e, callExpr) ->
|
}.forEach { (e, callExpr) ->
|
||||||
val (_, type) = e
|
val (_, type) = e
|
||||||
val classDescriptor = type.constructor.declarationDescriptor?.castOrNull<ClassDescriptor>()
|
val classDescriptor = type.constructor.declarationDescriptor?.castOrNull<ClassDescriptor>() ?: return@forEach
|
||||||
|
|
||||||
|
if (canBeSerializedInternally(classDescriptor)) return@forEach
|
||||||
|
|
||||||
val inspectionTarget = kotlin.run {
|
val inspectionTarget = kotlin.run {
|
||||||
val fqName = type.fqName ?: return@run null
|
val fqName = type.fqName ?: return@run null
|
||||||
callExpr.typeArguments.find { it.typeReference?.isReferencing(fqName) == true }
|
callExpr.typeArguments.find { it.typeReference?.isReferencing(fqName) == true }
|
||||||
} ?: return@forEach
|
} ?: return@forEach
|
||||||
|
|
||||||
if (classDescriptor == null
|
if (!classDescriptor.hasNoArgConstructor())
|
||||||
|| !classDescriptor.hasNoArgConstructor()
|
return@forEach context.report(MiraiConsoleErrors.NOT_CONSTRUCTABLE_TYPE.on(
|
||||||
) return@forEach context.report(MiraiConsoleErrors.NOT_CONSTRUCTABLE_TYPE.on(
|
|
||||||
inspectionTarget,
|
inspectionTarget,
|
||||||
callExpr,
|
callExpr,
|
||||||
type.fqName?.asString().toString())
|
type.fqName?.asString().toString())
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!classDescriptor.hasAnnotation(SERIALIZABLE_FQ_NAME)) // TODO: 2020/9/18 external serializers
|
if (!classDescriptor.hasAnnotation(SERIALIZABLE_FQ_NAME))
|
||||||
return@forEach context.report(MiraiConsoleErrors.UNSERIALIZABLE_TYPE.on(
|
return@forEach context.report(MiraiConsoleErrors.UNSERIALIZABLE_TYPE.on(
|
||||||
inspectionTarget,
|
inspectionTarget,
|
||||||
classDescriptor
|
classDescriptor
|
||||||
@ -62,3 +65,54 @@ class PluginDataValuesChecker : DeclarationChecker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun canBeSerializedInternally(descriptor: ClassDescriptor): Boolean {
|
||||||
|
@Suppress("UNUSED_VARIABLE") val name = when (descriptor.defaultType.getJetTypeFqName(false)) {
|
||||||
|
"kotlin.Unit" -> "UnitSerializer"
|
||||||
|
"Z", "kotlin.Boolean" -> "BooleanSerializer"
|
||||||
|
"B", "kotlin.Byte" -> "ByteSerializer"
|
||||||
|
"S", "kotlin.Short" -> "ShortSerializer"
|
||||||
|
"I", "kotlin.Int" -> "IntSerializer"
|
||||||
|
"J", "kotlin.Long" -> "LongSerializer"
|
||||||
|
"F", "kotlin.Float" -> "FloatSerializer"
|
||||||
|
"D", "kotlin.Double" -> "DoubleSerializer"
|
||||||
|
"C", "kotlin.Char" -> "CharSerializer"
|
||||||
|
"kotlin.String" -> "StringSerializer"
|
||||||
|
"kotlin.Pair" -> "PairSerializer"
|
||||||
|
"kotlin.Triple" -> "TripleSerializer"
|
||||||
|
"kotlin.collections.Collection", "kotlin.collections.List",
|
||||||
|
"kotlin.collections.ArrayList", "kotlin.collections.MutableList",
|
||||||
|
-> "ArrayListSerializer"
|
||||||
|
"kotlin.collections.Set", "kotlin.collections.LinkedHashSet", "kotlin.collections.MutableSet" -> "LinkedHashSetSerializer"
|
||||||
|
"kotlin.collections.HashSet" -> "HashSetSerializer"
|
||||||
|
"kotlin.collections.Map", "kotlin.collections.LinkedHashMap", "kotlin.collections.MutableMap" -> "LinkedHashMapSerializer"
|
||||||
|
"kotlin.collections.HashMap" -> "HashMapSerializer"
|
||||||
|
"kotlin.collections.Map.Entry" -> "MapEntrySerializer"
|
||||||
|
"kotlin.ByteArray" -> "ByteArraySerializer"
|
||||||
|
"kotlin.ShortArray" -> "ShortArraySerializer"
|
||||||
|
"kotlin.IntArray" -> "IntArraySerializer"
|
||||||
|
"kotlin.LongArray" -> "LongArraySerializer"
|
||||||
|
"kotlin.CharArray" -> "CharArraySerializer"
|
||||||
|
"kotlin.FloatArray" -> "FloatArraySerializer"
|
||||||
|
"kotlin.DoubleArray" -> "DoubleArraySerializer"
|
||||||
|
"kotlin.BooleanArray" -> "BooleanArraySerializer"
|
||||||
|
"java.lang.Boolean" -> "BooleanSerializer"
|
||||||
|
"java.lang.Byte" -> "ByteSerializer"
|
||||||
|
"java.lang.Short" -> "ShortSerializer"
|
||||||
|
"java.lang.Integer" -> "IntSerializer"
|
||||||
|
"java.lang.Long" -> "LongSerializer"
|
||||||
|
"java.lang.Float" -> "FloatSerializer"
|
||||||
|
"java.lang.Double" -> "DoubleSerializer"
|
||||||
|
"java.lang.Character" -> "CharSerializer"
|
||||||
|
"java.lang.String" -> "StringSerializer"
|
||||||
|
"java.util.Collection", "java.util.List", "java.util.ArrayList" -> "ArrayListSerializer"
|
||||||
|
"java.util.Set", "java.util.LinkedHashSet" -> "LinkedHashSetSerializer"
|
||||||
|
"java.util.HashSet" -> "HashSetSerializer"
|
||||||
|
"java.util.Map", "java.util.LinkedHashMap" -> "LinkedHashMapSerializer"
|
||||||
|
"java.util.HashMap" -> "HashMapSerializer"
|
||||||
|
"java.util.Map.Entry" -> "MapEntrySerializer"
|
||||||
|
else -> return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user