Revert "ability to compile text and small refactoring"

This reverts commit 4ddf101397.
This commit is contained in:
Alex Tkachman
2012-02-23 17:01:33 +02:00
parent edffe8f29e
commit 4f67177aa2
8 changed files with 106 additions and 173 deletions
@@ -17,21 +17,23 @@
package org.jetbrains.jet.compiler;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.testFramework.LightVirtualFile;
import com.intellij.util.LocalTimeCounter;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.Processor;
import jet.modules.AllModules;
import jet.modules.Module;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.codegen.ClassFileFactory;
import org.jetbrains.jet.codegen.GeneratedClassLoader;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetPsiUtil;
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.plugin.JetLanguage;
import org.jetbrains.jet.plugin.JetMainDetector;
import org.jetbrains.jet.plugin.compiler.PathUtil;
import java.io.*;
import java.lang.reflect.Method;
@@ -57,18 +59,10 @@ public class CompileEnvironment {
private boolean ignoreErrors = false;
public CompileEnvironment() {
this(FileNameTransformer.IDENTITY, null);
this(FileNameTransformer.IDENTITY);
}
public CompileEnvironment(ClassLoader loader) {
this(FileNameTransformer.IDENTITY, loader);
}
public CompileEnvironment(FileNameTransformer fileNameTransformer) {
this(fileNameTransformer, null);
}
public CompileEnvironment(FileNameTransformer fileNameTransformer, ClassLoader loader) {
myRootDisposable = new Disposable() {
@Override
public void dispose() {
@@ -76,10 +70,6 @@ public class CompileEnvironment {
};
myEnvironment = new JetCoreEnvironment(myRootDisposable);
myFileNameTransformer = fileNameTransformer;
if(loader != null) {
myEnvironment.addToClasspathFromClassLoader(loader);
}
}
public void setErrorStream(PrintStream errorStream) {
@@ -94,6 +84,78 @@ public class CompileEnvironment {
Disposer.dispose(myRootDisposable);
}
@Nullable
public static File getUnpackedRuntimePath() {
URL url = CompileEnvironment.class.getClassLoader().getResource("jet/JetObject.class");
if (url != null && url.getProtocol().equals("file")) {
return new File(url.getPath()).getParentFile().getParentFile();
}
return null;
}
@Nullable
public static File getRuntimeJarPath() {
URL url = CompileEnvironment.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("!/")));
}
return null;
}
public void ensureRuntime() {
ensureRuntime(myEnvironment);
}
public static void ensureRuntime(@NotNull JetCoreEnvironment env) {
Project project = env.getProject();
if (JavaPsiFacade.getInstance(project).findClass("java.lang.Object", GlobalSearchScope.allScope(project)) == null) {
// TODO: prepend
env.addToClasspath(findRtJar());
}
if (JavaPsiFacade.getInstance(project).findClass("jet.JetObject", GlobalSearchScope.allScope(project)) == null) {
// TODO: prepend
File kotlin = PathUtil.getDefaultRuntimePath();
if (kotlin == null || !kotlin.exists()) {
kotlin = getUnpackedRuntimePath();
if (kotlin == null) kotlin = getRuntimeJarPath();
}
if (kotlin == null) {
throw new IllegalStateException("kotlin runtime not found");
}
env.addToClasspath(kotlin);
}
}
public static File findRtJar() {
String javaHome = System.getProperty("java.home");
if ("jre".equals(new File(javaHome).getName())) {
javaHome = new File(javaHome).getParent();
}
File rtJar = findRtJar(javaHome);
if (rtJar == null || !rtJar.exists()) {
throw new CompileEnvironmentException("No JDK rt.jar found under" + javaHome);
}
return rtJar;
}
private static File findRtJar(String javaHome) {
File rtJar = new File(javaHome, "jre/lib/rt.jar");
if (rtJar.exists()) {
return rtJar;
}
File classesJar = new File(new File(javaHome).getParentFile().getAbsolutePath(), "Classes/classes.jar");
if (classesJar.exists()) {
return classesJar;
}
return null;
}
public void compileModuleScript(String moduleFile, @Nullable String jarPath, @Nullable String outputDir, boolean jarRuntime) {
final List<Module> modules = loadModuleScript(moduleFile);
@@ -127,7 +189,7 @@ public class CompileEnvironment {
public List<Module> loadModuleScript(String moduleFile) {
CompileSession scriptCompileSession = new CompileSession(myEnvironment, myFileNameTransformer);
scriptCompileSession.addSources(moduleFile);
myEnvironment.ensureRuntime();
ensureRuntime();
if (!scriptCompileSession.analyze(myErrorStream)) {
return null;
@@ -150,9 +212,8 @@ public class CompileEnvironment {
method.setAccessible(true);
method.invoke(null);
ArrayList<Module> modules = AllModules.modules.get();
ArrayList<Module> answer = new ArrayList<Module>(modules);
modules.clear();
ArrayList<Module> answer = new ArrayList<Module>(AllModules.modules);
AllModules.modules.clear();
return answer;
}
catch (Exception e) {
@@ -186,7 +247,7 @@ public class CompileEnvironment {
myEnvironment.addToClasspath(new File(classpathRoot));
}
myEnvironment.ensureRuntime();
ensureRuntime();
if (!moduleCompileSession.analyze(myErrorStream) && !ignoreErrors) {
return null;
@@ -224,7 +285,7 @@ public class CompileEnvironment {
}
private static void writeRuntimeToJar(final JarOutputStream stream) throws IOException {
final File unpackedRuntimePath = JetCoreEnvironment.getUnpackedRuntimePath();
final File unpackedRuntimePath = getUnpackedRuntimePath();
if (unpackedRuntimePath != null) {
FileUtil.processFilesRecursively(unpackedRuntimePath, new Processor<File>() {
@Override
@@ -247,7 +308,7 @@ public class CompileEnvironment {
});
}
else {
File runtimeJarPath = JetCoreEnvironment.getRuntimeJarPath();
File runtimeJarPath = getRuntimeJarPath();
if (runtimeJarPath != null) {
JarInputStream jis = new JarInputStream(new FileInputStream(runtimeJarPath));
try {
@@ -271,18 +332,6 @@ public class CompileEnvironment {
}
}
public ClassLoader compileText(String code) {
CompileSession session = new CompileSession(myEnvironment, myFileNameTransformer);
session.addSources(new LightVirtualFile("script" + LocalTimeCounter.currentTime() + ".kt", JetLanguage.INSTANCE, code));
if (!session.analyze(myErrorStream) && !ignoreErrors) {
return null;
}
ClassFileFactory factory = session.generate();
return new GeneratedClassLoader(factory);
}
public boolean compileBunchOfSources(String sourceFileOrDir, String jar, String outputDir, boolean includeRuntime) {
CompileSession session = new CompileSession(myEnvironment, myFileNameTransformer);
session.addSources(sourceFileOrDir);
@@ -296,7 +345,7 @@ public class CompileEnvironment {
}
}
myEnvironment.ensureRuntime();
ensureRuntime();
if (!session.analyze(myErrorStream) && !ignoreErrors) {
return false;
@@ -20,19 +20,12 @@ import com.intellij.core.JavaCoreEnvironment;
import com.intellij.lang.java.JavaParserDefinition;
import com.intellij.mock.MockApplication;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.parsing.JetParserDefinition;
import org.jetbrains.jet.lang.types.JetStandardLibrary;
import org.jetbrains.jet.plugin.JetFileType;
import org.jetbrains.jet.plugin.compiler.PathUtil;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
@@ -64,74 +57,6 @@ public class JetCoreEnvironment extends JavaCoreEnvironment {
JetStandardLibrary.initialize(getProject());
}
@Nullable
public static File getUnpackedRuntimePath() {
URL url = CompileEnvironment.class.getClassLoader().getResource("jet/JetObject.class");
if (url != null && url.getProtocol().equals("file")) {
return new File(url.getPath()).getParentFile().getParentFile();
}
return null;
}
@Nullable
public static File getRuntimeJarPath() {
URL url = CompileEnvironment.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("!/")));
}
return null;
}
public static File findRtJar() {
String javaHome = System.getProperty("java.home");
if ("jre".equals(new File(javaHome).getName())) {
javaHome = new File(javaHome).getParent();
}
File rtJar = findRtJar(javaHome);
if (rtJar == null || !rtJar.exists()) {
throw new CompileEnvironmentException("No JDK rt.jar found under" + javaHome);
}
return rtJar;
}
private static File findRtJar(String javaHome) {
File rtJar = new File(javaHome, "jre/lib/rt.jar");
if (rtJar.exists()) {
return rtJar;
}
File classesJar = new File(new File(javaHome).getParentFile().getAbsolutePath(), "Classes/classes.jar");
if (classesJar.exists()) {
return classesJar;
}
return null;
}
public void ensureRuntime() {
Project project = getProject();
if (JavaPsiFacade.getInstance(project).findClass("java.lang.Object", GlobalSearchScope.allScope(project)) == null) {
// TODO: prepend
addToClasspath(findRtJar());
}
if (JavaPsiFacade.getInstance(project).findClass("jet.JetObject", GlobalSearchScope.allScope(project)) == null) {
// TODO: prepend
File kotlin = PathUtil.getDefaultRuntimePath();
if (kotlin == null || !kotlin.exists()) {
kotlin = getUnpackedRuntimePath();
if (kotlin == null) kotlin = getRuntimeJarPath();
}
if (kotlin == null) {
throw new IllegalStateException("kotlin runtime not found");
}
addToClasspath(kotlin);
}
}
public MockApplication getApplication() {
return myApplication;
}
@@ -143,18 +68,4 @@ public class JetCoreEnvironment extends JavaCoreEnvironment {
public void setCompilerPlugins(List<CompilerPlugin> compilerPlugins) {
this.compilerPlugins = compilerPlugins;
}
public void addToClasspathFromClassLoader(ClassLoader loader) {
ClassLoader parent = loader.getParent();
if(parent != null)
addToClasspathFromClassLoader(parent);
if(loader instanceof URLClassLoader) {
for (URL url : ((URLClassLoader) loader).getURLs()) {
File file = new File(url.getPath());
if(!file.isFile() || file.getPath().endsWith(".jar"))
addToClasspath(file);
}
}
}
}
@@ -30,6 +30,7 @@ import com.intellij.testFramework.LightVirtualFile;
import com.intellij.testFramework.TestDataFile;
import com.intellij.testFramework.UsefulTestCase;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.jet.compiler.CompileEnvironment;
import org.jetbrains.jet.compiler.JetCoreEnvironment;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.plugin.JetLanguage;
@@ -74,7 +75,7 @@ public abstract class JetLiteFixture extends UsefulTestCase {
protected void createEnvironmentWithFullJdk() {
myEnvironment = new JetCoreEnvironment(getTestRootDisposable());
final File rtJar = JetCoreEnvironment.findRtJar();
final File rtJar = CompileEnvironment.findRtJar();
myEnvironment.addToClasspath(rtJar);
}
@@ -223,4 +223,18 @@ public abstract class CodegenTestCase extends JetLiteFixture {
return loadClass(name, codegens);
}
private static class MyClassLoader extends ClassLoader {
public MyClassLoader(ClassLoader parent) {
super(parent);
}
public Class doDefineClass(String name, byte[] data) {
return defineClass(name, data, 0, data.length);
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
return super.loadClass(name);
}
}
}
@@ -1,35 +0,0 @@
/*
* Copyright 2000-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.codegen;
import junit.framework.TestCase;
import org.jetbrains.jet.compiler.CompileEnvironment;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class CompileTextTest extends TestCase {
public void testMe() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
String text = "import org.jetbrains.jet.codegen.CompileTextTest; fun x() = CompileTextTest()";
CompileEnvironment compileEnvironment = new CompileEnvironment(getClass().getClassLoader());
ClassLoader classLoader = compileEnvironment.compileText(text);
Class<?> namespace = classLoader.loadClass("namespace");
Method x = namespace.getDeclaredMethod("x");
Object invoke = x.invoke(null);
assertTrue(invoke instanceof CompileTextTest);
}
}
@@ -16,6 +16,8 @@
package org.jetbrains.jet.codegen;
import org.jetbrains.jet.compiler.CompileEnvironment;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
@@ -30,7 +32,7 @@ public class StdlibTest extends CodegenTestCase {
super.setUp();
createEnvironmentWithFullJdk();
myEnvironment.addToClasspath(ForTestCompileStdlib.stdlibJarForTests());
myEnvironment.ensureRuntime();
CompileEnvironment.ensureRuntime(myEnvironment);
}
@Override
+1 -1
View File
@@ -6,7 +6,7 @@ import jet.modules.*
fun module(name: String, callback: ModuleBuilder.() -> Unit) {
val builder = ModuleBuilder(name)
builder.callback()
AllModules.modules.sure().get()?.add(builder)
AllModules.modules?.add(builder)
}
class SourcesBuilder(val parent: ModuleBuilder) {
+1 -10
View File
@@ -22,14 +22,5 @@ package jet.modules;
import java.util.ArrayList;
public class AllModules {
public static final ThreadLocal<ArrayList<Module>> modules = new ThreadLocal<ArrayList<Module>>() {
@Override
protected ArrayList<Module> initialValue() {
return new ArrayList<Module>();
}
};
private AllModules() {
}
public static final ArrayList<Module> modules = new ArrayList<Module>();
}