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.
This commit is contained in:
Alexander Udalov
2015-03-30 16:42:41 +03:00
parent d59e1ad7ac
commit d2ef040bcb
2 changed files with 5 additions and 1 deletions
@@ -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
@@ -28,7 +28,6 @@ class KClassImpl<T>(override val jClass: Class<T>) : KCallableContainerImpl(), K
// resulting in infinite recursion
val descriptor by ReflectProperties.lazySoft {
val moduleData = jClass.getOrCreateModule()
val classId = classId
val descriptor =