Fixed KT-5204 Converter from java could generate var's for locals when needed
#KT-5204 Fixed
This commit is contained in:
@@ -27,6 +27,7 @@ import org.jetbrains.jet.lang.diagnostics.Diagnostic
|
|||||||
import org.jetbrains.jet.lang.diagnostics.Errors
|
import org.jetbrains.jet.lang.diagnostics.Errors
|
||||||
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
|
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
|
||||||
import org.jetbrains.jet.lang.psi.JetUnaryExpression
|
import org.jetbrains.jet.lang.psi.JetUnaryExpression
|
||||||
|
import org.jetbrains.jet.lang.psi.JetProperty
|
||||||
|
|
||||||
class AfterConversionPass(val project: Project) {
|
class AfterConversionPass(val project: Project) {
|
||||||
public fun run(kotlinCode: String): String {
|
public fun run(kotlinCode: String): String {
|
||||||
@@ -58,13 +59,21 @@ class AfterConversionPass(val project: Project) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun fixForProblem(problem: Diagnostic): (() -> Unit)? {
|
private fun fixForProblem(problem: Diagnostic): (() -> Unit)? {
|
||||||
|
val psiElement = problem.getPsiElement()
|
||||||
return when (problem.getFactory()) {
|
return when (problem.getFactory()) {
|
||||||
Errors.UNNECESSARY_NOT_NULL_ASSERTION -> { () ->
|
Errors.UNNECESSARY_NOT_NULL_ASSERTION -> { () ->
|
||||||
val exclExclOp = problem.getPsiElement() as JetSimpleNameExpression
|
val exclExclOp = psiElement as JetSimpleNameExpression
|
||||||
val exclExclExpr = exclExclOp.getParent() as JetUnaryExpression
|
val exclExclExpr = exclExclOp.getParent() as JetUnaryExpression
|
||||||
exclExclExpr.replace(exclExclExpr.getBaseExpression()!!)
|
exclExclExpr.replace(exclExclExpr.getBaseExpression()!!)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Errors.VAL_REASSIGNMENT -> { () ->
|
||||||
|
val property = (psiElement as? JetSimpleNameExpression)?.getReference()?.resolve() as? JetProperty
|
||||||
|
if (property != null && !property.isVar()) {
|
||||||
|
property.getValOrVarNode().getPsi()!!.replace(JetPsiFactory.createVarNode(project).getPsi()!!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2037,6 +2037,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
|
|||||||
doTest("j2k/tests/testData/ast/localVariable/varTypeDoNotMatch.java");
|
doTest("j2k/tests/testData/ast/localVariable/varTypeDoNotMatch.java");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("varWithNoInitializer.java")
|
||||||
|
public void testVarWithNoInitializer() throws Exception {
|
||||||
|
doTest("j2k/tests/testData/ast/localVariable/varWithNoInitializer.java");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("j2k/tests/testData/ast/methodCallExpression")
|
@TestMetadata("j2k/tests/testData/ast/methodCallExpression")
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
//file
|
||||||
|
class C {
|
||||||
|
int foo(boolean p) {
|
||||||
|
int a;
|
||||||
|
int b;
|
||||||
|
a = 10;
|
||||||
|
b = 5;
|
||||||
|
if (p) a = 5; else b = 10;
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
class C {
|
||||||
|
fun foo(p: Boolean): Int {
|
||||||
|
var a: Int
|
||||||
|
var b: Int
|
||||||
|
a = 10
|
||||||
|
b = 5
|
||||||
|
if (p) a = 5 else b = 10
|
||||||
|
return a + b
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user