From d0a4f822038a3826a6993bb183699eae01685e6f Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 4 Sep 2015 20:18:59 +0300 Subject: [PATCH] Fix NO_KOTLIN_REFLECT directive handling in codegen tests It was disabling reflection on classpath at compile time, but not at runtime --- .../reflection/noKotlinReflect/javaClass.kt | 10 ++--- .../kotlin/codegen/CodegenTestCase.java | 9 ++++- .../jetbrains/kotlin/codegen/TestlibTest.java | 2 +- .../forTestCompile/ForTestCompileRuntime.java | 38 ++++++++++++++----- .../AbstractBlackBoxCodegenTest.java | 15 ++++---- ...bstractCompileKotlinAgainstKotlinTest.java | 2 +- .../AbstractJvmRuntimeDescriptorLoaderTest.kt | 2 +- .../kotlin/runtime/ProgressionUtilTest.java | 2 +- .../AbstractLocalClassProtoTest.kt | 2 +- 9 files changed, 53 insertions(+), 29 deletions(-) diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/noKotlinReflect/javaClass.kt b/compiler/testData/codegen/boxWithStdlib/reflection/noKotlinReflect/javaClass.kt index e97156d9624..f120be87f62 100644 --- a/compiler/testData/codegen/boxWithStdlib/reflection/noKotlinReflect/javaClass.kt +++ b/compiler/testData/codegen/boxWithStdlib/reflection/noKotlinReflect/javaClass.kt @@ -13,12 +13,12 @@ fun box(): String { assertEquals("Klass", jClass.getSimpleName()) assertEquals("Klass", kjClass.getSimpleName()) - assertEquals("Klass", kkClass.simpleName) + assertEquals("Klass", kkClass.java.simpleName) assertEquals(kjClass, jjClass) - failsWith(Error::class.java) { kClass.simpleName!! } - failsWith(Error::class.java) { kClass.qualifiedName!! } - failsWith(Error::class.java) { kClass.members } + try { kClass.simpleName; return "Fail 1" } catch (e: Error) {} + try { kClass.qualifiedName; return "Fail 2" } catch (e: Error) {} + try { kClass.members; return "Fail 3" } catch (e: Error) {} val jlError = Error::class.java val kljError = Error::class @@ -27,7 +27,7 @@ fun box(): String { assertEquals("Error", jlError.getSimpleName()) assertEquals("Error", jljError.getSimpleName()) - assertEquals("Error", jlkError.simpleName) + assertEquals("Error", jlkError.java.simpleName) return "OK" } diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java b/compiler/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java index de6bfde4436..d455b4e9c66 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java @@ -57,6 +57,7 @@ public abstract class CodegenTestCase extends UsefulTestCase { protected CodegenTestFiles myFiles; protected ClassFileFactory classFileFactory; protected GeneratedClassLoader initializedClassLoader; + protected ConfigurationKind configurationKind; protected void createEnvironmentWithMockJdkAndIdeaAnnotations(@NotNull ConfigurationKind configurationKind) { if (myEnvironment != null) { @@ -143,7 +144,13 @@ public abstract class CodegenTestCase extends UsefulTestCase { @NotNull protected GeneratedClassLoader createClassLoader() { - return new GeneratedClassLoader(generateClassesInFile(), ForTestCompileRuntime.runtimeJarClassLoader(), getClassPathURLs()); + return new GeneratedClassLoader( + generateClassesInFile(), + configurationKind == ConfigurationKind.NO_KOTLIN_REFLECT ? + ForTestCompileRuntime.runtimeJarClassLoader() : + ForTestCompileRuntime.runtimeAndReflectJarClassLoader(), + getClassPathURLs() + ); } @NotNull diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/TestlibTest.java b/compiler/tests/org/jetbrains/kotlin/codegen/TestlibTest.java index 89986eed5e0..2670a26a015 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/TestlibTest.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/TestlibTest.java @@ -100,7 +100,7 @@ public class TestlibTest extends UsefulTestCase { throw new RuntimeException("There were compilation errors"); } - classLoader = new GeneratedClassLoader(generationState.getFactory(), ForTestCompileRuntime.runtimeJarClassLoader()) { + classLoader = new GeneratedClassLoader(generationState.getFactory(), ForTestCompileRuntime.runtimeAndReflectJarClassLoader()) { @Override public Class loadClass(@NotNull String name) throws ClassNotFoundException { if (name.startsWith("junit.") || name.startsWith("org.junit.")) { diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/forTestCompile/ForTestCompileRuntime.java b/compiler/tests/org/jetbrains/kotlin/codegen/forTestCompile/ForTestCompileRuntime.java index 148cce29597..8b5198a7b80 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/forTestCompile/ForTestCompileRuntime.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/forTestCompile/ForTestCompileRuntime.java @@ -23,10 +23,13 @@ import java.lang.ref.SoftReference; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.List; import static org.jetbrains.kotlin.utils.UtilsPackage.rethrow; public class ForTestCompileRuntime { + private static volatile SoftReference reflectJarClassLoader = new SoftReference(null); private static volatile SoftReference runtimeJarClassLoader = new SoftReference(null); @NotNull @@ -47,22 +50,37 @@ public class ForTestCompileRuntime { return file; } + @NotNull + public static synchronized ClassLoader runtimeAndReflectJarClassLoader() { + ClassLoader loader = reflectJarClassLoader.get(); + if (loader == null) { + loader = createClassLoader(runtimeJarForTests(), reflectJarForTests()); + reflectJarClassLoader = new SoftReference(loader); + } + return loader; + } + @NotNull public static synchronized ClassLoader runtimeJarClassLoader() { ClassLoader loader = runtimeJarClassLoader.get(); if (loader == null) { - try { - loader = new URLClassLoader(new URL[] { - runtimeJarForTests().toURI().toURL(), - reflectJarForTests().toURI().toURL() - }, null); - } - catch (MalformedURLException e) { - throw rethrow(e); - } + loader = createClassLoader(runtimeJarForTests()); runtimeJarClassLoader = new SoftReference(loader); } - return loader; } + + @NotNull + private static ClassLoader createClassLoader(@NotNull File... files) { + try { + List urls = new ArrayList(2); + for (File file : files) { + urls.add(file.toURI().toURL()); + } + return new URLClassLoader(urls.toArray(new URL[urls.size()]), null); + } + catch (MalformedURLException e) { + throw rethrow(e); + } + } } diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/AbstractBlackBoxCodegenTest.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/AbstractBlackBoxCodegenTest.java index 07cf9f4261a..da32322e406 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/AbstractBlackBoxCodegenTest.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/AbstractBlackBoxCodegenTest.java @@ -66,16 +66,15 @@ public abstract class AbstractBlackBoxCodegenTest extends CodegenTestCase { } public void doTestWithStdlib(@NotNull String filename) { - myEnvironment = JetTestUtils.createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea( - getTestRootDisposable(), getTestConfigurationKind(filename), getTestJdkKind(filename) - ); - blackBoxFileByFullPath(filename); - } - - private static ConfigurationKind getTestConfigurationKind(String filename) { - return InTextDirectivesUtils.isDirectiveDefined( + configurationKind = InTextDirectivesUtils.isDirectiveDefined( IoPackage.readText(new File(filename), Charsets.UTF_8), "NO_KOTLIN_REFLECT" ) ? ConfigurationKind.NO_KOTLIN_REFLECT : ConfigurationKind.ALL; + + myEnvironment = JetTestUtils.createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea( + getTestRootDisposable(), configurationKind, getTestJdkKind(filename) + ); + + blackBoxFileByFullPath(filename); } public void doTestMultiFile(@NotNull String folderName) { diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstKotlinTest.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstKotlinTest.java index abdb2f74eb1..faa39f8e78f 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstKotlinTest.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstKotlinTest.java @@ -77,7 +77,7 @@ public abstract class AbstractCompileKotlinAgainstKotlinTest extends TestCaseWit private Class generatedClass() throws Exception { URLClassLoader classLoader = new URLClassLoader( new URL[]{ bDir.toURI().toURL(), aDir.toURI().toURL() }, - ForTestCompileRuntime.runtimeJarClassLoader() + ForTestCompileRuntime.runtimeAndReflectJarClassLoader() ); return classLoader.loadClass(PackageClassUtils.getPackageClassName(FqName.ROOT)); } diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/runtime/AbstractJvmRuntimeDescriptorLoaderTest.kt b/compiler/tests/org/jetbrains/kotlin/jvm/runtime/AbstractJvmRuntimeDescriptorLoaderTest.kt index 0ef3c8a7ca6..23e6e5ce3ef 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/runtime/AbstractJvmRuntimeDescriptorLoaderTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/jvm/runtime/AbstractJvmRuntimeDescriptorLoaderTest.kt @@ -79,7 +79,7 @@ public abstract class AbstractJvmRuntimeDescriptorLoaderTest : TestCaseWithTmpdi compileFile(file, text, jdkKind) - val classLoader = URLClassLoader(arrayOf(tmpdir.toURI().toURL()), ForTestCompileRuntime.runtimeJarClassLoader()) + val classLoader = URLClassLoader(arrayOf(tmpdir.toURI().toURL()), ForTestCompileRuntime.runtimeAndReflectJarClassLoader()) val actual = createReflectedPackageView(classLoader) diff --git a/compiler/tests/org/jetbrains/kotlin/runtime/ProgressionUtilTest.java b/compiler/tests/org/jetbrains/kotlin/runtime/ProgressionUtilTest.java index 0dcb98a3d23..24ef585feb3 100644 --- a/compiler/tests/org/jetbrains/kotlin/runtime/ProgressionUtilTest.java +++ b/compiler/tests/org/jetbrains/kotlin/runtime/ProgressionUtilTest.java @@ -31,7 +31,7 @@ public class ProgressionUtilTest extends UsefulTestCase { @Override protected void setUp() throws Exception { super.setUp(); - Class progressionUtil = ForTestCompileRuntime.runtimeJarClassLoader().loadClass("kotlin.internal.InternalPackage"); + Class progressionUtil = ForTestCompileRuntime.runtimeAndReflectJarClassLoader().loadClass("kotlin.internal.InternalPackage"); this.intMethod = progressionUtil.getMethod("getProgressionFinalElement", int.class, int.class, int.class); this.longMethod = progressionUtil.getMethod("getProgressionFinalElement", long.class, long.class, long.class); } diff --git a/compiler/tests/org/jetbrains/kotlin/serialization/AbstractLocalClassProtoTest.kt b/compiler/tests/org/jetbrains/kotlin/serialization/AbstractLocalClassProtoTest.kt index 6aa92cbffdf..efae2cd2a34 100644 --- a/compiler/tests/org/jetbrains/kotlin/serialization/AbstractLocalClassProtoTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/serialization/AbstractLocalClassProtoTest.kt @@ -41,7 +41,7 @@ public abstract class AbstractLocalClassProtoTest : TestCaseWithTmpdir() { val classNameSuffix = InTextDirectivesUtils.findStringWithPrefixes(source.readText(), "// CLASS_NAME_SUFFIX: ") ?: error("CLASS_NAME_SUFFIX directive not found in test data") - val classLoader = URLClassLoader(arrayOf(tmpdir.toURI().toURL()), ForTestCompileRuntime.runtimeJarClassLoader()) + val classLoader = URLClassLoader(arrayOf(tmpdir.toURI().toURL()), ForTestCompileRuntime.runtimeAndReflectJarClassLoader()) val classFile = tmpdir.listFiles().singleOrNull { it.getPath().endsWith("$classNameSuffix.class") } ?: error("Local class with suffix `$classNameSuffix` is not found in: ${tmpdir.listFiles().toList()}")