Kotlinx metadata, get Metadata Annotation processor


#1

I’m trying to use the kotlinx-metadata library during annotation processing. My problem is that I just can’t figure out how to get at the Metadata annotation because it is internal.
Is there some compiler flag I can use to get access to the 1.3 version of the library so I can access it?


#2

simple workaround:

val kotlinMetaClass = Class.forName("kotlin.Metadata") as Class<Annotation>
val ktMeta = element.getAnnotation(kotlinMetaClass)

#3

I still can’t access the data inside of the metadata annotation. Even if I try to get the properties using reflection I run into exceptions at runtime.


#4

So I made it work using a little jvm hack. I created “my own version” of the Metadata class. I just copied and pasted it from the kotlin sources and removed the internal keyword. I also needed to add -Xallow-kotlin-package as a compiler argument to allow creating a package named kotlin.
This hack lets you access any internal class you want as far as I know. I’m not sure whether it will lead to any problems done the line but I hope that kotlin 1.3 get’s released before I run into any problems because of this.

And it’s a hobby project anyways :blush:

Before I forget, full credit to @zcb7kbgxtgjt he mentioned this approach here first.


#5

Note that internal classes are compiled to public in bytecode, so it may be an easier hack to write a Java helper method which would see kotlin.Metadata as public.


#6

This would probably work as well, but as this is a hobby project of mine I am fine with using some less safe code and experimenting with internal classes. There are a few other internal parts of the standard library which I might experiment with and now I already have the required setup (the inline only annotation for one might be fun to play with and I don’t think there different way to apply internal annotations)