2

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?

1 Answer 1

2

Kotlin arrays and Java arrays are the same thing at the VM level. Array<*> is Kotlin's syntax for arrays, but the objects are the same thing. .isArray should work.

The proxy objects are typical for what you get when you call getClass on annotation objects in Java or Kotlin. You likely need to use annotationType instead of getClass.

Sign up to request clarification or add additional context in comments.

5 Comments

Kotlin arrays and Java arrays are the same thing at the VM level - I guess that applies to instances of arrays, yes, but here the problem seems to be about reflection on the types
The Proxy objects seem likely to reflect the common mistake -- not special to Kotlin, but present in pure Java -- of using getClass instead of annotationType on annotation objects.
The code of Javapoet doesn’t seem to have such a bug, but this just implies that we have a classical xy problem here. The question goes like “The issue is caused by…”, followed by the OP’s speculation about the causes, but completely forgets to describe the actual issue.
The OP describes that o.getClass() looks like com.sun.proxy.$Proxy54, which is exactly what happens for annotation classes at runtime -- uniquely, as far as I know. Given that, it seems to me as if this is the only plausible explanation -- that o is an annotation object. In general, you "detect if an object is a kotlin array from Java" by testing if it is an array; there is only one kind of array, there is no separate Java array.
It seems to got my comment wrong; your answer is correct, without any doubt. But there’s nothing wrong with the value being an annotation; the code of Javapoet checks for both, whether the value returned by the annotation method is an array and whether the returned value is an annotation. It also has code for the third possibility, that the value is neither, an array nor an annotation. To understand why the OP thinks that there’s a problem, we would have to know what actual operation went wrong and in which way. Obviously, whatever the actual problem is, it’s not related to Kotlin at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.