KT-51082 introduce Class<E : Enum<E>>.declaringJavaClass as a future replacement for synthetic declaringClass
This commit is contained in:
committed by
Space
parent
6bb24e0153
commit
82c959c4be
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
* 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.
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
*/
|
*/
|
||||||
@file:JvmName("JvmClassMappingKt")
|
@file:JvmName("JvmClassMappingKt")
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
package kotlin.jvm
|
package kotlin.jvm
|
||||||
|
|
||||||
|
import kotlin.internal.InlineOnly
|
||||||
import kotlin.jvm.internal.ClassBasedDeclarationContainer
|
import kotlin.jvm.internal.ClassBasedDeclarationContainer
|
||||||
import kotlin.jvm.internal.Reflection
|
import kotlin.jvm.internal.Reflection
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
@@ -105,3 +106,13 @@ public fun <reified T : Any> Array<*>.isArrayOf(): Boolean =
|
|||||||
*/
|
*/
|
||||||
public val <T : Annotation> T.annotationClass: KClass<out T>
|
public val <T : Annotation> T.annotationClass: KClass<out T>
|
||||||
get() = (this as java.lang.annotation.Annotation).annotationType().kotlin as KClass<out T>
|
get() = (this as java.lang.annotation.Annotation).annotationType().kotlin as KClass<out T>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a Java [Class] instance of the enum the given constant belongs to.
|
||||||
|
*
|
||||||
|
* @see java.lang.Enum.getDeclaringClass
|
||||||
|
*/
|
||||||
|
@SinceKotlin("1.7")
|
||||||
|
@InlineOnly
|
||||||
|
public inline val <E : Enum<E>> E.declaringJavaClass: Class<E>
|
||||||
|
get() = (this as java.lang.Enum<E>).declaringClass
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* 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 test.enums
|
||||||
|
|
||||||
|
import java.util.*
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.time.DurationUnit
|
||||||
|
|
||||||
|
class EnumDeclaringJavaClassTest {
|
||||||
|
|
||||||
|
private enum class TestEnum {
|
||||||
|
E
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testDeclaringClass() {
|
||||||
|
// This file
|
||||||
|
assertEquals(TestEnum::class.java, TestEnum.E.declaringJavaClass)
|
||||||
|
// From Java
|
||||||
|
assertEquals(TimeUnit::class.java, TimeUnit.MILLISECONDS.declaringJavaClass)
|
||||||
|
// From Kotlin
|
||||||
|
assertEquals(DurationUnit::class.java, DurationUnit.MILLISECONDS.declaringJavaClass)
|
||||||
|
}
|
||||||
|
|
||||||
|
private inline fun <reified E : Enum<E>> E.declaring() = declaringJavaClass
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testReified() {
|
||||||
|
// This file
|
||||||
|
assertEquals(TestEnum::class.java, TestEnum.E.declaring())
|
||||||
|
// From Java
|
||||||
|
assertEquals(TimeUnit::class.java, TimeUnit.MILLISECONDS.declaring())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testEnumSet() {
|
||||||
|
val set = EnumSet.noneOf(TestEnum.E.declaringJavaClass)
|
||||||
|
set.addAll(TestEnum.E.declaringJavaClass.enumConstants.toList())
|
||||||
|
assertEquals(EnumSet.of(TestEnum.E), set)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user