New J2K: preserve declarations order & place init sections to the end

This commit is contained in:
Ilya Kirillov
2019-05-29 15:24:21 +03:00
parent ab26061b5e
commit 95a6f59c62
29 changed files with 124 additions and 163 deletions
@@ -84,7 +84,7 @@ object ConversionsRunner {
+LiteralConversion() +LiteralConversion()
+CollectImportsConversion(context) +CollectImportsConversion(context)
+SortClassMembersConversion() +MoveInitBlocksToTheEndConversion()
+AddElementsInfoConversion(context) +AddElementsInfoConversion(context)
} }
@@ -7,12 +7,15 @@ package org.jetbrains.kotlin.nj2k.conversions
import org.jetbrains.kotlin.nj2k.tree.* import org.jetbrains.kotlin.nj2k.tree.*
//TODO temporary class MoveInitBlocksToTheEndConversion : RecursiveApplicableConversionBase() {
class SortClassMembersConversion : RecursiveApplicableConversionBase() {
override fun applyToElement(element: JKTreeElement): JKTreeElement { override fun applyToElement(element: JKTreeElement): JKTreeElement {
if (element !is JKClass) return recurse(element) if (element !is JKClassBody) return recurse(element)
element.classBody.declarations = element.declarationList element.declarations = element.declarations.sortedBy {
.sortedByDescending { it is JKVariable } when (it) {
is JKKtInitDeclaration -> 1
else -> 0
}
}
return recurse(element) return recurse(element)
} }
} }
+5 -5
View File
@@ -4,13 +4,9 @@ package test
class Test(str: String) { class Test(str: String) {
internal var myStr = "String2" internal var myStr = "String2"
init {
myStr = str
}
fun sout(str: String) { fun sout(str: String) {
// UNNECESSARY_NOT_NULL_ASSERTION heuristic does not work any more, instead we can skip generating !! altogether // UNNECESSARY_NOT_NULL_ASSERTION heuristic does not work any more, instead we can skip generating !! altogether
println(str) println(str)
} }
@@ -25,4 +21,8 @@ class Test(str: String) {
sout(dummy(test)) sout(dummy(test))
Test(test) Test(test)
} }
init {
myStr = str
}
} }
+4 -6
View File
@@ -3,11 +3,6 @@ package test
class Test(str: String?) { class Test(str: String?) {
internal var myStr: String? = "String2" internal var myStr: String? = "String2"
init {
myStr = str
}
fun sout(str: String?) { fun sout(str: String?) {
println(str) println(str)
} }
@@ -21,7 +16,10 @@ class Test(str: String?) {
val test: String = "String2" val test: String = "String2"
sout(test) sout(test)
sout(dummy(test)) sout(dummy(test))
Test(test) Test(test)
} }
init {
myStr = str
}
} }
@@ -1,10 +1,4 @@
class WithModifiersOnAccessors { class WithModifiersOnAccessors {
@get:Synchronized
@set:Synchronized
var sync = 0
@get:Strictfp
var strict = 0.0
@Synchronized @Synchronized
private fun methSync() { private fun methSync() {
} }
@@ -13,4 +7,10 @@ class WithModifiersOnAccessors {
protected fun methStrict() { protected fun methStrict() {
} }
@get:Synchronized
@set:Synchronized
var sync = 0
@get:Strictfp
var strict = 0.0
} }
+1 -2
View File
@@ -1,6 +1,5 @@
internal object Util { internal object Util {
const val CONSTANT = 10
fun util1() {} fun util1() {}
fun util2() {} fun util2() {}
const val CONSTANT = 10
} }
+1 -3
View File
@@ -1,7 +1,5 @@
internal object Util { internal object Util {
const val CONSTANT = 10
fun util1() {} fun util1() {}
fun util2() {} fun util2() {}
const val CONSTANT = 10
} }
+1 -2
View File
@@ -1,6 +1,5 @@
internal object Util { internal object Util {
const val CONSTANT = 10
fun util1() {} fun util1() {}
fun util2() {} fun util2() {}
const val CONSTANT = 10
} }
+2 -1
View File
@@ -3,16 +3,17 @@
package foo package foo
internal class A { internal class A {
private/*it's private*/ val field = 0
fun /* nothing to return */ foo(/* no parameters at all */) { fun /* nothing to return */ foo(/* no parameters at all */) {
// let declare a variable // let declare a variable
// with 2 comments before // with 2 comments before
val /*int*/ a /* it's a */ = 2 /* it's 2 */ + 1 /* it's 1 */ // variable a declared val /*int*/ a /* it's a */ = 2 /* it's 2 */ + 1 /* it's 1 */ // variable a declared
} // end of foo } // end of foo
fun /* we return int*/ foo( /*int*/ p: Int/* parameter p */): Int { /* body is empty */ fun /* we return int*/ foo( /*int*/ p: Int/* parameter p */): Int { /* body is empty */
} }
private/*it's private*/ val field = 0
/*it's public*/ fun foo(s: String?): Char {} /*it's public*/ fun foo(s: String?): Char {}
protected/*it's protected*/ fun foo(c: Char) {} protected/*it's protected*/ fun foo(c: Char) {}
/** /**
+5 -4
View File
@@ -1,5 +1,9 @@
internal class A @JvmOverloads constructor(p: Int = 1) { internal class A @JvmOverloads constructor(p: Int = 1) {
private val v: Int private val v: Int
// this is a secondary constructor 2
constructor(s: String) : this(s.length) {} // end of secondary constructor 2 body
// this is a secondary constructor 1 // this is a secondary constructor 1
@@ -10,16 +14,13 @@ internal class A @JvmOverloads constructor(p: Int = 1) {
// end of secondary constructor 1 body // end of secondary constructor 1 body
// this is a secondary constructor 2
constructor(s: String) : this(s.length) {} // end of secondary constructor 2 body
} }
internal class B // this constructor will disappear internal class B // this constructor will disappear
// end of constructor body // end of constructor body
(private val x: Int) { (private val x: Int) {
fun foo() {} fun foo() {}
} }
internal class CtorComment { internal class CtorComment {
+5 -8
View File
@@ -1,14 +1,14 @@
package org.test.customer package org.test.customer
internal class Customer(val firstName: String, val lastName: String) { internal class Customer(val firstName: String, val lastName: String) {
private fun doSmthBefore() {}
private fun doSmthAfter() {}
init { init {
doSmthBefore() doSmthBefore()
doSmthAfter() doSmthAfter()
} }
private fun doSmthBefore() {}
private fun doSmthAfter() {}
} }
internal class CustomerBuilder { internal class CustomerBuilder {
@@ -31,10 +31,7 @@ internal class CustomerBuilder {
object User { object User {
fun main() { fun main() {
val customer = CustomerBuilder() val customer = CustomerBuilder().WithFirstName("Homer").WithLastName("Simpson").Build()
.WithFirstName("Homer")
.WithLastName("Simpson")
.Build()
println(customer.firstName) println(customer.firstName)
println(customer.lastName) println(customer.lastName)
} }
+5 -5
View File
@@ -1,6 +1,11 @@
internal class C @JvmOverloads constructor(arg1: Int, arg2: Int = 0, arg3: Int = 0) { internal class C @JvmOverloads constructor(arg1: Int, arg2: Int = 0, arg3: Int = 0) {
private val field: Int private val field: Int
constructor(arg1: Int, arg2: Int) : this(arg1, arg2, 0) {
var arg2 = arg2
arg2++
}
init { init {
var arg1 = arg1 var arg1 = arg1
var arg3 = arg3 var arg3 = arg3
@@ -9,11 +14,6 @@ internal class C @JvmOverloads constructor(arg1: Int, arg2: Int = 0, arg3: Int =
field = arg3 field = arg3
arg3++ arg3++
} }
constructor(arg1: Int, arg2: Int) : this(arg1, arg2, 0) {
var arg2 = arg2
arg2++
}
} }
object User { object User {
@@ -2,11 +2,11 @@ class Test(count: Int) {
var count: Int var count: Int
private set private set
init {
this.count = count
}
fun inc() { fun inc() {
this.count++ this.count++
} }
}
init {
this.count = count
}
}
+4 -4
View File
@@ -11,10 +11,6 @@ internal class A {
var field8 = 0 var field8 = 0
private var field9: String? = "a" private var field9: String? = "a"
private var field10: String? = foo() private var field10: String? = foo()
private val anonymous: I = object : I {}
var anonymous2: I = object : I {}
private var anonymous3: I = object : I {}
private var iimpl = anonymous
fun foo(): String { fun foo(): String {
return "x" return "x"
} }
@@ -29,6 +25,10 @@ internal class A {
internal interface I internal interface I
private val anonymous: I = object : I {}
var anonymous2: I = object : I {}
private var anonymous3: I = object : I {}
private var iimpl = anonymous
fun testAnonymousObject(i: Any) { fun testAnonymousObject(i: Any) {
if (true) { if (true) {
iimpl = i as I iimpl = i as I
+3 -4
View File
@@ -8,6 +8,9 @@ internal class A(private val field6: Int, private val field8: Int, a: A) {
private val field9: Int private val field9: Int
private var field10 = 0 private var field10 = 0
private var field11 = 0 private var field11 = 0
fun foo() {
field3 = field2
}
init { init {
field7 = 10 field7 = 10
@@ -17,8 +20,4 @@ internal class A(private val field6: Int, private val field8: Int, a: A) {
} }
a.field11 = 10 a.field11 = 10
} }
fun foo() {
field3 = field2
}
} }
+1 -5
View File
@@ -1,10 +1,6 @@
internal class F { internal class F {
var i = 0
fun f1() {} fun f1() {}
fun f2() {} fun f2() {}
var i = 0
fun f3() {} fun f3() {}
} }
@@ -1,5 +1,4 @@
internal class F { internal class F {
var i = 0
//c1 //c1
@@ -13,9 +12,11 @@ internal class F {
//c4 //c4
fun f2() {}
fun f2() {}
var i = 0
fun f3() {} fun f3() {}
//c5 //c5
} }
@@ -1,31 +1,29 @@
internal class F { internal class F {
//c1
//c3 /*c2*///c3
//c4 //c4
fun f2() {} fun f2() {}
fun f3() {} fun f3() {}
fun f4() {} fun f4() {}
//c5
companion object { companion object {
//c1
/*c2*/
fun f1() {} fun f1() {}
var i = 0 var i = 0
//c5
fun f5() {} fun f5() {}
//c6
} }
//c6
} }
@@ -1,7 +1,4 @@
internal object F { internal object F {//c1
var i = 0
//c1
/*c2*/ /*c2*/
@@ -13,8 +10,9 @@ internal object F {
//c4 //c4
fun f2() {}
fun f2() {}
var i = 0
fun f3() {} fun f3() {}
//c5 //c5
+26 -22
View File
@@ -12,30 +12,11 @@ This is a block comment
internal class C { internal class C {
// This is a class comment // This is a class comment
/** /**
* This is a field doc comment. * This is a field doc comment.
*/ */
private val i: Int = 0 private val i = 0
//simple one line comment for field
var j: Int = 0
//double c style
//comment before field
var k: Int = 0
//combination
/** of
*/
//
/**
* different
*/
//comments
var l: Int = 0
/*two*/ /*comments*//*line*/
var z: Int = 0
/** /**
* This is a function doc comment. * This is a function doc comment.
@@ -47,17 +28,40 @@ internal class C {
//simple one line comment for function //simple one line comment for function
fun f1() {} fun f1() {}
//simple one line comment for field
var j = 0
//double c style //double c style
//comment before function //comment before function
fun f2() {} fun f2() {}
//double c style
//comment before field
var k = 0
//combination //combination
/** of /** of
*/ */
// //
/** /**
* different * different
*/ */
//comments //comments
fun f3() {} fun f3() {}
}
//combination
/** of
*/
//
/**
* different
*/
//comments
var l = 0
/*two*/ /*comments*//*line*/
var z = 0
}
@@ -2,11 +2,6 @@ import java.util.*
internal class A { internal class A {
private val collection: MutableCollection<String> private val collection: MutableCollection<String>
init {
collection = createCollection()
}
fun createCollection(): MutableCollection<String> { fun createCollection(): MutableCollection<String> {
return ArrayList() return ArrayList()
} }
@@ -18,4 +13,8 @@ internal class A {
fun getCollection(): Collection<String> { fun getCollection(): Collection<String> {
return collection return collection
} }
init {
collection = createCollection()
}
} }
+3 -5
View File
@@ -1,15 +1,13 @@
class Test { class Test {
private var nullableInitializerField = nullableString(3)
private val nullableInitializerFieldFinal = nullableString(3)
var nullableInitializerPublicField = nullableString(3)
fun nullableString(p: Int): String? { fun nullableString(p: Int): String? {
return if (p > 0) "response" else null return if (p > 0) "response" else null
} }
private var nullableInitializerField = nullableString(3)
private val nullableInitializerFieldFinal = nullableString(3)
var nullableInitializerPublicField = nullableString(3)
fun testProperty() { fun testProperty() {
nullableInitializerField = "aaa" nullableInitializerField = "aaa"
nullableInitializerField!![0] nullableInitializerField!![0]
nullableInitializerFieldFinal!![0] nullableInitializerFieldFinal!![0]
nullableInitializerPublicField!![0] nullableInitializerPublicField!![0]
+2 -4
View File
@@ -1,11 +1,10 @@
class TestJava { class TestJava {
var nullableInitializerFieldCast = nullableObj(3) as String?
private val nullableInitializerPrivateFieldCast = nullableObj(3) as String?
fun nullableObj(p: Int): Any? { fun nullableObj(p: Int): Any? {
return if (p > 0) "response" else null return if (p > 0) "response" else null
} }
var nullableInitializerFieldCast = nullableObj(3) as String?
private val nullableInitializerPrivateFieldCast = nullableObj(3) as String?
fun testProperty() { fun testProperty() {
nullableInitializerFieldCast!![0] nullableInitializerFieldCast!![0]
nullableInitializerPrivateFieldCast!![0] nullableInitializerPrivateFieldCast!![0]
@@ -13,7 +12,6 @@ class TestJava {
fun testLocalVariable() { fun testLocalVariable() {
val nullableInitializerValCast = nullableObj(3) as String? val nullableInitializerValCast = nullableObj(3) as String?
nullableInitializerValCast!![0] nullableInitializerValCast!![0]
} }
} }
+2 -3
View File
@@ -1,11 +1,10 @@
class Test { class Test {
private val notNullInitializerField = notNullString(3)
var notNullInitializerPublicField = notNullString(3)
fun notNullString(p: Int): String { fun notNullString(p: Int): String {
return "response" return "response"
} }
private val notNullInitializerField = notNullString(3)
var notNullInitializerPublicField = notNullString(3)
fun testProperty() { fun testProperty() {
notNullInitializerField[0] notNullInitializerField[0]
notNullInitializerPublicField[0] notNullInitializerPublicField[0]
+4 -10
View File
@@ -1,20 +1,14 @@
package test package test
open class BaseInheritorSamePackage { open class BaseInheritorSamePackage {
constructor() {}
var i = 1 protected constructor(x: Int) {}
constructor() {
}
protected constructor(x: Int) {
}
fun foo() { fun foo() {
BaseInheritorSamePackage(1) BaseInheritorSamePackage(1)
} }
var i = 1
} }
internal class DerivedInheritorSamePackage : BaseInheritorSamePackage() { internal class DerivedInheritorSamePackage : BaseInheritorSamePackage() {
+1 -5
View File
@@ -1,12 +1,8 @@
package test package test
class BaseSamePackage { class BaseSamePackage {
fun foo() {}
var i = 1 var i = 1
fun foo() {
}
} }
internal class DerivedSamePackage { internal class DerivedSamePackage {
+1 -5
View File
@@ -9,10 +9,6 @@ internal open class BaseSuperSamePackage {
} }
internal class DerivedSuperSamePackage : BaseSuperSamePackage() { internal class DerivedSuperSamePackage : BaseSuperSamePackage() {
fun foo() {}
var i = 1 var i = 1
fun foo() {
}
} }
+9 -20
View File
@@ -1,39 +1,28 @@
package test package test
class BaseProtectedConstructor { class BaseProtectedConstructor {
fun usageInConstructor() { fun usageInConstructor() {}
}
fun usageInPropertyInitializer(): Int { fun usageInPropertyInitializer(): Int {
return 1 return 1
} }
fun usageInStaticInit() { fun usageInStaticInit() {}
fun usageInMethod() {}
}
fun usageInMethod() {
}
} }
internal class DerivedSamePackage { internal class DerivedSamePackage {
private val i = BaseProtectedConstructor().usageInPropertyInitializer() private val i = BaseProtectedConstructor().usageInPropertyInitializer()
init { companion object {init {
BaseProtectedConstructor().usageInConstructor() BaseProtectedConstructor().usageInStaticInit()
} }
companion object {
init {
BaseProtectedConstructor().usageInStaticInit()
}
} }
fun usage() { fun usage() {
BaseProtectedConstructor().usageInMethod() BaseProtectedConstructor().usageInMethod()
} }
init {
BaseProtectedConstructor().usageInConstructor()
}
} }
+1 -1
View File
@@ -3,7 +3,6 @@ internal interface I {
} }
internal class C { internal class C {
var string: String? = null
val `object`: Any? val `object`: Any?
get() { get() {
foo(object : I { foo(object : I {
@@ -14,4 +13,5 @@ internal class C {
} }
fun foo(i: I) {} fun foo(i: I) {}
var string: String? = null
} }