New J2K: Add JavaStandardMethodsConversion
This commit is contained in:
committed by
Ilya Kirillov
parent
cd6bcb7878
commit
5cb4abf95d
+6
-3
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.kotlin.j2k.conversions
|
package org.jetbrains.kotlin.j2k.conversions
|
||||||
|
|
||||||
import org.jetbrains.kotlin.j2k.ConversionContext
|
import org.jetbrains.kotlin.j2k.ConversionContext
|
||||||
|
import org.jetbrains.kotlin.j2k.ast.Nullability
|
||||||
import org.jetbrains.kotlin.j2k.throwAnnotation
|
import org.jetbrains.kotlin.j2k.throwAnnotation
|
||||||
import org.jetbrains.kotlin.j2k.tree.*
|
import org.jetbrains.kotlin.j2k.tree.*
|
||||||
import org.jetbrains.kotlin.j2k.tree.impl.JKKtFunctionImpl
|
import org.jetbrains.kotlin.j2k.tree.impl.JKKtFunctionImpl
|
||||||
@@ -35,9 +36,11 @@ class JavaMethodToKotlinFunctionConversion(private val context: ConversionContex
|
|||||||
declaration.invalidate()
|
declaration.invalidate()
|
||||||
|
|
||||||
JKKtFunctionImpl(
|
JKKtFunctionImpl(
|
||||||
JKTypeElementImpl(
|
if (declaration.returnType.type.nullability != Nullability.NotNull)
|
||||||
declaration.returnType.type.updateNullability(declaration.returnTypeNullability(context))
|
JKTypeElementImpl(
|
||||||
),
|
declaration.returnType.type
|
||||||
|
.updateNullability(declaration.returnTypeNullability(context))
|
||||||
|
) else declaration.returnType,
|
||||||
declaration.name,
|
declaration.name,
|
||||||
declaration.parameters,
|
declaration.parameters,
|
||||||
declaration.block,
|
declaration.block,
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* 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.JKJavaMethodImpl
|
||||||
|
import org.jetbrains.kotlin.j2k.tree.impl.JKTypeElementImpl
|
||||||
|
|
||||||
|
class JavaStandartMethodsConversion : RecursiveApplicableConversionBase() {
|
||||||
|
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
||||||
|
if (element !is JKJavaMethodImpl) return recurse(element)
|
||||||
|
if (fixToStringMethod(element)) {
|
||||||
|
return recurse(element)
|
||||||
|
}
|
||||||
|
return recurse(element)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun fixToStringMethod(method: JKJavaMethodImpl): Boolean {
|
||||||
|
if (method.name.value != "toString") return false
|
||||||
|
if (method.parameters.isNotEmpty()) return false
|
||||||
|
val type = (method.returnType.type as? JKClassType)
|
||||||
|
?.takeIf { it.classReference.name == "String" }
|
||||||
|
?.updateNullability(Nullability.NotNull) ?: return false
|
||||||
|
method.returnType = JKTypeElementImpl(type)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -88,7 +88,7 @@ interface JKMethod : JKDeclaration, JKVisibilityOwner, JKModalityOwner, JKExtraM
|
|||||||
JKAnnotationListOwner {
|
JKAnnotationListOwner {
|
||||||
val name: JKNameIdentifier
|
val name: JKNameIdentifier
|
||||||
var parameters: List<JKParameter>
|
var parameters: List<JKParameter>
|
||||||
val returnType: JKTypeElement
|
var returnType: JKTypeElement
|
||||||
var block: JKBlock
|
var block: JKBlock
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user