New J2K: correctly convert Java String type to Kotlin String
This commit is contained in:
@@ -10,6 +10,7 @@ import com.intellij.openapi.project.Project
|
|||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
import org.jetbrains.kotlin.asJava.elements.KtLightDeclaration
|
import org.jetbrains.kotlin.asJava.elements.KtLightDeclaration
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
|
import org.jetbrains.kotlin.name.FqNameUnsafe
|
||||||
import org.jetbrains.kotlin.nj2k.conversions.JKResolver
|
import org.jetbrains.kotlin.nj2k.conversions.JKResolver
|
||||||
import org.jetbrains.kotlin.nj2k.tree.*
|
import org.jetbrains.kotlin.nj2k.tree.*
|
||||||
import org.jetbrains.kotlin.nj2k.tree.impl.*
|
import org.jetbrains.kotlin.nj2k.tree.impl.*
|
||||||
@@ -112,6 +113,9 @@ class JKSymbolProvider(project: Project, module: Module, contextElement: PsiElem
|
|||||||
fun provideClassSymbol(fqName: String): JKClassSymbol =
|
fun provideClassSymbol(fqName: String): JKClassSymbol =
|
||||||
provideClassSymbol(FqName(fqName.asSafeFqNameString()))
|
provideClassSymbol(FqName(fqName.asSafeFqNameString()))
|
||||||
|
|
||||||
|
fun provideClassSymbol(fqName: FqNameUnsafe): JKClassSymbol =
|
||||||
|
provideClassSymbol(fqName.toSafe())
|
||||||
|
|
||||||
fun provideMethodSymbol(fqName: FqName): JKMethodSymbol =
|
fun provideMethodSymbol(fqName: FqName): JKMethodSymbol =
|
||||||
symbolsByFqName.getOrPutIfNotNull(fqName.asString()) {
|
symbolsByFqName.getOrPutIfNotNull(fqName.asString()) {
|
||||||
resolver.resolveMethod(fqName)?.let {
|
resolver.resolveMethod(fqName)?.let {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.nj2k.conversions
|
package org.jetbrains.kotlin.nj2k.conversions
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.nj2k.*
|
import org.jetbrains.kotlin.nj2k.*
|
||||||
import org.jetbrains.kotlin.nj2k.tree.*
|
import org.jetbrains.kotlin.nj2k.tree.*
|
||||||
@@ -62,6 +63,13 @@ class BuiltinMembersConversion(private val context: NewJ2kConverterContext) : Re
|
|||||||
if (conversion.filter?.invoke(this) == false) return@firstOrNull false
|
if (conversion.filter?.invoke(this) == false) return@firstOrNull false
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
is JKJavaNewExpression ->
|
||||||
|
conversions[classSymbol.deepestFqName()]?.firstOrNull { conversion ->
|
||||||
|
if (conversion.from !is NewExpression) return@firstOrNull false
|
||||||
|
if (conversion.filter?.invoke(this) == false) return@firstOrNull false
|
||||||
|
true
|
||||||
|
}
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,6 +96,12 @@ class BuiltinMembersConversion(private val context: NewJ2kConverterContext) : Re
|
|||||||
JKArgumentListImpl(),
|
JKArgumentListImpl(),
|
||||||
JKTypeArgumentListImpl()
|
JKTypeArgumentListImpl()
|
||||||
).withNonCodeElementsFrom(from)
|
).withNonCodeElementsFrom(from)
|
||||||
|
is JKJavaNewExpression ->
|
||||||
|
JKKtCallExpressionImpl(
|
||||||
|
context.symbolProvider.provideMethodSymbol(fqName),
|
||||||
|
argumentsProvider(from::arguments.detached()),
|
||||||
|
JKTypeArgumentListImpl()
|
||||||
|
).withNonCodeElementsFrom(from)
|
||||||
else -> error("Bad conversion")
|
else -> error("Bad conversion")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -157,6 +171,7 @@ class BuiltinMembersConversion(private val context: NewJ2kConverterContext) : Re
|
|||||||
}
|
}
|
||||||
|
|
||||||
private data class Method(override val fqName: String) : SymbolInfo
|
private data class Method(override val fqName: String) : SymbolInfo
|
||||||
|
private data class NewExpression(override val fqName: String) : SymbolInfo
|
||||||
private data class Field(override val fqName: String) : SymbolInfo
|
private data class Field(override val fqName: String) : SymbolInfo
|
||||||
private data class ExtensionMethod(override val fqName: String) : SymbolInfo
|
private data class ExtensionMethod(override val fqName: String) : SymbolInfo
|
||||||
private data class CustomExpression(val expressionBuilder: (JKExpression) -> JKExpression) : Info
|
private data class CustomExpression(val expressionBuilder: (JKExpression) -> JKExpression) : Info
|
||||||
@@ -413,7 +428,17 @@ class BuiltinMembersConversion(private val context: NewJ2kConverterContext) : Re
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
Method("java.lang.String.format") convertTo CustomExpression { expression ->
|
||||||
|
JKClassAccessExpressionImpl(
|
||||||
|
context.symbolProvider.provideClassSymbol(KotlinBuiltIns.FQ_NAMES.string)
|
||||||
|
).callOn(
|
||||||
|
context.symbolProvider.provideMethodSymbol("kotlin.text.String.format"),
|
||||||
|
(expression as JKMethodCallExpression).arguments::arguments.detached()
|
||||||
|
)
|
||||||
|
} withReplaceType ReplaceType.REPLACE_WITH_QUALIFIER,
|
||||||
|
|
||||||
|
NewExpression("java.lang.String") convertTo Method("kotlin.text.String"),
|
||||||
|
NewExpression("kotlin.String") convertTo Method("kotlin.text.String"),
|
||||||
|
|
||||||
Method("java.util.Collections.singletonList") convertTo Method("kotlin.collections.listOf")
|
Method("java.util.Collections.singletonList") convertTo Method("kotlin.collections.listOf")
|
||||||
withReplaceType ReplaceType.REPLACE_WITH_QUALIFIER,
|
withReplaceType ReplaceType.REPLACE_WITH_QUALIFIER,
|
||||||
|
|||||||
@@ -137,11 +137,7 @@ class TypeMappingConversion(val context: NewJ2kConverterContext) : RecursiveAppl
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun JKClassSymbol.kotlinStandardType(): String? =
|
private fun JKClassSymbol.kotlinStandardType(): String? =
|
||||||
fqName.takeIf {
|
JavaToKotlinClassMap.mapJavaToKotlin(FqName(fqName))?.asString()
|
||||||
it !in ignoredJavaFqNames
|
|
||||||
}?.let {
|
|
||||||
JavaToKotlinClassMap.mapJavaToKotlin(FqName(it))?.asString()
|
|
||||||
} ?: fqName
|
|
||||||
|
|
||||||
private fun JKJavaPrimitiveType.mapPrimitiveType(): JKClassType {
|
private fun JKJavaPrimitiveType.mapPrimitiveType(): JKClassType {
|
||||||
val fqName = jvmPrimitiveType.primitiveType.typeFqName
|
val fqName = jvmPrimitiveType.primitiveType.typeFqName
|
||||||
@@ -160,10 +156,4 @@ class TypeMappingConversion(val context: NewJ2kConverterContext) : RecursiveAppl
|
|||||||
else -> false
|
else -> false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
|
||||||
val ignoredJavaFqNames = setOf(
|
|
||||||
"java.lang.String"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -253,7 +253,11 @@ fun JKType.isCollectionType(symbolProvider: JKSymbolProvider): Boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun JKType.isStringType(): Boolean =
|
fun JKType.isStringType(): Boolean =
|
||||||
(this as? JKClassType)?.classReference?.name == "String"
|
(this as? JKClassType)?.classReference?.isStringType() == true
|
||||||
|
|
||||||
|
fun JKClassSymbol.isStringType(): Boolean =
|
||||||
|
fqName == CommonClassNames.JAVA_LANG_STRING
|
||||||
|
|| fqName == KotlinBuiltIns.FQ_NAMES.string.asString()
|
||||||
|
|
||||||
fun JKLiteralExpression.LiteralType.toPrimitiveType(): JKJavaPrimitiveType? =
|
fun JKLiteralExpression.LiteralType.toPrimitiveType(): JKJavaPrimitiveType? =
|
||||||
when (this) {
|
when (this) {
|
||||||
|
|||||||
@@ -86,8 +86,5 @@ internal class D : JavaClassDerivedFromKotlinClassWithProperties() {
|
|||||||
return "a"
|
return "a"
|
||||||
}
|
}
|
||||||
|
|
||||||
override var someVar2: String?
|
override fun setSomeVar2(value: String?) {}
|
||||||
get() = super.someVar2
|
|
||||||
set(value) {}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user