From d565abd0ec1f446b326df8f2cb644f4dccb2a71c Mon Sep 17 00:00:00 2001 From: "xiaozihan.larryxiao" Date: Tue, 21 Feb 2023 14:34:19 +0800 Subject: [PATCH] Get AnnotationByType's invocation handler supports java array value --- .../main/kotlin/com/google/devtools/ksp/utils.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/api/src/main/kotlin/com/google/devtools/ksp/utils.kt b/api/src/main/kotlin/com/google/devtools/ksp/utils.kt index bdbd793494..7453c04cd0 100644 --- a/api/src/main/kotlin/com/google/devtools/ksp/utils.kt +++ b/api/src/main/kotlin/com/google/devtools/ksp/utils.kt @@ -371,6 +371,16 @@ private fun KSAnnotation.createInvocationHandler(clazz: Class<*>): InvocationHan } else -> { when { + // Workaround for java annotation value array type + // https://github.com/google/ksp/issues/1329 + method.returnType.isArray -> { + if (result !is Array<*>) { + val value = { result.asArray(method, clazz) } + cache.getOrPut(Pair(method.returnType, value), value) + } else { + throw IllegalStateException("unhandled value type, $ExceptionMessage") + } + } method.returnType.isEnum -> { val value = { result.asEnum(method.returnType) } cache.getOrPut(Pair(method.returnType, result), value) @@ -517,3 +527,6 @@ private fun List.asClasses(proxyClass: Class<*>) = try { } fun KSValueArgument.isDefault() = origin == Origin.SYNTHETIC + +@KspExperimental +private fun Any.asArray(method: Method, proxyClass: Class<*>) = listOf(this).asArray(method, proxyClass)