New J2K: correctly convert main method with varargs argument
#KT-33756 fixed
This commit is contained in:
@@ -7,39 +7,36 @@ package org.jetbrains.kotlin.nj2k.conversions
|
||||
|
||||
import org.jetbrains.kotlin.j2k.ast.Nullability
|
||||
import org.jetbrains.kotlin.nj2k.NewJ2kConverterContext
|
||||
import org.jetbrains.kotlin.nj2k.tree.*
|
||||
import org.jetbrains.kotlin.nj2k.types.JKClassType
|
||||
import org.jetbrains.kotlin.nj2k.jvmAnnotation
|
||||
import org.jetbrains.kotlin.nj2k.tree.JKMethod
|
||||
import org.jetbrains.kotlin.nj2k.tree.JKTreeElement
|
||||
import org.jetbrains.kotlin.nj2k.tree.OtherModifier
|
||||
import org.jetbrains.kotlin.nj2k.tree.hasOtherModifier
|
||||
import org.jetbrains.kotlin.nj2k.types.JKJavaArrayType
|
||||
import org.jetbrains.kotlin.nj2k.types.updateNullability
|
||||
import org.jetbrains.kotlin.nj2k.types.arrayInnerType
|
||||
import org.jetbrains.kotlin.nj2k.types.isStringType
|
||||
|
||||
|
||||
//TODO temporary
|
||||
class MainFunctionConversion(context: NewJ2kConverterContext) : RecursiveApplicableConversionBase(context) {
|
||||
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
||||
if (element !is JKMethod) return recurse(element)
|
||||
if (element.isMainFunctionDeclaration()) {
|
||||
element.parameters.single().apply {
|
||||
val oldType = type.type as JKJavaArrayType
|
||||
val oldTypeParameter = oldType.type as JKClassType
|
||||
val newType =
|
||||
JKJavaArrayType(
|
||||
oldTypeParameter.updateNullability(Nullability.NotNull),
|
||||
Nullability.NotNull
|
||||
)
|
||||
type = JKTypeElement(newType)
|
||||
type.type = JKJavaArrayType(typeFactory.types.string, Nullability.NotNull)
|
||||
isVarArgs = false
|
||||
}
|
||||
element.annotationList.annotations +=
|
||||
JKAnnotation(
|
||||
symbolProvider.provideClassSymbol("kotlin.jvm.JvmStatic"),
|
||||
emptyList()
|
||||
)
|
||||
element.annotationList.annotations += jvmAnnotation("JvmStatic", symbolProvider)
|
||||
}
|
||||
return recurse(element)
|
||||
}
|
||||
|
||||
private fun JKMethod.isMainFunctionDeclaration(): Boolean {
|
||||
val type = parameters.singleOrNull()?.type?.type as? JKJavaArrayType ?: return false
|
||||
val typeArgument = type.type as? JKClassType ?: return false
|
||||
return name.value == "main" && typeArgument.classReference.name == "String"
|
||||
if (name.value != "main") return false
|
||||
if (!hasOtherModifier(OtherModifier.STATIC)) return false
|
||||
val parameter = parameters.singleOrNull() ?: return false
|
||||
return when {
|
||||
parameter.type.type.arrayInnerType()?.isStringType() == true -> true
|
||||
parameter.isVarArgs && parameter.type.type.isStringType() -> true
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
public class Main {
|
||||
public static void main(String... args) {
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
object Main {
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
}
|
||||
}
|
||||
+3
-15
@@ -1,19 +1,7 @@
|
||||
object ArrayInitializerBugKt {
|
||||
private val GREETING = byteArrayOf(
|
||||
'H'.toByte(),
|
||||
'e'.toByte(),
|
||||
'l'.toByte(),
|
||||
'l'.toByte(),
|
||||
'o'.toByte(),
|
||||
','.toByte(),
|
||||
' '.toByte(),
|
||||
'b'.toByte(),
|
||||
'u'.toByte(),
|
||||
'g'.toByte(),
|
||||
'!'.toByte()
|
||||
)
|
||||
|
||||
fun main(vararg args: String?) {
|
||||
private val GREETING = byteArrayOf('H'.toByte(), 'e'.toByte(), 'l'.toByte(), 'l'.toByte(), 'o'.toByte(), ','.toByte(), ' '.toByte(), 'b'.toByte(), 'u'.toByte(), 'g'.toByte(), '!'.toByte())
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
val greeting = String(GREETING)
|
||||
println(greeting)
|
||||
}
|
||||
|
||||
+5
@@ -2388,6 +2388,11 @@ public class NewJavaToKotlinConverterSingleFileTestGenerated extends AbstractNew
|
||||
runTest("nj2k/testData/newJ2k/function/mainAndNullabilitySetting.java");
|
||||
}
|
||||
|
||||
@TestMetadata("mainVararg.java")
|
||||
public void testMainVararg() throws Exception {
|
||||
runTest("nj2k/testData/newJ2k/function/mainVararg.java");
|
||||
}
|
||||
|
||||
@TestMetadata("methodClassType.java")
|
||||
public void testMethodClassType() throws Exception {
|
||||
runTest("nj2k/testData/newJ2k/function/methodClassType.java");
|
||||
|
||||
Reference in New Issue
Block a user