From ba87779a8e172c71116176c8b51c6cf2dd3322df Mon Sep 17 00:00:00 2001 From: Pavel Talanov Date: Fri, 22 Nov 2013 20:00:28 +0400 Subject: [PATCH] Refactor converter: Move the logic dealing with imports to Imports.kt file, refactor the code --- j2k/src/org/jetbrains/jet/j2k/Converter.kt | 54 +++---------------- j2k/src/org/jetbrains/jet/j2k/ast/File.kt | 4 +- j2k/src/org/jetbrains/jet/j2k/ast/Import.kt | 22 -------- j2k/src/org/jetbrains/jet/j2k/ast/Imports.kt | 55 ++++++++++++++++++++ 4 files changed, 64 insertions(+), 71 deletions(-) delete mode 100644 j2k/src/org/jetbrains/jet/j2k/ast/Import.kt create mode 100644 j2k/src/org/jetbrains/jet/j2k/ast/Imports.kt diff --git a/j2k/src/org/jetbrains/jet/j2k/Converter.kt b/j2k/src/org/jetbrains/jet/j2k/Converter.kt index a3225b80b62..65408e0014b 100644 --- a/j2k/src/org/jetbrains/jet/j2k/Converter.kt +++ b/j2k/src/org/jetbrains/jet/j2k/Converter.kt @@ -41,8 +41,6 @@ public class Converter(val project: Project, val settings: ConverterSettings) { private val dispatcher: Dispatcher = Dispatcher(this) - private val javaToKotlinClassMap: JavaToKotlinClassMap = JavaToKotlinClassMap.getInstance() - public var methodReturnType: PsiType? = null private set @@ -75,22 +73,10 @@ public class Converter(val project: Project, val settings: ConverterSettings) { } public fun fileToFile(javaFile: PsiJavaFile): File { - val importList: PsiImportList? = javaFile.getImportList() - val imports: MutableList = (if (importList == null) - arrayListOf() - else - ArrayList(importsToImportList(importList.getAllImportStatements()) filter { - // If name is invalid, like with star imports, don't try to filter - if (!QualifiedNamesUtil.isValidJavaFqName(it.name)) - true - else { - // If imported class has a kotlin analog, drop the import - val kotlinAnalogsForClass = javaToKotlinClassMap.mapPlatformClass(FqName(it.name)) - kotlinAnalogsForClass.isEmpty() - } - })) + val psiImportList = javaFile.getImportList() + val importList = if (psiImportList != null) importsToImportList(psiImportList) else null - val body: ArrayList = arrayListOf() + val body = ArrayList() for (element in javaFile.getChildren()) { if (element !is PsiImportStatementBase) { val node = topElementToElement(element) @@ -99,7 +85,7 @@ public class Converter(val project: Project, val settings: ConverterSettings) { } } } - return File(quoteKeywords(javaFile.getPackageName()), imports, body, createMainFunction(javaFile)) + return File(quoteKeywords(javaFile.getPackageName()), importList, body, createMainFunction(javaFile)) } public fun anonymousClassToAnonymousClass(anonymousClass: PsiAnonymousClass): AnonymousClass { @@ -461,7 +447,7 @@ public class Converter(val project: Project, val settings: ConverterSettings) { return expression } - private fun quoteKeywords(packageName: String): String { + public fun quoteKeywords(packageName: String): String { return packageName.split("\\.").map { Identifier(it).toKotlin() }.makeString(".") } @@ -500,32 +486,6 @@ public class Converter(val project: Project, val settings: ConverterSettings) { } } - private fun importsToImportList(imports: Array): List { - val result = ArrayList() - for (i : PsiImportStatementBase? in imports) { - if (i == null) continue - val anImport: Import = importToImport(i) - val name: String = anImport.name - if (!name.isEmpty() && !NOT_NULL_ANNOTATIONS.contains(name)) { - result.add(anImport) - } - - } - return result - } - - private fun importToImport(i: PsiImportStatementBase): Import { - val reference: PsiJavaCodeReferenceElement? = i.getImportReference() - if (reference != null) { - return Import(quoteKeywords(reference.getQualifiedName()!!) + ((if (i.isOnDemand()) - ".*" - else - ""))) - } - - return Import("") - } - public fun identifierToIdentifier(identifier: PsiIdentifier?): Identifier { if (identifier == null) return Identifier.EMPTY_IDENTIFIER @@ -582,8 +542,8 @@ public class Converter(val project: Project, val settings: ConverterSettings) { } } -private val NOT_NULL_ANNOTATIONS: Set = ImmutableSet.of("org.jetbrains.annotations.NotNull", "com.sun.istack.internal.NotNull", "javax.annotation.Nonnull")!! -private val PRIMITIVE_TYPE_CONVERSIONS: Map = ImmutableMap.builder() +public val NOT_NULL_ANNOTATIONS: Set = ImmutableSet.of("org.jetbrains.annotations.NotNull", "com.sun.istack.internal.NotNull", "javax.annotation.Nonnull")!! +public val PRIMITIVE_TYPE_CONVERSIONS: Map = ImmutableMap.builder() ?.put("byte", BYTE.asString()) ?.put("short", SHORT.asString()) ?.put("int", INT.asString()) diff --git a/j2k/src/org/jetbrains/jet/j2k/ast/File.kt b/j2k/src/org/jetbrains/jet/j2k/ast/File.kt index b4b69c3d5aa..b5b81c90f97 100644 --- a/j2k/src/org/jetbrains/jet/j2k/ast/File.kt +++ b/j2k/src/org/jetbrains/jet/j2k/ast/File.kt @@ -17,12 +17,12 @@ package org.jetbrains.jet.j2k.ast public open class File(val packageName: String, - val imports: MutableList, + val imports: ImportList?, val body: MutableList, val mainFunction: String) : Node { public override fun toKotlin(): String { - val common: String = imports.toKotlin("\n") + "\n\n" + body.toKotlin("\n") + "\n" + mainFunction + val common = (imports?.toKotlin() ?: "") + "\n\n" + body.toKotlin("\n") + "\n" + mainFunction if (packageName.isEmpty()) { return common } diff --git a/j2k/src/org/jetbrains/jet/j2k/ast/Import.kt b/j2k/src/org/jetbrains/jet/j2k/ast/Import.kt deleted file mode 100644 index 4b59c3a5fe0..00000000000 --- a/j2k/src/org/jetbrains/jet/j2k/ast/Import.kt +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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. - * 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.ast - - -public class Import(val name: String) : Node { - public override fun toKotlin() = "import " + name -} diff --git a/j2k/src/org/jetbrains/jet/j2k/ast/Imports.kt b/j2k/src/org/jetbrains/jet/j2k/ast/Imports.kt new file mode 100644 index 00000000000..4182de709d6 --- /dev/null +++ b/j2k/src/org/jetbrains/jet/j2k/ast/Imports.kt @@ -0,0 +1,55 @@ +/* + * 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. + * 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.ast + +import com.intellij.psi.PsiImportStatementBase +import org.jetbrains.jet.j2k.* +import com.intellij.psi.PsiImportList +import org.jetbrains.jet.lang.resolve.name.FqName +import org.jetbrains.jet.util.QualifiedNamesUtil +import org.jetbrains.jet.lang.resolve.java.mapping.JavaToKotlinClassMap + + +public class Import(val name: String) : Node { + public override fun toKotlin() = "import " + name +} + +public class ImportList(val imports: List) : Node { + override fun toKotlin() = imports.filter { + !it.name.isEmpty() && it.name !in NOT_NULL_ANNOTATIONS + }.filter { + // If name is invalid, like with star imports, don't try to filter + if (!QualifiedNamesUtil.isValidJavaFqName(it.name)) + true + else { + // If imported class has a kotlin analog, drop the import + val kotlinAnalogsForClass = JavaToKotlinClassMap.getInstance().mapPlatformClass(FqName(it.name)) + kotlinAnalogsForClass.isEmpty() + } + }.toKotlin("\n") +} + +public fun Converter.importsToImportList(importList: PsiImportList): ImportList = + ImportList(importList.getAllImportStatements() map { importToImport(it) }) + +public fun Converter.importToImport(i: PsiImportStatementBase): Import { + val reference = i.getImportReference() + if (reference != null) { + return Import(quoteKeywords(reference.getQualifiedName()!!) + if (i.isOnDemand()) ".*" else "") + } + return Import("") +} \ No newline at end of file