Support equals, hashCode, toString for annotations instantiated with reflection
#KT-13106 Fixed
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
annotation class Foo
|
||||
|
||||
fun box(): String {
|
||||
val foo = Foo::class.constructors.single().call()
|
||||
assertEquals(Foo::class, foo.annotationClass)
|
||||
return "OK"
|
||||
}
|
||||
@@ -8,5 +8,6 @@ annotation class Anno(val klasses: Array<KClass<*>> = arrayOf(String::class, Int
|
||||
fun box(): String {
|
||||
val anno = Anno::class.constructors.single().callBy(emptyMap())
|
||||
assertEquals(listOf(String::class, Int::class), (anno.klasses as Array<KClass<*>>).toList() /* TODO: KT-9453 */)
|
||||
assertEquals("@Anno(klasses=[class java.lang.String, int])", anno.toString())
|
||||
return "OK"
|
||||
}
|
||||
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
annotation class Foo(val value: String)
|
||||
|
||||
annotation class Anno(
|
||||
val level: DeprecationLevel,
|
||||
val klass: KClass<*>,
|
||||
val foo: Foo,
|
||||
val levels: Array<DeprecationLevel>,
|
||||
val klasses: Array<KClass<*>>,
|
||||
val foos: Array<Foo>
|
||||
)
|
||||
|
||||
@Anno(
|
||||
DeprecationLevel.WARNING,
|
||||
Number::class,
|
||||
Foo("OK"),
|
||||
arrayOf(DeprecationLevel.WARNING),
|
||||
arrayOf(Number::class),
|
||||
arrayOf(Foo("OK"))
|
||||
)
|
||||
fun foo() {}
|
||||
|
||||
fun box(): String {
|
||||
// Construct an annotation with exactly the same parameters, check that the proxy created by Kotlin and by Java reflection are the same and have the same hash code
|
||||
val a1 = Anno::class.constructors.single().call(
|
||||
DeprecationLevel.WARNING,
|
||||
Number::class,
|
||||
Foo::class.constructors.single().call("OK"),
|
||||
arrayOf(DeprecationLevel.WARNING),
|
||||
arrayOf(Number::class),
|
||||
arrayOf(Foo::class.constructors.single().call("OK"))
|
||||
)
|
||||
val a2 = ::foo.annotations.single() as Anno
|
||||
|
||||
assertEquals(a1, a2)
|
||||
assertEquals(a2, a1)
|
||||
assertEquals(a1.hashCode(), a2.hashCode())
|
||||
|
||||
assertEquals("@Anno(level=WARNING, klass=class java.lang.Number, foo=@Foo(value=OK), " +
|
||||
"levels=[WARNING], klasses=[class java.lang.Number], foos=[@Foo(value=OK)])", a1.toString())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
// WITH_REFLECT
|
||||
|
||||
package test
|
||||
|
||||
annotation class A
|
||||
annotation class B(val s: String)
|
||||
|
||||
@A
|
||||
@B("2")
|
||||
fun javaReflectionAnnotationInstances() {}
|
||||
|
||||
fun box(): String {
|
||||
val createA = A::class.constructors.single()
|
||||
|
||||
val a1 = createA.call()
|
||||
if (a1.toString() != "@test.A()") return "Fail: toString does not correspond to the documentation of java.lang.annotation.Annotation#toString: $a1"
|
||||
|
||||
val a2 = createA.call()
|
||||
if (a1 === a2) return "Fail: instances created by the constructor should be different"
|
||||
if (a1 != a2) return "Fail: any instance of A should be equal to any other instance of A"
|
||||
if (a1.hashCode() != a2.hashCode()) return "Fail: hash codes of equal instances should be equal"
|
||||
if (a1.hashCode() != 0) return "Fail: hashCode does not correspond to the documentation of java.lang.annotation.Annotation#hashCode: ${a1.hashCode()}"
|
||||
|
||||
val createB = B::class.constructors.single()
|
||||
val b1 = createB.call("1")
|
||||
if (b1.toString() != "@test.B(s=1)") return "Fail: toString does not correspond to the documentation of java.lang.annotation.Annotation#toString: $b1"
|
||||
if (b1 != b1) return "Fail: instance should be equal to itself"
|
||||
|
||||
val b2 = createB.call("2")
|
||||
if (b1 == b2) return "Fail: instances with different data should not be equal"
|
||||
if (b1.hashCode() == b2.hashCode()) return "Fail: hash codes of different instances should very likely be also different"
|
||||
|
||||
val a3 = ::javaReflectionAnnotationInstances.annotations.filterIsInstance<A>().single()
|
||||
if (a1 === a3) return "Fail: instance created by the constructor and the one obtained from Java reflection should be different"
|
||||
if (a1 != a3) return "Fail: instance created by the constructor should be equal to the one obtained from Java reflection"
|
||||
if (a3 != a1) return "Fail: instance obtained from Java reflection should be equal to the one created by the constructor"
|
||||
if (a1.hashCode() != a3.hashCode()) return "Fail: hash codes of equal instances should be equal"
|
||||
|
||||
val b3 = ::javaReflectionAnnotationInstances.annotations.filterIsInstance<B>().single()
|
||||
if (b2 === b3) return "Fail: instance created by the constructor and the one obtained from Java reflection should be different"
|
||||
if (b2 != b3) return "Fail: instance created by the constructor should be equal to the one obtained from Java reflection"
|
||||
if (b3 != b2) return "Fail: instance obtained from Java reflection should be equal to the one created by the constructor"
|
||||
if (b2.hashCode() != b3.hashCode()) return "Fail: hash codes of equal instances should be equal"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotEquals
|
||||
|
||||
annotation class D(val d: Double)
|
||||
annotation class F(val f: Float)
|
||||
|
||||
/*
|
||||
// TODO: uncomment once KT-13887 is implemented
|
||||
@D(Double.NaN)
|
||||
fun dnan() {}
|
||||
|
||||
@F(Float.NaN)
|
||||
fun fnan() {}
|
||||
*/
|
||||
|
||||
@D(-0.0)
|
||||
fun dMinusZero() {}
|
||||
@D(+0.0)
|
||||
fun dPlusZero() {}
|
||||
|
||||
@F(-0.0f)
|
||||
fun fMinusZero() {}
|
||||
@F(+0.0f)
|
||||
fun fPlusZero() {}
|
||||
|
||||
fun check(x: Any, y: Any) {
|
||||
assertEquals(x, y)
|
||||
assertEquals(y, x)
|
||||
assertEquals(x.hashCode(), y.hashCode())
|
||||
assertEquals(x.toString(), y.toString())
|
||||
}
|
||||
|
||||
fun checkNot(x: Any, y: Any) {
|
||||
assertNotEquals(x, y)
|
||||
assertNotEquals(y, x)
|
||||
assertNotEquals(x.hashCode(), y.hashCode())
|
||||
assertNotEquals(x.toString(), y.toString())
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
/*
|
||||
check(::dnan.annotations.single() as D, D::class.constructors.single().call(Double.NaN))
|
||||
check(::fnan.annotations.single() as F, F::class.constructors.single().call(Float.NaN))
|
||||
*/
|
||||
|
||||
val dmz = D::class.constructors.single().call(-0.0)
|
||||
val dpz = D::class.constructors.single().call(+0.0)
|
||||
val fmz = F::class.constructors.single().call(-0.0f)
|
||||
val fpz = F::class.constructors.single().call(+0.0f)
|
||||
check(::dMinusZero.annotations.single() as D, dmz)
|
||||
check(::dPlusZero.annotations.single() as D, dpz)
|
||||
check(::fMinusZero.annotations.single() as F, fmz)
|
||||
check(::fPlusZero.annotations.single() as F, fpz)
|
||||
|
||||
checkNot(dmz, dpz)
|
||||
checkNot(fmz, fpz)
|
||||
checkNot(dmz, fmz)
|
||||
checkNot(dpz, fpz)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotEquals
|
||||
|
||||
annotation class Anno(val equals: Boolean)
|
||||
|
||||
fun box(): String {
|
||||
val t = Anno::class.constructors.single().call(true)
|
||||
val f = Anno::class.constructors.single().call(false)
|
||||
assertEquals(true, t.equals)
|
||||
assertEquals(false, f.equals)
|
||||
assertNotEquals(t, f)
|
||||
return "OK"
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
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 stra: Array<String>
|
||||
)
|
||||
|
||||
@Anno(
|
||||
1.toByte(),
|
||||
'x',
|
||||
3.14,
|
||||
-2.72f,
|
||||
42424242,
|
||||
239239239239239L,
|
||||
42.toShort(),
|
||||
true,
|
||||
byteArrayOf((-1).toByte()),
|
||||
charArrayOf('y'),
|
||||
doubleArrayOf(-3.14159),
|
||||
floatArrayOf(2.7218f),
|
||||
intArrayOf(424242),
|
||||
longArrayOf(239239239239L),
|
||||
shortArrayOf((-43).toShort()),
|
||||
booleanArrayOf(false, true),
|
||||
"lol",
|
||||
arrayOf("rofl")
|
||||
)
|
||||
fun foo() {}
|
||||
|
||||
fun box(): String {
|
||||
// Construct an annotation with exactly the same parameters, check that the proxy created by Kotlin and by Java reflection are the same and have the same hash code
|
||||
val a1 = Anno::class.constructors.single().call(
|
||||
1.toByte(),
|
||||
'x',
|
||||
3.14,
|
||||
-2.72f,
|
||||
42424242,
|
||||
239239239239239L,
|
||||
42.toShort(),
|
||||
true,
|
||||
byteArrayOf((-1).toByte()),
|
||||
charArrayOf('y'),
|
||||
doubleArrayOf(-3.14159),
|
||||
floatArrayOf(2.7218f),
|
||||
intArrayOf(424242),
|
||||
longArrayOf(239239239239L),
|
||||
shortArrayOf((-43).toShort()),
|
||||
booleanArrayOf(false, true),
|
||||
"lol",
|
||||
arrayOf("rofl")
|
||||
)
|
||||
|
||||
val a2 = ::foo.annotations.single() as Anno
|
||||
|
||||
assertEquals(a1, a2)
|
||||
assertEquals(a2, a1)
|
||||
assertEquals(a1.hashCode(), a2.hashCode())
|
||||
|
||||
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, stra=[rofl])", a1.toString())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user