diff --git a/compiler/testData/codegen/box/reflection/jvmKPackageCaching.kt b/compiler/testData/codegen/box/reflection/jvmKPackageCaching.kt index b2c64b831c5..6ba4666b880 100644 --- a/compiler/testData/codegen/box/reflection/jvmKPackageCaching.kt +++ b/compiler/testData/codegen/box/reflection/jvmKPackageCaching.kt @@ -2,12 +2,15 @@ // FULL_JDK // WITH_REFLECT import kotlin.jvm.internal.* +import kotlin.reflect.jvm.internal.* class A fun box(): String { - val pckg = Reflection.getOrCreateKotlinPackage(A::class.java) - System.gc() - val pckg2 = Reflection.getOrCreateKotlinPackage(A::class.java) - if (pckg === pckg2) return "OK" - return "Fail" + return synchronized(ReflectionFactoryImpl::class.java) { + val pckg = Reflection.getOrCreateKotlinPackage(A::class.java) + System.gc() + val pckg2 = Reflection.getOrCreateKotlinPackage(A::class.java) + if (pckg === pckg2) return@synchronized "OK" + return@synchronized "Fail" + } } diff --git a/compiler/testData/codegen/box/reflection/jvmKTypeCaching.kt b/compiler/testData/codegen/box/reflection/jvmKTypeCaching.kt index fcf8a48e9ab..64d09631260 100644 --- a/compiler/testData/codegen/box/reflection/jvmKTypeCaching.kt +++ b/compiler/testData/codegen/box/reflection/jvmKTypeCaching.kt @@ -1,13 +1,16 @@ // TARGET_BACKEND: JVM_IR // FULL_JDK // WITH_REFLECT +import kotlin.reflect.jvm.internal.* class A fun box(): String { - val clz = A::class - System.gc() - val clz2 = A::class - if (clz === clz2) return "OK" - return "Fail" + return synchronized(ReflectionFactoryImpl::class.java) { + val clz = A::class + System.gc() + val clz2 = A::class + if (clz === clz2) return@synchronized "OK" + return@synchronized "Fail" + } } diff --git a/compiler/testData/codegen/box/reflection/methodsFromAny/parametersEqualsWithClearCaches.kt b/compiler/testData/codegen/box/reflection/methodsFromAny/parametersEqualsWithClearCaches.kt index 647256599a9..49b419384ac 100644 --- a/compiler/testData/codegen/box/reflection/methodsFromAny/parametersEqualsWithClearCaches.kt +++ b/compiler/testData/codegen/box/reflection/methodsFromAny/parametersEqualsWithClearCaches.kt @@ -12,7 +12,9 @@ inline fun check(message: String, generate: () -> Any?) { x1 = generate() // Force clear the internal maps, as if the weak values in them are garbage-collected. - kotlin.reflect.jvm.internal.ReflectionFactoryImpl.clearCaches() + synchronized(kotlin.reflect.jvm.internal.ReflectionFactoryImpl::class.java) { + kotlin.reflect.jvm.internal.ReflectionFactoryImpl.clearCaches() + } x2 = generate() } catch (e: Throwable) { diff --git a/compiler/testData/codegen/box/reflection/methodsFromAny/typeParametersEqualsWithClearCaches.kt b/compiler/testData/codegen/box/reflection/methodsFromAny/typeParametersEqualsWithClearCaches.kt index 56f64ce43c1..f274012efd3 100644 --- a/compiler/testData/codegen/box/reflection/methodsFromAny/typeParametersEqualsWithClearCaches.kt +++ b/compiler/testData/codegen/box/reflection/methodsFromAny/typeParametersEqualsWithClearCaches.kt @@ -11,7 +11,9 @@ inline fun check(message: String, generate: () -> Any?) { x1 = generate() // Force clear the internal maps, as if the weak values in them are garbage-collected. - kotlin.reflect.jvm.internal.ReflectionFactoryImpl.clearCaches() + synchronized(kotlin.reflect.jvm.internal.ReflectionFactoryImpl::class.java) { + kotlin.reflect.jvm.internal.ReflectionFactoryImpl.clearCaches() + } x2 = generate() } catch (e: Throwable) { diff --git a/compiler/testData/codegen/box/reflection/typeOf/caching.kt b/compiler/testData/codegen/box/reflection/typeOf/caching.kt index 255632699d6..42dd30005c2 100644 --- a/compiler/testData/codegen/box/reflection/typeOf/caching.kt +++ b/compiler/testData/codegen/box/reflection/typeOf/caching.kt @@ -5,6 +5,7 @@ import kotlin.test.assertEquals import kotlin.test.assertSame import kotlin.reflect.KType import kotlin.reflect.typeOf +import kotlin.reflect.jvm.internal.* private inline fun check(isNullable: Boolean = false) { val t1 = typeOf() @@ -15,10 +16,12 @@ private inline fun check(isNullable: Boolean = false) { } fun box(): String { - check() - check(true) - check?>(true) - check>() - check?>(true) + synchronized(ReflectionFactoryImpl::class.java) { + check() + check(true) + check?>(true) + check>() + check?>(true) + } return "OK" } diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/JvmBoxRunner.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/JvmBoxRunner.kt index bb64135737c..d5a9da6c299 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/JvmBoxRunner.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/JvmBoxRunner.kt @@ -97,8 +97,6 @@ open class JvmBoxRunner(testServices: TestServices) : JvmBinaryArtifactHandler(t } } throw e - } finally { - clearReflectionCache(classLoader) } } diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/AbstractBlackBoxCodegenTest.java b/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/AbstractBlackBoxCodegenTest.java index 6e37a8fd204..f51577bae8a 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/AbstractBlackBoxCodegenTest.java +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/AbstractBlackBoxCodegenTest.java @@ -21,7 +21,6 @@ import java.io.File; import java.lang.reflect.Method; import java.util.List; -import static org.jetbrains.kotlin.codegen.TestUtilsKt.clearReflectionCache; import static org.jetbrains.kotlin.test.KotlinTestUtils.assertEqualsToFile; import static org.jetbrains.kotlin.test.clientserver.TestProcessServerKt.getBoxMethodOrNull; import static org.jetbrains.kotlin.test.clientserver.TestProcessServerKt.getGeneratedClass; @@ -100,9 +99,6 @@ public abstract class AbstractBlackBoxCodegenTest extends CodegenTestCase { } throw ExceptionUtilsKt.rethrow(e); } - finally { - clearReflectionCache(generatedClassLoader); - } } fail("Can't find box method!"); } diff --git a/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/testUtils.kt b/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/testUtils.kt index d369e793253..bb48a44739e 100644 --- a/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/testUtils.kt +++ b/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/testUtils.kt @@ -5,19 +5,9 @@ package org.jetbrains.kotlin.codegen -import org.jetbrains.kotlin.load.java.JvmAbi import java.net.URL import java.net.URLClassLoader -fun clearReflectionCache(classLoader: ClassLoader) { - try { - val klass = classLoader.loadClass(JvmAbi.REFLECTION_FACTORY_IMPL.asSingleFqName().asString()) - val method = klass.getDeclaredMethod("clearCaches") - method.invoke(null) - } catch (e: ClassNotFoundException) { - // This is OK for a test without kotlin-reflect in the dependencies - } -} fun ClassLoader?.extractUrls(): List { return (this as? URLClassLoader)?.let {