Don't add kotlin-runtime to classpath in CliTest
Also remove testing code from KotlinToJVMBytecodeCompiler, delete code duplication, etc.
This commit is contained in:
+23
-113
@@ -30,35 +30,29 @@ import org.jetbrains.jet.analyzer.AnalyzeExhaust;
|
||||
import org.jetbrains.jet.cli.common.CLIConfigurationKeys;
|
||||
import org.jetbrains.jet.cli.common.CompilerPlugin;
|
||||
import org.jetbrains.jet.cli.common.CompilerPluginContext;
|
||||
import org.jetbrains.jet.cli.common.messages.*;
|
||||
import org.jetbrains.jet.cli.common.messages.AnalyzerWithCompilerReport;
|
||||
import org.jetbrains.jet.cli.common.messages.MessageCollector;
|
||||
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
|
||||
import org.jetbrains.jet.codegen.*;
|
||||
import org.jetbrains.jet.codegen.state.GenerationState;
|
||||
import org.jetbrains.jet.codegen.state.Progress;
|
||||
import org.jetbrains.jet.config.CommonConfigurationKeys;
|
||||
import org.jetbrains.jet.config.CompilerConfiguration;
|
||||
import org.jetbrains.jet.lang.parsing.JetScriptDefinition;
|
||||
import org.jetbrains.jet.lang.parsing.JetScriptDefinitionProvider;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil;
|
||||
import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter;
|
||||
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
||||
import org.jetbrains.jet.lang.resolve.ScriptNameUtil;
|
||||
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
|
||||
import org.jetbrains.jet.lang.resolve.java.PackageClassUtils;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
import org.jetbrains.jet.plugin.JetMainDetector;
|
||||
import org.jetbrains.jet.utils.ExceptionUtils;
|
||||
import org.jetbrains.jet.utils.KotlinPaths;
|
||||
import org.jetbrains.jet.utils.PathUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class KotlinToJVMBytecodeCompiler {
|
||||
@@ -203,16 +197,16 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean compileAndExecuteScript(
|
||||
public static void compileAndExecuteScript(
|
||||
@NotNull KotlinPaths paths,
|
||||
@NotNull JetCoreEnvironment environment,
|
||||
@NotNull List<String> scriptArgs) {
|
||||
Class<?> scriptClass = compileScript(paths, environment, null);
|
||||
if(scriptClass == null)
|
||||
return false;
|
||||
@NotNull List<String> scriptArgs
|
||||
) {
|
||||
Class<?> scriptClass = compileScript(paths, environment);
|
||||
if (scriptClass == null) return;
|
||||
|
||||
try {
|
||||
scriptClass.getConstructor(String[].class).newInstance(new Object[]{scriptArgs.toArray(new String[0])});
|
||||
scriptClass.getConstructor(String[].class).newInstance(new Object[]{scriptArgs.toArray(new String[scriptArgs.size()])});
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
throw e;
|
||||
@@ -220,29 +214,25 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException("Failed to evaluate script: " + e, e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Class<?> compileScript(
|
||||
@NotNull KotlinPaths paths, @NotNull JetCoreEnvironment environment, @Nullable ClassLoader parentLoader) {
|
||||
|
||||
GenerationState generationState = analyzeAndGenerate(environment);
|
||||
if (generationState == null) {
|
||||
@Nullable
|
||||
public static Class<?> compileScript(@NotNull KotlinPaths paths, @NotNull JetCoreEnvironment environment) {
|
||||
GenerationState state = analyzeAndGenerate(environment);
|
||||
if (state == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
GeneratedClassLoader classLoader = null;
|
||||
try {
|
||||
ClassFileFactory factory = generationState.getFactory();
|
||||
classLoader = new GeneratedClassLoader(factory,
|
||||
new URLClassLoader(new URL[] {
|
||||
// TODO: add all classpath
|
||||
paths.getRuntimePath().toURI().toURL()
|
||||
},
|
||||
parentLoader == null ? AllModules.class.getClassLoader() : parentLoader));
|
||||
classLoader = new GeneratedClassLoader(state.getFactory(),
|
||||
new URLClassLoader(new URL[] {
|
||||
// TODO: add all classpath
|
||||
paths.getRuntimePath().toURI().toURL()
|
||||
}, AllModules.class.getClassLoader())
|
||||
);
|
||||
|
||||
JetFile scriptFile = environment.getSourceFiles().get(0);
|
||||
return classLoader.loadClass(ScriptNameUtil.classNameForScript(scriptFile));
|
||||
return classLoader.loadClass(ScriptNameUtil.classNameForScript(environment.getSourceFiles().get(0)));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException("Failed to evaluate script: " + e, e);
|
||||
@@ -251,14 +241,12 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
if (classLoader != null) {
|
||||
classLoader.dispose();
|
||||
}
|
||||
generationState.destroy();
|
||||
state.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static GenerationState analyzeAndGenerate(
|
||||
JetCoreEnvironment environment
|
||||
) {
|
||||
public static GenerationState analyzeAndGenerate(@NotNull JetCoreEnvironment environment) {
|
||||
AnalyzeExhaust exhaust = analyze(environment);
|
||||
|
||||
if (exhaust == null) {
|
||||
@@ -271,7 +259,7 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static AnalyzeExhaust analyze(final JetCoreEnvironment environment) {
|
||||
private static AnalyzeExhaust analyze(@NotNull final JetCoreEnvironment environment) {
|
||||
AnalyzerWithCompilerReport analyzerWithCompilerReport = new AnalyzerWithCompilerReport(
|
||||
environment.getConfiguration().get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY));
|
||||
analyzerWithCompilerReport.analyzeAndReport(
|
||||
@@ -296,10 +284,7 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static GenerationState generate(
|
||||
JetCoreEnvironment environment,
|
||||
AnalyzeExhaust exhaust
|
||||
) {
|
||||
private static GenerationState generate(@NotNull JetCoreEnvironment environment, @NotNull AnalyzeExhaust exhaust) {
|
||||
Project project = environment.getProject();
|
||||
CompilerConfiguration configuration = environment.getConfiguration();
|
||||
GenerationState generationState = new GenerationState(
|
||||
@@ -316,79 +301,4 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
}
|
||||
return generationState;
|
||||
}
|
||||
|
||||
public static Class compileScript(
|
||||
@NotNull ClassLoader parentLoader,
|
||||
@NotNull KotlinPaths paths,
|
||||
@NotNull String scriptPath,
|
||||
@Nullable List<AnalyzerScriptParameter> scriptParameters,
|
||||
@Nullable List<JetScriptDefinition> scriptDefinitions) {
|
||||
MessageRenderer messageRenderer = MessageRenderer.PLAIN;
|
||||
GroupingMessageCollector messageCollector = new GroupingMessageCollector(new PrintingMessageCollector(System.err, messageRenderer, false));
|
||||
Disposable rootDisposable = Disposer.newDisposable();
|
||||
try {
|
||||
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
|
||||
compilerConfiguration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector);
|
||||
compilerConfiguration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, getClasspath(parentLoader));
|
||||
compilerConfiguration.add(JVMConfigurationKeys.CLASSPATH_KEY, PathUtil.findRtJar());
|
||||
compilerConfiguration.addAll(JVMConfigurationKeys.ANNOTATIONS_PATH_KEY, Collections.singletonList(
|
||||
paths.getJdkAnnotationsPath()));
|
||||
compilerConfiguration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, scriptPath);
|
||||
compilerConfiguration.addAll(CommonConfigurationKeys.SCRIPT_DEFINITIONS_KEY,
|
||||
scriptDefinitions != null ? scriptDefinitions : Collections.<JetScriptDefinition>emptyList());
|
||||
compilerConfiguration.put(JVMConfigurationKeys.SCRIPT_PARAMETERS, scriptParameters);
|
||||
|
||||
JetCoreEnvironment environment = JetCoreEnvironment.createForProduction(rootDisposable, compilerConfiguration);
|
||||
|
||||
try {
|
||||
JetScriptDefinitionProvider.getInstance(environment.getProject()).markFileAsScript(environment.getSourceFiles().get(0));
|
||||
return compileScript(paths, environment, parentLoader);
|
||||
}
|
||||
catch (CompilationException e) {
|
||||
messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(e),
|
||||
MessageUtil.psiElementToMessageLocation(e.getElement()));
|
||||
return null;
|
||||
}
|
||||
catch (Throwable t) {
|
||||
MessageCollectorUtil.reportException(messageCollector, t);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
finally {
|
||||
messageCollector.flush();
|
||||
Disposer.dispose(rootDisposable);
|
||||
}
|
||||
}
|
||||
|
||||
private static Collection<File> getClasspath(ClassLoader loader) {
|
||||
return getClasspath(loader, new LinkedList<File>());
|
||||
}
|
||||
|
||||
private static Collection<File> getClasspath(ClassLoader loader, LinkedList<File> files) {
|
||||
ClassLoader parent = loader.getParent();
|
||||
if(parent != null)
|
||||
getClasspath(parent, files);
|
||||
|
||||
if(loader instanceof URLClassLoader) {
|
||||
for (URL url : ((URLClassLoader) loader).getURLs()) {
|
||||
String urlFile = url.getFile();
|
||||
|
||||
if (urlFile.contains("%")) {
|
||||
try {
|
||||
urlFile = url.toURI().getPath();
|
||||
}
|
||||
catch (URISyntaxException e) {
|
||||
throw ExceptionUtils.rethrow(e);
|
||||
}
|
||||
}
|
||||
|
||||
File file = new File(urlFile);
|
||||
if(file.exists() && (file.isDirectory() || file.getName().endsWith(".jar"))) {
|
||||
files.add(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user