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.j2k.ast.Nullability
|
||||||
import org.jetbrains.kotlin.nj2k.NewJ2kConverterContext
|
import org.jetbrains.kotlin.nj2k.NewJ2kConverterContext
|
||||||
import org.jetbrains.kotlin.nj2k.tree.*
|
import org.jetbrains.kotlin.nj2k.jvmAnnotation
|
||||||
import org.jetbrains.kotlin.nj2k.types.JKClassType
|
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.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) {
|
class MainFunctionConversion(context: NewJ2kConverterContext) : RecursiveApplicableConversionBase(context) {
|
||||||
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
||||||
if (element !is JKMethod) return recurse(element)
|
if (element !is JKMethod) return recurse(element)
|
||||||
if (element.isMainFunctionDeclaration()) {
|
if (element.isMainFunctionDeclaration()) {
|
||||||
element.parameters.single().apply {
|
element.parameters.single().apply {
|
||||||
val oldType = type.type as JKJavaArrayType
|
type.type = JKJavaArrayType(typeFactory.types.string, Nullability.NotNull)
|
||||||
val oldTypeParameter = oldType.type as JKClassType
|
isVarArgs = false
|
||||||
val newType =
|
|
||||||
JKJavaArrayType(
|
|
||||||
oldTypeParameter.updateNullability(Nullability.NotNull),
|
|
||||||
Nullability.NotNull
|
|
||||||
)
|
|
||||||
type = JKTypeElement(newType)
|
|
||||||
}
|
}
|
||||||
element.annotationList.annotations +=
|
element.annotationList.annotations += jvmAnnotation("JvmStatic", symbolProvider)
|
||||||
JKAnnotation(
|
|
||||||
symbolProvider.provideClassSymbol("kotlin.jvm.JvmStatic"),
|
|
||||||
emptyList()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
return recurse(element)
|
return recurse(element)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun JKMethod.isMainFunctionDeclaration(): Boolean {
|
private fun JKMethod.isMainFunctionDeclaration(): Boolean {
|
||||||
val type = parameters.singleOrNull()?.type?.type as? JKJavaArrayType ?: return false
|
if (name.value != "main") return false
|
||||||
val typeArgument = type.type as? JKClassType ?: return false
|
if (!hasOtherModifier(OtherModifier.STATIC)) return false
|
||||||
return name.value == "main" && typeArgument.classReference.name == "String"
|
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 {
|
object ArrayInitializerBugKt {
|
||||||
private val GREETING = byteArrayOf(
|
private val GREETING = byteArrayOf('H'.toByte(), 'e'.toByte(), 'l'.toByte(), 'l'.toByte(), 'o'.toByte(), ','.toByte(), ' '.toByte(), 'b'.toByte(), 'u'.toByte(), 'g'.toByte(), '!'.toByte())
|
||||||
'H'.toByte(),
|
@JvmStatic
|
||||||
'e'.toByte(),
|
fun main(args: Array<String>) {
|
||||||
'l'.toByte(),
|
|
||||||
'l'.toByte(),
|
|
||||||
'o'.toByte(),
|
|
||||||
','.toByte(),
|
|
||||||
' '.toByte(),
|
|
||||||
'b'.toByte(),
|
|
||||||
'u'.toByte(),
|
|
||||||
'g'.toByte(),
|
|
||||||
'!'.toByte()
|
|
||||||
)
|
|
||||||
|
|
||||||
fun main(vararg args: String?) {
|
|
||||||
val greeting = String(GREETING)
|
val greeting = String(GREETING)
|
||||||
println(greeting)
|
println(greeting)
|
||||||
}
|
}
|
||||||
|
|||||||
+5
@@ -2388,6 +2388,11 @@ public class NewJavaToKotlinConverterSingleFileTestGenerated extends AbstractNew
|
|||||||
runTest("nj2k/testData/newJ2k/function/mainAndNullabilitySetting.java");
|
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")
|
@TestMetadata("methodClassType.java")
|
||||||
public void testMethodClassType() throws Exception {
|
public void testMethodClassType() throws Exception {
|
||||||
runTest("nj2k/testData/newJ2k/function/methodClassType.java");
|
runTest("nj2k/testData/newJ2k/function/methodClassType.java");
|
||||||
|
|||||||
Reference in New Issue
Block a user