Java to Kotlin converter: fixed bug with secondary constructor conversion
This commit is contained in:
@@ -308,19 +308,23 @@ class ExpressionVisitor(private val converter: Converter,
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun visitReferenceExpression(expression: PsiReferenceExpression) {
|
override fun visitReferenceExpression(expression: PsiReferenceExpression) {
|
||||||
|
val referencedName = expression.getReferenceName()!!
|
||||||
val target = expression.getReference()?.resolve()
|
val target = expression.getReference()?.resolve()
|
||||||
val isNullable = if (target is PsiVariable) typeConverter.variableNullability(target).isNullable(converter.settings) else false
|
val isNullable = if (target is PsiVariable) typeConverter.variableNullability(target).isNullable(converter.settings) else false
|
||||||
val referencedName = expression.getReferenceName()!!
|
|
||||||
var identifier: Expression = Identifier(referencedName, isNullable).assignNoPrototype()
|
|
||||||
val qualifier = expression.getQualifierExpression()
|
|
||||||
|
|
||||||
val containingConstructor = expression.getContainingConstructor()
|
val containingConstructor = expression.getContainingConstructor()
|
||||||
val insideSecondaryConstructor = containingConstructor != null && !containingConstructor.isPrimaryConstructor()
|
val insideSecondaryConstructor = containingConstructor != null && !containingConstructor.isPrimaryConstructor()
|
||||||
|
if (insideSecondaryConstructor &&
|
||||||
if (insideSecondaryConstructor && (expression.getReference()?.resolve() as? PsiField)?.getContainingClass() == containingConstructor!!.getContainingClass()) {
|
isQualifierEmptyOrThis(expression) &&
|
||||||
identifier = QualifiedExpression(FactoryFunction.tempValIdentifier(), Identifier(referencedName, isNullable).assignNoPrototype())
|
(expression.getReference()?.resolve() as? PsiField)?.getContainingClass() == containingConstructor!!.getContainingClass()) {
|
||||||
|
result = QualifiedExpression(FactoryFunction.tempValIdentifier(), Identifier(referencedName, isNullable).assignNoPrototype())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
else if (qualifier != null && qualifier.getType() is PsiArrayType && referencedName == "length") {
|
|
||||||
|
var identifier = Identifier(referencedName, isNullable).assignNoPrototype()
|
||||||
|
val qualifier = expression.getQualifierExpression()
|
||||||
|
|
||||||
|
if (qualifier != null && qualifier.getType() is PsiArrayType && referencedName == "length") {
|
||||||
identifier = Identifier("size", isNullable).assignNoPrototype()
|
identifier = Identifier("size", isNullable).assignNoPrototype()
|
||||||
}
|
}
|
||||||
else if (qualifier != null) {
|
else if (qualifier != null) {
|
||||||
|
|||||||
@@ -16,14 +16,17 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.j2k.test;
|
package org.jetbrains.jet.j2k.test;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
import junit.framework.TestSuite;
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import org.jetbrains.jet.JetTestUtils;
|
import org.jetbrains.jet.JetTestUtils;
|
||||||
import org.jetbrains.jet.test.InnerTestClasses;
|
import org.jetbrains.jet.test.InnerTestClasses;
|
||||||
import org.jetbrains.jet.test.TestMetadata;
|
import org.jetbrains.jet.test.TestMetadata;
|
||||||
|
|
||||||
import java.io.File;
|
import org.jetbrains.jet.j2k.test.AbstractJavaToKotlinConverterTest;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
@@ -868,6 +871,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
|
|||||||
doTest("j2k/tests/testData/ast/constructors/privateConstructors.java");
|
doTest("j2k/tests/testData/ast/constructors/privateConstructors.java");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("qualifiedRefInFactoryFun.java")
|
||||||
|
public void testQualifiedRefInFactoryFun() throws Exception {
|
||||||
|
doTest("j2k/tests/testData/ast/constructors/qualifiedRefInFactoryFun.java");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("withManyDefaultParams.java")
|
@TestMetadata("withManyDefaultParams.java")
|
||||||
public void testWithManyDefaultParams() throws Exception {
|
public void testWithManyDefaultParams() throws Exception {
|
||||||
doTest("j2k/tests/testData/ast/constructors/withManyDefaultParams.java");
|
doTest("j2k/tests/testData/ast/constructors/withManyDefaultParams.java");
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
//file
|
||||||
|
class C {
|
||||||
|
private final int arg1;
|
||||||
|
private final int arg2;
|
||||||
|
private final int arg3;
|
||||||
|
|
||||||
|
C(int arg1, int arg2, int arg3) {
|
||||||
|
this.arg1 = arg1;
|
||||||
|
this.arg2 = arg2;
|
||||||
|
this.arg3 = arg3;
|
||||||
|
}
|
||||||
|
|
||||||
|
C(int arg1, int arg2, C other) {
|
||||||
|
this(arg1, arg2, 0);
|
||||||
|
System.out.println(this.arg1 + other.arg2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class User {
|
||||||
|
void foo() {
|
||||||
|
C c1 = new C(100, 100, 100);
|
||||||
|
C c2 = new C(100, 100, c1);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
class C(private val arg1: Int, private val arg2: Int, private val arg3: Int) {
|
||||||
|
class object {
|
||||||
|
|
||||||
|
fun create(arg1: Int, arg2: Int, other: C): C {
|
||||||
|
val __ = C(arg1, arg2, 0)
|
||||||
|
System.out.println(__.arg1 + other.arg2)
|
||||||
|
return __
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class User {
|
||||||
|
fun foo() {
|
||||||
|
val c1 = C(100, 100, 100)
|
||||||
|
val c2 = C.create(100, 100, c1)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user