From eb240f3a75847cafeed59753e2c428717067c8b6 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 10 Apr 2021 15:45:52 +0800 Subject: [PATCH] Fix JAutoSavePluginData.typedValue, fix #311 --- .../src/data/java/JAutoSavePluginData.kt | 19 +++++---- .../mirai-console/test/AbstractConsoleTest.kt | 30 ++++++++++++++ .../test/data/JavaPluginDescriptionTests.kt | 41 +++++++++++++++++++ 3 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 backend/mirai-console/test/AbstractConsoleTest.kt create mode 100644 backend/mirai-console/test/data/JavaPluginDescriptionTests.kt diff --git a/backend/mirai-console/src/data/java/JAutoSavePluginData.kt b/backend/mirai-console/src/data/java/JAutoSavePluginData.kt index 5118f5ecf..7f13e956b 100644 --- a/backend/mirai-console/src/data/java/JAutoSavePluginData.kt +++ b/backend/mirai-console/src/data/java/JAutoSavePluginData.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 Mamoe Technologies and contributors. * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found through the following link. + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. * - * https://github.com/mamoe/mirai/blob/master/LICENSE + * https://github.com/mamoe/mirai/blob/master/LICENSE */ @file:Suppress("unused", "EXPOSED_SUPER_CLASS") @@ -16,9 +16,12 @@ import net.mamoe.mirai.console.internal.data.cast import net.mamoe.mirai.console.internal.data.setValueBySerializer import net.mamoe.mirai.console.internal.data.valueImpl import net.mamoe.mirai.console.plugin.jvm.JvmPlugin +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.ConcurrentMap import kotlin.reflect.KClass import kotlin.reflect.KType import kotlin.reflect.KTypeProjection +import kotlin.reflect.KVariance import kotlin.reflect.full.createType /** @@ -120,16 +123,16 @@ public abstract class JAutoSavePluginData public constructor(saveName: String) : /** * 构造一个支持泛型的 [Value]. * - * 对于 [Map], [Set], [List] 等标准库类型, 这个函数会尝试构造 [LinkedHashMap], [LinkedHashSet], [ArrayList] 等相关类型. + * 对于 [Map], [Set], [List], [ConcurrentMap] 等标准库类型, 这个函数会尝试构造 [LinkedHashMap], [LinkedHashSet], [ArrayList], [ConcurrentHashMap] 等相关类型. * 而对于自定义数据类型, 本函数只会反射获取 [objectInstance][KClass.objectInstance] 或使用*无参构造器*构造实例. * * @param type Kotlin 类型. 可通过 [createKType] 获得 * * @param T 类型 T. 仅支持: * - 基础数据类型, [String] - * - 标准库集合类型 ([List], [Map], [Set]) + * - 标准库集合类型 ([List], [Map], [Set], [ConcurrentMap]) * - 标准库数据类型 ([Map.Entry], [Pair], [Triple]) - * - 使用 [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization) 的 [Serializable] 标记的类 + * - 使用 [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization) 的 [Serializable] 标记的 **Kotlin** 类 */ @JvmOverloads public fun typedValue(type: KType, default: T? = null): SerializerAwareValue { @@ -151,7 +154,7 @@ public abstract class JAutoSavePluginData public constructor(saveName: String) : */ @JvmStatic public fun createKType(clazz: Class, nullable: Boolean, vararg genericArguments: KType): KType { - return clazz.kotlin.createType(genericArguments.map { KTypeProjection(null, it) }, nullable) + return clazz.kotlin.createType(genericArguments.map { KTypeProjection(KVariance.INVARIANT, it) }, nullable) } /** diff --git a/backend/mirai-console/test/AbstractConsoleTest.kt b/backend/mirai-console/test/AbstractConsoleTest.kt new file mode 100644 index 000000000..60f7e19fc --- /dev/null +++ b/backend/mirai-console/test/AbstractConsoleTest.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2019-2021 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console + +import kotlinx.coroutines.cancel +import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription +import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach + +abstract class AbstractConsoleTest { + val mockPlugin = object : KotlinPlugin(JvmPluginDescription("org.test.test", "1.0.0")) {} + + @BeforeEach + fun beforeTest() { + initTestEnvironment() + } + + @AfterEach + fun afterTest() { + MiraiConsole.cancel() + } +} \ No newline at end of file diff --git a/backend/mirai-console/test/data/JavaPluginDescriptionTests.kt b/backend/mirai-console/test/data/JavaPluginDescriptionTests.kt new file mode 100644 index 000000000..bf92ee6b8 --- /dev/null +++ b/backend/mirai-console/test/data/JavaPluginDescriptionTests.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2019-2021 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ +package data + +import net.mamoe.mirai.console.AbstractConsoleTest +import net.mamoe.mirai.console.data.Value +import net.mamoe.mirai.console.data.java.JAutoSavePluginData +import net.mamoe.mirai.console.plugin.jvm.reloadPluginData +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals + +@Suppress("unused") +class JavaPluginDescriptionTests : AbstractConsoleTest() { + + @Test + fun testSimpleValue() { + class MyJavaData : JAutoSavePluginData("data") { + val list: Value = value("str") + } + + val d = MyJavaData() + mockPlugin.reloadPluginData(d) + assertEquals("str", d.list.value) + } + + @Test + fun testValueSet() { + class MyJavaData : JAutoSavePluginData("data") { + val list: Value> = typedValue(createKType(MutableSet::class.java, createKType(String::class.java))) + } + + mockPlugin.reloadPluginData(MyJavaData()) + } + +} \ No newline at end of file