[kpm] kotlin-tooling-core: Move Type into Extras.Type
^KT-52568 Verification Pending
This commit is contained in:
committed by
Space
parent
08bb163021
commit
12b6d32e73
+21
@@ -63,6 +63,27 @@ import java.io.Serializable
|
||||
* ```
|
||||
*/
|
||||
interface Extras : Collection<Entry<out Any>> {
|
||||
class Type<T> @UnsafeApi("Use 'extrasTypeOf()' instead") @PublishedApi internal constructor(
|
||||
internal val signature: String
|
||||
) : Serializable {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is Type<*>) return false
|
||||
if (other.signature != this.signature) return false
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return 31 * signature.hashCode()
|
||||
}
|
||||
|
||||
override fun toString(): String = signature
|
||||
|
||||
internal companion object {
|
||||
private const val serialVersionUID = 0L
|
||||
}
|
||||
}
|
||||
|
||||
/* Not implemented as data class to ensure more controllable binary compatibility */
|
||||
class Key<T : Any> @PublishedApi internal constructor(
|
||||
val type: Type<T>,
|
||||
|
||||
+8
-1
@@ -5,6 +5,8 @@
|
||||
|
||||
package org.jetbrains.kotlin.tooling.core
|
||||
|
||||
import kotlin.reflect.typeOf
|
||||
|
||||
/**
|
||||
* Creates a value based key for accessing any [Extras] container
|
||||
*
|
||||
@@ -25,7 +27,7 @@ package org.jetbrains.kotlin.tooling.core
|
||||
* ```
|
||||
*/
|
||||
inline fun <reified T : Any> extrasKeyOf(name: String? = null): Extras.Key<T> =
|
||||
Extras.Key(Type(), name)
|
||||
Extras.Key(extrasTypeOf(), name)
|
||||
|
||||
fun emptyExtras(): Extras = EmptyExtras
|
||||
|
||||
@@ -37,6 +39,11 @@ fun mutableExtrasOf(): MutableExtras = MutableExtrasImpl()
|
||||
|
||||
fun mutableExtrasOf(vararg entries: Extras.Entry<*>): MutableExtras = MutableExtrasImpl(entries.toList())
|
||||
|
||||
inline fun <reified T> extrasTypeOf(): Extras.Type<T> {
|
||||
@OptIn(UnsafeApi::class, ExperimentalStdlibApi::class)
|
||||
return Extras.Type(renderReifiedTypeSignatureString(typeOf<T>()))
|
||||
}
|
||||
|
||||
fun Iterable<Extras.Entry<*>>.toExtras(): Extras = ImmutableExtrasImpl(this)
|
||||
|
||||
fun Iterable<Extras.Entry<*>>.toMutableExtras(): MutableExtras = MutableExtrasImpl(this)
|
||||
|
||||
+1
-29
@@ -4,41 +4,13 @@
|
||||
*/
|
||||
|
||||
@file:Suppress("FunctionName")
|
||||
@file:JvmName("TypeUtils")
|
||||
|
||||
package org.jetbrains.kotlin.tooling.core
|
||||
|
||||
import java.io.Serializable
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KType
|
||||
import kotlin.reflect.KVariance
|
||||
import kotlin.reflect.typeOf
|
||||
|
||||
inline fun <reified T> Type(): Type<T> {
|
||||
@OptIn(UnsafeApi::class, ExperimentalStdlibApi::class)
|
||||
return Type(renderReifiedTypeSignatureString(typeOf<T>()))
|
||||
}
|
||||
|
||||
class Type<T>
|
||||
@UnsafeApi("Use 'Type' instead")
|
||||
@PublishedApi internal constructor(internal val signature: String) : Serializable {
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is Type<*>) return false
|
||||
if (other.signature != this.signature) return false
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return 31 * signature.hashCode()
|
||||
}
|
||||
|
||||
override fun toString(): String = signature
|
||||
|
||||
internal companion object {
|
||||
private const val serialVersionUID = 0L
|
||||
}
|
||||
}
|
||||
|
||||
@PublishedApi
|
||||
@UnsafeApi("Use 'ReifiedTypeSignature' instead")
|
||||
+14
-14
@@ -24,21 +24,21 @@ class TypeTest {
|
||||
@Test
|
||||
fun `test - sample 0`() {
|
||||
assertEquals(
|
||||
"kotlin.collections.List<kotlin.Int>", Type<List<Int>>().signature
|
||||
"kotlin.collections.List<kotlin.Int>", extrasTypeOf<List<Int>>().signature
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test - sample 1`() {
|
||||
assertEquals(
|
||||
"kotlin.collections.List<*>", Type<List<*>>().signature
|
||||
"kotlin.collections.List<*>", extrasTypeOf<List<*>>().signature
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test - sample 2`() {
|
||||
assertEquals(
|
||||
"kotlin.String", Type<String>().signature
|
||||
"kotlin.String", extrasTypeOf<String>().signature
|
||||
)
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ class TypeTest {
|
||||
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
|
||||
extrasTypeOf<Map<String, List<Pair<Int, String>>>>().signature
|
||||
)
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ class TypeTest {
|
||||
fun `test - sample 5 - inner class`() {
|
||||
assertEquals(
|
||||
"org.jetbrains.kotlin.tooling.core.TypeTest.Outer.Inner",
|
||||
Type<Outer.Inner>().signature
|
||||
extrasTypeOf<Outer.Inner>().signature
|
||||
)
|
||||
}
|
||||
|
||||
@@ -63,43 +63,43 @@ class TypeTest {
|
||||
fun `test - sample 6 - recursive types`() {
|
||||
assertEquals(
|
||||
"org.jetbrains.kotlin.tooling.core.TypeTest.Recursive<*>",
|
||||
Type<Recursive<*>>().signature
|
||||
extrasTypeOf<Recursive<*>>().signature
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
"org.jetbrains.kotlin.tooling.core.TypeTest.AnyRecursive",
|
||||
Type<AnyRecursive>().signature
|
||||
extrasTypeOf<AnyRecursive>().signature
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
"org.jetbrains.kotlin.tooling.core.TypeTest" +
|
||||
".Recursive<org.jetbrains.kotlin.tooling.core.TypeTest.AnyRecursive>",
|
||||
Type<Recursive<AnyRecursive>>().signature
|
||||
extrasTypeOf<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)
|
||||
assertEquals("kotlin.collections.List<*>?", extrasTypeOf<List<*>?>().signature)
|
||||
assertEquals("kotlin.collections.List<kotlin.Int?>", extrasTypeOf<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
|
||||
extrasTypeOf<Box<out Int>>().signature
|
||||
)
|
||||
assertEquals(
|
||||
"org.jetbrains.kotlin.tooling.core.TypeTest.Box<in kotlin.Int>",
|
||||
Type<Box<in Int>>().signature
|
||||
extrasTypeOf<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>>())
|
||||
assertEquals(extrasTypeOf<List<Int>>(), extrasTypeOf<List<Int>>())
|
||||
assertNotEquals(extrasTypeOf<List<Int?>>(), extrasTypeOf<List<Int>>())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user