Fix KAnnotatedElement.findAnnotation when no annotation is found
#KT-15540 Fixed
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
// WITH_REFLECT
|
// WITH_REFLECT
|
||||||
|
|
||||||
import kotlin.reflect.findAnnotation
|
import kotlin.reflect.findAnnotation
|
||||||
|
import kotlin.test.assertNull
|
||||||
|
|
||||||
annotation class Yes(val value: String)
|
annotation class Yes(val value: String)
|
||||||
annotation class No(val value: String)
|
annotation class No(val value: String)
|
||||||
@@ -10,6 +11,11 @@ annotation class No(val value: String)
|
|||||||
@No("Fail")
|
@No("Fail")
|
||||||
class Foo
|
class Foo
|
||||||
|
|
||||||
|
class Bar
|
||||||
|
|
||||||
fun box(): String {
|
fun box(): String {
|
||||||
|
assertNull(Bar::class.findAnnotation<Yes>())
|
||||||
|
assertNull(Bar::class.findAnnotation<No>())
|
||||||
|
|
||||||
return Foo::class.findAnnotation<Yes>()?.value ?: "Fail: no annotation"
|
return Foo::class.findAnnotation<Yes>()?.value ?: "Fail: no annotation"
|
||||||
}
|
}
|
||||||
|
|||||||
+5
@@ -1,3 +1,8 @@
|
|||||||
|
@kotlin.Metadata
|
||||||
|
public final class Bar {
|
||||||
|
public method <init>(): void
|
||||||
|
}
|
||||||
|
|
||||||
@kotlin.Metadata
|
@kotlin.Metadata
|
||||||
public final class FindAnnotationKt {
|
public final class FindAnnotationKt {
|
||||||
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||||
|
|||||||
@@ -23,4 +23,4 @@ package kotlin.reflect
|
|||||||
@Deprecated("Use 'findAnnotation' from kotlin.reflect.full package", ReplaceWith("this.findAnnotation<T>()", "kotlin.reflect.full.findAnnotation"), level = DeprecationLevel.WARNING)
|
@Deprecated("Use 'findAnnotation' from kotlin.reflect.full package", ReplaceWith("this.findAnnotation<T>()", "kotlin.reflect.full.findAnnotation"), level = DeprecationLevel.WARNING)
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
inline fun <reified T : Annotation> KAnnotatedElement.findAnnotation(): T? =
|
inline fun <reified T : Annotation> KAnnotatedElement.findAnnotation(): T? =
|
||||||
annotations.first { it is T } as T
|
annotations.firstOrNull { it is T } as T?
|
||||||
|
|||||||
@@ -24,4 +24,4 @@ import kotlin.reflect.*
|
|||||||
*/
|
*/
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
inline fun <reified T : Annotation> KAnnotatedElement.findAnnotation(): T? =
|
inline fun <reified T : Annotation> KAnnotatedElement.findAnnotation(): T? =
|
||||||
annotations.first { it is T } as T
|
annotations.firstOrNull { it is T } as T?
|
||||||
|
|||||||
Reference in New Issue
Block a user