Lazy module name propagation

This commit is contained in:
Michael Bogdanov
2015-08-28 12:14:14 +03:00
parent c274ceffe8
commit 9f0662468f
10 changed files with 57 additions and 33 deletions
@@ -39,7 +39,7 @@ import kotlin.reflect.KotlinReflectionInternalError
abstract class KDeclarationContainerImpl : ClassBasedDeclarationContainer {
// 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(null)
jClass.getOrCreateModule()
}
abstract val constructorDescriptors: Collection<ConstructorDescriptor>
@@ -29,7 +29,8 @@ import kotlin.reflect.KPackage
class KPackageImpl(override val jClass: Class<*>, val moduleName: String) : KDeclarationContainerImpl(), KPackage {
val descriptor by ReflectProperties.lazySoft {
val moduleData = jClass.getOrCreateModule(moduleName)
val moduleData = jClass.getOrCreateModule()
moduleData.packageFacadeProvider.registerModule(moduleName)
val fqName = jClass.classId.getPackageFqName()
moduleData.module.getPackage(fqName)
@@ -44,8 +44,9 @@ private class WeakClassLoaderBox(classLoader: ClassLoader) {
ref.get()?.let { it.toString() } ?: "<null>"
}
internal fun Class<*>.getOrCreateModule(moduleName: String?): RuntimeModuleData {
internal fun Class<*>.getOrCreateModule(): RuntimeModuleData {
val classLoader = this.safeClassLoader
val key = WeakClassLoaderBox(classLoader)
val cached = moduleByClassLoader[key]
@@ -54,7 +55,7 @@ internal fun Class<*>.getOrCreateModule(moduleName: String?): RuntimeModuleData
moduleByClassLoader.remove(key, cached)
}
val module = RuntimeModuleData.create(classLoader, moduleName)
val module = RuntimeModuleData.create(classLoader)
try {
while (true) {
val ref = moduleByClassLoader.putIfAbsent(key, WeakReference(module))
@@ -92,9 +92,18 @@ public val KType.javaType: Type
* See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/java-interop.html#package-level-functions)
* for more information.
*/
@deprecated("After package refactoring it would be impossible to retrieve package by class")
public val Class<*>.kotlinPackage: KPackage?
get() = if (getSimpleName().endsWith("Package") &&
getAnnotation(javaClass<kotlin.jvm.internal.KotlinPackage>()) != null) KPackageImpl(this, "undefined") else null
getAnnotation(javaClass<kotlin.jvm.internal.KotlinPackage>()) != null) {
val field = this.getField("\$moduleName")
if (field != null) {
KPackageImpl(this, field.get(null) as String)
} else {
null
}
} else null
/**