From d2ef040bcb3902b972c4effdcc74a112dcbecdea Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Mon, 30 Mar 2015 16:42:41 +0300 Subject: [PATCH] Fix occasional reflection test failure The cache in moduleByClassLoader.kt contains weak references to instances of RuntimeModuleData. However, prior to this change no one else had any _strong_ reference to the corresponding RuntimeModuleData: KClassImpl depends on the ModuleDescriptor almost directly. Therefore the weak references were sometimes garbage collected and a new module was constructed for a new reflection object, which resulted in broken equality between two reflection objects. No test added because it was rather hard to come up with (and, more importantly, to support) a test case. --- .../kotlin/reflect/jvm/internal/KCallableContainerImpl.kt | 5 +++++ .../src/kotlin/reflect/jvm/internal/KClassImpl.kt | 1 - 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableContainerImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableContainerImpl.kt index c35c62f08f6..52ad25aa4d3 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableContainerImpl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableContainerImpl.kt @@ -33,6 +33,11 @@ import java.lang.reflect.Method import kotlin.reflect.KotlinReflectionInternalError abstract class KCallableContainerImpl { + // Note: this is stored here on a soft reference to prevent GC from destroying the weak reference to it in the moduleByClassLoader cache + val moduleData by ReflectProperties.lazySoft { + jClass.getOrCreateModule() + } + abstract val jClass: Class<*> abstract val scope: JetScope diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt index 6af91c90280..6d1a44a6d44 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt @@ -28,7 +28,6 @@ class KClassImpl(override val jClass: Class) : KCallableContainerImpl(), K // resulting in infinite recursion val descriptor by ReflectProperties.lazySoft { - val moduleData = jClass.getOrCreateModule() val classId = classId val descriptor =