Annotate kotlin.reflect.jvm.reflect with ExperimentalReflectionOnLambdas

This function was always experimental, as explained in its kdoc, but it
was introduced before opt-in requirement markers were supported. Thus,
breaking changes (such as in KT-42746) were always expected, and the
`@ExperimentalReflectionOnLambdas` annotation just makes it clearer.

 #KT-45486 Fixed
This commit is contained in:
Alexander Udalov
2021-03-11 14:56:48 +01:00
parent 43140db65e
commit fc36178f3a
11 changed files with 42 additions and 3 deletions
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.metadata.deserialization.TypeTable
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmMetadataVersion
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmProtoBufUtil
import org.jetbrains.kotlin.serialization.deserialization.MemberDeserializer
import kotlin.annotation.AnnotationTarget.*
import kotlin.reflect.KFunction
import kotlin.reflect.jvm.internal.EmptyContainerForLocal
import kotlin.reflect.jvm.internal.KFunctionImpl
@@ -31,6 +32,7 @@ import kotlin.reflect.jvm.internal.deserializeToDescriptor
* returns a [KFunction] instance providing introspection capabilities for that lambda or function expression and its parameters.
* Not all features are currently supported, in particular [KCallable.call] and [KCallable.callBy] will fail at the moment.
*/
@ExperimentalReflectionOnLambdas
fun <R> Function<R>.reflect(): KFunction<R>? {
val annotation = javaClass.getAnnotation(Metadata::class.java) ?: return null
val data = annotation.data1.takeUnless(Array<String>::isEmpty) ?: return null
@@ -47,3 +49,30 @@ fun <R> Function<R>.reflect(): KFunction<R>? {
@Suppress("UNCHECKED_CAST")
return KFunctionImpl(EmptyContainerForLocal, descriptor) as KFunction<R>
}
/**
* This annotation marks the experimental kotlin-reflect API that allows to approximate a Kotlin lambda or a function expression instance
* to a [KFunction] instance. The behavior of this API may be changed or the API may be removed completely in any further release.
*
* Any usage of a declaration annotated with `@ExperimentalReflectionOnLambdas` should be accepted either by
* annotating that usage with the [OptIn] annotation, e.g. `@OptIn(ExperimentalReflectionOnLambdas::class)`,
* or by using the compiler argument `-Xopt-in=kotlin.reflect.jvm.ExperimentalReflectionOnLambdas`.
*/
@RequiresOptIn(level = RequiresOptIn.Level.WARNING)
@Retention(AnnotationRetention.BINARY)
@Target(
CLASS,
ANNOTATION_CLASS,
PROPERTY,
FIELD,
LOCAL_VARIABLE,
VALUE_PARAMETER,
CONSTRUCTOR,
FUNCTION,
PROPERTY_GETTER,
PROPERTY_SETTER,
TYPEALIAS
)
@MustBeDocumented
@SinceKotlin("1.5")
annotation class ExperimentalReflectionOnLambdas