Inherit KType from KAnnotatedElement, implement KType.annotations

#KT-16795 Fixed
This commit is contained in:
Alexander Udalov
2018-08-03 18:02:23 +02:00
committed by Ilya Gorbunov
parent e4bbe2d5e2
commit ceb909d261
14 changed files with 280 additions and 7 deletions
@@ -0,0 +1,75 @@
// IGNORE_BACKEND: JVM_IR
// TARGET_BACKEND: JVM
// WITH_REFLECT
import kotlin.reflect.KClass
import kotlin.test.assertEquals
annotation class Nested(val value: String)
@Target(AnnotationTarget.TYPE)
annotation class Anno(
val b: Byte,
val c: Char,
val d: Double,
val f: Float,
val i: Int,
val j: Long,
val s: Short,
val z: Boolean,
val ba: ByteArray,
val ca: CharArray,
val da: DoubleArray,
val fa: FloatArray,
val ia: IntArray,
val ja: LongArray,
val sa: ShortArray,
val za: BooleanArray,
val str: String,
val k: KClass<*>,
val e: AnnotationTarget,
val a: Nested,
val stra: Array<String>,
// val ka: Array<KClass<*>>, // Arrays of class literals are not supported yet in AnnotationDeserializer
val ea: Array<AnnotationTarget>,
val aa: Array<Nested>
)
fun f(): @Anno(
1.toByte(),
'x',
3.14,
-2.72f,
42424242,
239239239239239L,
42.toShort(),
true,
[(-1).toByte()],
['y'],
[-3.14159],
[2.7218f],
[424242],
[239239239239L],
[(-43).toShort()],
[false, true],
"lol",
Number::class,
AnnotationTarget.EXPRESSION,
Nested("1"),
["lmao"],
// [Double::class, Unit::class],
[AnnotationTarget.TYPEALIAS, AnnotationTarget.FIELD],
[Nested("2"), Nested("3")]
) Unit {}
fun box(): String {
assertEquals(
"[@Anno(b=1, c=x, d=3.14, f=-2.72, i=42424242, j=239239239239239, s=42, z=true, " +
"ba=[-1], ca=[y], da=[-3.14159], fa=[2.7218], ia=[424242], ja=[239239239239], sa=[-43], za=[false, true], " +
"str=lol, k=class java.lang.Number, e=EXPRESSION, a=@Nested(value=1), " +
"stra=[lmao], ea=[TYPEALIAS, FIELD], aa=[@Nested(value=2), @Nested(value=3)])]",
::f.returnType.annotations.toString()
)
return "OK"
}
@@ -0,0 +1,49 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM_IR
// WITH_REFLECT
import kotlin.reflect.KCallable
import kotlin.reflect.KType
@Target(AnnotationTarget.TYPE)
annotation class InRange(val from: Int, val to: Int)
val propertyType: @InRange(1, 10) Int = 5
fun functionType(): @InRange(1, 10) Int = 5
fun parameterType(param: @InRange(1, 10) Int) {}
fun (@InRange(1, 10) Int).receiverType() {}
abstract class Supertype : @InRange(1, 10) Number() {
fun <T : @InRange(1, 10) Number> typeParameterBound(t: T): T = t
inner class Inner
}
fun typeArgument(): List<@InRange(1, 10) Int>? = null
// -------
private fun check(what: String, type: KType) {
val annotations = type.annotations
if (annotations.isEmpty()) throw AssertionError("No annotations found on $what")
val a = annotations.single() as InRange
if (a.from != 1 || a.to != 10) throw AssertionError("Incorrect from/to values: ${a.from} ${a.to}")
}
fun box(): String {
check("property return type", ::propertyType.returnType)
check("function return type", ::functionType.returnType)
check("parameter type", ::parameterType.parameters.single().type)
check("receiver type", Int::receiverType.parameters.single().type)
check("supertype", Supertype::class.supertypes.single())
val typeParameterBound = Supertype::class.members.single { it.name == "typeParameterBound" } as KCallable
check("type parameter bound", typeParameterBound.typeParameters.single().upperBounds.single())
check("type argument", ::typeArgument.returnType.arguments.single().type!!)
return "OK"
}