Fixed KT-5315 J2K: Generate local var when parameter is being assigned
#KT-5315 Fixed
This commit is contained in:
@@ -349,13 +349,35 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
||||
val annotations = (convertAnnotations(method) + convertThrows(method)).assignNoPrototype()
|
||||
var modifiers = convertModifiers(method)
|
||||
|
||||
val statementsToInsert = ArrayList<Statement>()
|
||||
for (parameter in method.getParameterList().getParameters()) {
|
||||
if (parameter.hasWriteAccesses(method)) {
|
||||
val variable = LocalVariable(parameter.declarationIdentifier(),
|
||||
Annotations.Empty,
|
||||
Modifiers.Empty,
|
||||
null,
|
||||
parameter.declarationIdentifier(),
|
||||
false).assignNoPrototype()
|
||||
statementsToInsert.add(DeclarationStatement(listOf(variable)).assignNoPrototype())
|
||||
}
|
||||
}
|
||||
val postProcessBody: (Block) -> Block = { body ->
|
||||
if (statementsToInsert.isEmpty()) {
|
||||
body
|
||||
}
|
||||
else {
|
||||
Block(statementsToInsert + body.statements, body.lBrace, body.rBrace).assignPrototypesFrom(body)
|
||||
}
|
||||
}
|
||||
|
||||
if (method.isConstructor()) {
|
||||
if (method.isPrimaryConstructor()) {
|
||||
return convertPrimaryConstructor(method, annotations, modifiers, membersToRemove)
|
||||
return convertPrimaryConstructor(method, annotations, modifiers, membersToRemove, postProcessBody)
|
||||
}
|
||||
else {
|
||||
val params = convertParameterList(method.getParameterList())
|
||||
return SecondaryConstructor(this, annotations, modifiers, params, convertBlock(method.getBody()))
|
||||
var body = postProcessBody(convertBlock(method.getBody()))
|
||||
return SecondaryConstructor(this, annotations, modifiers, params, body)
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -376,8 +398,8 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
||||
|
||||
var params = convertParameterList(method.getParameterList())
|
||||
val typeParameterList = convertTypeParameterList(method.getTypeParameterList())
|
||||
val block = convertBlock(method.getBody())
|
||||
return Function(this, method.declarationIdentifier(), annotations, modifiers, returnType, typeParameterList, params, block, containingClass?.isInterface() ?: false)
|
||||
var body = postProcessBody(convertBlock(method.getBody()))
|
||||
return Function(this, method.declarationIdentifier(), annotations, modifiers, returnType, typeParameterList, params, body, containingClass?.isInterface() ?: false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -419,7 +441,8 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
||||
private fun convertPrimaryConstructor(constructor: PsiMethod,
|
||||
annotations: Annotations,
|
||||
modifiers: Modifiers,
|
||||
membersToRemove: MutableSet<PsiMember>): PrimaryConstructor {
|
||||
membersToRemove: MutableSet<PsiMember>,
|
||||
postProcessBody: (Block) -> Block): PrimaryConstructor {
|
||||
val params = constructor.getParameterList().getParameters()
|
||||
val parameterToField = HashMap<PsiParameter, Pair<PsiField, Type>>()
|
||||
val body = constructor.getBody()
|
||||
@@ -451,7 +474,8 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
||||
}
|
||||
}
|
||||
|
||||
withExpressionVisitor { ExpressionVisitor(it, usageReplacementMap) }.convertBlock(body, false, { !statementsToRemove.contains(it) })
|
||||
val correctedConverter = withExpressionVisitor { ExpressionVisitor(it, usageReplacementMap) }
|
||||
postProcessBody(correctedConverter.convertBlock(body, false, { !statementsToRemove.contains(it) }))
|
||||
}
|
||||
else {
|
||||
Block.Empty
|
||||
|
||||
@@ -856,6 +856,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
|
||||
doTest("j2k/tests/testData/ast/constructors/identifier.java");
|
||||
}
|
||||
|
||||
@TestMetadata("parameterModification.java")
|
||||
public void testParameterModification() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/parameterModification.java");
|
||||
}
|
||||
|
||||
@TestMetadata("privateConstructors.java")
|
||||
public void testPrivateConstructors() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/privateConstructors.java");
|
||||
@@ -1361,6 +1366,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
|
||||
doTest("j2k/tests/testData/ast/function/ownSeveralGenericParams.java");
|
||||
}
|
||||
|
||||
@TestMetadata("parameterModification.java")
|
||||
public void testParameterModification() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/function/parameterModification.java");
|
||||
}
|
||||
|
||||
@TestMetadata("private.java")
|
||||
public void testPrivate() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/function/private.java");
|
||||
@@ -1386,11 +1396,6 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
|
||||
doTest("j2k/tests/testData/ast/function/varVararg.java");
|
||||
}
|
||||
|
||||
@TestMetadata("writableParameter.java")
|
||||
public void testWritableParameter() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/function/writableParameter.java");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("j2k/tests/testData/ast/identifier")
|
||||
|
||||
@@ -2,6 +2,7 @@ class C(p: Int) {
|
||||
private val p: Int
|
||||
|
||||
{
|
||||
var p = p
|
||||
this.p = p
|
||||
System.out.println(p++)
|
||||
System.out.println(p)
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
//file
|
||||
class C {
|
||||
private val field;
|
||||
|
||||
C(int arg1, int arg2, int arg3) {
|
||||
arg1++;
|
||||
System.out.print(arg1 + arg2);
|
||||
field = arg3;
|
||||
arg3++;
|
||||
}
|
||||
|
||||
C(int arg1, int arg2) {
|
||||
this(arg1, arg2, 0);
|
||||
arg2++;
|
||||
}
|
||||
|
||||
C(int arg1) {
|
||||
this(arg1, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public class User {
|
||||
public static void main() {
|
||||
C c1 = new C(100, 100, 100);
|
||||
C c2 = new C(100, 100);
|
||||
C c3 = new C(100);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
class C(arg1: Int, arg2: Int, arg3: Int) {
|
||||
private val field: `val`
|
||||
|
||||
{
|
||||
var arg1 = arg1
|
||||
var arg3 = arg3
|
||||
arg1++
|
||||
System.out.print(arg1 + arg2)
|
||||
field = arg3
|
||||
arg3++
|
||||
}
|
||||
|
||||
class object {
|
||||
|
||||
fun create(arg1: Int, arg2: Int): C {
|
||||
var arg2 = arg2
|
||||
val __ = C(arg1, arg2, 0)
|
||||
arg2++
|
||||
return __
|
||||
}
|
||||
|
||||
fun create(arg1: Int): C {
|
||||
val __ = C(arg1, 0, 0)
|
||||
return __
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class User() {
|
||||
class object {
|
||||
public fun main() {
|
||||
val c1 = C(100, 100, 100)
|
||||
val c2 = C.create(100, 100)
|
||||
val c3 = C.create(100)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
//method
|
||||
int foo(int p1, int p2, int p3) {
|
||||
p1++;
|
||||
if (p2 > 0) p3 = 0;
|
||||
return p1 + p2 + p3;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo(p1: Int, p2: Int, p3: Int): Int {
|
||||
var p1 = p1
|
||||
var p3 = p3
|
||||
p1++
|
||||
if (p2 > 0) p3 = 0
|
||||
return p1 + p2 + p3
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package demo
|
||||
|
||||
class Test() {
|
||||
fun test(vararg args: Any) {
|
||||
var args = args
|
||||
args = array<Int>(1, 2, 3)
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
//file
|
||||
package demo;
|
||||
|
||||
class Test {
|
||||
int test(int i) {
|
||||
i = 10;
|
||||
return i + 20;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package demo
|
||||
|
||||
class Test() {
|
||||
fun test(i: Int): Int {
|
||||
i = 10
|
||||
return i + 20
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user