Java to Kotlin converter: converting usages of Kotlin class object members
This commit is contained in:
@@ -32,6 +32,7 @@ import org.jetbrains.jet.lang.psi.JetClassOrObject
|
||||
import org.jetbrains.jet.lang.psi.psiUtil.getParentByType
|
||||
import org.jetbrains.jet.lang.psi.psiUtil.isExtensionDeclaration
|
||||
import org.jetbrains.jet.lang.psi.JetPropertyAccessor
|
||||
import com.intellij.psi.impl.light.LightField
|
||||
|
||||
open class ExpressionVisitor(protected val converter: Converter,
|
||||
private val usageReplacementMap: Map<PsiVariable, String> = mapOf()) : JavaElementVisitor() {
|
||||
@@ -292,7 +293,16 @@ open class ExpressionVisitor(protected val converter: Converter,
|
||||
else if (qualifier != null && qualifier.getType() is PsiArrayType && referencedName == "length") {
|
||||
identifier = Identifier("size", isNullable)
|
||||
}
|
||||
else if (qualifier == null) {
|
||||
else if (qualifier != null) {
|
||||
if (referencedName == "object$") {
|
||||
val target = expression.getReference()?.resolve()
|
||||
if (target is LightField) { //TODO: should be KotlinLightField with check of origin here, see KT-5188
|
||||
result = converter.convertExpression(qualifier)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
val target = expression.getReference()?.resolve()
|
||||
|
||||
if (target is PsiClass) {
|
||||
|
||||
@@ -1556,6 +1556,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("j2k/tests/testData/ast/kotlinApiAccess"), Pattern.compile("^(.+)\\.java$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("ClassObjectMembers.java")
|
||||
public void testClassObjectMembers() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/kotlinApiAccess/ClassObjectMembers.java");
|
||||
}
|
||||
|
||||
@TestMetadata("ExtensionFunction.java")
|
||||
public void testExtensionFunction() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/kotlinApiAccess/ExtensionFunction.java");
|
||||
|
||||
@@ -2,6 +2,14 @@ package kotlinApi
|
||||
|
||||
public open class KotlinClass {
|
||||
public var property: String = ""
|
||||
|
||||
class object {
|
||||
public fun statucFun(p: Int): Int = p
|
||||
public var staticVar: Int = 1
|
||||
public var staticProperty: Int
|
||||
get() = 1
|
||||
set(value) {}
|
||||
}
|
||||
}
|
||||
|
||||
public fun globalFunction(s: String): String = s
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
//file
|
||||
import kotlinApi.KotlinClass;
|
||||
|
||||
class C {
|
||||
int foo() {
|
||||
KotlinClass.staticVar = KotlinClass.staticVar * 2;
|
||||
KotlinClass.object$.setStaticProperty(KotlinClass.object$.getStaticVar() + KotlinClass.object$.getStaticProperty());
|
||||
return KotlinClass.object$.statucFun(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import kotlinApi.KotlinClass
|
||||
|
||||
class C() {
|
||||
fun foo(): Int {
|
||||
KotlinClass.staticVar = KotlinClass.staticVar * 2
|
||||
KotlinClass.staticProperty = KotlinClass.staticVar + KotlinClass.staticProperty
|
||||
return KotlinClass.statucFun(1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user