Skip to content

Commit 559da84

Browse files
committed
Clear reflection caches in black box codegen tests
To prevent tests from altering outcomes of the subsequent tests. Also expose the clearCaches method (in the internal class ReflectionFactoryImpl) so that it can be used in other places in similar circumstances
1 parent e19c1b5 commit 559da84

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

compiler/tests-common/org/jetbrains/kotlin/codegen/AbstractBlackBoxCodegenTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.jetbrains.annotations.Nullable;
2727
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
2828
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil;
29+
import org.jetbrains.kotlin.load.java.JvmAbi;
2930
import org.jetbrains.kotlin.psi.KtDeclaration;
3031
import org.jetbrains.kotlin.psi.KtFile;
3132
import org.jetbrains.kotlin.psi.KtNamedFunction;
@@ -159,6 +160,23 @@ protected void blackBox() {
159160
System.out.println(generateToText());
160161
throw ExceptionUtilsKt.rethrow(e);
161162
}
163+
finally {
164+
clearReflectionCache(generatedClassLoader);
165+
}
166+
}
167+
}
168+
169+
private static void clearReflectionCache(@NotNull ClassLoader classLoader) {
170+
try {
171+
Class<?> klass = classLoader.loadClass(JvmAbi.REFLECTION_FACTORY_IMPL.asSingleFqName().asString());
172+
Method method = klass.getDeclaredMethod("clearCaches");
173+
method.invoke(null);
174+
}
175+
catch (ClassNotFoundException e) {
176+
// This is OK for a test without kotlin-reflect in the dependencies
177+
}
178+
catch (Exception e) {
179+
throw ExceptionUtilsKt.rethrow(e);
162180
}
163181
}
164182

core/reflection.jvm/src/kotlin/reflect/jvm/internal/ReflectionFactoryImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,11 @@ private static KDeclarationContainerImpl getOwner(CallableReference reference) {
105105
KDeclarationContainer owner = reference.getOwner();
106106
return owner instanceof KDeclarationContainerImpl ? ((KDeclarationContainerImpl) owner) : EmptyContainerForLocal.INSTANCE;
107107
}
108+
109+
// Misc
110+
111+
public static void clearCaches() {
112+
KClassCacheKt.clearKClassCache();
113+
ModuleByClassLoaderKt.clearModuleByClassLoaderCache();
114+
}
108115
}

core/reflection.jvm/src/kotlin/reflect/jvm/internal/kClassCache.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ internal fun <T : Any> getOrCreateKotlinClass(jClass: Class<T>): KClassImpl<T> {
6464
K_CLASS_CACHE = K_CLASS_CACHE.plus(name, WeakReference(newKClass))
6565
return newKClass
6666
}
67+
68+
internal fun clearKClassCache() {
69+
K_CLASS_CACHE = HashPMap.empty()
70+
}

core/reflection.jvm/src/kotlin/reflect/jvm/internal/moduleByClassLoader.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private class WeakClassLoaderBox(classLoader: ClassLoader) {
4141
identityHashCode
4242

4343
override fun toString() =
44-
ref.get()?.let { it.toString() } ?: "<null>"
44+
ref.get()?.toString() ?: "<null>"
4545
}
4646

4747
internal fun Class<*>.getOrCreateModule(): RuntimeModuleData {
@@ -70,3 +70,7 @@ internal fun Class<*>.getOrCreateModule(): RuntimeModuleData {
7070
key.temporaryStrongRef = null
7171
}
7272
}
73+
74+
internal fun clearModuleByClassLoaderCache() {
75+
moduleByClassLoader.clear()
76+
}

0 commit comments

Comments
 (0)