https://kotlinlang.org logo
Title
j

jameskleeh

10/13/2021, 6:48 PM
Is it possible to determine if an annotation value is present because it was the default or if it was set manually?
g

Grégory Lureau

10/13/2021, 7:54 PM
How is it possible to set an annotation by "default"? What does that mean?
j

Jiaxiang

10/13/2021, 8:03 PM
I think it means when you define an annotation class, you can specify a default value for the annotation value arguments.
👍 1
j

jameskleeh

10/13/2021, 8:13 PM
Correct
In Micronaut we support the notion of an argument that is actually set vs a default value
I’ve determined the likely way to determine this is through the constructor of the annotation, however I don’t know how to get the default values from the parameters. Any ideas @Jiaxiang?
j

Jiaxiang

10/13/2021, 8:15 PM
I can think of it, but what is the usage of that? I feel like you have all you need with getting the value itself, it shouldn’t matter whether it comes from a default value or defined in the code?
j

jameskleeh

10/13/2021, 8:16 PM
its more efficient to store the default values from annotations once instead of each time they are used
so its split out that way to reduce memory usage
Micronaut stores annotation data in our own API that is accessible at runtime so no runtime reflection is necessary to retrieve that info
j

Jiaxiang

10/13/2021, 8:20 PM
to store it I will need a flag in KSP to determine if a value is present in code, or alternatively do a computation every time user query it, either sacrifices space or time efficiency in my opinion.
also we don’t actually do runtime reflection to get the annotation value, if reflection is your concern.
j

jameskleeh

10/13/2021, 8:22 PM
reflection is a concern for us at runtime, not compile time
So are you saying that it isn’t possible with the current version of KSP?
j

Jiaxiang

10/13/2021, 8:24 PM
it isn’t possible in current version, I can see it is doable but need more context on the necessity of this use case.
j

jameskleeh

10/13/2021, 8:24 PM
A boolean on KSValueArgument
isDefaultValue
or something of the sort would be sufficient I believe
perhaps not actually
j

Jiaxiang

10/13/2021, 8:25 PM
I would call it
isPresent
since user can still write a value equal to default value, and I think you are more interested in if user has written the value or not, rather than the actual value
j

jameskleeh

10/13/2021, 8:25 PM
I’ll create an issue
👍 1