首页 > 解决方案 > 如何更改构造函数 JVM 签名以防止 Kotlin 中的平台声明冲突

问题描述

我正在尝试使用模型数据制作可扩展的回收器视图。但我收到签名错误。尝试了不同的解决方案但没有奏效。如何将 JvmName 设置为构造函数?

错误: 平台声明冲突:以下声明具有相同的 JVM 签名 ((ILjava/util/List;Z)V):

行模型类:

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 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 = ""
}

儿童班:

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 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 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 = ""
}

标签: kotlinconstructorjvm-crashexpandablerecyclerview

解决方案


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

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

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

推荐阅读