Converter:
Fix converting import list Add tests for imports problem Add 'element' test type
This commit is contained in:
@@ -69,13 +69,11 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
|||||||
is PsiStatement -> statementToStatement(element)
|
is PsiStatement -> statementToStatement(element)
|
||||||
is PsiExpression -> expressionToExpression(element)
|
is PsiExpression -> expressionToExpression(element)
|
||||||
is PsiComment -> Comment(element.getText()!!)
|
is PsiComment -> Comment(element.getText()!!)
|
||||||
|
is PsiImportList -> importsToImportList(element)
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun fileToFile(javaFile: PsiJavaFile): File {
|
public fun fileToFile(javaFile: PsiJavaFile): File {
|
||||||
val psiImportList = javaFile.getImportList()
|
|
||||||
val importList = if (psiImportList != null) importsToImportList(psiImportList) else null
|
|
||||||
|
|
||||||
val body = ArrayList<Node>()
|
val body = ArrayList<Node>()
|
||||||
for (element in javaFile.getChildren()) {
|
for (element in javaFile.getChildren()) {
|
||||||
if (element !is PsiImportStatementBase) {
|
if (element !is PsiImportStatementBase) {
|
||||||
@@ -85,7 +83,7 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return File(quoteKeywords(javaFile.getPackageName()), importList, body, createMainFunction(javaFile))
|
return File(quoteKeywords(javaFile.getPackageName()), body, createMainFunction(javaFile))
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun anonymousClassToAnonymousClass(anonymousClass: PsiAnonymousClass): AnonymousClass {
|
public fun anonymousClassToAnonymousClass(anonymousClass: PsiAnonymousClass): AnonymousClass {
|
||||||
|
|||||||
@@ -17,12 +17,11 @@
|
|||||||
package org.jetbrains.jet.j2k.ast
|
package org.jetbrains.jet.j2k.ast
|
||||||
|
|
||||||
public open class File(val packageName: String,
|
public open class File(val packageName: String,
|
||||||
val imports: ImportList?,
|
|
||||||
val body: MutableList<Node>,
|
val body: MutableList<Node>,
|
||||||
val mainFunction: String) : Node {
|
val mainFunction: String) : Node {
|
||||||
|
|
||||||
public override fun toKotlin(): String {
|
public override fun toKotlin(): String {
|
||||||
val common = (imports?.toKotlin() ?: "") + "\n\n" + body.toKotlin("\n") + "\n" + mainFunction
|
val common = body.toKotlin("\n") + "\n" + mainFunction
|
||||||
if (packageName.isEmpty()) {
|
if (packageName.isEmpty()) {
|
||||||
return common
|
return common
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ public abstract class AbstractJavaToKotlinConverterBasicTest() : AbstractJavaToK
|
|||||||
public abstract class AbstractJavaToKotlinConverterTest(val kotlinFileExtension: String,
|
public abstract class AbstractJavaToKotlinConverterTest(val kotlinFileExtension: String,
|
||||||
val settings: ConverterSettings) : UsefulTestCase() {
|
val settings: ConverterSettings) : UsefulTestCase() {
|
||||||
|
|
||||||
val testHeaderPattern = Pattern.compile("//(expression|statement|method|class|file|comp)\n")
|
val testHeaderPattern = Pattern.compile("//(element|expression|statement|method|class|file|comp)\n")
|
||||||
|
|
||||||
protected fun doTest(javaPath: String) {
|
protected fun doTest(javaPath: String) {
|
||||||
val jetCoreEnvironment = createEnvironmentWithMockJdkAndIdeaAnnotations(getTestRootDisposable(), ConfigurationKind.JDK_ONLY)
|
val jetCoreEnvironment = createEnvironmentWithMockJdkAndIdeaAnnotations(getTestRootDisposable(), ConfigurationKind.JDK_ONLY)
|
||||||
@@ -56,6 +56,7 @@ public abstract class AbstractJavaToKotlinConverterTest(val kotlinFileExtension:
|
|||||||
val prefix = matcher.group().trim().substring(2)
|
val prefix = matcher.group().trim().substring(2)
|
||||||
val javaCode = matcher.replaceFirst("")
|
val javaCode = matcher.replaceFirst("")
|
||||||
val actual = when (prefix) {
|
val actual = when (prefix) {
|
||||||
|
"element" -> elementToKotlin(converter, javaCode)
|
||||||
"expression" -> expressionToKotlin(converter, javaCode)
|
"expression" -> expressionToKotlin(converter, javaCode)
|
||||||
"statement" -> statementToKotlin(converter, javaCode)
|
"statement" -> statementToKotlin(converter, javaCode)
|
||||||
"method" -> methodToKotlin(converter, javaCode)
|
"method" -> methodToKotlin(converter, javaCode)
|
||||||
@@ -77,6 +78,12 @@ public abstract class AbstractJavaToKotlinConverterTest(val kotlinFileExtension:
|
|||||||
Assert.assertEquals(expected, actual)
|
Assert.assertEquals(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun elementToKotlin(converter: Converter, text: String): String {
|
||||||
|
val fileWithText = JavaToKotlinTranslator.createFile(converter.project, text)!!
|
||||||
|
val element = fileWithText.getFirstChild()!!
|
||||||
|
return prettify(converter.elementToKotlin(element))
|
||||||
|
}
|
||||||
|
|
||||||
private fun fileToKotlin(converter: Converter, text: String): String {
|
private fun fileToKotlin(converter: Converter, text: String): String {
|
||||||
return generateKotlinCode(converter, JavaToKotlinTranslator.createFile(converter.project, text))
|
return generateKotlinCode(converter, JavaToKotlinTranslator.createFile(converter.project, text))
|
||||||
}
|
}
|
||||||
|
|||||||
+10
@@ -1233,6 +1233,16 @@ public class JavaToKotlinConverterBasicTestGenerated extends AbstractJavaToKotli
|
|||||||
doTest("j2k/tests/testData/ast/importStatement/importWithStar.jav");
|
doTest("j2k/tests/testData/ast/importStatement/importWithStar.jav");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("oneImport.jav")
|
||||||
|
public void testOneImport() throws Exception {
|
||||||
|
doTest("j2k/tests/testData/ast/importStatement/oneImport.jav");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("onlyImports.jav")
|
||||||
|
public void testOnlyImports() throws Exception {
|
||||||
|
doTest("j2k/tests/testData/ast/importStatement/onlyImports.jav");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("simpleImport.jav")
|
@TestMetadata("simpleImport.jav")
|
||||||
public void testSimpleImport() throws Exception {
|
public void testSimpleImport() throws Exception {
|
||||||
doTest("j2k/tests/testData/ast/importStatement/simpleImport.jav");
|
doTest("j2k/tests/testData/ast/importStatement/simpleImport.jav");
|
||||||
|
|||||||
+10
@@ -1233,6 +1233,16 @@ public class JavaToKotlinConverterPluginTestGenerated extends AbstractJavaToKotl
|
|||||||
doTest("j2k/tests/testData/ast/importStatement/importWithStar.jav");
|
doTest("j2k/tests/testData/ast/importStatement/importWithStar.jav");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("oneImport.jav")
|
||||||
|
public void testOneImport() throws Exception {
|
||||||
|
doTest("j2k/tests/testData/ast/importStatement/oneImport.jav");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("onlyImports.jav")
|
||||||
|
public void testOnlyImports() throws Exception {
|
||||||
|
doTest("j2k/tests/testData/ast/importStatement/onlyImports.jav");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("simpleImport.jav")
|
@TestMetadata("simpleImport.jav")
|
||||||
public void testSimpleImport() throws Exception {
|
public void testSimpleImport() throws Exception {
|
||||||
doTest("j2k/tests/testData/ast/importStatement/simpleImport.jav");
|
doTest("j2k/tests/testData/ast/importStatement/simpleImport.jav");
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
import java.util.Arrays
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
//element
|
||||||
|
import java.util.Arrays;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
import java.util.Arrays
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
import java.util.Arrays
|
||||||
|
import java.util.concurrent.ArrayBlockingQueue
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
//element
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
import java.util.Arrays
|
||||||
|
import java.util.concurrent.ArrayBlockingQueue
|
||||||
Reference in New Issue
Block a user