https://kotlinlang.org logo
#reflect
Title
# reflect
t

Timo Gruen

12/28/2020, 3:28 PM
Hi Folks, i currently struggle with Kotlin Annotations and the Annotation Processor. I brought up a custom Annotation
Property
Copy code
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.PROPERTY_GETTER)
@Retention(AnnotationRetention.RUNTIME)
annotation class Property
When using the annotation on a property e.g.:
Copy code
data class Example(@Property val key: String)
and introspecting the class with a AnnotationProcessor i can find three different elements which are related to that
key
property. The first one is
key
, the second one is
getKey
and the third (which is currently probably the reason for my confusion) is the
getKey$annotations()
one, which is the only element having the annotation from java annotation processor perspective. I would have assumed that either
key
or
getKey
would also be annotated on byte-code level. Any guidance on how i can access the element
getKey
OR
key
using the annotation processor api? My actual goal is to retrieve the name
key
as well as the type
String
of the annotated element
tl;tr: Have found the issue (but found another one). If the
@target
is only
AnnotationTarget.Field
it just works fine - but then i’m having issues with the Kotlin Reflection not having the annotation at runtime. (Even if the
AnnotationRetention
is
Runtime
) Any advice here?
m

Marc Knaup

12/29/2020, 3:55 PM
It’s safer to use Kotlin Metadata to inspect Kotlin types as it contains more information. Check out my library https://github.com/fluidsonic/fluid-meta which helps with that, using
Meta.of(yourElement)
. Here I use it in my JSON annotation processor: https://github.com/fluidsonic/fluid-json/blob/accd358687a1247785366625341eeb7ca6be0b99/annotation-processor/sources/jvm-jdk8/collection/CollectionPhase.kt?ts=4#L61
Using Metadata you can make such links:
t

Timo Gruen

12/29/2020, 4:13 PM
Actually pretty nice library. Thanks @Marc Knaup - gonna give it a try for some other modules I have to write in the next few days But for the fix I rather used a mapping for all
xy$annotations()
to
xy
. Enables me to be compliant with java and kotlin and not just kotlin.
m

Marc Knaup

12/29/2020, 4:23 PM
What do you mean by “with Java”? There is no
$annotations
hack in Java 🤔
t

Timo Gruen

12/29/2020, 4:28 PM
That’s true actually - but currently i’m using the same AnnotationProcessor for both - java and kotlin. The
$annotations
are only present if it’s processing a kotlin file but still, i can use the entire code for both (as long as i’m sanitizing that
$annotations
thingy when approaching kotlin).
👌 1
btw. you got any reference what that
$annotations?
actually is? Haven’t found any documentation
m

Marc Knaup

12/29/2020, 4:31 PM
If I recall correctly it’s because Java doesn’t have properties. Kotlin puts field annotation on the JVM field. Kotlin puts getter annotation on the JVM getter. Kotlin puts property annotation on a synthetic JVM getter.
t

Timo Gruen

12/29/2020, 4:34 PM
Oh ok. So it’s just synthetic to have a dedicated method to put the property annotations to instead of putting them directly on the field (backed by the getter or similar). Thanks!
5 Views