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