Java to Kotlin converter: refactored code to drop ArrayInitializerExpression

This commit is contained in:
Valentin Kipyatkov
2014-06-06 17:29:40 +04:00
parent 60d9e53e9f
commit f652c50c8c
4 changed files with 41 additions and 76 deletions
@@ -1,73 +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
import org.jetbrains.jet.lang.types.expressions.OperatorConventions
open class ArrayInitializerExpression(val arrayType: ArrayType, val initializers: List<Expression>) : Expression() {
override fun toKotlin(): String {
return createArrayFunction() + "(" + createInitializers() + ")"
}
private fun createInitializers(): String {
return initializers.map { explicitConvertIfNeeded(it) }.makeString(", ")
}
private fun createArrayFunction(): String {
val elementType = arrayType.elementType
if (elementType.isPrimitive()) {
return (elementType.toNotNullType().toKotlin() + "Array").decapitalize()
}
return arrayType.toNotNullType().toKotlin().decapitalize()
}
private fun innerTypeStr(): String {
return arrayType.toNotNullType().toKotlin().replace("Array", "").toLowerCase()
}
private fun explicitConvertIfNeeded(i: Expression): String {
val doubleOrFloatTypes = setOf("double", "float", "java.lang.double", "java.lang.float")
val afterReplace: String = innerTypeStr().replace(">", "").replace("<", "").replace("?", "")
if (doubleOrFloatTypes.contains(afterReplace))
{
if (i is LiteralExpression) {
if (i.toKotlin().contains(".")) {
return i.toKotlin()
}
return i.toKotlin() + ".0"
}
return "(" + i.toKotlin() + ")" + getConversion(afterReplace)
}
return i.toKotlin()
}
class object {
private open fun getConversion(afterReplace: String): String {
if (afterReplace.contains("double"))
return "." + OperatorConventions.DOUBLE + "()"
if (afterReplace.contains("float"))
return "." + OperatorConventions.FLOAT + "()"
return ""
}
}
}
@@ -16,6 +16,8 @@
package org.jetbrains.jet.j2k.ast
import org.jetbrains.jet.lang.types.expressions.OperatorConventions
class ArrayAccessExpression(val expression: Expression, val index: Expression, val lvalue: Boolean) : Expression() {
override fun toKotlin() = operandToKotlin(expression) +
(if (!lvalue && expression.isNullable) "!!" else "") +
@@ -91,3 +93,39 @@ class PolyadicExpression(val expressions: List<Expression>, val token: String) :
return expressionsWithConversions.makeString(" " + token + " ")
}
}
fun createArrayInitializerExpression(arrayType: ArrayType, initializers: List<Expression>) : MethodCallExpression {
val elementType = arrayType.elementType
val createArrayFunction = if (elementType.isPrimitive()) {
(elementType.toNotNullType().toKotlin() + "Array").decapitalize()
}
else
arrayType.toNotNullType().toKotlin().decapitalize()
val doubleOrFloatTypes = setOf("double", "float", "java.lang.double", "java.lang.float")
val afterReplace = arrayType.toNotNullType().toKotlin().replace("Array", "").toLowerCase().replace(">", "").replace("<", "").replace("?", "")
fun explicitConvertIfNeeded(initializer: Expression): Expression {
if (doubleOrFloatTypes.contains(afterReplace)) {
if (initializer is LiteralExpression) {
if (!initializer.toKotlin().contains(".")) {
return LiteralExpression(initializer.literalText + ".0")
}
}
else {
val conversionFunction = when {
afterReplace.contains("double") -> OperatorConventions.DOUBLE.getIdentifier()
afterReplace.contains("float") -> OperatorConventions.FLOAT.getIdentifier()
else -> null
}
if (conversionFunction != null) {
return MethodCallExpression(QualifiedExpression(initializer, Identifier(conversionFunction)), listOf(), listOf())
}
}
}
return initializer
}
return MethodCallExpression(Identifier(createArrayFunction), initializers.map { explicitConvertIfNeeded(it) }, listOf())
}
@@ -51,8 +51,8 @@ class ExpressionVisitor(private val converter: Converter,
override fun visitArrayInitializerExpression(expression: PsiArrayInitializerExpression) {
val expressionType = typeConverter.convertType(expression.getType())
assert(expressionType is ArrayType, "Array initializer must have array type")
result = ArrayInitializerExpression(expressionType as ArrayType,
converter.convertExpressions(expression.getInitializers()))
result = createArrayInitializerExpression(expressionType as ArrayType,
converter.convertExpressions(expression.getInitializers()))
}
override fun visitAssignmentExpression(expression: PsiAssignmentExpression) {
@@ -1,4 +1,4 @@
val a = 0
val b = 0
val c = 0
val ds = doubleArray((a).toDouble(), (b).toDouble(), (c).toDouble())
val ds = doubleArray(a.toDouble(), b.toDouble(), c.toDouble())