Search code examples
kotlinconstructorjvm-crashexpandablerecyclerview

How to change constructors JVM signature for preventing platform declaration clash in Kotlin


I am trying to make a expandable recyclerview with model data. But I am getting a signature error. Tried different solutions but didn't work.How can i set JvmName to a constructor?

Error: Platform declaration clash: The following declarations have the same JVM signature ( (ILjava/util/List;Z)V):

RowModel class:

class RowModel {
companion object{

    @IntDef(WEB_MENU, CHILD, CHILDX, CHILDXX)
    @Retention(AnnotationRetention.SOURCE)
    annotation class RowType

    const val WEB_MENU = 1
    const val CHILD = 2
    const val CHILDX = 3
    const val CHILDXX = 4

}
@RowType var type : Int
lateinit var webMenuItem : List<WebMenuItem>
lateinit var child: List<Child>
lateinit var childX: List<ChildX>
lateinit var childXX: List<ChildXX>
var isExpanded : Boolean

constructor (@RowType type : Int, webMenuItem: List<WebMenuItem>, isExpanded : Boolean = false){
    this.type = type
    this.webMenuItem = webMenuItem
    this.isExpanded = isExpanded
}


constructor(@RowType type : Int, child: List<Child>, isExpanded : Boolean = false){
    this.type = type
    this.child = child
    this.isExpanded = isExpanded
}

constructor(@RowType type : Int, childX: List<ChildX>, isExpanded : Boolean = false){
    this.type = type
    this.childX = childX
    this.isExpanded = isExpanded
}


constructor(@RowType type : Int, childXX: List<ChildXX>, isExpanded : Boolean = false){
    this.type = type
    this.childXX = childXX
    this.isExpanded = isExpanded
}

}

webMenuItem class:

class WebMenuItem {
val authFunctionTag: String = ""
val callFunctionName: Any = ""
val childs: MutableList<Child> = mutableListOf()
val id: Int = Int.MIN_VALUE
val isQueryWindow: Boolean = false
val menuIcon: String = ""
val menuName: String = ""
val queryServiceName: Any = ""
val queryTableName: Any = ""
val queryUniqColumnName: Any = ""
val queryUniqFieldName: Any = ""
val menuOrder: Int = ""
}

Child class:

class Child{
val authFunctionTag: String = ""
val callFunctionName: String = ""
var childs: List<ChildX> = mutableListOf()
val id: Int = Int.MIN_VALUE
val isQueryWindow: Boolean = false
val menuIcon: String = ""
val menuName: String = ""
val menuOrder: Int = Int.MIN_VALUE
val queryServiceName: String = ""
val queryTableName: String = ""
val queryUniqColumnName: String = ""
val queryUniqFieldName: String = ""
}

ChildX class:

class ChildX{
val authFunctionTag: String = ""
val callFunctionName: String = ""
var childs: List<ChildXX> = mutableListOf()
val id: Int = Int.MIN_VALUE
val isQueryWindow: Boolean = false
val menuIcon: String = ""
val menuName: String = ""
val menuOrder: Int = Int.MIN_VALUE
val queryServiceName: String = ""
val queryTableName: String = ""
val queryUniqColumnName: String = ""
val queryUniqFieldName: String = ""
}

ChildXX class:

class ChildXX{
val authFunctionTag: String = ""
val callFunctionName: String = ""
var childs: List<Any> = mutableListOf()
val id: Int = Int.MIN_VALUE
val isQueryWindow: Boolean = false
val menuIcon: String = ""
val menuName: String = ""
val menuOrder: Int = Int.MIN_VALUE
val queryServiceName: String = ""
val queryTableName: String = ""
val queryUniqColumnName: String = ""
val queryUniqFieldName: String = ""
}

Solution

  • Kotlin (like Java) has type erasure. The parameter with type List<Child> will be erased to List<*> after compilation, hence all your constructor overloads have the same JVM signature upon compilation.

    You can get around this with factory functions with different names either at the top-level in the file, or in the companion object, e.g. RowModel.withWebMenuItem(...) and RowModel.withChild(...) and have the actual constructor for RowModel be what they all have in common:

    constructor(@RowType type : Int, isExpanded : Boolean = false){
        this.type = type
        this.isExpanded = isExpanded
    }