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

juancho

10/28/2018, 6:36 PM
Hi everyone, not sure if this was the right channel but I have a question regarding Kotlin Reflection. I posted the question in Stackoverflow: https://stackoverflow.com/questions/53034830/kotlin-reflection-how-to-know-if-a-kotlin-class-is-marked-with-internal-modif The question: I’m autogenerating code with KotlinPoet and Auto Service. I didn’t find any way to know if an annotated class has “internal” modifier so I can create another class with same modifier. For example:
Copy code
@MyAnnotation
internal class Car
So I thought using Kotlin Reflection I would be able to get this information but no luck. Any clues on how to do it?
d

diesieben07

10/29/2018, 12:05 PM
Read the
@Metadata
annotation from whatever source you have for your kotlin class. Once you have that, you can use kotlinx-metadata-jvm (https://github.com/JetBrains/kotlin/tree/master/libraries/kotlinx-metadata/jvm) to parse this metadata. Where the metadata comes from does not matter. It can be from an annotation read at runtime or in an annotation processor.
👍 2
j

juancho

10/29/2018, 4:47 PM
Thanks man! It’s working!!! 👏
my only concern is how I’m parsing the metadata to create the KotlinClassHeader 🤔 . I’m manually parsing metadata to the params requested by KotlinClassHeader
Copy code
values.forEach { value ->
                when(value.fst.name.toString()) {
                    "mv" -> metadataVersion = (value.snd.value as List<Attribute.Constant>).map { it.value.toString().toInt() }.toIntArray()
                    "bv" -> bytecodeVersion = (value.snd.value as List<Attribute.Constant>).map { it.value.toString().toInt() }.toIntArray()
                    "k" -> kind = value.snd.value.toString().toInt()
                    "d1" -> data1 = (value.snd.value as List<Attribute.Constant>).map { it.value.toString() }.toTypedArray()
                    "d2" -> data2 = (value.snd.value as List<Attribute.Constant>).map { it.value.toString() }.toTypedArray()
                }
            }
Do you know if there is an easy way to do that?
d

diesieben07

10/29/2018, 4:52 PM
I am not sure what API you are using, but it does not seem to be the javax.lang.model one.
AnnotationMirror
gives you a map of annotation values, where each one is an
AnnotationValue
instance with a
getValue
method. That one would return a
List<Int>
for an int array, for example.
j

juancho

10/29/2018, 7:20 PM
looks like is the same that I’m using but accessed from this other API annotationMirrors. Unfortunately it returns a List<Attribute$Constant> so I have to map this to a List<Int>, like I’m doing in the code above
d

diesieben07

10/29/2018, 7:26 PM
I am not sure what that API is. Do you have docs for it?
j

juancho

10/29/2018, 9:01 PM
and this is what I see:
image.png
and for annotationMirrors is the same:
image.png
and className is:
Copy code
override fun process(annotations: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean {
        roundEnv.getElementsAnnotatedWith(AutoDsl::class.java).forEach { classElement ->
d

diesieben07

10/29/2018, 9:19 PM
Don't get distracted by the debugger stuff. Look at the Javadocs, here is AnnotationMirror, for example: https://docs.oracle.com/javase/10/docs/api/javax/lang/model/element/AnnotationMirror.html
It has a quite straightforward API.
j

juancho

11/03/2018, 1:49 AM
Thanks @diesieben07 I’ll give it a try!
2 Views