Dispose GeneratedClassLoader

This commit is contained in:
Nikolay Krasko
2013-01-21 18:49:18 +04:00
parent a6c4a36ce5
commit 44dcdde513
6 changed files with 140 additions and 137 deletions
@@ -16,7 +16,9 @@
package org.jetbrains.jet.codegen;
import com.intellij.testFramework.UsefulTestCase;
import gnu.trove.THashSet;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@@ -42,7 +44,6 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.parsing.JetParsingTest;
import org.jetbrains.jet.utils.ExceptionUtils;
import java.io.File;
import java.lang.reflect.Constructor;
@@ -51,107 +52,39 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.util.Set;
public class TestlibTest extends CodegenTestCase {
private File junitJar;
@SuppressWarnings("JUnitTestCaseWithNoTests")
public class TestlibTest extends UsefulTestCase {
public static Test suite() {
return new TestlibTest().buildSuite();
return new TestlibTest().buildTestSuite();
}
protected TestSuite buildSuite() {
try {
setUp();
return doBuildSuite();
} catch (Exception e) {
throw ExceptionUtils.rethrow(e);
}
finally {
try {
tearDown();
} catch (Exception e) {
throw ExceptionUtils.rethrow(e);
private TestSuite suite;
private File junitJar;
private GeneratedClassLoader classLoader;
private JetTypeMapper typeMapper;
private GenerationState generationState;
private JetCoreEnvironment myEnvironment;
private Test buildTestSuite() {
suite = new TestSuite("stdlib_test");
return new TestSetup(suite) {
@Override
protected void setUp() throws Exception {
TestlibTest.this.setUp();
}
}
@Override
protected void tearDown() throws Exception {
TestlibTest.this.tearDown();
}
};
}
private TestSuite doBuildSuite() {
try {
GenerationState generationState = KotlinToJVMBytecodeCompiler.analyzeAndGenerate(myEnvironment, false);
if (generationState == null) {
throw new RuntimeException("There were compilation errors");
}
ClassFileFactory classFileFactory = generationState.getFactory();
final GeneratedClassLoader loader = new GeneratedClassLoader(
classFileFactory,
new URLClassLoader(new URL[]{ForTestCompileRuntime.runtimeJarForTests().toURI().toURL(), junitJar.toURI().toURL()},
TestCase.class.getClassLoader()));
JetTypeMapper typeMapper = generationState.getTypeMapper();
TestSuite suite = new TestSuite("stdlib_test");
try {
for(JetFile jetFile : myEnvironment.getSourceFiles()) {
for(JetDeclaration decl : jetFile.getDeclarations()) {
if (decl instanceof JetClass) {
JetClass jetClass = (JetClass) decl;
ClassDescriptor descriptor = (ClassDescriptor) generationState.getBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, jetClass);
Set<JetType> allSuperTypes = new THashSet<JetType>();
DescriptorUtils.addSuperTypes(descriptor.getDefaultType(), allSuperTypes);
for(JetType type : allSuperTypes) {
String internalName = typeMapper.mapType(type, JetTypeMapperMode.IMPL).getInternalName();
if(internalName.equals("junit/framework/Test")) {
String name = typeMapper.mapType(descriptor.getDefaultType(), JetTypeMapperMode.IMPL).getInternalName();
System.out.println(name);
Class<TestCase> aClass = (Class<TestCase>) loader.loadClass(name.replace('/', '.'));
if ((aClass.getModifiers() & Modifier.ABSTRACT) == 0
&& (aClass.getModifiers() & Modifier.PUBLIC) != 0) {
try {
Constructor<TestCase> constructor = aClass.getConstructor();
if (constructor != null && (constructor.getModifiers() & Modifier.PUBLIC) != 0) {
suite.addTestSuite(aClass);
}
}
//catch (final VerifyError e) {
// suite.addTest(new TestCase(aClass.getName()) {
// @Override
// public int countTestCases() {
// return 1;
// }
//
// @Override
// public void run(TestResult result) {
// result.addError(this, new RuntimeException(e));
// }
// });
//}
catch (NoSuchMethodException e) {
}
}
break;
}
}
}
}
}
}
finally {
typeMapper = null;
}
return suite;
} catch (Exception e) {
throw ExceptionUtils.rethrow(e);
}
}
@Override
public void setUp() throws Exception {
super.setUp();
CompilerConfiguration configuration = JetTestUtils.compilerConfigurationForTests(ConfigurationKind.ALL,
TestJdkKind.FULL_JDK);
configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, JetTestUtils.getAnnotationsJar());
@@ -165,5 +98,77 @@ public class TestlibTest extends CodegenTestCase {
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR);
myEnvironment = new JetCoreEnvironment(getTestRootDisposable(), configuration);
generationState = KotlinToJVMBytecodeCompiler.analyzeAndGenerate(myEnvironment, false);
if (generationState == null) {
throw new RuntimeException("There were compilation errors");
}
ClassFileFactory classFileFactory = generationState.getFactory();
classLoader = new GeneratedClassLoader(classFileFactory,
new URLClassLoader(new URL[] {ForTestCompileRuntime.runtimeJarForTests().toURI().toURL(), junitJar.toURI().toURL()},
TestCase.class.getClassLoader()));
typeMapper = generationState.getTypeMapper();
for (JetFile jetFile : myEnvironment.getSourceFiles()) {
for (JetDeclaration decl : jetFile.getDeclarations()) {
if (decl instanceof JetClass) {
JetClass jetClass = (JetClass) decl;
ClassDescriptor descriptor = (ClassDescriptor) generationState.getBindingContext().get(
BindingContext.DECLARATION_TO_DESCRIPTOR, jetClass);
assertNotNull("Descriptor for declaration " + jetClass + " shouldn't be null", descriptor);
Set<JetType> allSuperTypes = new THashSet<JetType>();
DescriptorUtils.addSuperTypes(descriptor.getDefaultType(), allSuperTypes);
for (JetType type : allSuperTypes) {
String internalName = typeMapper.mapType(type, JetTypeMapperMode.IMPL).getInternalName();
if (internalName.equals("junit/framework/Test")) {
String name = typeMapper.mapType(descriptor.getDefaultType(), JetTypeMapperMode.IMPL).getInternalName();
//noinspection UseOfSystemOutOrSystemErr
System.out.println(name);
@SuppressWarnings("unchecked")
Class<TestCase> aClass = (Class<TestCase>) classLoader.loadClass(name.replace('/', '.'));
if ((aClass.getModifiers() & Modifier.ABSTRACT) == 0 && (aClass.getModifiers() & Modifier.PUBLIC) != 0) {
try {
Constructor<TestCase> constructor = aClass.getConstructor();
if ((constructor.getModifiers() & Modifier.PUBLIC) != 0) {
suite.addTestSuite(aClass);
}
}
catch (NoSuchMethodException e) {
// Ignore test classes we can't instantiate
}
}
break;
}
}
}
}
}
}
@Override
protected void tearDown() throws Exception {
typeMapper = null;
classLoader.dispose();
classLoader = null;
generationState = null;
myEnvironment = null;
junitJar = null;
super.tearDown();
}
}