New J2K: Implement basic default argument converter
This commit is contained in:
committed by
Ilya Kirillov
parent
34b535c8ab
commit
911f234ba4
@@ -24,6 +24,7 @@ object ConversionsRunner {
|
|||||||
private fun createConversions(context: ConversionContext) = listOf(
|
private fun createConversions(context: ConversionContext) = listOf(
|
||||||
ModalityConversion(context),
|
ModalityConversion(context),
|
||||||
ImplicitInitializerConversion(),
|
ImplicitInitializerConversion(),
|
||||||
|
DefaultArgumentsConversion(context),
|
||||||
TypeMappingConversion(context),
|
TypeMappingConversion(context),
|
||||||
FieldToPropertyConversion(),
|
FieldToPropertyConversion(),
|
||||||
AssignmentAsExpressionToAlsoConversion(context),
|
AssignmentAsExpressionToAlsoConversion(context),
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* 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.JKKtFieldAccessExpressionImpl
|
||||||
|
import org.jetbrains.kotlin.j2k.tree.impl.JKUniverseMethodSymbol
|
||||||
|
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||||
|
|
||||||
|
class DefaultArgumentsConversion(private val context: ConversionContext) : RecursiveApplicableConversionBase() {
|
||||||
|
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
||||||
|
// TODO: Declaration list owner
|
||||||
|
if (element !is JKClass) return recurse(element)
|
||||||
|
|
||||||
|
val methods = element.declarationList.filterIsInstance<JKMethod>().sortedBy { it.parameters.size }
|
||||||
|
|
||||||
|
checkMethod@ for (method in methods) {
|
||||||
|
val block = method.block as? JKBlock ?: continue
|
||||||
|
val singleStatement = block.statements.singleOrNull()
|
||||||
|
|
||||||
|
val call =
|
||||||
|
singleStatement.safeAs<JKExpressionStatement>()?.expression?.safeAs<JKMethodCallExpression>()
|
||||||
|
?: singleStatement.safeAs<JKReturnStatement>()?.expression?.safeAs()
|
||||||
|
?: continue
|
||||||
|
val callee = call.identifier as? JKUniverseMethodSymbol ?: continue
|
||||||
|
val calledMethod = callee.target
|
||||||
|
if (calledMethod.parent != method.parent
|
||||||
|
|| callee.name != method.name.value
|
||||||
|
|| calledMethod.returnType.type != method.returnType.type
|
||||||
|
|| call.arguments.expressions.size <= method.parameters.size
|
||||||
|
) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: Filter by annotations, visibility, modality, modifiers like synchronized
|
||||||
|
|
||||||
|
for (i in method.parameters.indices) {
|
||||||
|
val parameter = method.parameters[i]
|
||||||
|
val targetParameter = calledMethod.parameters[i]
|
||||||
|
val argument = call.arguments.expressions[i]
|
||||||
|
if (parameter.name.value != targetParameter.name.value) continue@checkMethod
|
||||||
|
if (parameter.type.type != targetParameter.type.type) continue@checkMethod
|
||||||
|
if (argument !is JKFieldAccessExpression || argument.identifier.target != parameter) continue@checkMethod
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
call.arguments.invalidate()
|
||||||
|
val defaults = call.arguments.expressions
|
||||||
|
.zip(calledMethod.parameters)
|
||||||
|
.drop(method.parameters.size)
|
||||||
|
|
||||||
|
for ((defaultValue, parameter) in defaults) {
|
||||||
|
fun remapParameterSymbol(on: JKTreeElement): JKTreeElement {
|
||||||
|
if (on is JKFieldAccessExpression) {
|
||||||
|
val target = on.identifier.target
|
||||||
|
if (target is JKParameter) {
|
||||||
|
val newSymbol =
|
||||||
|
context.symbolProvider.provideUniverseSymbol(calledMethod.parameters[method.parameters.indexOf(target)])
|
||||||
|
return JKKtFieldAccessExpressionImpl(newSymbol)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return applyRecursive(on, ::remapParameterSymbol)
|
||||||
|
}
|
||||||
|
|
||||||
|
parameter.initializer = remapParameterSymbol(defaultValue) as JKExpression
|
||||||
|
}
|
||||||
|
|
||||||
|
element.declarationList -= method
|
||||||
|
}
|
||||||
|
|
||||||
|
return recurse(element)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.j2k.tree.impl.JKMethodSymbol
|
|||||||
interface JKJavaField : JKField, JKBranchElement
|
interface JKJavaField : JKField, JKBranchElement
|
||||||
|
|
||||||
interface JKJavaMethod : JKMethod, JKBranchElement {
|
interface JKJavaMethod : JKMethod, JKBranchElement {
|
||||||
val block: JKBlock
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface JKJavaMethodCallExpression : JKMethodCallExpression
|
interface JKJavaMethodCallExpression : JKMethodCallExpression
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ interface JKMethod : JKDeclaration, JKModifierListOwner {
|
|||||||
val name: JKNameIdentifier
|
val name: JKNameIdentifier
|
||||||
var parameters: List<JKParameter>
|
var parameters: List<JKParameter>
|
||||||
val returnType: JKTypeElement
|
val returnType: JKTypeElement
|
||||||
|
var block: JKBlock
|
||||||
}
|
}
|
||||||
|
|
||||||
interface JKField : JKDeclaration, JKModifierListOwner {
|
interface JKField : JKDeclaration, JKModifierListOwner {
|
||||||
|
|||||||
@@ -22,13 +22,11 @@ interface JKKtProperty : JKField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface JKKtFunction : JKMethod {
|
interface JKKtFunction : JKMethod {
|
||||||
val block: JKBlock
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface JKKtConstructor : JKDeclaration, JKModifierListOwner, JKMethod, JKBranchElement {
|
interface JKKtConstructor : JKDeclaration, JKModifierListOwner, JKMethod, JKBranchElement {
|
||||||
override var name: JKNameIdentifier
|
override var name: JKNameIdentifier
|
||||||
override var parameters: List<JKParameter>
|
override var parameters: List<JKParameter>
|
||||||
var block: JKBlock
|
|
||||||
var delegationCall: JKExpression
|
var delegationCall: JKExpression
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user