JvmOverloads annotation for class primary construc

2019-02-16 05:36发布

问题:

Why is it prohibited to autogenerate many constructors visible to Java from class primary constructor with default params likes this?

@JvmOverloads
class Video(private val id: Long, val ownerId: Long, var title: String? = null, var imgLink: String? = null, var videoLink: String? = null,
        var description: String? = null, var created: Date? = null, var accessKey: String? = null, var duration: Long? = null,
        var views: Long? = null, var comments: Long? = null) : Entity

This annotation is not applicable to target 'class'

回答1:

It's not prohibited, you are just applying @JvmOverloads to the wrong target. The proper way to annotate primary constructor is to explicitly specify constructor keyword with @JvmOverloads before:

class Video @JvmOverloads constructor(
    private val id: Long,
    val ownerId: Long,
    var title: String? = null,
    var imgLink: String? = null,
    var videoLink: String? = null,
    var description: String? = null,
    var created: Date? = null,
    var accessKey: String? = null,
    var duration: Long? = null,
    var views: Long? = null,
    var comments: Long? = null
) : Entity