New J2K: Add string.length conversion
This commit is contained in:
committed by
Ilya Kirillov
parent
9704a7aa7f
commit
cc2f27f9cd
@@ -62,6 +62,7 @@ object ConversionsRunner {
|
||||
+CollectionOperationsConversion(context)
|
||||
+ArrayOperationsConversion(context)
|
||||
+TypeMappingConversion(context)
|
||||
+StringMethodsConversion(context)
|
||||
|
||||
//Kotlin --> Kotlin conversions
|
||||
+InnerClassConversion()
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.j2k.conversions
|
||||
|
||||
import org.jetbrains.kotlin.j2k.ConversionContext
|
||||
import org.jetbrains.kotlin.j2k.tree.*
|
||||
import org.jetbrains.kotlin.j2k.tree.impl.JKFieldAccessExpressionImpl
|
||||
|
||||
|
||||
class StringMethodsConversion(private val context: ConversionContext) : RecursiveApplicableConversionBase() {
|
||||
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
||||
if (element !is JKQualifiedExpression) return recurse(element)
|
||||
val receiverType = element.receiver.type(context) ?: return recurse(element)
|
||||
if (!receiverType.isStringType()) return recurse(element)
|
||||
|
||||
convertLengthCall(element.selector)?.also {
|
||||
element.selector = it
|
||||
}
|
||||
|
||||
return recurse(element)
|
||||
}
|
||||
|
||||
private fun convertLengthCall(selector: JKExpression): JKFieldAccessExpressionImpl? {
|
||||
if (selector !is JKMethodCallExpression) return null
|
||||
return if (selector.identifier.name == "length") {
|
||||
JKFieldAccessExpressionImpl(context.symbolProvider.provideByFqName("kotlin.String.length"))
|
||||
} else null
|
||||
}
|
||||
}
|
||||
@@ -168,6 +168,9 @@ fun JKType.isCollectionType(symbolProvider: JKSymbolProvider): Boolean {
|
||||
return this.isSubtypeOf(collectionType, symbolProvider)
|
||||
}
|
||||
|
||||
fun JKType.isStringType(): Boolean =
|
||||
(this as? JKClassType)?.classReference?.name == "String"
|
||||
|
||||
fun JKLiteralExpression.LiteralType.toPrimitiveType(): JKJavaPrimitiveType? =
|
||||
when (this) {
|
||||
JKLiteralExpression.LiteralType.CHAR -> JKJavaPrimitiveTypeImpl.CHAR
|
||||
|
||||
Reference in New Issue
Block a user