如何更改构造函数 JVM 签名以防止 Kotlin 中的平台声明冲突

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

我正在尝试使用模型数据制作一个可扩展的回收视图。但是我收到签名错误。尝试了不同的解决方案但没有 work.How 我可以将 JvmName 设置为构造函数吗?

错误: 平台声明冲突:以下声明具有相同的 JVM 签名 ((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 = ""
}

Kotlin(如 Java)有 type erasureList<Child> 类型的参数在编译后将被擦除为 List<*>,因此您的所有构造函数重载在编译时都具有相同的 JVM 签名。

您可以在文件的顶层或伴随对象中使用不同名称的工厂函数来解决这个问题,例如RowModel.withWebMenuItem(...)RowModel.withChild(...) 以及 RowModel 的实际构造函数是它们的共同点:

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