KT-18786: J2K should move all properties to top of class

#KT-18786 fixed
This commit is contained in:
Dimach
2017-07-14 03:01:39 +03:00
committed by Simon Ogorodnik
parent ccfcfd8721
commit a59021a25e
30 changed files with 117 additions and 103 deletions
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.j2k.append
abstract class Member(var annotations: Annotations, val modifiers: Modifiers) : Element()
class ClassBody (
class ClassBody(
val primaryConstructor: PrimaryConstructor?,
val primaryConstructorSignature: PrimaryConstructorSignature?,
val baseClassParams: List<DeferredElement<Expression>>?,
@@ -39,7 +39,7 @@ class ClassBody (
builder append " " append lBrace append "\n"
if (!classKind.isEnum()) {
builder.append(membersFiltered, "\n")
builder.append(membersFiltered.sortedWith(PropertyComparator), "\n")
}
else {
val (constants, otherMembers) = membersFiltered.partition { it is EnumConstant }
@@ -50,7 +50,7 @@ class ClassBody (
builder.append(";\n")
}
builder.append(otherMembers, "\n")
builder.append(otherMembers.sortedWith(PropertyComparator), "\n")
}
appendCompanionObject(builder, membersFiltered.isNotEmpty())
@@ -64,3 +64,12 @@ class ClassBody (
builder.append(companionObjectMembers, "\n", "companion object {\n", "\n}")
}
}
private object PropertyComparator : Comparator<Member> {
override fun compare(o1: Member?, o2: Member?): Int = when {
o1 is Property && o2 is Property -> 0
o1 is Property -> -1
o2 is Property -> 1
else -> 0
}
}
@@ -1,8 +1,8 @@
class WithModifiersOnAccessors {
@Synchronized private fun methSync() {}
@Strictfp protected fun methStrict() {}
@get:Synchronized @set:Synchronized var sync = 0
@get:Strictfp var strict = 0.0
@Synchronized private fun methSync() {}
@Strictfp protected fun methStrict() {}
}
@@ -1,8 +1,9 @@
internal object Library {
fun call() {}
val string: String
get() = ""
fun call() {}
}
internal class User {
@@ -1,10 +1,11 @@
// !forceNotNullTypes: false
// !specifyLocalVariableTypeByDefault: true
internal class Library {
fun call() {}
val string: String
get() = ""
fun call() {}
}
internal class User {
@@ -1,8 +1,9 @@
internal class Library {
fun call() {}
val string: String
get() = ""
fun call() {}
}
internal class User {
+2 -2
View File
@@ -1,6 +1,6 @@
internal object Util {
fun util1() {}
fun util2() {}
val CONSTANT = 10
fun util1() {}
fun util2() {}
}
+3 -3
View File
@@ -1,7 +1,7 @@
internal object Util {
val CONSTANT = 10
fun util1() {}
fun util2() {}
val CONSTANT = 10
}
}
+3 -3
View File
@@ -1,6 +1,6 @@
internal object Util {
fun util1() {}
fun util2() {}
val CONSTANT = 10
}
fun util1() {}
fun util2() {}
}
+2 -2
View File
@@ -3,6 +3,8 @@
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
@@ -12,8 +14,6 @@ internal class A {
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) {}
+4 -4
View File
@@ -3,11 +3,11 @@ internal interface I {
var something2: Int
fun setSomething3(value: Int)
val something4: Int
fun setSomething4(value: String)
val something5: Int
fun setSomething3(value: Int)
fun setSomething4(value: String)
fun setSomething5(value: Int): Int
}
}
+9 -9
View File
@@ -15,9 +15,9 @@ internal interface I {
}
internal interface I1 : I {
fun setSomething1(value: Int)
val something6: Int
fun setSomething1(value: Int)
}
internal open class B {
@@ -59,14 +59,6 @@ internal abstract class C(override val something1: Int) : B(), I {
}
fun getSomething6(): Int {
return mySomething6
}
override fun setSomething6(value: Int) {
mySomething6 = value
}
override val fromB1: String
get() = super.fromB1
@@ -91,6 +83,14 @@ internal abstract class C(override val something1: Int) : B(), I {
val fromB5: String
get() = ""
fun getSomething6(): Int {
return mySomething6
}
override fun setSomething6(value: Int) {
mySomething6 = value
}
override fun setFromB5(value: String) {
super.setFromB5(value)
}
@@ -4,6 +4,8 @@ internal class A {
private val list1 = ArrayList<String>()
private val list2 = ArrayList<String>()
private val myList3 = ArrayList<String>()
val list3: List<String>
get() = myList3
fun getList1(): List<String> {
return list1
@@ -13,9 +15,6 @@ internal class A {
return list2
}
val list3: List<String>
get() = myList3
fun foo() {
list1.add("a")
list2.add("a")
+14 -14
View File
@@ -15,20 +15,6 @@ internal class A {
private var field9: String? = "a"
private var field10: String? = foo()
fun foo(): String {
return "x"
}
fun bar() {
field5 = ArrayList()
field7++
field8++
field9 = null
field10 = null
}
internal interface I
private val anonymous: I = object : I {
}
@@ -43,6 +29,20 @@ internal class A {
private var iimpl = anonymous
fun foo(): String {
return "x"
}
fun bar() {
field5 = ArrayList()
field7++
field8++
field9 = null
field10 = null
}
internal interface I
fun testAnonymousObject(i: Any) {
if (true) {
iimpl = i as I
+1 -1
View File
@@ -1,10 +1,10 @@
internal class F {
var i: Int? = 0
fun f1() {}
fun f2() {}
var i: Int? = 0
fun f3() {}
}
@@ -1,4 +1,5 @@
internal class F {
var i: Int? = 0
//c1
@@ -13,7 +14,6 @@ internal class F {
//c4
fun f2() {}
var i: Int? = 0
fun f3() {}
@@ -1,4 +1,5 @@
internal object F {
var i: Int? = 0
//c1
@@ -13,7 +14,6 @@ internal object F {
//c4
fun f2() {}
var i: Int? = 0
fun f3() {}
+2 -1
View File
@@ -1,7 +1,8 @@
class Foo {
private external fun nativeMethod()
var bar: Int
external get
external set
private external fun nativeMethod()
}
+20 -20
View File
@@ -17,23 +17,9 @@ internal class C {
*/
private val i: Int = 0
/**
* This is a function doc comment.
*/
fun foo() {
/* This is a function comment */
}
//simple one line comment for function
fun f1() {}
//simple one line comment for field
var j: Int = 0
//double c style
//comment before function
fun f2() {}
//double c style
//comment before field
var k: Int = 0
@@ -46,7 +32,24 @@ internal class C {
* different
*/
//comments
fun f3() {}
var l: Int = 0
/*two*/ /*comments*//*line*/
var z: Int = 0
/**
* This is a function doc comment.
*/
fun foo() {
/* This is a function comment */
}
//simple one line comment for function
fun f1() {}
//double c style
//comment before function
fun f2() {}
//combination
/** of
@@ -56,8 +59,5 @@ internal class C {
* different
*/
//comments
var l: Int = 0
/*two*/ /*comments*//*line*/
var z: Int = 0
}
fun f3() {}
}
+2 -3
View File
@@ -1,10 +1,9 @@
// ERROR: Variable cannot be initialized before declaration
package demo
internal class C(a: Int) {
var abc = 0
init {
abc = a * 2
}
var abc = 0
}
@@ -1,11 +1,11 @@
class Test {
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 nullableString(p: Int): String? {
return if (p > 0) "response" else null
}
fun testProperty() {
nullableInitializerField = "aaa"
@@ -1,11 +1,11 @@
// ERROR: Type mismatch: inferred type is String? but String was expected
class TestJava {
fun nullableObj(p: Int): Any? {
return if (p > 0) "response" else null
}
var nullableInitializerFieldCast: String = nullableObj(3) as String?
private val nullableInitializerPrivateFieldCast = nullableObj(3) as String?
fun nullableObj(p: Int): Any? {
return if (p > 0) "response" else null
}
fun testProperty() {
nullableInitializerFieldCast[0]
@@ -1,10 +1,10 @@
class Test {
fun notNullString(p: Int): String {
return "response"
}
private val notNullInitializerField = notNullString(3)
var notNullInitializerPublicField = notNullString(3)
fun notNullString(p: Int): String {
return "response"
}
fun testProperty() {
notNullInitializerField[0]
@@ -1,6 +1,9 @@
package test
open class BaseInheritorSamePackage {
var i = 1
constructor() {
}
@@ -12,8 +15,6 @@ open class BaseInheritorSamePackage {
fun foo() {
BaseInheritorSamePackage(1)
}
var i = 1
}
internal class DerivedInheritorSamePackage : BaseInheritorSamePackage() {
@@ -2,11 +2,11 @@ package test
class BaseSamePackage {
var i = 1
fun foo() {
}
var i = 1
}
internal class DerivedSamePackage {
+2 -2
View File
@@ -10,9 +10,9 @@ internal open class BaseSuperSamePackage {
internal class DerivedSuperSamePackage : BaseSuperSamePackage() {
var i = 1
fun foo() {
}
var i = 1
}
+3 -2
View File
@@ -19,12 +19,13 @@ class BaseProtectedConstructor {
}
internal class DerivedSamePackage {
private val i = BaseProtectedConstructor().usageInPropertyInitializer()
init {
BaseProtectedConstructor().usageInConstructor()
}
private val i = BaseProtectedConstructor().usageInPropertyInitializer()
fun usage() {
BaseProtectedConstructor().usageInMethod()
}
@@ -12,7 +12,7 @@ internal class C {
return string
}
fun foo(i: I) {}
var string: String? = null
fun foo(i: I) {}
}
+5 -4
View File
@@ -1,9 +1,10 @@
class B {
var yy = ""
private set
internal fun foo(a: AAA) {
a.x = a.x + 1
yy += "a"
}
var yy = ""
private set
}
}
@@ -2,9 +2,9 @@ package test
open class BaseOtherPackage protected constructor() {
protected var i = 1
protected fun foo() {
}
protected var i = 1
}
+3 -3
View File
@@ -1,10 +1,10 @@
package test
internal object Utils {
val staticField = 1
fun foo1(p: Int) {}
fun foo2(): Int {
return 1
}
val staticField = 1
}
}