Java to Kotlin converter: minor code refactoring

This commit is contained in:
Valentin Kipyatkov
2014-06-10 18:45:07 +04:00
parent b71061eb5c
commit 1b948ef8f1
4 changed files with 42 additions and 23 deletions
+1 -1
View File
@@ -481,7 +481,7 @@ public class Converter(val project: Project, val settings: ConverterSettings, va
if (isConversionNeeded(actualType, expectedType) && expression !is LiteralExpression) { if (isConversionNeeded(actualType, expectedType) && expression !is LiteralExpression) {
val conversion = PRIMITIVE_TYPE_CONVERSIONS[expectedType?.getCanonicalText()] val conversion = PRIMITIVE_TYPE_CONVERSIONS[expectedType?.getCanonicalText()]
if (conversion != null) { if (conversion != null) {
expression = MethodCallExpression.build(expression, conversion) expression = MethodCallExpression.buildNotNull(expression, conversion)
} }
} }
@@ -116,7 +116,7 @@ fun createArrayInitializerExpression(arrayType: ArrayType, initializers: List<Ex
else -> null else -> null
} }
if (conversionFunction != null) { if (conversionFunction != null) {
return MethodCallExpression(QualifiedExpression(initializer, Identifier(conversionFunction)), listOf(), listOf()) return MethodCallExpression.buildNotNull(initializer, conversionFunction)
} }
} }
} }
@@ -124,5 +124,5 @@ fun createArrayInitializerExpression(arrayType: ArrayType, initializers: List<Ex
return initializer return initializer
} }
return MethodCallExpression(Identifier(createArrayFunction), initializers.map { explicitConvertIfNeeded(it) }, listOf()) return MethodCallExpression.buildNotNull(null, createArrayFunction, initializers.map { explicitConvertIfNeeded(it) })
} }
@@ -21,24 +21,41 @@ import java.util.ArrayList
class MethodCallExpression( class MethodCallExpression(
val methodExpression: Expression, val methodExpression: Expression,
val arguments: List<Expression>, val arguments: List<Expression>,
val typeParameters: List<Type>, val typeArguments: List<Type>,
override val isNullable: Boolean = false override val isNullable: Boolean
) : Expression() { ) : Expression() {
override fun toKotlin(): String { override fun toKotlin(): String {
return operandToKotlin(methodExpression) + return operandToKotlin(methodExpression) +
typeParameters.toKotlin(", ", "<", ">") + typeArguments.toKotlin(", ", "<", ">") +
"(" + "(" +
arguments.map { it.toKotlin() }.makeString(", ") + arguments.map { it.toKotlin() }.makeString(", ") +
")" ")"
} }
class object { class object {
public fun build(receiver: Expression, methodName: String, arguments: List<Expression> = ArrayList()): MethodCallExpression { public fun buildNotNull(receiver: Expression?,
return MethodCallExpression(QualifiedExpression(receiver, Identifier(methodName, false)), methodName: String,
arguments: List<Expression> = listOf(),
typeArguments: List<Type> = listOf()): MethodCallExpression
= build(receiver, methodName, arguments, typeArguments, false)
public fun buildNullable(receiver: Expression?,
methodName: String,
arguments: List<Expression> = listOf(),
typeArguments: List<Type> = listOf()): MethodCallExpression
= build(receiver, methodName, arguments, typeArguments, true)
public fun build(receiver: Expression?,
methodName: String,
arguments: List<Expression>,
typeArguments: List<Type>,
isNullable: Boolean): MethodCallExpression {
val identifier = Identifier(methodName, false)
return MethodCallExpression(if (receiver != null) QualifiedExpression(receiver, identifier) else identifier,
arguments, arguments,
listOf(), typeArguments,
false) isNullable)
} }
} }
} }
@@ -82,7 +82,7 @@ class ExpressionVisitor(private val converter: Converter,
val lhs = converter.convertExpression(expression.getLOperand(), expression.getType()) val lhs = converter.convertExpression(expression.getLOperand(), expression.getType())
val rhs = converter.convertExpression(expression.getROperand(), expression.getType()) val rhs = converter.convertExpression(expression.getROperand(), expression.getType())
if (expression.getOperationSign().getTokenType() == JavaTokenType.GTGTGT) { if (expression.getOperationSign().getTokenType() == JavaTokenType.GTGTGT) {
result = MethodCallExpression.build(lhs, "ushr", listOf(rhs)) result = MethodCallExpression.buildNotNull(lhs, "ushr", listOf(rhs))
} }
else { else {
result = BinaryExpression(lhs, rhs, getOperatorString(expression.getOperationSign().getTokenType())) result = BinaryExpression(lhs, rhs, getOperatorString(expression.getOperationSign().getTokenType()))
@@ -91,7 +91,7 @@ class ExpressionVisitor(private val converter: Converter,
override fun visitClassObjectAccessExpression(expression: PsiClassObjectAccessExpression) { override fun visitClassObjectAccessExpression(expression: PsiClassObjectAccessExpression) {
val typeElement = converter.convertTypeElement(expression.getOperand()) val typeElement = converter.convertTypeElement(expression.getOperand())
result = MethodCallExpression(Identifier("javaClass"), listOf(), listOf(typeElement.`type`.toNotNullType()), false) result = MethodCallExpression.buildNotNull(null, "javaClass", listOf(), listOf(typeElement.`type`.toNotNullType()))
} }
override fun visitConditionalExpression(expression: PsiConditionalExpression) { override fun visitConditionalExpression(expression: PsiConditionalExpression) {
@@ -193,16 +193,18 @@ class ExpressionVisitor(private val converter: Converter,
if (isTopLevel) { if (isTopLevel) {
result = if (origin.isExtensionDeclaration()) { result = if (origin.isExtensionDeclaration()) {
val qualifier = converter.convertExpression(arguments.firstOrNull()) val qualifier = converter.convertExpression(arguments.firstOrNull())
MethodCallExpression(QualifiedExpression(qualifier, Identifier(origin.getName()!!, false)), MethodCallExpression.build(qualifier,
convertArguments(expression, isExtension = true), origin.getName()!!,
typeArguments, convertArguments(expression, isExtension = true),
isNullable) typeArguments,
isNullable)
} }
else { else {
MethodCallExpression(Identifier(origin.getName()!!, false), MethodCallExpression.build(null,
convertArguments(expression), origin.getName()!!,
typeArguments, convertArguments(expression),
isNullable) typeArguments,
isNullable)
} }
return return
} }
@@ -255,7 +257,7 @@ class ExpressionVisitor(private val converter: Converter,
val reference = expression.getClassReference() val reference = expression.getClassReference()
val typeParameters = if (reference != null) typeConverter.convertTypes(reference.getTypeParameters()) else listOf() val typeParameters = if (reference != null) typeConverter.convertTypes(reference.getTypeParameters()) else listOf()
return QualifiedExpression(Identifier(constructor.getName(), false), return QualifiedExpression(Identifier(constructor.getName(), false),
MethodCallExpression(Identifier("init"), converter.convertExpressions(arguments), typeParameters, false)) MethodCallExpression.buildNotNull(null, "init", converter.convertExpressions(arguments), typeParameters))
} }
return NewClassExpression(converter.convertElement(classReference), return NewClassExpression(converter.convertElement(classReference),
@@ -277,7 +279,7 @@ class ExpressionVisitor(private val converter: Converter,
val operand = converter.convertExpression(expression.getOperand(), expression.getOperand()!!.getType()) val operand = converter.convertExpression(expression.getOperand(), expression.getOperand()!!.getType())
val token = expression.getOperationTokenType() val token = expression.getOperationTokenType()
if (token == JavaTokenType.TILDE) { if (token == JavaTokenType.TILDE) {
result = MethodCallExpression.build(operand, "inv", ArrayList()) result = MethodCallExpression.buildNotNull(operand, "inv")
} }
else if (token == JavaTokenType.EXCL && operand is BinaryExpression && operand.op == "==") { // happens when equals is converted to == else if (token == JavaTokenType.EXCL && operand is BinaryExpression && operand.op == "==") { // happens when equals is converted to ==
result = BinaryExpression(operand.left, operand.right, "!=") result = BinaryExpression(operand.left, operand.right, "!=")
@@ -366,7 +368,7 @@ class ExpressionVisitor(private val converter: Converter,
val typeText = castType.getType().getCanonicalText() val typeText = castType.getType().getCanonicalText()
val typeConversion = PRIMITIVE_TYPE_CONVERSIONS[typeText] val typeConversion = PRIMITIVE_TYPE_CONVERSIONS[typeText]
if (operandType is PsiPrimitiveType && typeConversion != null) { if (operandType is PsiPrimitiveType && typeConversion != null) {
result = MethodCallExpression.build(converter.convertExpression(operand), typeConversion) result = MethodCallExpression.buildNotNull(converter.convertExpression(operand), typeConversion)
} }
else { else {
result = TypeCastExpression(typeConverter.convertType(castType.getType()), result = TypeCastExpression(typeConverter.convertType(castType.getType()),