Refactor converter:
Move the logic dealing with imports to Imports.kt file, refactor the code
This commit is contained in:
committed by
Pavel V. Talanov
parent
0f9a93ffca
commit
ba87779a8e
@@ -41,8 +41,6 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
|||||||
|
|
||||||
private val dispatcher: Dispatcher = Dispatcher(this)
|
private val dispatcher: Dispatcher = Dispatcher(this)
|
||||||
|
|
||||||
private val javaToKotlinClassMap: JavaToKotlinClassMap = JavaToKotlinClassMap.getInstance()
|
|
||||||
|
|
||||||
public var methodReturnType: PsiType? = null
|
public var methodReturnType: PsiType? = null
|
||||||
private set
|
private set
|
||||||
|
|
||||||
@@ -75,22 +73,10 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public fun fileToFile(javaFile: PsiJavaFile): File {
|
public fun fileToFile(javaFile: PsiJavaFile): File {
|
||||||
val importList: PsiImportList? = javaFile.getImportList()
|
val psiImportList = javaFile.getImportList()
|
||||||
val imports: MutableList<Import> = (if (importList == null)
|
val importList = if (psiImportList != null) importsToImportList(psiImportList) else 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 body: ArrayList<Node> = arrayListOf()
|
val body = ArrayList<Node>()
|
||||||
for (element in javaFile.getChildren()) {
|
for (element in javaFile.getChildren()) {
|
||||||
if (element !is PsiImportStatementBase) {
|
if (element !is PsiImportStatementBase) {
|
||||||
val node = topElementToElement(element)
|
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 {
|
public fun anonymousClassToAnonymousClass(anonymousClass: PsiAnonymousClass): AnonymousClass {
|
||||||
@@ -461,7 +447,7 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
|||||||
return expression
|
return expression
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun quoteKeywords(packageName: String): String {
|
public fun quoteKeywords(packageName: String): String {
|
||||||
return packageName.split("\\.").map { Identifier(it).toKotlin() }.makeString(".")
|
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<PsiImportStatementBase>): List<Import> {
|
|
||||||
val result = ArrayList<Import>()
|
|
||||||
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 {
|
public fun identifierToIdentifier(identifier: PsiIdentifier?): Identifier {
|
||||||
if (identifier == null)
|
if (identifier == null)
|
||||||
return Identifier.EMPTY_IDENTIFIER
|
return Identifier.EMPTY_IDENTIFIER
|
||||||
@@ -582,8 +542,8 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val NOT_NULL_ANNOTATIONS: Set<String> = ImmutableSet.of<String>("org.jetbrains.annotations.NotNull", "com.sun.istack.internal.NotNull", "javax.annotation.Nonnull")!!
|
public val NOT_NULL_ANNOTATIONS: Set<String> = ImmutableSet.of<String>("org.jetbrains.annotations.NotNull", "com.sun.istack.internal.NotNull", "javax.annotation.Nonnull")!!
|
||||||
private val PRIMITIVE_TYPE_CONVERSIONS: Map<String, String> = ImmutableMap.builder<String, String>()
|
public val PRIMITIVE_TYPE_CONVERSIONS: Map<String, String> = ImmutableMap.builder<String, String>()
|
||||||
?.put("byte", BYTE.asString())
|
?.put("byte", BYTE.asString())
|
||||||
?.put("short", SHORT.asString())
|
?.put("short", SHORT.asString())
|
||||||
?.put("int", INT.asString())
|
?.put("int", INT.asString())
|
||||||
|
|||||||
@@ -17,12 +17,12 @@
|
|||||||
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: MutableList<Import>,
|
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: 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()) {
|
if (packageName.isEmpty()) {
|
||||||
return common
|
return common
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -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<Import>) : 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("")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user