Provide "equals" and "hashCode" for reflection objects

This commit is contained in:
Alexander Udalov
2014-06-30 19:16:59 +04:00
parent 704de8992e
commit c5d92cc03e
8 changed files with 81 additions and 2 deletions
@@ -0,0 +1,29 @@
import kotlin.test.*
val top = 42
var top2 = -23
val Int.intExt: Int get() = this
val Char.charExt: Int get() = this.toInt()
class A(var mem: String)
class B(var mem: String)
fun checkEqual(x: Any, y: Any) {
assertEquals(x, y)
assertEquals(x.hashCode(), y.hashCode(), "Elements are equal but their hash codes are not: ${x.hashCode()} != ${y.hashCode()}")
}
fun box(): String {
checkEqual(::top, ::top)
checkEqual(::top2, ::top2)
checkEqual(Int::intExt, Int::intExt)
checkEqual(A::mem, A::mem)
assertFalse(::top == ::top2)
assertFalse(Int::intExt == Char::charExt)
assertFalse(A::mem == B::mem)
return "OK"
}
@@ -1381,7 +1381,7 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
}
@TestMetadata("compiler/testData/codegen/boxWithStdlib/reflection")
@InnerTestClasses({Reflection.Enclosing.class, Reflection.GenericSignature.class, Reflection.Mapping.class})
@InnerTestClasses({Reflection.Enclosing.class, Reflection.GenericSignature.class, Reflection.Mapping.class, Reflection.MethodsFromAny.class})
public static class Reflection extends AbstractBlackBoxCodegenTest {
public void testAllFilesPresentInReflection() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxWithStdlib/reflection"), Pattern.compile("^(.+)\\.kt$"), true);
@@ -1530,12 +1530,26 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
}
@TestMetadata("compiler/testData/codegen/boxWithStdlib/reflection/methodsFromAny")
public static class MethodsFromAny extends AbstractBlackBoxCodegenTest {
public void testAllFilesPresentInMethodsFromAny() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxWithStdlib/reflection/methodsFromAny"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("equalsHashCode.kt")
public void testEqualsHashCode() throws Exception {
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/methodsFromAny/equalsHashCode.kt");
}
}
public static Test innerSuite() {
TestSuite suite = new TestSuite("Reflection");
suite.addTestSuite(Reflection.class);
suite.addTest(Enclosing.innerSuite());
suite.addTestSuite(GenericSignature.class);
suite.addTestSuite(Mapping.class);
suite.addTestSuite(MethodsFromAny.class);
return suite;
}
}
@@ -56,4 +56,10 @@ class KClassImpl<out T>(val jClass: Class<T>) : KClass<T> {
else {
KMutableForeignMemberProperty<T, Any>(name, this)
}
override fun equals(other: Any?): Boolean =
other is KClassImpl<*> && jClass == other.jClass
override fun hashCode(): Int =
jClass.hashCode()
}
@@ -30,6 +30,12 @@ open class KForeignMemberProperty<T : Any, out R>(
override fun get(receiver: T): R {
return field.get(receiver) as R
}
override fun equals(other: Any?): Boolean =
other is KForeignMemberProperty<*, *> && name == other.name && owner == other.owner
override fun hashCode(): Int =
name.hashCode() * 31 + owner.hashCode()
}
class KMutableForeignMemberProperty<T : Any, out R>(
@@ -39,6 +39,12 @@ open class KMemberPropertyImpl<T : Any, out R>(
override fun get(receiver: T): R {
return getter(receiver) as R
}
override fun equals(other: Any?): Boolean =
other is KMemberPropertyImpl<*, *> && name == other.name && owner == other.owner
override fun hashCode(): Int =
name.hashCode() * 31 + owner.hashCode()
}
class KMutableMemberPropertyImpl<T : Any, R>(
@@ -18,4 +18,10 @@ package kotlin.reflect.jvm.internal
import kotlin.reflect.KPackage
class KPackageImpl(val jClass: Class<*>) : KPackage
class KPackageImpl(val jClass: Class<*>) : KPackage {
override fun equals(other: Any?): Boolean =
other is KPackageImpl && jClass == other.jClass
override fun hashCode(): Int =
jClass.hashCode()
}
@@ -32,6 +32,12 @@ open class KTopLevelExtensionPropertyImpl<T, out R>(
override fun get(receiver: T): R {
return getter(null, receiver) as R
}
override fun equals(other: Any?): Boolean =
other is KTopLevelExtensionPropertyImpl<*, *> && name == other.name && owner == other.owner && receiverClass == other.receiverClass
override fun hashCode(): Int =
(name.hashCode() * 31 + owner.hashCode()) * 31 + receiverClass.hashCode()
}
class KMutableTopLevelExtensionPropertyImpl<T, R>(
@@ -32,6 +32,12 @@ open class KTopLevelVariableImpl<out R>(
override fun get(): R {
return getter(null) as R
}
override fun equals(other: Any?): Boolean =
other is KTopLevelVariableImpl<*> && name == other.name && owner == other.owner
override fun hashCode(): Int =
name.hashCode() * 31 + owner.hashCode()
}
class KMutableTopLevelVariableImpl<R>(