diff --git a/compiler/testData/codegen/box/reflection/annotations/findAnnotation.kt b/compiler/testData/codegen/box/reflection/annotations/findAnnotation.kt index 3e3824e2f42..38db10486b4 100644 --- a/compiler/testData/codegen/box/reflection/annotations/findAnnotation.kt +++ b/compiler/testData/codegen/box/reflection/annotations/findAnnotation.kt @@ -2,6 +2,7 @@ // WITH_REFLECT import kotlin.reflect.findAnnotation +import kotlin.test.assertNull annotation class Yes(val value: String) annotation class No(val value: String) @@ -10,6 +11,11 @@ annotation class No(val value: String) @No("Fail") class Foo +class Bar + fun box(): String { + assertNull(Bar::class.findAnnotation()) + assertNull(Bar::class.findAnnotation()) + return Foo::class.findAnnotation()?.value ?: "Fail: no annotation" } diff --git a/compiler/testData/codegen/light-analysis/reflection/annotations/findAnnotation.txt b/compiler/testData/codegen/light-analysis/reflection/annotations/findAnnotation.txt index 51af943eea1..eb6e30b0c22 100644 --- a/compiler/testData/codegen/light-analysis/reflection/annotations/findAnnotation.txt +++ b/compiler/testData/codegen/light-analysis/reflection/annotations/findAnnotation.txt @@ -1,3 +1,8 @@ +@kotlin.Metadata +public final class Bar { + public method (): void +} + @kotlin.Metadata public final class FindAnnotationKt { public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String diff --git a/core/reflection.jvm/src/kotlin/reflect/KAnnotatedElements.kt b/core/reflection.jvm/src/kotlin/reflect/KAnnotatedElements.kt index 5b8a07f8fcb..4fe78f34a94 100644 --- a/core/reflection.jvm/src/kotlin/reflect/KAnnotatedElements.kt +++ b/core/reflection.jvm/src/kotlin/reflect/KAnnotatedElements.kt @@ -23,4 +23,4 @@ package kotlin.reflect @Deprecated("Use 'findAnnotation' from kotlin.reflect.full package", ReplaceWith("this.findAnnotation()", "kotlin.reflect.full.findAnnotation"), level = DeprecationLevel.WARNING) @Suppress("UNCHECKED_CAST") inline fun KAnnotatedElement.findAnnotation(): T? = - annotations.first { it is T } as T + annotations.firstOrNull { it is T } as T? diff --git a/core/reflection.jvm/src/kotlin/reflect/full/KAnnotatedElements.kt b/core/reflection.jvm/src/kotlin/reflect/full/KAnnotatedElements.kt index 5b2dd07e879..0d940470835 100644 --- a/core/reflection.jvm/src/kotlin/reflect/full/KAnnotatedElements.kt +++ b/core/reflection.jvm/src/kotlin/reflect/full/KAnnotatedElements.kt @@ -24,4 +24,4 @@ import kotlin.reflect.* */ @Suppress("UNCHECKED_CAST") inline fun KAnnotatedElement.findAnnotation(): T? = - annotations.first { it is T } as T + annotations.firstOrNull { it is T } as T?