Fix NO_KOTLIN_REFLECT directive handling in codegen tests

It was disabling reflection on classpath at compile time, but not at runtime
This commit is contained in:
Alexander Udalov
2015-09-04 20:18:59 +03:00
parent 308d24eeb4
commit d0a4f82203
9 changed files with 53 additions and 29 deletions
@@ -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"
}
@@ -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
@@ -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.")) {
@@ -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<ClassLoader> reflectJarClassLoader = new SoftReference<ClassLoader>(null);
private static volatile SoftReference<ClassLoader> runtimeJarClassLoader = new SoftReference<ClassLoader>(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<ClassLoader>(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<ClassLoader>(loader);
}
return loader;
}
@NotNull
private static ClassLoader createClassLoader(@NotNull File... files) {
try {
List<URL> urls = new ArrayList<URL>(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);
}
}
}
@@ -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) {
@@ -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));
}
@@ -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)
@@ -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);
}
@@ -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()}")