Merge boxWithStdlib testData into box, delete BoxWithStdlib test
This commit is contained in:
committed by
Alexander Udalov
parent
22bfc9786a
commit
06a67e6602
@@ -0,0 +1,32 @@
|
||||
// WITH_RUNTIME
|
||||
// KT-5665
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class First
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Second(val value: String)
|
||||
|
||||
enum class E {
|
||||
@First
|
||||
E1 {
|
||||
fun foo() = "something"
|
||||
},
|
||||
|
||||
@Second("OK")
|
||||
E2
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val e = E::class.java
|
||||
|
||||
val e1 = e.getDeclaredField(E.E1.toString()).getAnnotations()
|
||||
if (e1.size != 1) return "Fail E1 size: ${e1.toList()}"
|
||||
if (e1[0].annotationClass.java != First::class.java) return "Fail E1: ${e1.toList()}"
|
||||
|
||||
val e2 = e.getDeclaredField(E.E2.toString()).getAnnotations()
|
||||
if (e2.size != 1) return "Fail E2 size: ${e2.toList()}"
|
||||
if (e2[0].annotationClass.java != Second::class.java) return "Fail E2: ${e2.toList()}"
|
||||
|
||||
return (e2[0] as Second).value
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import java.lang.reflect.Method
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Ann(val x: String)
|
||||
|
||||
fun foo0(block: () -> Unit) = block.javaClass
|
||||
fun foo1(block: (String) -> Unit) = block.javaClass
|
||||
|
||||
fun testMethod(method: Method, name: String) {
|
||||
assertEquals("OK", method.getAnnotation(Ann::class.java).x, "On method of test named `$name`")
|
||||
|
||||
for ((index, annotations) in method.getParameterAnnotations().withIndex()) {
|
||||
val ann = annotations.filterIsInstance<Ann>().single()
|
||||
assertEquals("OK$index", ann.x, "On parameter $index of test named `$name`")
|
||||
}
|
||||
}
|
||||
|
||||
fun testClass(clazz: Class<*>, name: String) {
|
||||
val invokes = clazz.getDeclaredMethods().single() { !it.isBridge() }
|
||||
testMethod(invokes, name)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
testClass(foo0( @Ann("OK") fun() {} ), "1")
|
||||
testClass(foo1( @Ann("OK") fun(@Ann("OK0") x: String) {} ), "2")
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import java.lang.reflect.Method
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Ann(val x: String)
|
||||
|
||||
fun foo0(block: () -> Unit) = block.javaClass
|
||||
|
||||
fun testMethod(method: Method, name: String) {
|
||||
assertEquals("OK", method.getAnnotation(Ann::class.java).x, "On method of test named `$name`")
|
||||
|
||||
for ((index, annotations) in method.getParameterAnnotations().withIndex()) {
|
||||
val ann = annotations.filterIsInstance<Ann>().single()
|
||||
assertEquals("OK$index", ann.x, "On parameter $index of test named `$name`")
|
||||
}
|
||||
}
|
||||
|
||||
fun testClass(clazz: Class<*>, name: String) {
|
||||
val invokes = clazz.getDeclaredMethods().single() { !it.isBridge() }
|
||||
testMethod(invokes, name)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
testClass(foo0(@Ann("OK") { }), "1")
|
||||
testClass(foo0( @Ann("OK") { }), "2")
|
||||
|
||||
testClass(foo0() @Ann("OK") { }, "3")
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
annotation class Ann(val v: String = "???")
|
||||
@Ann open class My
|
||||
fun box(): String {
|
||||
val v = @Ann("OK") object: My() {}
|
||||
val klass = v.javaClass
|
||||
|
||||
val annotations = klass.annotations.toList()
|
||||
// Ann, kotlin.Metadata
|
||||
if (annotations.size != 2) return "Fail annotations size is ${annotations.size}: $annotations"
|
||||
val annotation = annotations.filterIsInstance<Ann>().firstOrNull()
|
||||
?: return "Fail no @Ann: $annotations"
|
||||
|
||||
return annotation.v
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Ann(val x: Int)
|
||||
class A {
|
||||
@Ann(1) fun foo(x: Int, y: Int = 2, z: Int) {}
|
||||
|
||||
@Ann(1) constructor(x: Int, y: Int = 2, z: Int)
|
||||
}
|
||||
|
||||
class B @Ann(1) constructor(x: Int, y: Int = 2, z: Int) {}
|
||||
|
||||
fun test(name: String, annotations: Array<out Annotation>) {
|
||||
assertEquals(1, annotations.filterIsInstance<Ann>().single().x, "$name[0]")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val foo = A::class.java.getDeclaredMethods().first { it.getName() == "foo" }
|
||||
test("foo", foo.getDeclaredAnnotations())
|
||||
|
||||
val fooDefault = A::class.java.getDeclaredMethods().first { it.getName() == "foo\$default" }
|
||||
test("foo", foo.getDeclaredAnnotations())
|
||||
|
||||
val (secondary, secondaryDefault) = A::class.java.getDeclaredConstructors().partition { it.getParameterTypes().size == 3 }
|
||||
|
||||
test("secondary", secondary[0].getDeclaredAnnotations())
|
||||
test("secondary\$default", secondaryDefault[0].getDeclaredAnnotations())
|
||||
|
||||
val (primary, primaryDefault) = B::class.java.getConstructors().partition { it.getParameterTypes().size == 3 }
|
||||
|
||||
test("primary", primary[0].getDeclaredAnnotations())
|
||||
test("primary\$default", primaryDefault[0].getDeclaredAnnotations())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Ann(
|
||||
val i: Int = 1,
|
||||
val s: String = "a",
|
||||
val a: Ann2 = Ann2(),
|
||||
val e: MyEnum = MyEnum.A,
|
||||
val c: KClass<*> = A::class,
|
||||
val ia: IntArray = intArrayOf(1, 2),
|
||||
val sa: Array<String> = arrayOf("a", "b")
|
||||
)
|
||||
|
||||
fun box(): String {
|
||||
val ann = MyClass::class.java.getAnnotation(Ann::class.java)
|
||||
if (ann == null) return "fail: cannot find Ann on MyClass}"
|
||||
if (ann.i != 1) return "fail: annotation parameter i should be 1, but was ${ann.i}"
|
||||
if (ann.s != "a") return "fail: annotation parameter s should be \"a\", but was ${ann.s}"
|
||||
val annSimpleName = ann.a.annotationClass.java.getSimpleName()
|
||||
if (annSimpleName != "Ann2") return "fail: annotation parameter a should be of class Ann2, but was $annSimpleName"
|
||||
if (ann.e != MyEnum.A) return "fail: annotation parameter e should be MyEnum.A, but was ${ann.e}"
|
||||
if (ann.c.java != A::class.java) return "fail: annotation parameter c should be of class A, but was ${ann.c}"
|
||||
if (ann.ia[0] != 1 || ann.ia[1] != 2) return "fail: annotation parameter ia should be [1, 2], but was ${ann.ia}"
|
||||
if (ann.sa[0] != "a" || ann.sa[1] != "b") return "fail: annotation parameter ia should be [\"a\", \"b\"], but was ${ann.sa}"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
annotation class Ann2
|
||||
|
||||
enum class MyEnum {
|
||||
A
|
||||
}
|
||||
|
||||
class A
|
||||
|
||||
@Ann class MyClass
|
||||
@@ -0,0 +1,29 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class First
|
||||
|
||||
class MyClass() {
|
||||
public var x: String by Delegate()
|
||||
@First set
|
||||
}
|
||||
|
||||
class Delegate {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): String {
|
||||
return "OK"
|
||||
}
|
||||
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: String) {}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val e = MyClass::class.java
|
||||
|
||||
val e1 = e.getDeclaredMethod("setX", String::class.java).getAnnotations()
|
||||
if (e1.size != 1) return "Fail E1 size: ${e1.toList()}"
|
||||
if (e1[0].annotationClass.java != First::class.java) return "Fail: ${e1.toList()}"
|
||||
|
||||
return MyClass().x
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@file:StringHolder("OK")
|
||||
@file:JvmName("FileClass")
|
||||
|
||||
@Target(AnnotationTarget.FILE)
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
public annotation class StringHolder(val value: String)
|
||||
|
||||
fun box(): String =
|
||||
Class.forName("FileClass").getAnnotation(StringHolder::class.java)?.value ?: "null"
|
||||
@@ -0,0 +1,31 @@
|
||||
// WITH_RUNTIME
|
||||
// FULL_JDK
|
||||
|
||||
import java.lang.reflect.Modifier
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class CustomDelegate {
|
||||
operator fun getValue(thisRef: Any?, prop: KProperty<*>): String = prop.name
|
||||
}
|
||||
|
||||
class C {
|
||||
@Volatile var vol = 1
|
||||
@Transient val tra = 1
|
||||
@delegate:Transient val del: String by CustomDelegate()
|
||||
|
||||
@Strictfp fun str() {}
|
||||
@Synchronized fun sync() {}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c = C::class.java
|
||||
|
||||
if (c.getDeclaredField("vol").getModifiers() and Modifier.VOLATILE == 0) return "Fail: volatile"
|
||||
if (c.getDeclaredField("tra").getModifiers() and Modifier.TRANSIENT == 0) return "Fail: transient"
|
||||
if (c.getDeclaredField("del\$delegate").getModifiers() and Modifier.TRANSIENT == 0) return "Fail: delegate transient"
|
||||
|
||||
if (c.getDeclaredMethod("str").getModifiers() and Modifier.STRICT == 0) return "Fail: strict"
|
||||
if (c.getDeclaredMethod("sync").getModifiers() and Modifier.SYNCHRONIZED == 0) return "Fail: synchronized"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@Ann(Foo.i, Foo.s, Foo.f, Foo.d, Foo.l, Foo.b, Foo.bool, Foo.c, Foo.str) class MyClass
|
||||
|
||||
fun box(): String {
|
||||
val ann = MyClass::class.java.getAnnotation(Ann::class.java)
|
||||
if (ann == null) return "fail: cannot find Ann on MyClass}"
|
||||
if (ann.i != 2) return "fail: annotation parameter i should be 2, but was ${ann.i}"
|
||||
if (ann.s != 2.toShort()) return "fail: annotation parameter i should be 2, but was ${ann.i}"
|
||||
if (ann.f != 2.toFloat()) return "fail: annotation parameter i should be 2, but was ${ann.i}"
|
||||
if (ann.d != 2.toDouble()) return "fail: annotation parameter i should be 2, but was ${ann.i}"
|
||||
if (ann.l != 2.toLong()) return "fail: annotation parameter i should be 2, but was ${ann.i}"
|
||||
if (ann.b != 2.toByte()) return "fail: annotation parameter i should be 2, but was ${ann.i}"
|
||||
if (!ann.bool) return "fail: annotation parameter i should be true, but was ${ann.i}"
|
||||
if (ann.c != 'c') return "fail: annotation parameter i should be c, but was ${ann.i}"
|
||||
if (ann.str != "str") return "fail: annotation parameter i should be str, but was ${ann.i}"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Ann(
|
||||
val i: Int,
|
||||
val s: Short,
|
||||
val f: Float,
|
||||
val d: Double,
|
||||
val l: Long,
|
||||
val b: Byte,
|
||||
val bool: Boolean,
|
||||
val c: Char,
|
||||
val str: String
|
||||
)
|
||||
|
||||
class Foo {
|
||||
companion object {
|
||||
const val i: Int = 2
|
||||
const val s: Short = 2
|
||||
const val f: Float = 2.0.toFloat()
|
||||
const val d: Double = 2.0
|
||||
const val l: Long = 2
|
||||
const val b: Byte = 2
|
||||
const val bool: Boolean = true
|
||||
const val c: Char = 'c'
|
||||
const val str: String = "str"
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@Ann(i, s, f, d, l, b, bool, c, str) class MyClass
|
||||
|
||||
fun box(): String {
|
||||
val ann = MyClass::class.java.getAnnotation(Ann::class.java)
|
||||
if (ann == null) return "fail: cannot find Ann on MyClass}"
|
||||
if (ann.i != 2) return "fail: annotation parameter i should be 2, but was ${ann.i}"
|
||||
if (ann.s != 2.toShort()) return "fail: annotation parameter i should be 2, but was ${ann.i}"
|
||||
if (ann.f != 2.toFloat()) return "fail: annotation parameter i should be 2, but was ${ann.i}"
|
||||
if (ann.d != 2.toDouble()) return "fail: annotation parameter i should be 2, but was ${ann.i}"
|
||||
if (ann.l != 2.toLong()) return "fail: annotation parameter i should be 2, but was ${ann.i}"
|
||||
if (ann.b != 2.toByte()) return "fail: annotation parameter i should be 2, but was ${ann.i}"
|
||||
if (!ann.bool) return "fail: annotation parameter i should be true, but was ${ann.i}"
|
||||
if (ann.c != 'c') return "fail: annotation parameter i should be c, but was ${ann.i}"
|
||||
if (ann.str != "str") return "fail: annotation parameter i should be str, but was ${ann.i}"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Ann(
|
||||
val i: Int,
|
||||
val s: Short,
|
||||
val f: Float,
|
||||
val d: Double,
|
||||
val l: Long,
|
||||
val b: Byte,
|
||||
val bool: Boolean,
|
||||
val c: Char,
|
||||
val str: String
|
||||
)
|
||||
|
||||
const val i: Int = 2
|
||||
const val s: Short = 2
|
||||
const val f: Float = 2.0.toFloat()
|
||||
const val d: Double = 2.0
|
||||
const val l: Long = 2
|
||||
const val b: Byte = 2
|
||||
const val bool: Boolean = true
|
||||
const val c: Char = 'c'
|
||||
const val str: String = "str"
|
||||
@@ -0,0 +1,21 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
annotation class A
|
||||
|
||||
@Target(AnnotationTarget.CLASS)
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class B(val items: Array<A> = arrayOf(A()))
|
||||
|
||||
@B
|
||||
class C
|
||||
|
||||
fun box(): String {
|
||||
val bClass = B::class.java
|
||||
val cClass = C::class.java
|
||||
|
||||
val items = cClass.getAnnotation(bClass).items
|
||||
assert(items.size == 1) { "Expected: [A()], got ${items.asList()}" }
|
||||
assert(items[0] is A) { "Expected: [A()], got ${items.asList()}" }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@Ann(A.B.i) class MyClass
|
||||
|
||||
fun box(): String {
|
||||
val ann = MyClass::class.java.getAnnotation(Ann::class.java)
|
||||
if (ann == null) return "fail: cannot find Ann on MyClass}"
|
||||
if (ann.i != 1) return "fail: annotation parameter i should be 1, but was ${ann.i}"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Ann(val i: Int)
|
||||
|
||||
class A {
|
||||
class B {
|
||||
companion object {
|
||||
const val i = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Ann(
|
||||
val b: Byte,
|
||||
val s: Short,
|
||||
val i: Int,
|
||||
val f: Float,
|
||||
val d: Double,
|
||||
val l: Long,
|
||||
val c: Char,
|
||||
val bool: Boolean
|
||||
)
|
||||
|
||||
fun box(): String {
|
||||
val ann = MyClass::class.java.getAnnotation(Ann::class.java)
|
||||
if (ann == null) return "fail: cannot find Ann on MyClass}"
|
||||
if (ann.b != 1.toByte()) return "fail: annotation parameter b should be 1, but was ${ann.b}"
|
||||
if (ann.s != 1.toShort()) return "fail: annotation parameter s should be 1, but was ${ann.s}"
|
||||
if (ann.i != 1) return "fail: annotation parameter i should be 1, but was ${ann.i}"
|
||||
if (ann.f != 1.toFloat()) return "fail: annotation parameter f should be 1, but was ${ann.f}"
|
||||
if (ann.d != 1.0) return "fail: annotation parameter d should be 1, but was ${ann.d}"
|
||||
if (ann.l != 1.toLong()) return "fail: annotation parameter l should be 1, but was ${ann.l}"
|
||||
if (ann.c != 'c') return "fail: annotation parameter c should be 1, but was ${ann.c}"
|
||||
if (!ann.bool) return "fail: annotation parameter bool should be 1, but was ${ann.bool}"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@Ann(1, 1, 1, 1.0.toFloat(), 1.0, 1, 'c', true) class MyClass
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@Ann(i) class MyClass
|
||||
|
||||
fun box(): String {
|
||||
val ann = MyClass::class.java.getAnnotation(Ann::class.java)
|
||||
if (ann == null) return "fail: cannot find Ann on MyClass}"
|
||||
if (ann.i != 1) return "fail: annotation parameter i should be 1, but was ${ann.i}"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Ann(val i: Int)
|
||||
|
||||
const val i2: Int = 1
|
||||
const val i: Int = i2
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
@kotlin.internal.LowPriorityInOverloadResolution
|
||||
fun foo(i: Int) = 1
|
||||
|
||||
fun foo(a: Any) = 2
|
||||
|
||||
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
@kotlin.internal.LowPriorityInOverloadResolution
|
||||
fun bar(a: String?) = 3
|
||||
|
||||
fun bar(a: Any) = 4
|
||||
|
||||
fun box(): String {
|
||||
if (foo(1) != 2) return "fail1"
|
||||
if (bar(null) != 3) return "fail2"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Ann(vararg val p: Int)
|
||||
|
||||
@Ann() class MyClass1
|
||||
@Ann(1) class MyClass2
|
||||
@Ann(1, 2) class MyClass3
|
||||
|
||||
@Ann(*intArrayOf()) class MyClass4
|
||||
@Ann(*intArrayOf(1)) class MyClass5
|
||||
@Ann(*intArrayOf(1, 2)) class MyClass6
|
||||
|
||||
@Ann(p = 1) class MyClass7
|
||||
|
||||
@Ann(p = *intArrayOf()) class MyClass8
|
||||
@Ann(p = *intArrayOf(1)) class MyClass9
|
||||
@Ann(p = *intArrayOf(1, 2)) class MyClass10
|
||||
|
||||
fun box(): String {
|
||||
test(MyClass1::class.java, "")
|
||||
test(MyClass2::class.java, "1")
|
||||
test(MyClass3::class.java, "12")
|
||||
|
||||
test(MyClass4::class.java, "")
|
||||
test(MyClass5::class.java, "1")
|
||||
test(MyClass6::class.java, "12")
|
||||
|
||||
test(MyClass7::class.java, "1")
|
||||
|
||||
test(MyClass8::class.java, "")
|
||||
test(MyClass9::class.java, "1")
|
||||
test(MyClass10::class.java, "12")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun test(klass: Class<*>, expected: String) {
|
||||
val ann = klass.getAnnotation(Ann::class.java)
|
||||
if (ann == null) throw AssertionError("fail: cannot find Ann on ${klass}")
|
||||
|
||||
var result = ""
|
||||
for (i in ann.p) {
|
||||
result += i
|
||||
}
|
||||
|
||||
if (result != expected) {
|
||||
throw AssertionError("fail: expected = ${expected}, actual = ${result}")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user