kj2k initial checkin
This commit is contained in:
committed by
Pavel V. Talanov
parent
6fbf97bbcf
commit
34da39c55b
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Copyright 2010-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.j2k;
|
||||
|
||||
import com.intellij.core.JavaCoreEnvironment;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiJavaFile;
|
||||
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 java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
||||
private final String myDataPath;
|
||||
private final String myName;
|
||||
@NotNull
|
||||
private final static JavaCoreEnvironment myJavaCoreEnvironment = JavaToKotlinTranslator.setUpJavaCoreEnvironment();
|
||||
|
||||
public StandaloneJavaToKotlinConverterTest(String dataPath, String name) {
|
||||
myDataPath = dataPath;
|
||||
myName = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runTest() throws Throwable {
|
||||
Converter converter = new Converter();
|
||||
String javaPath = "tests/testData/" + getTestFilePath();
|
||||
String kotlinPath = javaPath.replace(".jav", ".kt");
|
||||
|
||||
final File kotlinFile = new File(kotlinPath);
|
||||
if (!kotlinFile.exists()) {
|
||||
FileUtil.writeToFile(kotlinFile, "");
|
||||
}
|
||||
final String expected = FileUtil.loadFile(kotlinFile);
|
||||
final File javaFile = new File(javaPath);
|
||||
final String javaCode = FileUtil.loadFile(javaFile);
|
||||
|
||||
String actual = "";
|
||||
String parentFileName = javaFile.getParentFile().getName();
|
||||
if (parentFileName.equals("expression")) {
|
||||
actual = expressionToKotlin(converter, javaCode);
|
||||
}
|
||||
else if (parentFileName.equals("statement")) {
|
||||
actual = statementToKotlin(converter, javaCode);
|
||||
}
|
||||
else if (parentFileName.equals("method")) {
|
||||
actual = methodToKotlin(converter, javaCode);
|
||||
}
|
||||
else if (parentFileName.equals("class")) {
|
||||
actual = fileToKotlin(converter, javaCode);
|
||||
}
|
||||
else if (parentFileName.equals("file")) {
|
||||
actual = fileToKotlin(converter, javaCode);
|
||||
}
|
||||
else if (parentFileName.equals("comp")) actual = fileToFileWithCompatibilityImport(javaCode);
|
||||
|
||||
assert !actual.isEmpty() : "Specify what is it: file, class, method, statement or expression: " + javaPath + " parent: " + parentFileName;
|
||||
|
||||
final File tmp = new File(kotlinPath + ".tmp");
|
||||
if (!expected.equals(actual)) FileUtil.writeToFile(tmp, actual);
|
||||
if (expected.equals(actual) && tmp.exists()) //noinspection ResultOfMethodCallIgnored
|
||||
{
|
||||
tmp.delete();
|
||||
}
|
||||
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
String getTestFilePath() {
|
||||
return myDataPath + "/" + myName + ".jav";
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return "test_" + myName;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
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() {
|
||||
@NotNull
|
||||
@Override
|
||||
public Test createTest(@NotNull String dataPath, @NotNull String name) {
|
||||
return new StandaloneJavaToKotlinConverterTest(dataPath, name);
|
||||
}
|
||||
}));
|
||||
return suite;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String fileToFileWithCompatibilityImport(@NotNull String text) {
|
||||
return JavaToKotlinTranslator.generateKotlinCodeWithCompatibilityImport(text);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String fileToKotlin(Converter converter, @NotNull String text) {
|
||||
return generateKotlinCode(converter, JavaToKotlinTranslator.createFile(myJavaCoreEnvironment, text));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String generateKotlinCode(@NotNull Converter converter, @Nullable PsiFile file) {
|
||||
if (file != null && file instanceof PsiJavaFile) {
|
||||
JavaToKotlinTranslator.setClassIdentifiers(converter, file);
|
||||
return prettify(converter.elementToKotlin(file));
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private 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("}"));
|
||||
return prettify(result);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private 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 \\{", "");
|
||||
return prettify(result);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String expressionToKotlin(Converter converter, String code) throws Exception {
|
||||
String result = statementToKotlin(converter, "Object o =" + code + "}");
|
||||
result = result.replaceFirst("var o : Any\\? =", "");
|
||||
return prettify(result);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String prettify(@Nullable String code) {
|
||||
if (code == null) {
|
||||
return "";
|
||||
}
|
||||
return code
|
||||
.trim()
|
||||
.replaceAll("\r\n", "\n")
|
||||
.replaceAll(" \n", "\n")
|
||||
.replaceAll("\n ", "\n")
|
||||
.replaceAll("\n+", "\n")
|
||||
.replaceAll(" +", " ")
|
||||
.trim()
|
||||
;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright 2010-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.j2k;
|
||||
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FilenameFilter;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
abstract class TestCaseBuilder {
|
||||
@NotNull
|
||||
private static final FilenameFilter emptyFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File file, String name) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
@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) {
|
||||
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) {
|
||||
TestSuite suite = new TestSuite(dataPath);
|
||||
final String extensionJava = ".jav";
|
||||
|
||||
final FilenameFilter extensionFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, @NotNull String name) {
|
||||
return name.endsWith(extensionJava);
|
||||
}
|
||||
};
|
||||
FilenameFilter resultFilter;
|
||||
if (filter != emptyFilter) {
|
||||
resultFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File file, String s) {
|
||||
return extensionFilter.accept(file, s) && filter.accept(file, s);
|
||||
}
|
||||
};
|
||||
}
|
||||
else {
|
||||
resultFilter = extensionFilter;
|
||||
}
|
||||
File dir = new File(baseDataDir + dataPath);
|
||||
FileFilter dirFilter = new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(@NotNull File pathname) {
|
||||
return pathname.isDirectory();
|
||||
}
|
||||
};
|
||||
if (recursive) {
|
||||
File[] files = dir.listFiles(dirFilter);
|
||||
assert files != null : dir;
|
||||
List<File> subdirs = Arrays.asList(files);
|
||||
Collections.sort(subdirs);
|
||||
for (File subdir : subdirs) {
|
||||
suite.addTest(suiteForDirectory(baseDataDir, dataPath + "/" + subdir.getName(), recursive, filter, factory));
|
||||
}
|
||||
}
|
||||
List<File> files = Arrays.asList(dir.listFiles(resultFilter));
|
||||
Collections.sort(files);
|
||||
for (File file : files) {
|
||||
String fileName = file.getName();
|
||||
assert fileName != null;
|
||||
suite.addTest(factory.createTest(dataPath, fileName.substring(0, fileName.length() - extensionJava.length())));
|
||||
}
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package test;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class Test {
|
||||
@NotNull String myStr = "String2";
|
||||
|
||||
public Test(@NotNull String str) {
|
||||
myStr = str;
|
||||
}
|
||||
|
||||
public void sout(@NotNull String str) {
|
||||
System.out.println(str);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String dummy(@NotNull String str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
public void test() {
|
||||
sout("String");
|
||||
@NotNull String test = "String2";
|
||||
sout(test);
|
||||
sout(dummy(test));
|
||||
|
||||
new Test(test);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package test
|
||||
public open class Test(str : String) {
|
||||
var myStr : String? = "String2"
|
||||
public open fun sout(str : String) : Unit {
|
||||
System.out?.println(str)
|
||||
}
|
||||
public open fun dummy(str : String) : String {
|
||||
return str
|
||||
}
|
||||
public open fun test() : Unit {
|
||||
sout("String")
|
||||
var test : String = "String2"
|
||||
sout(test)
|
||||
sout(dummy(test))
|
||||
Test(test)
|
||||
}
|
||||
{
|
||||
myStr = str
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package test
|
||||
public open class Test(str : String?) {
|
||||
var myStr : String? = "String2"
|
||||
public open fun sout(str : String?) : Unit {
|
||||
System.out?.println(str)
|
||||
}
|
||||
public open fun dummy(str : String?) : String? {
|
||||
return str
|
||||
}
|
||||
public open fun test() : Unit {
|
||||
sout("String")
|
||||
var test : String? = "String2"
|
||||
sout(test)
|
||||
sout(dummy(test))
|
||||
Test(test)
|
||||
}
|
||||
{
|
||||
myStr = str
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class Test {
|
||||
String str;
|
||||
{
|
||||
str = "Ola";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
open class Test() {
|
||||
var str : String? = null
|
||||
{
|
||||
str = "Ola"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class Test {
|
||||
String str;
|
||||
static {
|
||||
str = "Ola";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
open class Test() {
|
||||
var str : String? = null
|
||||
class object {
|
||||
{
|
||||
str = "Ola"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
myArray[myLibrary.calculateIndex(100)]
|
||||
@@ -0,0 +1 @@
|
||||
myArray[myLibrary.calculateIndex(100)]
|
||||
@@ -0,0 +1 @@
|
||||
myArray[10]
|
||||
@@ -0,0 +1 @@
|
||||
myArray[10]
|
||||
@@ -0,0 +1 @@
|
||||
myArray[i]
|
||||
@@ -0,0 +1 @@
|
||||
myArray[i]
|
||||
@@ -0,0 +1 @@
|
||||
new int[] {1, 2, 3};
|
||||
@@ -0,0 +1 @@
|
||||
intArray(1, 2, 3)
|
||||
@@ -0,0 +1 @@
|
||||
double[] a = new double[]{1.0, 2, 3}
|
||||
@@ -0,0 +1 @@
|
||||
var a : DoubleArray? = doubleArray(1.0, 2.0, 3.0)
|
||||
@@ -0,0 +1,2 @@
|
||||
double a = 0, b = 0, c = 0;
|
||||
double ds[] = {a, b, c};
|
||||
@@ -0,0 +1,4 @@
|
||||
var a : Double = 0
|
||||
var b : Double = 0
|
||||
var c : Double = 0
|
||||
var ds : DoubleArray? = doubleArray((a).toDouble(), (b).toDouble(), (c).toDouble())
|
||||
@@ -0,0 +1 @@
|
||||
float[] a = new float[]{1, 2, 3.0}
|
||||
@@ -0,0 +1 @@
|
||||
var a : FloatArray? = floatArray(1.0, 2.0, 3.0)
|
||||
@@ -0,0 +1 @@
|
||||
java.lang.Double[] a = new java.lang.Double[]{1, 2, 3};
|
||||
@@ -0,0 +1 @@
|
||||
var a : Array<java.lang.Double?>? = array<java.lang.Double?>(1.0, 2.0, 3.0)
|
||||
@@ -0,0 +1 @@
|
||||
java.lang.Float[] a = new java.lang.Float[]{1, 2, 3};
|
||||
@@ -0,0 +1 @@
|
||||
var a : Array<java.lang.Float?>? = array<java.lang.Float?>(1.0, 2.0, 3.0)
|
||||
@@ -0,0 +1 @@
|
||||
byte[] a = new byte[] {1, 2, 3};
|
||||
@@ -0,0 +1 @@
|
||||
var a : ByteArray? = byteArray(1, 2, 3)
|
||||
@@ -0,0 +1 @@
|
||||
int[] a = {1, 2, 3};
|
||||
@@ -0,0 +1 @@
|
||||
var a : IntArray? = intArray(1, 2, 3)
|
||||
@@ -0,0 +1,2 @@
|
||||
int a = 0, b = 0, c = 0;
|
||||
int is[] = {a, b, c};
|
||||
@@ -0,0 +1,4 @@
|
||||
var a : Int = 0
|
||||
var b : Int = 0
|
||||
var c : Int = 0
|
||||
var `is` : IntArray? = intArray(a, b, c)
|
||||
@@ -0,0 +1 @@
|
||||
int[][] a = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
|
||||
@@ -0,0 +1 @@
|
||||
var a : Array<IntArray?>? = array<IntArray?>(intArray(1, 2, 3), intArray(4, 5, 6), intArray(7, 8, 9))
|
||||
@@ -0,0 +1 @@
|
||||
void fromArrayToCollection(Foo[] a) {}
|
||||
@@ -0,0 +1,2 @@
|
||||
fun fromArrayToCollection(a : Array<Foo?>?) : Unit {
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
int [][] d2 = new int[][]{};
|
||||
@@ -0,0 +1 @@
|
||||
var d2 : Array<IntArray?>? = array<IntArray?>()
|
||||
@@ -0,0 +1 @@
|
||||
int [][] d2 = new int[5][];
|
||||
@@ -0,0 +1 @@
|
||||
var d2 : Array<IntArray?>? = Array<IntArray?>(5)
|
||||
@@ -0,0 +1 @@
|
||||
int [][][] d3 = new int[5][5][];
|
||||
@@ -0,0 +1 @@
|
||||
var d3 : Array<Array<IntArray?>?>? = Array<Array<IntArray?>?>(5, {Array<IntArray?>(5)})
|
||||
+1
@@ -0,0 +1 @@
|
||||
int [][] d2 = new int[5][5];
|
||||
+1
@@ -0,0 +1 @@
|
||||
var d2 : Array<IntArray?>? = Array<IntArray?>(5, {IntArray(5)})
|
||||
@@ -0,0 +1 @@
|
||||
String [][] ss = new String[5][5];
|
||||
@@ -0,0 +1 @@
|
||||
var ss : Array<Array<String?>?>? = Array<Array<String?>?>(5, {Array<String?>(5)})
|
||||
@@ -0,0 +1 @@
|
||||
String [][][] sss = new String[5][5][5];
|
||||
@@ -0,0 +1 @@
|
||||
var sss : Array<Array<Array<String?>?>?>? = Array<Array<Array<String?>?>?>(5, {Array<Array<String?>?>(5, {Array<String?>(5)})})
|
||||
@@ -0,0 +1 @@
|
||||
long[] a = new long[]{1, 2, 3}
|
||||
@@ -0,0 +1 @@
|
||||
var a : LongArray? = longArray(1, 2, 3)
|
||||
@@ -0,0 +1 @@
|
||||
int[] a = new int[]{1, 2, 3}
|
||||
@@ -0,0 +1 @@
|
||||
var a : IntArray? = intArray(1, 2, 3)
|
||||
@@ -0,0 +1 @@
|
||||
String[] a = new String[]{"abc"}
|
||||
@@ -0,0 +1 @@
|
||||
var a : Array<String?>? = array<String?>("abc")
|
||||
@@ -0,0 +1 @@
|
||||
assert boolMethod();
|
||||
@@ -0,0 +1 @@
|
||||
assert {boolMethod()}
|
||||
@@ -0,0 +1 @@
|
||||
assert(boolMethod());
|
||||
@@ -0,0 +1 @@
|
||||
assert {(boolMethod())}
|
||||
@@ -0,0 +1 @@
|
||||
assert true : "string details";
|
||||
@@ -0,0 +1 @@
|
||||
assert("string details") {true}
|
||||
@@ -0,0 +1 @@
|
||||
x &= 2
|
||||
@@ -0,0 +1 @@
|
||||
x = x and 2
|
||||
@@ -0,0 +1 @@
|
||||
x = 2
|
||||
@@ -0,0 +1 @@
|
||||
x = 2
|
||||
@@ -0,0 +1 @@
|
||||
x /= 2
|
||||
@@ -0,0 +1 @@
|
||||
x /= 2
|
||||
@@ -0,0 +1 @@
|
||||
x -= 2
|
||||
@@ -0,0 +1 @@
|
||||
x -= 2
|
||||
@@ -0,0 +1 @@
|
||||
x *= 2
|
||||
@@ -0,0 +1 @@
|
||||
x *= 2
|
||||
@@ -0,0 +1 @@
|
||||
x |= 2
|
||||
@@ -0,0 +1 @@
|
||||
x = x or 2
|
||||
@@ -0,0 +1 @@
|
||||
x += 2
|
||||
@@ -0,0 +1 @@
|
||||
x += 2
|
||||
@@ -0,0 +1 @@
|
||||
x %= 2
|
||||
@@ -0,0 +1 @@
|
||||
x %= 2
|
||||
@@ -0,0 +1 @@
|
||||
x <<= 2
|
||||
@@ -0,0 +1 @@
|
||||
x = x shl 2
|
||||
@@ -0,0 +1 @@
|
||||
x >>= 2
|
||||
@@ -0,0 +1 @@
|
||||
x = x shr 2
|
||||
@@ -0,0 +1 @@
|
||||
x >>>= 2
|
||||
@@ -0,0 +1 @@
|
||||
x = x ushr 2
|
||||
@@ -0,0 +1 @@
|
||||
x ^= 2
|
||||
@@ -0,0 +1 @@
|
||||
x = x xor 2
|
||||
@@ -0,0 +1 @@
|
||||
i = 1;
|
||||
@@ -0,0 +1 @@
|
||||
i = 1
|
||||
@@ -0,0 +1 @@
|
||||
x & 2
|
||||
@@ -0,0 +1 @@
|
||||
x and 2
|
||||
@@ -0,0 +1 @@
|
||||
true && false
|
||||
@@ -0,0 +1 @@
|
||||
true && false
|
||||
@@ -0,0 +1 @@
|
||||
true || false
|
||||
@@ -0,0 +1 @@
|
||||
true || false
|
||||
@@ -0,0 +1 @@
|
||||
1 / 2
|
||||
@@ -0,0 +1 @@
|
||||
1 / 2
|
||||
@@ -0,0 +1 @@
|
||||
1 > 2
|
||||
@@ -0,0 +1 @@
|
||||
1 > 2
|
||||
@@ -0,0 +1 @@
|
||||
1 >= 2
|
||||
@@ -0,0 +1 @@
|
||||
1 >= 2
|
||||
@@ -0,0 +1 @@
|
||||
1 < 2
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user