Gor rid of K2JVMCompileEnvironmentConfiguration and CompileEnvironmentConfiguration. Using JetCoreEnvironment instead of K2JVMCompileEnvironmentConfiguration.

This commit is contained in:
Evgeny Gerashchenko
2012-07-23 20:52:27 +04:00
parent a76c2bf2eb
commit 2727aa7da4
8 changed files with 48 additions and 145 deletions
@@ -49,14 +49,14 @@ public class BytecodeCompiler {
/**
* Creates new instance of {@link org.jetbrains.jet.cli.jvm.compiler.K2JVMCompileEnvironmentConfiguration} instance using the arguments specified.
* Creates new instance of {@link JetCoreEnvironment} instance using the arguments specified.
*
* @param stdlib path to "kotlin-runtime.jar", only used if not null and not empty
* @param classpath compilation classpath, only used if not null and not empty
* @param sourceRoots
* @return compile environment instance
*/
private K2JVMCompileEnvironmentConfiguration env(String stdlib, String[] classpath, String[] sourceRoots) {
private JetCoreEnvironment env(String stdlib, String[] classpath, String[] sourceRoots) {
CompilerConfiguration configuration = new CompilerConfiguration();
configuration.add(CLASSPATH_KEY, PathUtil.findRtJar());
if ((stdlib != null) && (stdlib.trim().length() > 0)) {
@@ -80,13 +80,11 @@ public class BytecodeCompiler {
configuration.addAll(CommonConfigurationKeys.SOURCE_ROOTS_KEY, Arrays.asList(sourceRoots));
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.PLAIN_TEXT_TO_SYSTEM_ERR);
JetCoreEnvironment environment = new JetCoreEnvironment(CompileEnvironmentUtil.createMockDisposable(), configuration);
K2JVMCompileEnvironmentConfiguration env = new K2JVMCompileEnvironmentConfiguration(environment);
// lets register any compiler plugins
configuration.addAll(CLIConfigurationKeys.COMPILER_PLUGINS, getCompilerPlugins());
return env;
return new JetCoreEnvironment(CompileEnvironmentUtil.createMockDisposable(), configuration);
}
@@ -114,9 +112,9 @@ public class BytecodeCompiler {
*/
public void sourcesToDir(@NotNull String src, @NotNull String output, @Nullable String stdlib, @Nullable String[] classpath) {
try {
K2JVMCompileEnvironmentConfiguration configuration = env(stdlib, classpath, new String[]{src});
JetCoreEnvironment environment = env(stdlib, classpath, new String[]{src});
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration, null, new File(output), true);
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, null, new File(output), true);
if (!success) {
throw new CompileEnvironmentException(errorMessage(src, false));
}
@@ -142,9 +140,9 @@ public class BytecodeCompiler {
@Nullable String stdlib,
@Nullable String[] classpath) {
try {
K2JVMCompileEnvironmentConfiguration configuration = env(stdlib, classpath, new String[]{src});
JetCoreEnvironment environment = env(stdlib, classpath, new String[]{src});
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration, new File(jar), null, includeRuntime);
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, new File(jar), null, includeRuntime);
if (!success) {
throw new CompileEnvironmentException(errorMessage(src, false));
}
@@ -175,7 +173,7 @@ public class BytecodeCompiler {
for (Module m : modules) {
sourcesRoots.addAll(m.getSourceFiles());
}
K2JVMCompileEnvironmentConfiguration env = env(stdlib, classpath, sourcesRoots.toArray(new String[0]));
JetCoreEnvironment env = env(stdlib, classpath, sourcesRoots.toArray(new String[0]));
File directory = new File(module).getParentFile();
boolean success = KotlinToJVMBytecodeCompiler.compileModules(env, modules, directory, new File(jar), null, includeRuntime);
if (!success) {
@@ -1,35 +0,0 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.cli.common;
import com.intellij.openapi.util.Disposer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.common.messages.MessageCollector;
/**
* @author Pavel Talanov
*/
public abstract class CompileEnvironmentConfiguration {
/**
* NOTE: It's very important to call dispose for every object of this class or there will be memory leaks.
*
* @see Disposer
*/
public CompileEnvironmentConfiguration() {
}
}
@@ -106,8 +106,6 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
compilerConfiguration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector);
K2JVMCompileEnvironmentConfiguration configuration = new K2JVMCompileEnvironmentConfiguration(environment);
messageCollector.report(CompilerMessageSeverity.LOGGING, "Configuring the compilation environment",
CompilerMessageLocation.NO_LOCATION);
try {
@@ -123,16 +121,16 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
List<Module> modules = CompileEnvironmentUtil.loadModuleScript(arguments.module, messageCollector);
messageCollector.setVerbose(oldVerbose);
File directory = new File(arguments.module).getParentFile();
noErrors = KotlinToJVMBytecodeCompiler.compileModules(configuration, modules,
noErrors = KotlinToJVMBytecodeCompiler.compileModules(environment, modules,
directory, jar, outputDir,
arguments.includeRuntime);
}
else if (arguments.script) {
List<String> scriptArgs = arguments.freeArgs.subList(1, arguments.freeArgs.size());
noErrors = KotlinToJVMBytecodeCompiler.compileAndExecuteScript(configuration, scriptArgs);
noErrors = KotlinToJVMBytecodeCompiler.compileAndExecuteScript(environment, scriptArgs);
}
else {
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration, jar, outputDir, arguments.includeRuntime);
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, jar, outputDir, arguments.includeRuntime);
}
return noErrors ? OK : COMPILATION_ERROR;
}
@@ -63,7 +63,7 @@ public class CompileEnvironmentUtil {
@Nullable
public static File getUnpackedRuntimePath() {
URL url = K2JVMCompileEnvironmentConfiguration.class.getClassLoader().getResource("jet/JetObject.class");
URL url = KotlinToJVMBytecodeCompiler.class.getClassLoader().getResource("jet/JetObject.class");
if (url != null && url.getProtocol().equals("file")) {
return new File(url.getPath()).getParentFile().getParentFile();
}
@@ -72,7 +72,7 @@ public class CompileEnvironmentUtil {
@Nullable
public static File getRuntimeJarPath() {
URL url = K2JVMCompileEnvironmentConfiguration.class.getClassLoader().getResource("jet/JetObject.class");
URL url = KotlinToJVMBytecodeCompiler.class.getClassLoader().getResource("jet/JetObject.class");
if (url != null && url.getProtocol().equals("jar")) {
String path = url.getPath();
return new File(path.substring(path.indexOf(":") + 1, path.indexOf("!/")));
@@ -103,8 +103,7 @@ public class CompileEnvironmentUtil {
JetCoreEnvironment scriptEnvironment = JetCoreEnvironment.createCoreEnvironmentForJVM(disposable, configuration);
GenerationState generationState = KotlinToJVMBytecodeCompiler.analyzeAndGenerate(
new K2JVMCompileEnvironmentConfiguration(scriptEnvironment), false);
GenerationState generationState = KotlinToJVMBytecodeCompiler.analyzeAndGenerate(scriptEnvironment, false);
if (generationState == null) {
throw new CompileEnvironmentException("Module script " + moduleScriptFile + " analyze failed");
}
@@ -136,7 +135,7 @@ public class CompileEnvironmentUtil {
}
}
else {
loader = new GeneratedClassLoader(factory, K2JVMCompileEnvironmentConfiguration.class.getClassLoader());
loader = new GeneratedClassLoader(factory, KotlinToJVMBytecodeCompiler.class.getClassLoader());
}
try {
Class namespaceClass = loader.loadClass(JvmAbi.PACKAGE_CLASS);
@@ -1,42 +0,0 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.cli.jvm.compiler;
import com.intellij.openapi.util.Disposer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.common.CompileEnvironmentConfiguration;
import org.jetbrains.jet.cli.common.messages.MessageCollector;
/**
* @author abreslav
*/
public class K2JVMCompileEnvironmentConfiguration extends CompileEnvironmentConfiguration {
private final JetCoreEnvironment environment;
/**
* NOTE: It's very important to call dispose for every object of this class or there will be memory leaks.
*
* @see Disposer
*/
public K2JVMCompileEnvironmentConfiguration(@NotNull JetCoreEnvironment environment) {
this.environment = environment;
}
public JetCoreEnvironment getEnvironment() {
return environment;
}
}
@@ -73,17 +73,13 @@ public class KotlinToJVMBytecodeCompiler {
}
@Nullable
public static ClassFileFactory compileModule(
K2JVMCompileEnvironmentConfiguration configuration,
Module moduleBuilder,
File directory
) {
public static ClassFileFactory compileModule(JetCoreEnvironment environment, Module moduleBuilder, File directory) {
if (moduleBuilder.getSourceFiles().isEmpty()) {
throw new CompileEnvironmentException("No source files where defined");
}
// TODO creating environment copy each time - not good. original environment shouldn't be passed at all
CompilerConfiguration compilerConfiguration = configuration.getEnvironment().getConfiguration().copy();
CompilerConfiguration compilerConfiguration = environment.getConfiguration().copy();
for (String sourceFile : moduleBuilder.getSourceFiles()) {
File source = new File(sourceFile);
if (!source.isAbsolute()) {
@@ -105,32 +101,27 @@ public class KotlinToJVMBytecodeCompiler {
compilerConfiguration.add(JVMConfigurationKeys.ANNOTATIONS_PATH_KEY, new File(annotationsRoot));
}
Disposable parentDisposable = new Disposable() {
@Override
public void dispose() {
}
};
JetCoreEnvironment environment = null;
Disposable parentDisposable = CompileEnvironmentUtil.createMockDisposable();
JetCoreEnvironment moduleEnvironment = null;
try {
environment = JetCoreEnvironment.createCoreEnvironmentForJVM(parentDisposable,
moduleEnvironment = JetCoreEnvironment.createCoreEnvironmentForJVM(parentDisposable,
compilerConfiguration);
K2JVMCompileEnvironmentConfiguration currentK2JVMConfiguration = new K2JVMCompileEnvironmentConfiguration(environment);
GenerationState generationState = analyzeAndGenerate(currentK2JVMConfiguration);
GenerationState generationState = analyzeAndGenerate(moduleEnvironment);
if (generationState == null) {
return null;
}
return generationState.getFactory();
} finally {
if (environment != null) {
if (moduleEnvironment != null) {
Disposer.dispose(parentDisposable);
}
}
}
public static boolean compileModules(
K2JVMCompileEnvironmentConfiguration configuration,
JetCoreEnvironment environment,
@NotNull List<Module> modules,
@NotNull File directory,
@Nullable File jarPath,
@@ -138,7 +129,7 @@ public class KotlinToJVMBytecodeCompiler {
boolean jarRuntime) {
for (Module moduleBuilder : modules) {
ClassFileFactory moduleFactory = compileModule(configuration, moduleBuilder, directory);
ClassFileFactory moduleFactory = compileModule(environment, moduleBuilder, directory);
if (moduleFactory == null) {
return false;
}
@@ -184,15 +175,15 @@ public class KotlinToJVMBytecodeCompiler {
}
public static boolean compileBunchOfSources(
K2JVMCompileEnvironmentConfiguration configuration,
JetCoreEnvironment environment,
@Nullable File jar,
@Nullable File outputDir,
boolean includeRuntime
) {
FqName mainClass = findMainClass(configuration.getEnvironment().getSourceFiles());
FqName mainClass = findMainClass(environment.getSourceFiles());
GenerationState generationState = analyzeAndGenerate(configuration);
GenerationState generationState = analyzeAndGenerate(environment);
if (generationState == null) {
return false;
}
@@ -230,9 +221,9 @@ public class KotlinToJVMBytecodeCompiler {
}
public static boolean compileAndExecuteScript(
@NotNull K2JVMCompileEnvironmentConfiguration configuration,
@NotNull JetCoreEnvironment environment,
@NotNull List<String> scriptArgs) {
Class<?> scriptClass = compileScript(configuration, null);
Class<?> scriptClass = compileScript(environment, null);
if(scriptClass == null)
return false;
@@ -249,9 +240,9 @@ public class KotlinToJVMBytecodeCompiler {
}
public static Class<?> compileScript(
@NotNull K2JVMCompileEnvironmentConfiguration configuration, ClassLoader parentLoader) {
@NotNull JetCoreEnvironment environment, ClassLoader parentLoader) {
GenerationState generationState = analyzeAndGenerate(configuration);
GenerationState generationState = analyzeAndGenerate(environment);
if (generationState == null) {
return null;
}
@@ -264,7 +255,7 @@ public class KotlinToJVMBytecodeCompiler {
CompilerPathUtil.getRuntimePath().toURI().toURL()
},
parentLoader == null ? AllModules.class.getClassLoader() : parentLoader));
JetFile scriptFile = configuration.getEnvironment().getSourceFiles().get(0);
JetFile scriptFile = environment.getSourceFiles().get(0);
return classLoader.loadClass(ScriptCodegen.classNameForScript(scriptFile));
}
catch (Exception e) {
@@ -277,27 +268,27 @@ public class KotlinToJVMBytecodeCompiler {
}
@Nullable
public static GenerationState analyzeAndGenerate(K2JVMCompileEnvironmentConfiguration configuration) {
return analyzeAndGenerate(configuration, configuration.getEnvironment().getConfiguration().get(JVMConfigurationKeys.STUBS, false),
configuration.getEnvironment().getConfiguration().getList(JVMConfigurationKeys.SCRIPT_PARAMETERS));
public static GenerationState analyzeAndGenerate(JetCoreEnvironment environment) {
return analyzeAndGenerate(environment, environment.getConfiguration().get(JVMConfigurationKeys.STUBS, false),
environment.getConfiguration().getList(JVMConfigurationKeys.SCRIPT_PARAMETERS));
}
@Nullable
public static GenerationState analyzeAndGenerate(
K2JVMCompileEnvironmentConfiguration configuration,
JetCoreEnvironment environment,
boolean stubs
) {
return analyzeAndGenerate(configuration, stubs,
configuration.getEnvironment().getConfiguration().getList(JVMConfigurationKeys.SCRIPT_PARAMETERS));
return analyzeAndGenerate(environment, stubs,
environment.getConfiguration().getList(JVMConfigurationKeys.SCRIPT_PARAMETERS));
}
@Nullable
public static GenerationState analyzeAndGenerate(
K2JVMCompileEnvironmentConfiguration configuration,
JetCoreEnvironment environment,
boolean stubs,
List<AnalyzerScriptParameter> scriptParameters
) {
AnalyzeExhaust exhaust = analyze(configuration, scriptParameters, stubs);
AnalyzeExhaust exhaust = analyze(environment, scriptParameters, stubs);
if (exhaust == null) {
return null;
@@ -305,17 +296,16 @@ public class KotlinToJVMBytecodeCompiler {
exhaust.throwIfError();
return generate(configuration, exhaust, stubs);
return generate(environment, exhaust, stubs);
}
@Nullable
private static AnalyzeExhaust analyze(
final K2JVMCompileEnvironmentConfiguration configuration,
final JetCoreEnvironment environment,
final List<AnalyzerScriptParameter> scriptParameters,
boolean stubs) {
final JetCoreEnvironment environment = configuration.getEnvironment();
AnalyzerWithCompilerReport analyzerWithCompilerReport = new AnalyzerWithCompilerReport(configuration.getEnvironment().getConfiguration().get(
CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY));
AnalyzerWithCompilerReport analyzerWithCompilerReport = new AnalyzerWithCompilerReport(
environment.getConfiguration().get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY));
final Predicate<PsiFile> filesToAnalyzeCompletely =
stubs ? Predicates.<PsiFile>alwaysFalse() : Predicates.<PsiFile>alwaysTrue();
analyzerWithCompilerReport.analyzeAndReport(
@@ -339,10 +329,9 @@ public class KotlinToJVMBytecodeCompiler {
@NotNull
private static GenerationState generate(
K2JVMCompileEnvironmentConfiguration configuration,
final JetCoreEnvironment environment,
AnalyzeExhaust exhaust,
boolean stubs) {
final JetCoreEnvironment environment = configuration.getEnvironment();
Project project = environment.getProject();
Progress backendProgress = new Progress() {
@Override
@@ -384,11 +373,10 @@ public class KotlinToJVMBytecodeCompiler {
compilerConfiguration.put(JVMConfigurationKeys.SCRIPT_PARAMETERS, scriptParameters);
JetCoreEnvironment environment = JetCoreEnvironment.createCoreEnvironmentForJVM(rootDisposable, compilerConfiguration);
K2JVMCompileEnvironmentConfiguration configuration = new K2JVMCompileEnvironmentConfiguration(environment, messageCollector);
try {
JetScriptDefinitionProvider.getInstance(environment.getProject()).markFileAsScript(environment.getSourceFiles().get(0));
return compileScript(configuration, parentLoader);
return compileScript(environment, parentLoader);
}
catch (CompilationException e) {
messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(e),
@@ -28,7 +28,6 @@ import org.jetbrains.jet.cli.common.CLIConfigurationKeys;
import org.jetbrains.jet.cli.common.messages.MessageCollector;
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.cli.jvm.compiler.K2JVMCompileEnvironmentConfiguration;
import org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler;
import org.jetbrains.jet.codegen.forTestCompile.ForTestCompileRuntime;
import org.jetbrains.jet.config.CommonConfigurationKeys;
@@ -79,8 +78,7 @@ public class TestlibTest extends CodegenTestCase {
private TestSuite doBuildSuite() {
try {
GenerationState generationState = KotlinToJVMBytecodeCompiler.analyzeAndGenerate(
new K2JVMCompileEnvironmentConfiguration(myEnvironment), false);
GenerationState generationState = KotlinToJVMBytecodeCompiler.analyzeAndGenerate(myEnvironment, false);
if (generationState == null) {
throw new RuntimeException("There were compilation errors");
@@ -2,11 +2,10 @@ package org.jetbrains.kotlin.doc
import java.io.PrintStream
import org.jetbrains.jet.cli.common.CLICompiler
import org.jetbrains.jet.cli.common.CLIConfigurationKeys
import org.jetbrains.jet.cli.jvm.K2JVMCompiler
import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments
import org.jetbrains.jet.cli.jvm.compiler.K2JVMCompileEnvironmentConfiguration
import org.jetbrains.jet.config.CompilerConfiguration
import org.jetbrains.jet.cli.common.CLIConfigurationKeys
/**
* Main for running the KDocCompiler