New J2K: Main function conversion

This commit is contained in:
Ilya Kirillov
2018-11-14 17:15:25 +03:00
committed by Ilya Kirillov
parent 2f1a8831b2
commit 28ef4c5dfa
2 changed files with 45 additions and 0 deletions
@@ -44,6 +44,7 @@ object ConversionsRunner {
+InsertDefaultPrimaryConstructorConversion(context)
+FieldInitializersInPrimaryFromParamsConversion(context)
+JavaMethodToKotlinFunctionConversion()
+MainFunctionConversion()
+LiteralConversion()
+InnerClassConversion()
+ModifiersConversion(context)
@@ -0,0 +1,44 @@
/*
* 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.ast.Nullability
import org.jetbrains.kotlin.j2k.tree.*
import org.jetbrains.kotlin.j2k.tree.impl.JKAnnotationImpl
import org.jetbrains.kotlin.j2k.tree.impl.JKClassTypeImpl
import org.jetbrains.kotlin.j2k.tree.impl.JKNameIdentifierImpl
import org.jetbrains.kotlin.j2k.tree.impl.JKTypeElementImpl
//TODO temporary
class MainFunctionConversion : RecursiveApplicableConversionBase() {
override fun applyToElement(element: JKTreeElement): JKTreeElement {
if (element !is JKKtFunction) return recurse(element)
if (element.isMainFunctionDeclaration()) {
element.parameters.single().apply {
val oldType = type.type as JKClassType
val oldTypeParameter = oldType.parameters.single() as JKClassType
val newType =
JKClassTypeImpl(
oldType.classReference,
listOf(oldTypeParameter.updateNullability(Nullability.NotNull)),
Nullability.NotNull
)
type = JKTypeElementImpl(newType)
}
element.annotationList.annotations += JKAnnotationImpl(JKNameIdentifierImpl("JvmStatic"))
}
return recurse(element)
}
fun JKKtFunction.isMainFunctionDeclaration(): Boolean {
val type = parameters.singleOrNull()?.type?.type as? JKClassType ?: return false
val typeArgument = type.parameters.singleOrNull() as? JKClassType ?: return false
return name.value == "main" &&
type.classReference.name == "Array" &&
typeArgument.classReference.name == "String"
}
}