[kpm] kotlin-tooling-core: Expose 'Type' in Extras.Key
This type can later be used for extension points to register serializers for certain known types. ^KT-52568 In Progress
This commit is contained in:
committed by
Space
parent
9a8832a241
commit
58e2f6d5d7
+3
-3
@@ -65,7 +65,7 @@ import java.io.Serializable
|
|||||||
interface Extras : Collection<Entry<out Any>> {
|
interface Extras : Collection<Entry<out Any>> {
|
||||||
/* Not implemented as data class to ensure more controllable binary compatibility */
|
/* Not implemented as data class to ensure more controllable binary compatibility */
|
||||||
class Key<T : Any> @PublishedApi internal constructor(
|
class Key<T : Any> @PublishedApi internal constructor(
|
||||||
internal val type: ReifiedTypeSignature<T>,
|
val type: Type<T>,
|
||||||
val name: String? = null,
|
val name: String? = null,
|
||||||
) : Serializable {
|
) : Serializable {
|
||||||
|
|
||||||
@@ -95,8 +95,8 @@ interface Extras : Collection<Entry<out Any>> {
|
|||||||
fun fromString(stableString: String): Key<*> {
|
fun fromString(stableString: String): Key<*> {
|
||||||
@OptIn(UnsafeApi::class) return if (stableString.contains(';')) {
|
@OptIn(UnsafeApi::class) return if (stableString.contains(';')) {
|
||||||
val split = stableString.split(';', limit = 2)
|
val split = stableString.split(';', limit = 2)
|
||||||
Key(ReifiedTypeSignature(split[0]), split[1])
|
Key(Type(split[0]), split[1])
|
||||||
} else Key(ReifiedTypeSignature(stableString))
|
} else Key(Type(stableString))
|
||||||
}
|
}
|
||||||
|
|
||||||
private const val serialVersionUID = 0L
|
private const val serialVersionUID = 0L
|
||||||
|
|||||||
+1
-1
@@ -25,7 +25,7 @@ package org.jetbrains.kotlin.tooling.core
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
inline fun <reified T : Any> extrasKeyOf(name: String? = null): Extras.Key<T> =
|
inline fun <reified T : Any> extrasKeyOf(name: String? = null): Extras.Key<T> =
|
||||||
Extras.Key(ReifiedTypeSignature(), name)
|
Extras.Key(Type(), name)
|
||||||
|
|
||||||
fun emptyExtras(): Extras = EmptyExtras
|
fun emptyExtras(): Extras = EmptyExtras
|
||||||
|
|
||||||
|
|||||||
+6
-8
@@ -13,20 +13,18 @@ import kotlin.reflect.KType
|
|||||||
import kotlin.reflect.KVariance
|
import kotlin.reflect.KVariance
|
||||||
import kotlin.reflect.typeOf
|
import kotlin.reflect.typeOf
|
||||||
|
|
||||||
@PublishedApi
|
inline fun <reified T> Type(): Type<T> {
|
||||||
internal inline fun <reified T> ReifiedTypeSignature(): ReifiedTypeSignature<T> {
|
|
||||||
@OptIn(UnsafeApi::class, ExperimentalStdlibApi::class)
|
@OptIn(UnsafeApi::class, ExperimentalStdlibApi::class)
|
||||||
return ReifiedTypeSignature(renderReifiedTypeSignatureString(typeOf<T>()))
|
return Type(renderReifiedTypeSignatureString(typeOf<T>()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@PublishedApi
|
class Type<T>
|
||||||
internal class ReifiedTypeSignature<T>
|
@UnsafeApi("Use 'Type' instead")
|
||||||
@UnsafeApi("Use 'reifiedTypeSignatureOf' instead")
|
@PublishedApi internal constructor(internal val signature: String) : Serializable {
|
||||||
@PublishedApi internal constructor(val signature: String) : Serializable {
|
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
override fun equals(other: Any?): Boolean {
|
||||||
if (this === other) return true
|
if (this === other) return true
|
||||||
if (other !is ReifiedTypeSignature<*>) return false
|
if (other !is Type<*>) return false
|
||||||
if (other.signature != this.signature) return false
|
if (other.signature != this.signature) return false
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
-105
@@ -1,105 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.kotlin.tooling.core
|
|
||||||
|
|
||||||
import kotlin.test.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
import kotlin.test.assertNotEquals
|
|
||||||
|
|
||||||
class ReifiedTypeSignatureTest {
|
|
||||||
|
|
||||||
class Box<T>
|
|
||||||
|
|
||||||
class Outer {
|
|
||||||
inner class Inner
|
|
||||||
}
|
|
||||||
|
|
||||||
open class Recursive<T : Recursive<T>>
|
|
||||||
|
|
||||||
class AnyRecursive : Recursive<AnyRecursive>()
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `test - sample 0`() {
|
|
||||||
assertEquals(
|
|
||||||
"kotlin.collections.List<kotlin.Int>", ReifiedTypeSignature<List<Int>>().signature
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `test - sample 1`() {
|
|
||||||
assertEquals(
|
|
||||||
"kotlin.collections.List<*>", ReifiedTypeSignature<List<*>>().signature
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `test - sample 2`() {
|
|
||||||
assertEquals(
|
|
||||||
"kotlin.String", ReifiedTypeSignature<String>().signature
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `test - sample 3`() {
|
|
||||||
assertEquals(
|
|
||||||
"kotlin.collections.Map<kotlin.String, kotlin.collections.List<kotlin.Pair<kotlin.Int, kotlin.String>>>",
|
|
||||||
ReifiedTypeSignature<Map<String, List<Pair<Int, String>>>>().signature
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `test - sample 5 - inner class`() {
|
|
||||||
assertEquals(
|
|
||||||
"org.jetbrains.kotlin.tooling.core.ReifiedTypeSignatureTest.Outer.Inner",
|
|
||||||
ReifiedTypeSignature<Outer.Inner>().signature
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `test - sample 6 - recursive types`() {
|
|
||||||
assertEquals(
|
|
||||||
"org.jetbrains.kotlin.tooling.core.ReifiedTypeSignatureTest.Recursive<*>",
|
|
||||||
ReifiedTypeSignature<Recursive<*>>().signature
|
|
||||||
)
|
|
||||||
|
|
||||||
assertEquals(
|
|
||||||
"org.jetbrains.kotlin.tooling.core.ReifiedTypeSignatureTest.AnyRecursive",
|
|
||||||
ReifiedTypeSignature<AnyRecursive>().signature
|
|
||||||
)
|
|
||||||
|
|
||||||
assertEquals(
|
|
||||||
"org.jetbrains.kotlin.tooling.core.ReifiedTypeSignatureTest" +
|
|
||||||
".Recursive<org.jetbrains.kotlin.tooling.core.ReifiedTypeSignatureTest.AnyRecursive>",
|
|
||||||
ReifiedTypeSignature<Recursive<AnyRecursive>>().signature
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `test sample 7 - nullable`() {
|
|
||||||
assertEquals("kotlin.collections.List<*>?", ReifiedTypeSignature<List<*>?>().signature)
|
|
||||||
assertEquals("kotlin.collections.List<kotlin.Int?>", ReifiedTypeSignature<List<Int?>>().signature)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `test sample 8 - variance`() {
|
|
||||||
assertEquals(
|
|
||||||
"org.jetbrains.kotlin.tooling.core.ReifiedTypeSignatureTest.Box<out kotlin.Int>",
|
|
||||||
ReifiedTypeSignature<Box<out Int>>().signature
|
|
||||||
)
|
|
||||||
assertEquals(
|
|
||||||
"org.jetbrains.kotlin.tooling.core.ReifiedTypeSignatureTest.Box<in kotlin.Int>",
|
|
||||||
ReifiedTypeSignature<Box<in Int>>().signature
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Suppress("RemoveExplicitTypeArguments", "TYPE_INFERENCE_ONLY_INPUT_TYPES_WARNING")
|
|
||||||
@Test
|
|
||||||
fun `test - equals`() {
|
|
||||||
assertEquals(ReifiedTypeSignature<List<Int>>(), ReifiedTypeSignature<List<Int>>())
|
|
||||||
assertNotEquals(ReifiedTypeSignature<List<Int?>>(), ReifiedTypeSignature<List<Int>>())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+105
@@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.tooling.core
|
||||||
|
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertNotEquals
|
||||||
|
|
||||||
|
class TypeTest {
|
||||||
|
|
||||||
|
class Box<T>
|
||||||
|
|
||||||
|
class Outer {
|
||||||
|
inner class Inner
|
||||||
|
}
|
||||||
|
|
||||||
|
open class Recursive<T : Recursive<T>>
|
||||||
|
|
||||||
|
class AnyRecursive : Recursive<AnyRecursive>()
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test - sample 0`() {
|
||||||
|
assertEquals(
|
||||||
|
"kotlin.collections.List<kotlin.Int>", Type<List<Int>>().signature
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test - sample 1`() {
|
||||||
|
assertEquals(
|
||||||
|
"kotlin.collections.List<*>", Type<List<*>>().signature
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test - sample 2`() {
|
||||||
|
assertEquals(
|
||||||
|
"kotlin.String", Type<String>().signature
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test - sample 3`() {
|
||||||
|
assertEquals(
|
||||||
|
"kotlin.collections.Map<kotlin.String, kotlin.collections.List<kotlin.Pair<kotlin.Int, kotlin.String>>>",
|
||||||
|
Type<Map<String, List<Pair<Int, String>>>>().signature
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test - sample 5 - inner class`() {
|
||||||
|
assertEquals(
|
||||||
|
"org.jetbrains.kotlin.tooling.core.TypeTest.Outer.Inner",
|
||||||
|
Type<Outer.Inner>().signature
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test - sample 6 - recursive types`() {
|
||||||
|
assertEquals(
|
||||||
|
"org.jetbrains.kotlin.tooling.core.TypeTest.Recursive<*>",
|
||||||
|
Type<Recursive<*>>().signature
|
||||||
|
)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
"org.jetbrains.kotlin.tooling.core.TypeTest.AnyRecursive",
|
||||||
|
Type<AnyRecursive>().signature
|
||||||
|
)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
"org.jetbrains.kotlin.tooling.core.TypeTest" +
|
||||||
|
".Recursive<org.jetbrains.kotlin.tooling.core.TypeTest.AnyRecursive>",
|
||||||
|
Type<Recursive<AnyRecursive>>().signature
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test sample 7 - nullable`() {
|
||||||
|
assertEquals("kotlin.collections.List<*>?", Type<List<*>?>().signature)
|
||||||
|
assertEquals("kotlin.collections.List<kotlin.Int?>", Type<List<Int?>>().signature)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test sample 8 - variance`() {
|
||||||
|
assertEquals(
|
||||||
|
"org.jetbrains.kotlin.tooling.core.TypeTest.Box<out kotlin.Int>",
|
||||||
|
Type<Box<out Int>>().signature
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"org.jetbrains.kotlin.tooling.core.TypeTest.Box<in kotlin.Int>",
|
||||||
|
Type<Box<in Int>>().signature
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("RemoveExplicitTypeArguments", "TYPE_INFERENCE_ONLY_INPUT_TYPES_WARNING")
|
||||||
|
@Test
|
||||||
|
fun `test - equals`() {
|
||||||
|
assertEquals(Type<List<Int>>(), Type<List<Int>>())
|
||||||
|
assertNotEquals(Type<List<Int?>>(), Type<List<Int>>())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user