Produciton code uses a shared application between many projects.
Tests always dispose the application together with JetCoreEnvironment
This commit is contained in:
@@ -75,7 +75,7 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
|
||||
|
||||
CompilerConfiguration configuration = new CompilerConfiguration();
|
||||
configuration.addAll(CommonConfigurationKeys.SOURCE_ROOTS_KEY, Arrays.asList(arguments.sourceFiles));
|
||||
JetCoreEnvironment environmentForJS = new JetCoreEnvironment(rootDisposable, configuration);
|
||||
JetCoreEnvironment environmentForJS = JetCoreEnvironment.createForProduction(rootDisposable, configuration);
|
||||
|
||||
Project project = environmentForJS.getProject();
|
||||
|
||||
|
||||
@@ -140,11 +140,11 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
|
||||
}
|
||||
else if (arguments.script) {
|
||||
List<String> scriptArgs = arguments.freeArgs.subList(1, arguments.freeArgs.size());
|
||||
JetCoreEnvironment environment = new JetCoreEnvironment(rootDisposable, configuration);
|
||||
JetCoreEnvironment environment = JetCoreEnvironment.createForProduction(rootDisposable, configuration);
|
||||
KotlinToJVMBytecodeCompiler.compileAndExecuteScript(paths, environment, scriptArgs);
|
||||
}
|
||||
else {
|
||||
JetCoreEnvironment environment = new JetCoreEnvironment(rootDisposable, configuration);
|
||||
JetCoreEnvironment environment = JetCoreEnvironment.createForProduction(rootDisposable, configuration);
|
||||
KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, jar, outputDir, arguments.includeRuntime);
|
||||
}
|
||||
return OK;
|
||||
|
||||
@@ -118,7 +118,7 @@ public class CompileEnvironmentUtil {
|
||||
|
||||
List<Module> modules;
|
||||
try {
|
||||
JetCoreEnvironment scriptEnvironment = new JetCoreEnvironment(disposable, configuration);
|
||||
JetCoreEnvironment scriptEnvironment = JetCoreEnvironment.createForProduction(disposable, configuration);
|
||||
GenerationState generationState = KotlinToJVMBytecodeCompiler.analyzeAndGenerate(scriptEnvironment);
|
||||
if (generationState == null) {
|
||||
throw new CompileEnvironmentException("Module script " + moduleScriptFile + " analyze failed:\n" +
|
||||
|
||||
@@ -29,6 +29,7 @@ import com.intellij.openapi.components.ServiceManager;
|
||||
import com.intellij.openapi.extensions.Extensions;
|
||||
import com.intellij.openapi.fileTypes.PlainTextFileType;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiElementFinder;
|
||||
import com.intellij.psi.PsiFile;
|
||||
@@ -36,6 +37,7 @@ import com.intellij.psi.PsiManager;
|
||||
import com.intellij.psi.impl.compiled.ClsCustomNavigationPolicy;
|
||||
import com.intellij.psi.impl.file.impl.JavaFileManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
import org.jetbrains.jet.CompilerModeProvider;
|
||||
import org.jetbrains.jet.OperationModeProvider;
|
||||
import org.jetbrains.jet.asJava.JavaElementFinder;
|
||||
@@ -64,10 +66,50 @@ import static org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity.WARN
|
||||
|
||||
public class JetCoreEnvironment {
|
||||
|
||||
private final JavaCoreApplicationEnvironment applicationEnvironment;
|
||||
private static final Object APPLICATION_LOCK = new Object();
|
||||
private static JavaCoreApplicationEnvironment ourApplicationEnvironment;
|
||||
|
||||
@NotNull
|
||||
private JavaCoreApplicationEnvironment initApplication(@NotNull Disposable parentDisposable) {
|
||||
public static JetCoreEnvironment createForProduction(@NotNull Disposable parentDisposable, @NotNull CompilerConfiguration configuration) {
|
||||
return new JetCoreEnvironment(parentDisposable, getOrCreateApplicationEnvironmentForProduction(), configuration);
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
@NotNull
|
||||
public static JetCoreEnvironment createForTests(@NotNull Disposable parentDisposable, @NotNull CompilerConfiguration configuration) {
|
||||
return new JetCoreEnvironment(parentDisposable, createApplicationEnvironment(parentDisposable), configuration);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static JavaCoreApplicationEnvironment getOrCreateApplicationEnvironmentForProduction() {
|
||||
synchronized (APPLICATION_LOCK) {
|
||||
if (ourApplicationEnvironment != null) return ourApplicationEnvironment;
|
||||
|
||||
Disposable parentDisposable = Disposer.newDisposable();
|
||||
ourApplicationEnvironment = createApplicationEnvironment(parentDisposable);
|
||||
Disposer.register(parentDisposable, new Disposable() {
|
||||
@Override
|
||||
public void dispose() {
|
||||
synchronized (APPLICATION_LOCK) {
|
||||
//noinspection AssignmentToStaticFieldFromInstanceMethod
|
||||
ourApplicationEnvironment = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
return ourApplicationEnvironment;
|
||||
}
|
||||
}
|
||||
|
||||
public static void disposeApplicationEnvironment() {
|
||||
synchronized (APPLICATION_LOCK) {
|
||||
if (ourApplicationEnvironment == null) return;
|
||||
JavaCoreApplicationEnvironment environment = ourApplicationEnvironment;
|
||||
ourApplicationEnvironment = null;
|
||||
Disposer.dispose(environment.getParentDisposable());
|
||||
}
|
||||
}
|
||||
|
||||
private static JavaCoreApplicationEnvironment createApplicationEnvironment(Disposable parentDisposable) {
|
||||
JavaCoreApplicationEnvironment applicationEnvironment = new JavaCoreApplicationEnvironment(parentDisposable);
|
||||
|
||||
// ability to get text from annotations xml files
|
||||
@@ -81,6 +123,7 @@ public class JetCoreEnvironment {
|
||||
applicationEnvironment.registerParserDefinition(new JetParserDefinition());
|
||||
|
||||
applicationEnvironment.getApplication().registerService(OperationModeProvider.class, new CompilerModeProvider());
|
||||
|
||||
return applicationEnvironment;
|
||||
}
|
||||
|
||||
@@ -92,12 +135,14 @@ public class JetCoreEnvironment {
|
||||
|
||||
private final CompilerConfiguration configuration;
|
||||
|
||||
public JetCoreEnvironment(Disposable parentDisposable, @NotNull CompilerConfiguration configuration) {
|
||||
private JetCoreEnvironment(
|
||||
@NotNull Disposable parentDisposable,
|
||||
@NotNull JavaCoreApplicationEnvironment applicationEnvironment,
|
||||
@NotNull CompilerConfiguration configuration
|
||||
) {
|
||||
this.configuration = configuration.copy();
|
||||
this.configuration.setReadOnly(true);
|
||||
|
||||
this.applicationEnvironment = initApplication(parentDisposable);
|
||||
|
||||
projectEnvironment = new JavaCoreProjectEnvironment(parentDisposable, applicationEnvironment);
|
||||
|
||||
MockProject project = projectEnvironment.getProject();
|
||||
@@ -140,9 +185,14 @@ public class JetCoreEnvironment {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CoreApplicationEnvironment getMyApplicationEnvironment() {
|
||||
return projectEnvironment.getEnvironment();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public MockApplication getApplication() {
|
||||
return applicationEnvironment.getApplication();
|
||||
return getMyApplicationEnvironment().getApplication();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -168,7 +218,7 @@ public class JetCoreEnvironment {
|
||||
}
|
||||
}
|
||||
else {
|
||||
VirtualFile fileByPath = applicationEnvironment.getLocalFileSystem().findFileByPath(file.getAbsolutePath());
|
||||
VirtualFile fileByPath = getMyApplicationEnvironment().getLocalFileSystem().findFileByPath(file.getAbsolutePath());
|
||||
if (fileByPath != null) {
|
||||
PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(fileByPath);
|
||||
if (psiFile instanceof JetFile) {
|
||||
@@ -183,7 +233,7 @@ public class JetCoreEnvironment {
|
||||
return;
|
||||
}
|
||||
|
||||
VirtualFile vFile = applicationEnvironment.getLocalFileSystem().findFileByPath(path);
|
||||
VirtualFile vFile = getMyApplicationEnvironment().getLocalFileSystem().findFileByPath(path);
|
||||
if (vFile == null) {
|
||||
report(ERROR, "Source file or directory not found: " + path);
|
||||
return;
|
||||
@@ -198,7 +248,7 @@ public class JetCoreEnvironment {
|
||||
|
||||
private void addToClasspath(File path) {
|
||||
if (path.isFile()) {
|
||||
VirtualFile jarFile = applicationEnvironment.getJarFileSystem().findFileByPath(path + "!/");
|
||||
VirtualFile jarFile = getMyApplicationEnvironment().getJarFileSystem().findFileByPath(path + "!/");
|
||||
if (jarFile == null) {
|
||||
report(WARNING, "Classpath entry points to a file that is not a JAR archive: " + path);
|
||||
return;
|
||||
@@ -207,7 +257,7 @@ public class JetCoreEnvironment {
|
||||
classPath.add(jarFile);
|
||||
}
|
||||
else {
|
||||
VirtualFile root = applicationEnvironment.getLocalFileSystem().findFileByPath(path.getAbsolutePath());
|
||||
VirtualFile root = getMyApplicationEnvironment().getLocalFileSystem().findFileByPath(path.getAbsolutePath());
|
||||
if (root == null) {
|
||||
report(WARNING, "Classpath entry points to a non-existent location: " + path);
|
||||
return;
|
||||
|
||||
+2
-2
@@ -100,7 +100,7 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
Disposable parentDisposable = CompileEnvironmentUtil.createMockDisposable();
|
||||
JetCoreEnvironment moduleEnvironment = null;
|
||||
try {
|
||||
moduleEnvironment = new JetCoreEnvironment(parentDisposable, compilerConfiguration);
|
||||
moduleEnvironment = JetCoreEnvironment.createForProduction(parentDisposable, compilerConfiguration);
|
||||
|
||||
|
||||
GenerationState generationState = analyzeAndGenerate(moduleEnvironment);
|
||||
@@ -338,7 +338,7 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
scriptDefinitions != null ? scriptDefinitions : Collections.<JetScriptDefinition>emptyList());
|
||||
compilerConfiguration.put(JVMConfigurationKeys.SCRIPT_PARAMETERS, scriptParameters);
|
||||
|
||||
JetCoreEnvironment environment = new JetCoreEnvironment(rootDisposable, compilerConfiguration);
|
||||
JetCoreEnvironment environment = JetCoreEnvironment.createForProduction(rootDisposable, compilerConfiguration);
|
||||
|
||||
try {
|
||||
JetScriptDefinitionProvider.getInstance(environment.getProject()).markFileAsScript(environment.getSourceFiles().get(0));
|
||||
|
||||
@@ -93,7 +93,7 @@ public class ReplInterpreter {
|
||||
private final ModuleDescriptorImpl module;
|
||||
|
||||
public ReplInterpreter(@NotNull Disposable disposable, @NotNull CompilerConfiguration configuration) {
|
||||
jetCoreEnvironment = new JetCoreEnvironment(disposable, configuration);
|
||||
jetCoreEnvironment = JetCoreEnvironment.createForProduction(disposable, configuration);
|
||||
Project project = jetCoreEnvironment.getProject();
|
||||
trace = new BindingTraceContext();
|
||||
module = AnalyzerFacadeForJVM.createJavaModule("<repl>");
|
||||
|
||||
Reference in New Issue
Block a user