Java to Kotlin converter: no intermediate __ variable in factory function if body is simple
This commit is contained in:
@@ -254,16 +254,23 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
||||
}
|
||||
}
|
||||
|
||||
val initializer = MethodCallExpression.buildNotNull(null, className.name, finalOrWithEmptyInitializerFields.map { initializers[it]!! })
|
||||
initializer.assignNoPrototype()
|
||||
val localVar = LocalVariable(FactoryFunction.tempValIdentifier(),
|
||||
Annotations.Empty,
|
||||
Modifiers.Empty,
|
||||
null,
|
||||
initializer,
|
||||
true).assignNoPrototype()
|
||||
statements.add(0, DeclarationStatement(listOf(localVar)).assignNoPrototype())
|
||||
statements.add(ReturnStatement(FactoryFunction.tempValIdentifier()).assignNoPrototype())
|
||||
val initializer = MethodCallExpression.buildNotNull(null,
|
||||
className.name,
|
||||
finalOrWithEmptyInitializerFields.map { initializers[it]!! }).assignNoPrototype()
|
||||
if (statements.isNotEmpty()) {
|
||||
val localVar = LocalVariable(FactoryFunction.tempValIdentifier(),
|
||||
Annotations.Empty,
|
||||
Modifiers.Empty,
|
||||
null,
|
||||
initializer,
|
||||
true).assignNoPrototype()
|
||||
statements.add(0, DeclarationStatement(listOf(localVar)).assignNoPrototype())
|
||||
statements.add(ReturnStatement(FactoryFunction.tempValIdentifier()).assignNoPrototype())
|
||||
}
|
||||
else {
|
||||
statements.add(ReturnStatement(initializer).assignNoPrototype())
|
||||
}
|
||||
|
||||
factoryFunction.body = Block(statements, LBrace().assignNoPrototype(), RBrace().assignNoPrototype()).assignNoPrototype()
|
||||
}
|
||||
|
||||
@@ -282,27 +289,36 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
||||
private fun correctFactoryFunctions(classBody: ClassBody, className: String) {
|
||||
for (factoryFunction in classBody.factoryFunctions()) {
|
||||
val body = factoryFunction.body!!
|
||||
val statements = ArrayList(body.statements)
|
||||
|
||||
// searching for other constructor call in form "this(...)"
|
||||
// it's not necessary the first statement because of statements inserted for writable parameters
|
||||
for (i in statements.indices) {
|
||||
val statement = statements[i]
|
||||
if (statement is MethodCallExpression) {
|
||||
if ((statement.methodExpression as? Identifier)?.name == "this") {
|
||||
val constructorCall = MethodCallExpression.buildNotNull(null, className, statement.arguments).assignPrototypesFrom(statement)
|
||||
val localVar = LocalVariable(FactoryFunction.tempValIdentifier(), Annotations.Empty, Modifiers.Empty, null, constructorCall, true).assignNoPrototype()
|
||||
statements[i] = DeclarationStatement(listOf(localVar)).assignNoPrototype()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
statements.add(ReturnStatement(FactoryFunction.tempValIdentifier()).assignNoPrototype())
|
||||
val statements = correctFactoryFunctionStatements(body, className)
|
||||
factoryFunction.body = Block(statements, body.lBrace, body.rBrace).assignPrototypesFrom(body)
|
||||
}
|
||||
}
|
||||
|
||||
private fun correctFactoryFunctionStatements(body: Block, className: String): List<Statement> {
|
||||
val statements = ArrayList(body.statements)
|
||||
|
||||
// searching for other constructor call in form "this(...)"
|
||||
// it's not necessary the first statement because of statements inserted for writable parameters
|
||||
for (i in statements.indices) {
|
||||
val statement = statements[i]
|
||||
if (statement is MethodCallExpression) {
|
||||
if ((statement.methodExpression as? Identifier)?.name == "this") {
|
||||
val constructorCall = MethodCallExpression.buildNotNull(null, className, statement.arguments).assignPrototypesFrom(statement)
|
||||
if (i == statements.lastIndex) { // constructor call is the last statement - no intermediate variable needed
|
||||
statements[i] = ReturnStatement(constructorCall).assignNoPrototype()
|
||||
return statements
|
||||
}
|
||||
val localVar = LocalVariable(FactoryFunction.tempValIdentifier(), Annotations.Empty, Modifiers.Empty, null, constructorCall, true).assignNoPrototype()
|
||||
statements[i] = DeclarationStatement(listOf(localVar)).assignNoPrototype()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
statements.add(ReturnStatement(FactoryFunction.tempValIdentifier()).assignNoPrototype())
|
||||
return statements
|
||||
}
|
||||
|
||||
private fun convertInitializer(initializer: PsiClassInitializer): Initializer {
|
||||
return Initializer(convertBlock(initializer.getBody()), convertModifiers(initializer)).assignPrototype(initializer)
|
||||
}
|
||||
|
||||
@@ -5,12 +5,10 @@ import java.util.HashMap
|
||||
class Test private() {
|
||||
class object {
|
||||
fun create(): Test {
|
||||
val __ = Test()
|
||||
return __
|
||||
return Test()
|
||||
}
|
||||
fun create(s: String): Test {
|
||||
val __ = Test()
|
||||
return __
|
||||
return Test()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,7 @@ class A// this is a primary constructor
|
||||
|
||||
// this is a secondary constructor
|
||||
fun create(): A {
|
||||
val __ = A(1)
|
||||
return __
|
||||
return A(1)
|
||||
} // end of secondary constructor body
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,11 @@ class C(arg1: Int, arg2: Int, arg3: Int) {
|
||||
class object {
|
||||
|
||||
fun create(arg1: Int, arg2: Int): C {
|
||||
val __ = C(arg1, arg2, 0)
|
||||
return __
|
||||
return C(arg1, arg2, 0)
|
||||
}
|
||||
|
||||
fun create(arg1: Int): C {
|
||||
val __ = C(arg1, 0, 0)
|
||||
return __
|
||||
return C(arg1, 0, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,7 @@ public class Identifier<T> private(private val myName: T, private val myHasDolla
|
||||
class object {
|
||||
|
||||
public fun <T> create(name: T): Identifier<T> {
|
||||
val __ = Identifier(name, false)
|
||||
return __
|
||||
return Identifier(name, false)
|
||||
}
|
||||
|
||||
public fun <T> create(name: T, isNullable: Boolean): Identifier<T> {
|
||||
|
||||
@@ -8,8 +8,7 @@ public class Identifier private(private val myName: String, private val myHasDol
|
||||
class object {
|
||||
|
||||
public fun create(name: String): Identifier {
|
||||
val __ = Identifier(name, false)
|
||||
return __
|
||||
return Identifier(name, false)
|
||||
}
|
||||
|
||||
public fun create(name: String, isNullable: Boolean): Identifier {
|
||||
|
||||
@@ -20,8 +20,7 @@ class C(arg1: Int, arg2: Int, arg3: Int) {
|
||||
}
|
||||
|
||||
fun create(arg1: Int): C {
|
||||
val __ = C(arg1, 0, 0)
|
||||
return __
|
||||
return C(arg1, 0, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,11 @@ class C private(arg1: Int, arg2: Int, arg3: Int) {
|
||||
class object {
|
||||
|
||||
private fun create(arg1: Int, arg2: Int): C {
|
||||
val __ = C(arg1, arg2, 0)
|
||||
return __
|
||||
return C(arg1, arg2, 0)
|
||||
}
|
||||
|
||||
public fun create(arg1: Int): C {
|
||||
val __ = C(arg1, 0, 0)
|
||||
return __
|
||||
return C(arg1, 0, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,11 @@ public class Test private(private val myName: String, var a: Boolean, var b: Dou
|
||||
class object {
|
||||
|
||||
public fun create(): Test {
|
||||
val __ = Test(0, false, 0.toDouble(), 0.toFloat(), 0, 0, 0, ' ')
|
||||
return __
|
||||
return Test(0, false, 0.toDouble(), 0.toFloat(), 0, 0, 0, ' ')
|
||||
}
|
||||
|
||||
public fun create(name: String): Test {
|
||||
val __ = Test(foo(name), false, 0.toDouble(), 0.toFloat(), 0, 0, 0, ' ')
|
||||
return __
|
||||
return Test(foo(name), false, 0.toDouble(), 0.toFloat(), 0, 0, 0, ' ')
|
||||
}
|
||||
|
||||
fun foo(n: String): String {
|
||||
|
||||
@@ -8,8 +8,7 @@ public class Identifier<T> private(private val myName: T, private val myHasDolla
|
||||
class object {
|
||||
|
||||
public fun <T> create(name: T): Identifier<T> {
|
||||
val __ = Identifier(name, false)
|
||||
return __
|
||||
return Identifier(name, false)
|
||||
}
|
||||
|
||||
public fun <T> create(name: T, isNullable: Boolean): Identifier<T> {
|
||||
|
||||
Reference in New Issue
Block a user