Moinkhan
Moinkhan

Reputation: 12932

Kotlin generating priavate field: @MyAnnotation field must not be private or static

I am learning Kotlin.

I developed one library in Java based on annotation processing. This library works perfectly on Java. My library contains annotations which the user can put on class fields. But that field should not be private or static in order to process that field.

@MyAnnotation
private String userName;

This will throw a compile-time error suggesting the user to remove the private modifier to be processed by my library's annotation processor.

Once user removes private it's working.

Now in case of Kotlin

@MyAnnotation
var spUserName : String? = null

This is generating the private field like below.

...
private java.lang.String spUserName;
...

So my annotation processor is throwing a compile-time error.

After searching the web, adding @JvmField on that field is working perfectly like below.

@JvmField
@MyAnnotation
var spUserName : String? = null

But I don't want my users to add one more annotation above my annotation. It increasing the code if there are so many fields.

Please suggest any other alternative to avoid @JvmField.

So my question is: how can I make my field non-private in Kotlin without using @JvmField?

Upvotes: 2

Views: 1066

Answers (1)

f0f1
f0f1

Reputation: 33

@MyAnnotation
public var str: String? = null

Add public modifier

I donot know why I deleted this answer. If you visited this link you would see the default modifier for members inside class is private.

Upvotes: -1

Related Questions