J2KTranslator: remove findRtJar method. Use PathUtil instead
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
<orderEntry type="library" scope="PROVIDED" name="intellij-core" level="project" />
|
<orderEntry type="library" scope="PROVIDED" name="intellij-core" level="project" />
|
||||||
<orderEntry type="module" module-name="frontend" />
|
<orderEntry type="module" module-name="frontend" />
|
||||||
|
<orderEntry type="module" module-name="util" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
|
|||||||
@@ -20,15 +20,14 @@ import com.intellij.core.JavaCoreProjectEnvironment;
|
|||||||
import com.intellij.lang.java.JavaLanguage;
|
import com.intellij.lang.java.JavaLanguage;
|
||||||
import com.intellij.openapi.Disposable;
|
import com.intellij.openapi.Disposable;
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
import com.intellij.openapi.util.text.StringUtil;
|
|
||||||
import com.intellij.psi.PsiElement;
|
import com.intellij.psi.PsiElement;
|
||||||
import com.intellij.psi.PsiFile;
|
import com.intellij.psi.PsiFile;
|
||||||
import com.intellij.psi.PsiFileFactory;
|
import com.intellij.psi.PsiFileFactory;
|
||||||
import com.intellij.psi.PsiJavaFile;
|
import com.intellij.psi.PsiJavaFile;
|
||||||
import com.intellij.util.Function;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.j2k.visitors.ClassVisitor;
|
import org.jetbrains.jet.j2k.visitors.ClassVisitor;
|
||||||
|
import org.jetbrains.jet.utils.PathUtil;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -70,7 +69,7 @@ public class JavaToKotlinTranslator {
|
|||||||
JavaCoreApplicationEnvironment applicationEnvironment = new JavaCoreApplicationEnvironment(DISPOSABLE);
|
JavaCoreApplicationEnvironment applicationEnvironment = new JavaCoreApplicationEnvironment(DISPOSABLE);
|
||||||
JavaCoreProjectEnvironment javaCoreEnvironment = new JavaCoreProjectEnvironment(DISPOSABLE, applicationEnvironment);
|
JavaCoreProjectEnvironment javaCoreEnvironment = new JavaCoreProjectEnvironment(DISPOSABLE, applicationEnvironment);
|
||||||
|
|
||||||
javaCoreEnvironment.addJarToClassPath(findRtJar());
|
javaCoreEnvironment.addJarToClassPath(PathUtil.findRtJar());
|
||||||
File annotations = findAnnotations();
|
File annotations = findAnnotations();
|
||||||
if (annotations != null && annotations.exists()) {
|
if (annotations != null && annotations.exists()) {
|
||||||
javaCoreEnvironment.addJarToClassPath(annotations);
|
javaCoreEnvironment.addJarToClassPath(annotations);
|
||||||
@@ -94,45 +93,6 @@ public class JavaToKotlinTranslator {
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static File findRtJar() {
|
|
||||||
String javaHome = System.getenv("JAVA_HOME");
|
|
||||||
File rtJar;
|
|
||||||
if (javaHome == null) {
|
|
||||||
rtJar = findActiveRtJar(true);
|
|
||||||
|
|
||||||
if (rtJar == null) {
|
|
||||||
throw new SetupJavaCoreEnvironmentException("JAVA_HOME environment variable needs to be defined");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
rtJar = findRtJar(javaHome);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rtJar == null || !rtJar.exists()) {
|
|
||||||
rtJar = findActiveRtJar(true);
|
|
||||||
|
|
||||||
if ((rtJar == null || !rtJar.exists())) {
|
|
||||||
throw new SetupJavaCoreEnvironmentException("No rt.jar found under JAVA_HOME=" + javaHome);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return rtJar;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static File findAnnotations() {
|
public static File findAnnotations() {
|
||||||
ClassLoader classLoader = JavaToKotlinTranslator.class.getClassLoader();
|
ClassLoader classLoader = JavaToKotlinTranslator.class.getClassLoader();
|
||||||
@@ -149,37 +109,6 @@ public class JavaToKotlinTranslator {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private static File findActiveRtJar(boolean failOnError) {
|
|
||||||
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
|
|
||||||
if (systemClassLoader instanceof URLClassLoader) {
|
|
||||||
URLClassLoader loader = (URLClassLoader) systemClassLoader;
|
|
||||||
for (URL url : loader.getURLs()) {
|
|
||||||
if ("file".equals(url.getProtocol())) {
|
|
||||||
if (url.getFile().endsWith("/lib/rt.jar")) {
|
|
||||||
return new File(url.getFile());
|
|
||||||
}
|
|
||||||
if (url.getFile().endsWith("/Classes/classes.jar")) {
|
|
||||||
return new File(url.getFile()).getAbsoluteFile();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (failOnError) {
|
|
||||||
throw new SetupJavaCoreEnvironmentException("Could not find rt.jar in system class loader: " + StringUtil.join(loader.getURLs(), new Function<URL, String>() {
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public String fun(@NotNull URL url) {
|
|
||||||
return url.toString() + "\n";
|
|
||||||
}
|
|
||||||
}, ", "));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (failOnError) {
|
|
||||||
throw new SetupJavaCoreEnvironmentException("System class loader is not an URLClassLoader: " + systemClassLoader);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void setClassIdentifiers(@NotNull Converter converter, @NotNull PsiElement psiFile) {
|
static void setClassIdentifiers(@NotNull Converter converter, @NotNull PsiElement psiFile) {
|
||||||
ClassVisitor c = new ClassVisitor();
|
ClassVisitor c = new ClassVisitor();
|
||||||
psiFile.accept(c);
|
psiFile.accept(c);
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
<orderEntry type="module" module-name="j2k" />
|
<orderEntry type="module" module-name="j2k" />
|
||||||
<orderEntry type="module" module-name="cli" scope="TEST" />
|
<orderEntry type="module" module-name="cli" scope="TEST" />
|
||||||
<orderEntry type="module" module-name="frontend" scope="TEST" />
|
<orderEntry type="module" module-name="frontend" scope="TEST" />
|
||||||
|
<orderEntry type="module" module-name="util" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
|
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
|
||||||
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
|
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
|
||||||
import org.jetbrains.jet.config.CompilerConfiguration;
|
import org.jetbrains.jet.config.CompilerConfiguration;
|
||||||
|
import org.jetbrains.jet.utils.PathUtil;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -42,7 +43,7 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
|||||||
static {
|
static {
|
||||||
CompilerConfiguration config = new CompilerConfiguration();
|
CompilerConfiguration config = new CompilerConfiguration();
|
||||||
|
|
||||||
config.add(JVMConfigurationKeys.CLASSPATH_KEY, JavaToKotlinTranslator.findRtJar());
|
config.add(JVMConfigurationKeys.CLASSPATH_KEY, PathUtil.findRtJar());
|
||||||
File annotations = JavaToKotlinTranslator.findAnnotations();
|
File annotations = JavaToKotlinTranslator.findAnnotations();
|
||||||
if (annotations != null && annotations.exists()) {
|
if (annotations != null && annotations.exists()) {
|
||||||
config.add(JVMConfigurationKeys.CLASSPATH_KEY, annotations);
|
config.add(JVMConfigurationKeys.CLASSPATH_KEY, annotations);
|
||||||
@@ -74,9 +75,9 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
|||||||
if (!kotlinFile.exists()) {
|
if (!kotlinFile.exists()) {
|
||||||
FileUtil.writeToFile(kotlinFile, "");
|
FileUtil.writeToFile(kotlinFile, "");
|
||||||
}
|
}
|
||||||
final String expected = FileUtil.loadFile(kotlinFile);
|
final String expected = FileUtil.loadFile(kotlinFile, true);
|
||||||
final File javaFile = new File(javaPath);
|
final File javaFile = new File(javaPath);
|
||||||
final String javaCode = FileUtil.loadFile(javaFile);
|
final String javaCode = FileUtil.loadFile(javaFile, true);
|
||||||
|
|
||||||
String actual = "";
|
String actual = "";
|
||||||
String parentFileName = javaFile.getParentFile().getName();
|
String parentFileName = javaFile.getParentFile().getName();
|
||||||
|
|||||||
Reference in New Issue
Block a user