mardi 1 novembre 2022

In Java, detect if an Object is a Kotlin array

Sorry if this is very specific...

I am looking at Javapoet's implementation of AnnotationSpec.get(Annotation). It recursively clones an annotation so that you can copy it to another element. In my case, I am trying to copy an @OpenApi annotation from one method to another.

The issue is caused by the annotations being written in kotlin. Anywhere the annotation needs a class, it is using KClass<*>. Javapoet only handles Java types, like Class<?>. From Java, I can say if (o instanceof Class || o instance of KClass) no problem.

However, there's also code that says o.getClass().isArray() but, from what I can tell, kotlin annotations use Array<*> for arrays, so that check is failing. The actual type appears to be com.sun.proxy.$Proxy54 when I inspect it, but I have no idea what that is.

How do you detect if an object is an kotlin array from Java? Can this be converted to a Java array? Is there some universal way to make kotlin annotations appear as java annotations using Class and built-in arrays and so on?





Aucun commentaire:

Enregistrer un commentaire