Introduce OptionalExpectation for annotations missing on some platforms

This commits adds a new annotation OptionalExpectation to the standard
library, which is experimental. To enable its usage, either pass
'-Xuse-experimental=kotlin.ExperimentalMultiplatform' as a compiler
argument, or '-Xuse-experimental=kotlin.Experimental' and also annotate
each usage with `@UseExperimental(ExperimentalMultiplatform::class)`

 #KT-18882 Fixed
This commit is contained in:
Alexander Udalov
2018-05-17 18:17:48 +02:00
parent ec5110e1f4
commit bf3419c3bd
31 changed files with 434 additions and 21 deletions
+13
View File
@@ -0,0 +1,13 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
@JvmName("bar")
fun foo() {}
fun getJvmName(): JvmName? =
Class.forName("LoadJvmNameKt").declaredMethods.single { it.name == "bar" }.getAnnotation(JvmName::class.java)
fun box(): String {
// JvmName is binary-retained and should not be accessible via reflection
return if (getJvmName() == null) "OK" else "Fail"
}
@@ -0,0 +1,49 @@
// !LANGUAGE: +MultiPlatformProjects
// !USE_EXPERIMENTAL: kotlin.ExperimentalMultiplatform
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: common.kt
@OptionalExpectation
expect annotation class Anno(val s: String)
// FILE: jvm.kt
import java.lang.reflect.AnnotatedElement
@Anno("Foo")
class Foo @Anno("<init>") constructor(@Anno("x") x: Int) {
@Anno("bar")
fun bar() {}
@Anno("getX")
var x = x
@Anno("setX")
set
@Anno("Nested")
interface Nested
}
private fun check(element: AnnotatedElement) {
check(element.annotations)
}
private fun check(annotations: Array<Annotation>) {
val filtered = annotations.filterNot { it.annotationClass.java.name == "kotlin.Metadata" }
if (filtered.isNotEmpty()) {
throw AssertionError("Annotations should be empty: $filtered")
}
}
fun box(): String {
val foo = Foo::class.java
check(foo)
check(Foo.Nested::class.java)
check(foo.declaredMethods.single { it.name == "bar" })
check(foo.declaredMethods.single { it.name == "getX" })
check(foo.declaredMethods.single { it.name == "setX" })
check(foo.constructors.single())
check(foo.constructors.single().parameterAnnotations.single())
return "OK"
}