Rollback facade classes to main repo version
This commit is contained in:
@@ -16,14 +16,12 @@ import org.jetbrains.jet.lang.types.expressions.OperatorConventions.*
|
||||
import com.intellij.openapi.util.Pair
|
||||
import java.text.MessageFormat
|
||||
import com.intellij.psi.util.PsiUtil
|
||||
import org.jetbrains.jet.config.CompilerConfiguration
|
||||
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment
|
||||
import com.intellij.openapi.Disposable
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName
|
||||
import org.jetbrains.jet.util.QualifiedNamesUtil
|
||||
import org.jetbrains.jet.lang.resolve.java.mapping.JavaToKotlinClassMap
|
||||
import com.intellij.openapi.project.Project
|
||||
|
||||
public open class Converter() {
|
||||
public open class Converter(val project: Project) {
|
||||
|
||||
private var classIdentifiersSet: MutableSet<String> = Sets.newHashSet()!!
|
||||
|
||||
@@ -31,15 +29,6 @@ public open class Converter() {
|
||||
|
||||
private val flags: MutableSet<J2KConverterFlags?>? = Sets.newHashSet()
|
||||
|
||||
private val jetCoreEnvironment = JetCoreEnvironment(
|
||||
object : Disposable {
|
||||
public override fun dispose() {
|
||||
}
|
||||
}, CompilerConfiguration())
|
||||
|
||||
|
||||
private val project = jetCoreEnvironment.getProject();
|
||||
|
||||
private val javaToKotlinClassMap: JavaToKotlinClassMap = JavaToKotlinClassMap.getInstance()
|
||||
|
||||
public open var methodReturnType: PsiType? = null
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2012 JetBrains s.r.o.
|
||||
* Copyright 2010-2013 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.
|
||||
@@ -19,6 +19,8 @@ import com.intellij.core.JavaCoreApplicationEnvironment;
|
||||
import com.intellij.core.JavaCoreProjectEnvironment;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiFileFactory;
|
||||
@@ -35,11 +37,12 @@ import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
public class JavaToKotlinTranslator {
|
||||
|
||||
private static final Disposable DISPOSABLE = Disposer.newDisposable();
|
||||
|
||||
private JavaToKotlinTranslator() {
|
||||
}
|
||||
|
||||
private static final Converter CONVERTER = new Converter();
|
||||
|
||||
@Nullable
|
||||
private static PsiFile createFile(@NotNull String text) {
|
||||
JavaCoreProjectEnvironment javaCoreEnvironment = setUpJavaCoreEnvironment();
|
||||
@@ -49,21 +52,16 @@ public class JavaToKotlinTranslator {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
static PsiFile createFile(@NotNull JavaCoreProjectEnvironment javaCoreEnvironment, @NotNull String text) {
|
||||
return PsiFileFactory.getInstance(javaCoreEnvironment.getProject()).createFileFromText(
|
||||
static PsiFile createFile(@NotNull Project project, @NotNull String text) {
|
||||
return PsiFileFactory.getInstance(project).createFileFromText(
|
||||
"test.java", JavaLanguage.INSTANCE, text
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static JavaCoreProjectEnvironment setUpJavaCoreEnvironment() {
|
||||
Disposable parentDisposable = new Disposable() {
|
||||
@Override
|
||||
public void dispose() {
|
||||
}
|
||||
};
|
||||
JavaCoreApplicationEnvironment applicationEnvironment = new JavaCoreApplicationEnvironment(parentDisposable);
|
||||
JavaCoreProjectEnvironment javaCoreEnvironment = new JavaCoreProjectEnvironment(parentDisposable, applicationEnvironment);
|
||||
JavaCoreApplicationEnvironment applicationEnvironment = new JavaCoreApplicationEnvironment(DISPOSABLE);
|
||||
JavaCoreProjectEnvironment javaCoreEnvironment = new JavaCoreProjectEnvironment(DISPOSABLE, applicationEnvironment);
|
||||
|
||||
javaCoreEnvironment.addJarToClassPath(PathUtil.findRtJar());
|
||||
File annotations = findAnnotations();
|
||||
@@ -90,7 +88,7 @@ public class JavaToKotlinTranslator {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static File findAnnotations() {
|
||||
public static File findAnnotations() {
|
||||
ClassLoader classLoader = JavaToKotlinTranslator.class.getClassLoader();
|
||||
while (classLoader != null) {
|
||||
if (classLoader instanceof URLClassLoader) {
|
||||
@@ -116,8 +114,9 @@ public class JavaToKotlinTranslator {
|
||||
static String generateKotlinCode(@NotNull String javaCode) {
|
||||
PsiFile file = createFile(javaCode);
|
||||
if (file != null && file instanceof PsiJavaFile) {
|
||||
setClassIdentifiers(CONVERTER, file);
|
||||
return prettify(CONVERTER.fileToFile((PsiJavaFile) file).toKotlin());
|
||||
Converter converter = new Converter(file.getProject());
|
||||
setClassIdentifiers(converter, file);
|
||||
return prettify(converter.fileToFile((PsiJavaFile) file).toKotlin());
|
||||
}
|
||||
return "";
|
||||
}
|
||||
@@ -126,15 +125,16 @@ public class JavaToKotlinTranslator {
|
||||
static String generateKotlinCodeWithCompatibilityImport(@NotNull String javaCode) {
|
||||
PsiFile file = createFile(javaCode);
|
||||
if (file != null && file instanceof PsiJavaFile) {
|
||||
setClassIdentifiers(CONVERTER, file);
|
||||
return prettify(CONVERTER.fileToFileWithCompatibilityImport((PsiJavaFile) file).toKotlin());
|
||||
Converter converter = new Converter(file.getProject());
|
||||
setClassIdentifiers(converter, file);
|
||||
return prettify(converter.fileToFileWithCompatibilityImport((PsiJavaFile) file).toKotlin());
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static void main(@NotNull String[] args) throws IOException {
|
||||
//noinspection UseOfSystemOutOrSystemErr
|
||||
final PrintStream out = System.out;
|
||||
PrintStream out = System.out;
|
||||
if (args.length == 1) {
|
||||
String kotlinCode = "";
|
||||
try {
|
||||
@@ -154,6 +154,8 @@ public class JavaToKotlinTranslator {
|
||||
}
|
||||
}
|
||||
|
||||
// Used in the Kotlin Web Demo.
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public static String translateToKotlin(String code) {
|
||||
return generateKotlinCode(code);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2012 JetBrains s.r.o.
|
||||
* Copyright 2010-2013 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.
|
||||
@@ -16,27 +16,29 @@
|
||||
|
||||
package org.jetbrains.jet.j2k;
|
||||
|
||||
import com.intellij.core.JavaCoreProjectEnvironment;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiJavaFile;
|
||||
import com.intellij.testFramework.UsefulTestCase;
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.ConfigurationKind;
|
||||
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
||||
import static org.jetbrains.jet.JetTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations;
|
||||
|
||||
@SuppressWarnings("JUnitTestCaseWithNoTests")
|
||||
public class StandaloneJavaToKotlinConverterTest extends UsefulTestCase {
|
||||
private final String myDataPath;
|
||||
private final String myName;
|
||||
@NotNull
|
||||
private final static JavaCoreProjectEnvironment myJavaCoreEnvironment = JavaToKotlinTranslator.setUpJavaCoreEnvironment();
|
||||
|
||||
@SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors")
|
||||
public StandaloneJavaToKotlinConverterTest(String dataPath, String name) {
|
||||
myDataPath = dataPath;
|
||||
myName = name;
|
||||
@@ -44,17 +46,20 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
||||
|
||||
@Override
|
||||
protected void runTest() throws Throwable {
|
||||
Converter converter = new Converter();
|
||||
String javaPath = "tests/testData/" + getTestFilePath();
|
||||
JetCoreEnvironment jetCoreEnvironment = createEnvironmentWithMockJdkAndIdeaAnnotations(getTestRootDisposable(), ConfigurationKind.JDK_ONLY);
|
||||
|
||||
Converter converter = new Converter(jetCoreEnvironment.getProject());
|
||||
|
||||
String javaPath = "j2k/tests/testData/" + getTestFilePath();
|
||||
String kotlinPath = javaPath.replace(".jav", ".kt");
|
||||
|
||||
final File kotlinFile = new File(kotlinPath);
|
||||
File kotlinFile = new File(kotlinPath);
|
||||
if (!kotlinFile.exists()) {
|
||||
FileUtil.writeToFile(kotlinFile, "");
|
||||
}
|
||||
final String expected = StringUtil.convertLineSeparators(FileUtil.loadFile(kotlinFile));
|
||||
final File javaFile = new File(javaPath);
|
||||
final String javaCode = FileUtil.loadFile(javaFile);
|
||||
String expected = FileUtil.loadFile(kotlinFile, true);
|
||||
File javaFile = new File(javaPath);
|
||||
String javaCode = FileUtil.loadFile(javaFile, true);
|
||||
|
||||
String actual = "";
|
||||
String parentFileName = javaFile.getParentFile().getName();
|
||||
@@ -75,14 +80,12 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
||||
}
|
||||
else if (parentFileName.equals("comp")) actual = fileToFileWithCompatibilityImport(javaCode);
|
||||
|
||||
actual = StringUtil.convertLineSeparators(actual);
|
||||
|
||||
assert !actual.isEmpty() : "Specify what is it: file, class, method, statement or expression: " + javaPath + " parent: " + parentFileName;
|
||||
|
||||
final File tmp = new File(kotlinPath + ".tmp");
|
||||
File tmp = new File(kotlinPath + ".tmp");
|
||||
if (!expected.equals(actual)) FileUtil.writeToFile(tmp, actual);
|
||||
if (expected.equals(actual) && tmp.exists()) //noinspection ResultOfMethodCallIgnored
|
||||
{
|
||||
if (expected.equals(actual) && tmp.exists()) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
tmp.delete();
|
||||
}
|
||||
|
||||
@@ -105,7 +108,7 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite();
|
||||
// suite.addTest(new StandaloneJavaToKotlinConverterTest("ast/class/file", "kt-639"));
|
||||
suite.addTest(TestCaseBuilder.suiteForDirectory("tests/testData", "/ast", new TestCaseBuilder.NamedTestFactory() {
|
||||
suite.addTest(TestCaseBuilder.suiteForDirectory("j2k/tests/testData", "/ast", new TestCaseBuilder.NamedTestFactory() {
|
||||
@NotNull
|
||||
@Override
|
||||
public Test createTest(@NotNull String dataPath, @NotNull String name) {
|
||||
@@ -121,8 +124,8 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String fileToKotlin(Converter converter, @NotNull String text) {
|
||||
return generateKotlinCode(converter, JavaToKotlinTranslator.createFile(myJavaCoreEnvironment, text));
|
||||
private static String fileToKotlin(Converter converter, @NotNull String text) {
|
||||
return generateKotlinCode(converter, JavaToKotlinTranslator.createFile(converter.getProject(), text));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -135,7 +138,7 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String methodToKotlin(Converter converter, String text) throws IOException {
|
||||
private static String methodToKotlin(Converter converter, String text) throws IOException {
|
||||
String result = fileToKotlin(converter, "final class C {" + text + "}")
|
||||
.replaceAll("class C\\(\\) \\{", "");
|
||||
result = result.substring(0, result.lastIndexOf("}"));
|
||||
@@ -143,7 +146,7 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String statementToKotlin(Converter converter, String text) throws Exception {
|
||||
private static String statementToKotlin(Converter converter, String text) throws Exception {
|
||||
String result = methodToKotlin(converter, "void main() {" + text + "}");
|
||||
int pos = result.lastIndexOf("}");
|
||||
result = result.substring(0, pos).replaceFirst("fun main\\(\\) : Unit \\{", "");
|
||||
@@ -151,7 +154,7 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String expressionToKotlin(Converter converter, String code) throws Exception {
|
||||
private static String expressionToKotlin(Converter converter, String code) throws Exception {
|
||||
String result = statementToKotlin(converter, "Object o =" + code + "}");
|
||||
result = result.replaceFirst("var o : Any\\? =", "");
|
||||
return prettify(result);
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.jetbrains.jet.j2k;
|
||||
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -37,27 +36,18 @@ abstract class TestCaseBuilder {
|
||||
}
|
||||
};
|
||||
|
||||
@NotNull
|
||||
public static String getTestDataPathBase() {
|
||||
return "testData";
|
||||
}
|
||||
|
||||
public static String getHomeDirectory() {
|
||||
return new File(PathManager.getResourceRoot(TestCaseBuilder.class, "/org/jetbrains/jet/TestCaseBuilder.class")).getParentFile().getParentFile().getParent();
|
||||
}
|
||||
|
||||
public interface NamedTestFactory {
|
||||
@NotNull
|
||||
Test createTest(@NotNull String dataPath, @NotNull String name);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath, @NotNull NamedTestFactory factory) {
|
||||
public static TestSuite suiteForDirectory(String baseDataDir, @NotNull String dataPath, @NotNull NamedTestFactory factory) {
|
||||
return suiteForDirectory(baseDataDir, dataPath, true, emptyFilter, factory);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath, boolean recursive, @NotNull final FilenameFilter filter, @NotNull NamedTestFactory factory) {
|
||||
private static TestSuite suiteForDirectory(String baseDataDir, @NotNull String dataPath, boolean recursive, @NotNull final FilenameFilter filter, @NotNull NamedTestFactory factory) {
|
||||
TestSuite suite = new TestSuite(dataPath);
|
||||
final String extensionJava = ".jav";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user