prom85
prom85

Reputation: 17818

How to get the default value of an annotated variable

I have an annotation like following:

@Arg
internal var stringArg1: String? = null
@Arg
internal var stringArg2: String = "default value"

And I iterate over all of my annotated classes variables like following:

for (e in annotatedElement.enclosedElements) {
    if (e.getAnnotation(Arg::class.java) != null) {
        val defaultValue = ??? 
    }
}

Question:

Is it possible to get the default value of an annotated variable? In my example I want to retrieve null and "default value" for my two variables.

Upvotes: 1

Views: 790

Answers (1)

Bartek Lipinski
Bartek Lipinski

Reputation: 31438

No, it's not possible.

Default values would have to be stored in Kotlin metadata for you to access them in your annotation processor (and they are not stored there).

You can see some details about that in the Kotlin repository:

docs

Upvotes: 1

Related Questions