Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JvmOverloads annotation for class primary constructor

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'

like image 887
Odysseus Avatar asked Mar 02 '16 13:03

Odysseus


People also ask

What is @JvmOverloads?

The @JvmOverloads annotation is a convenience feature in Kotlin for interoperating with Java code, but there is one specific use case on Android where it shouldn't be used carelessly. Let's first look at what this annotation does, and then get to the specific issue.

What is @JvmField?

@JvmField indicates weather the kotlin compiler should generate getters/setters for this property or not. If its set then it will not generate getters/setters for this property . You can omit it in this case .

What is Jvmstatic in Kotlin?

Put simply, this annotation tells the Kotlin compiler to generate one additional static method for the annotated function under the hood. Moreover, the most important use case for this annotation is, of course, better Java interoperability.


1 Answers

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 
like image 135
Vladimir Mironov Avatar answered Sep 26 '22 06:09

Vladimir Mironov