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) diff --git a/test-utils/testData/api/javaAnnotatedUtil.kt b/test-utils/testData/api/javaAnnotatedUtil.kt index e6da243a6b..30fcbb45bd 100644 --- a/test-utils/testData/api/javaAnnotatedUtil.kt +++ b/test-utils/testData/api/javaAnnotatedUtil.kt @@ -35,6 +35,9 @@ // Test: ParameterArraysTestAnnotationWithDefaultTest // IsPresent: class com.google.devtools.ksp.processor.ParameterArraysTestAnnotation // ByType: ParameterArraysTestAnnotation[booleanArrayValue=[true, false],byteArrayValue=[-2, 4],shortArrayValue=[-1, 2, 3],charArrayValue=[a, b, c],doubleArrayValue=[1.1, 2.2, 3.3],floatArrayValue=[1.0, 2.0, 3.3],intArrayValue=[1, 2, 4, 8, 16],longArrayValue=[1, 2, 4, 8, 16, 32],stringArrayValue=[first, second, third],kClassArrayValue=[class kotlin.Throwable, class com.google.devtools.ksp.processor.ParametersTestAnnotation],enumArrayValue=[VALUE1, VALUE2, VALUE1, VALUE2]] +// Test: ParameterArraysTestAnnotationWithSingleAsArrayTest +// IsPresent: class com.google.devtools.ksp.processor.ParameterArraysTestAnnotation +// ByType: ParameterArraysTestAnnotation[booleanArrayValue=[true],byteArrayValue=[-2],shortArrayValue=[-1],charArrayValue=[a],doubleArrayValue=[1.1],floatArrayValue=[1.0],intArrayValue=[1],longArrayValue=[1],stringArrayValue=[first],kClassArrayValue=[class kotlin.Throwable],enumArrayValue=[VALUE1]] // Test: AnnotationWithinAnAnnotationTest // IsPresent: class com.google.devtools.ksp.processor.OuterAnnotation // ByType: com.google.devtools.ksp.processor.OuterAnnotation[innerAnnotation=com.google.devtools.ksp.processor.InnerAnnotation[value=hello from the other side]] @@ -164,6 +167,22 @@ public class ParametersTestWithNegativeDefaultsAnnotationTest {} @Test public class ParameterArraysTestAnnotationWithDefaultTest {} +@ParameterArraysTestAnnotation( + booleanArrayValue = true, + byteArrayValue = -2, + shortArrayValue = -1, + charArrayValue = 'a', + doubleArrayValue = 1.1, + floatArrayValue = 1.0f, + intArrayValue = 1, + longArrayValue = 1L, + stringArrayValue = "first", + kClassArrayValue = java.lang.Throwable.class, + enumArrayValue = TestEnum.VALUE1 +) +@Test +public class ParameterArraysTestAnnotationWithSingleAsArrayTest {} + @OuterAnnotation(innerAnnotation = @InnerAnnotation(value = "hello from the other side")) @Test public class AnnotationWithinAnAnnotationTest {}