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 com.intellij.openapi.util.Pair
|
||||||
import java.text.MessageFormat
|
import java.text.MessageFormat
|
||||||
import com.intellij.psi.util.PsiUtil
|
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.lang.resolve.name.FqName
|
||||||
import org.jetbrains.jet.util.QualifiedNamesUtil
|
import org.jetbrains.jet.util.QualifiedNamesUtil
|
||||||
import org.jetbrains.jet.lang.resolve.java.mapping.JavaToKotlinClassMap
|
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()!!
|
private var classIdentifiersSet: MutableSet<String> = Sets.newHashSet()!!
|
||||||
|
|
||||||
@@ -31,15 +29,6 @@ public open class Converter() {
|
|||||||
|
|
||||||
private val flags: MutableSet<J2KConverterFlags?>? = Sets.newHashSet()
|
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()
|
private val javaToKotlinClassMap: JavaToKotlinClassMap = JavaToKotlinClassMap.getInstance()
|
||||||
|
|
||||||
public open var methodReturnType: PsiType? = null
|
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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.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.util.Disposer;
|
||||||
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;
|
||||||
@@ -35,11 +37,12 @@ import java.net.URL;
|
|||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
|
|
||||||
public class JavaToKotlinTranslator {
|
public class JavaToKotlinTranslator {
|
||||||
|
|
||||||
|
private static final Disposable DISPOSABLE = Disposer.newDisposable();
|
||||||
|
|
||||||
private JavaToKotlinTranslator() {
|
private JavaToKotlinTranslator() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Converter CONVERTER = new Converter();
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private static PsiFile createFile(@NotNull String text) {
|
private static PsiFile createFile(@NotNull String text) {
|
||||||
JavaCoreProjectEnvironment javaCoreEnvironment = setUpJavaCoreEnvironment();
|
JavaCoreProjectEnvironment javaCoreEnvironment = setUpJavaCoreEnvironment();
|
||||||
@@ -49,21 +52,16 @@ public class JavaToKotlinTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
static PsiFile createFile(@NotNull JavaCoreProjectEnvironment javaCoreEnvironment, @NotNull String text) {
|
static PsiFile createFile(@NotNull Project project, @NotNull String text) {
|
||||||
return PsiFileFactory.getInstance(javaCoreEnvironment.getProject()).createFileFromText(
|
return PsiFileFactory.getInstance(project).createFileFromText(
|
||||||
"test.java", JavaLanguage.INSTANCE, text
|
"test.java", JavaLanguage.INSTANCE, text
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
static JavaCoreProjectEnvironment setUpJavaCoreEnvironment() {
|
static JavaCoreProjectEnvironment setUpJavaCoreEnvironment() {
|
||||||
Disposable parentDisposable = new Disposable() {
|
JavaCoreApplicationEnvironment applicationEnvironment = new JavaCoreApplicationEnvironment(DISPOSABLE);
|
||||||
@Override
|
JavaCoreProjectEnvironment javaCoreEnvironment = new JavaCoreProjectEnvironment(DISPOSABLE, applicationEnvironment);
|
||||||
public void dispose() {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
JavaCoreApplicationEnvironment applicationEnvironment = new JavaCoreApplicationEnvironment(parentDisposable);
|
|
||||||
JavaCoreProjectEnvironment javaCoreEnvironment = new JavaCoreProjectEnvironment(parentDisposable, applicationEnvironment);
|
|
||||||
|
|
||||||
javaCoreEnvironment.addJarToClassPath(PathUtil.findRtJar());
|
javaCoreEnvironment.addJarToClassPath(PathUtil.findRtJar());
|
||||||
File annotations = findAnnotations();
|
File annotations = findAnnotations();
|
||||||
@@ -90,7 +88,7 @@ public class JavaToKotlinTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private static File findAnnotations() {
|
public static File findAnnotations() {
|
||||||
ClassLoader classLoader = JavaToKotlinTranslator.class.getClassLoader();
|
ClassLoader classLoader = JavaToKotlinTranslator.class.getClassLoader();
|
||||||
while (classLoader != null) {
|
while (classLoader != null) {
|
||||||
if (classLoader instanceof URLClassLoader) {
|
if (classLoader instanceof URLClassLoader) {
|
||||||
@@ -116,8 +114,9 @@ public class JavaToKotlinTranslator {
|
|||||||
static String generateKotlinCode(@NotNull String javaCode) {
|
static String generateKotlinCode(@NotNull String javaCode) {
|
||||||
PsiFile file = createFile(javaCode);
|
PsiFile file = createFile(javaCode);
|
||||||
if (file != null && file instanceof PsiJavaFile) {
|
if (file != null && file instanceof PsiJavaFile) {
|
||||||
setClassIdentifiers(CONVERTER, file);
|
Converter converter = new Converter(file.getProject());
|
||||||
return prettify(CONVERTER.fileToFile((PsiJavaFile) file).toKotlin());
|
setClassIdentifiers(converter, file);
|
||||||
|
return prettify(converter.fileToFile((PsiJavaFile) file).toKotlin());
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@@ -126,15 +125,16 @@ public class JavaToKotlinTranslator {
|
|||||||
static String generateKotlinCodeWithCompatibilityImport(@NotNull String javaCode) {
|
static String generateKotlinCodeWithCompatibilityImport(@NotNull String javaCode) {
|
||||||
PsiFile file = createFile(javaCode);
|
PsiFile file = createFile(javaCode);
|
||||||
if (file != null && file instanceof PsiJavaFile) {
|
if (file != null && file instanceof PsiJavaFile) {
|
||||||
setClassIdentifiers(CONVERTER, file);
|
Converter converter = new Converter(file.getProject());
|
||||||
return prettify(CONVERTER.fileToFileWithCompatibilityImport((PsiJavaFile) file).toKotlin());
|
setClassIdentifiers(converter, file);
|
||||||
|
return prettify(converter.fileToFileWithCompatibilityImport((PsiJavaFile) file).toKotlin());
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(@NotNull String[] args) throws IOException {
|
public static void main(@NotNull String[] args) throws IOException {
|
||||||
//noinspection UseOfSystemOutOrSystemErr
|
//noinspection UseOfSystemOutOrSystemErr
|
||||||
final PrintStream out = System.out;
|
PrintStream out = System.out;
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
String kotlinCode = "";
|
String kotlinCode = "";
|
||||||
try {
|
try {
|
||||||
@@ -154,6 +154,8 @@ public class JavaToKotlinTranslator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Used in the Kotlin Web Demo.
|
||||||
|
@SuppressWarnings("UnusedDeclaration")
|
||||||
public static String translateToKotlin(String code) {
|
public static String translateToKotlin(String code) {
|
||||||
return generateKotlinCode(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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,27 +16,29 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.j2k;
|
package org.jetbrains.jet.j2k;
|
||||||
|
|
||||||
import com.intellij.core.JavaCoreProjectEnvironment;
|
|
||||||
import com.intellij.openapi.util.io.FileUtil;
|
import com.intellij.openapi.util.io.FileUtil;
|
||||||
import com.intellij.openapi.util.text.StringUtil;
|
|
||||||
import com.intellij.psi.PsiFile;
|
import com.intellij.psi.PsiFile;
|
||||||
import com.intellij.psi.PsiJavaFile;
|
import com.intellij.psi.PsiJavaFile;
|
||||||
|
import com.intellij.testFramework.UsefulTestCase;
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
import junit.framework.TestCase;
|
|
||||||
import junit.framework.TestSuite;
|
import junit.framework.TestSuite;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
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.File;
|
||||||
import java.io.IOException;
|
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 myDataPath;
|
||||||
private final String myName;
|
private final String myName;
|
||||||
@NotNull
|
|
||||||
private final static JavaCoreProjectEnvironment myJavaCoreEnvironment = JavaToKotlinTranslator.setUpJavaCoreEnvironment();
|
|
||||||
|
|
||||||
|
@SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors")
|
||||||
public StandaloneJavaToKotlinConverterTest(String dataPath, String name) {
|
public StandaloneJavaToKotlinConverterTest(String dataPath, String name) {
|
||||||
myDataPath = dataPath;
|
myDataPath = dataPath;
|
||||||
myName = name;
|
myName = name;
|
||||||
@@ -44,17 +46,20 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void runTest() throws Throwable {
|
protected void runTest() throws Throwable {
|
||||||
Converter converter = new Converter();
|
JetCoreEnvironment jetCoreEnvironment = createEnvironmentWithMockJdkAndIdeaAnnotations(getTestRootDisposable(), ConfigurationKind.JDK_ONLY);
|
||||||
String javaPath = "tests/testData/" + getTestFilePath();
|
|
||||||
|
Converter converter = new Converter(jetCoreEnvironment.getProject());
|
||||||
|
|
||||||
|
String javaPath = "j2k/tests/testData/" + getTestFilePath();
|
||||||
String kotlinPath = javaPath.replace(".jav", ".kt");
|
String kotlinPath = javaPath.replace(".jav", ".kt");
|
||||||
|
|
||||||
final File kotlinFile = new File(kotlinPath);
|
File kotlinFile = new File(kotlinPath);
|
||||||
if (!kotlinFile.exists()) {
|
if (!kotlinFile.exists()) {
|
||||||
FileUtil.writeToFile(kotlinFile, "");
|
FileUtil.writeToFile(kotlinFile, "");
|
||||||
}
|
}
|
||||||
final String expected = StringUtil.convertLineSeparators(FileUtil.loadFile(kotlinFile));
|
String expected = FileUtil.loadFile(kotlinFile, true);
|
||||||
final File javaFile = new File(javaPath);
|
File javaFile = new File(javaPath);
|
||||||
final String javaCode = FileUtil.loadFile(javaFile);
|
String javaCode = FileUtil.loadFile(javaFile, true);
|
||||||
|
|
||||||
String actual = "";
|
String actual = "";
|
||||||
String parentFileName = javaFile.getParentFile().getName();
|
String parentFileName = javaFile.getParentFile().getName();
|
||||||
@@ -75,14 +80,12 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
|||||||
}
|
}
|
||||||
else if (parentFileName.equals("comp")) actual = fileToFileWithCompatibilityImport(javaCode);
|
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;
|
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)) FileUtil.writeToFile(tmp, actual);
|
||||||
if (expected.equals(actual) && tmp.exists()) //noinspection ResultOfMethodCallIgnored
|
if (expected.equals(actual) && tmp.exists()) {
|
||||||
{
|
//noinspection ResultOfMethodCallIgnored
|
||||||
tmp.delete();
|
tmp.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,7 +108,7 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
|||||||
public static Test suite() {
|
public static Test suite() {
|
||||||
TestSuite suite = new TestSuite();
|
TestSuite suite = new TestSuite();
|
||||||
// suite.addTest(new StandaloneJavaToKotlinConverterTest("ast/class/file", "kt-639"));
|
// 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
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public Test createTest(@NotNull String dataPath, @NotNull String name) {
|
public Test createTest(@NotNull String dataPath, @NotNull String name) {
|
||||||
@@ -121,8 +124,8 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private String fileToKotlin(Converter converter, @NotNull String text) {
|
private static String fileToKotlin(Converter converter, @NotNull String text) {
|
||||||
return generateKotlinCode(converter, JavaToKotlinTranslator.createFile(myJavaCoreEnvironment, text));
|
return generateKotlinCode(converter, JavaToKotlinTranslator.createFile(converter.getProject(), text));
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -135,7 +138,7 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@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 + "}")
|
String result = fileToKotlin(converter, "final class C {" + text + "}")
|
||||||
.replaceAll("class C\\(\\) \\{", "");
|
.replaceAll("class C\\(\\) \\{", "");
|
||||||
result = result.substring(0, result.lastIndexOf("}"));
|
result = result.substring(0, result.lastIndexOf("}"));
|
||||||
@@ -143,7 +146,7 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@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 + "}");
|
String result = methodToKotlin(converter, "void main() {" + text + "}");
|
||||||
int pos = result.lastIndexOf("}");
|
int pos = result.lastIndexOf("}");
|
||||||
result = result.substring(0, pos).replaceFirst("fun main\\(\\) : Unit \\{", "");
|
result = result.substring(0, pos).replaceFirst("fun main\\(\\) : Unit \\{", "");
|
||||||
@@ -151,7 +154,7 @@ public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@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 + "}");
|
String result = statementToKotlin(converter, "Object o =" + code + "}");
|
||||||
result = result.replaceFirst("var o : Any\\? =", "");
|
result = result.replaceFirst("var o : Any\\? =", "");
|
||||||
return prettify(result);
|
return prettify(result);
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.j2k;
|
package org.jetbrains.jet.j2k;
|
||||||
|
|
||||||
import com.intellij.openapi.application.PathManager;
|
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
import junit.framework.TestSuite;
|
import junit.framework.TestSuite;
|
||||||
import org.jetbrains.annotations.NotNull;
|
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 {
|
public interface NamedTestFactory {
|
||||||
@NotNull
|
@NotNull
|
||||||
Test createTest(@NotNull String dataPath, @NotNull String name);
|
Test createTest(@NotNull String dataPath, @NotNull String name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@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);
|
return suiteForDirectory(baseDataDir, dataPath, true, emptyFilter, factory);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@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);
|
TestSuite suite = new TestSuite(dataPath);
|
||||||
final String extensionJava = ".jav";
|
final String extensionJava = ".jav";
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user