From 25ed6bb8d5592aefae03fba1abf221feeaf488b3 Mon Sep 17 00:00:00 2001 From: Mikhail Zinchenko Date: Mon, 11 Apr 2022 14:02:07 +0300 Subject: [PATCH] Add proxy ClassLoader for annotation class parameter instantiation --- .../main/kotlin/com/google/devtools/ksp/utils.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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 0284c0155c..9587731be2 100644 --- a/api/src/main/kotlin/com/google/devtools/ksp/utils.kt +++ b/api/src/main/kotlin/com/google/devtools/ksp/utils.kt @@ -366,7 +366,7 @@ private fun KSAnnotation.createInvocationHandler(clazz: Class<*>): InvocationHan when (val result = argument.value ?: method.defaultValue) { is Proxy -> result is List<*> -> { - val value = { result.asArray(method) } + val value = { result.asArray(method, clazz) } cache.getOrPut(Pair(method.returnType, result), value) } else -> { @@ -382,7 +382,7 @@ private fun KSAnnotation.createInvocationHandler(clazz: Class<*>): InvocationHan method.returnType.name == "java.lang.Class" -> { cache.getOrPut(Pair(method.returnType, result)) { when (result) { - is KSType -> result.asClass() + is KSType -> result.asClass(clazz) // Handles com.intellij.psi.impl.source.PsiImmediateClassType using reflection // since api doesn't contain a reference to this else -> Class.forName( @@ -434,7 +434,7 @@ private fun KSAnnotation.asAnnotation( @KspExperimental @Suppress("UNCHECKED_CAST") -private fun List<*>.asArray(method: Method) = +private fun List<*>.asArray(method: Method, proxyClass: Class<*>) = when (method.returnType.componentType.name) { "boolean" -> (this as List).toBooleanArray() "byte" -> (this as List).toByteArray() @@ -444,7 +444,7 @@ private fun List<*>.asArray(method: Method) = "float" -> (this as List).toFloatArray() "int" -> (this as List).toIntArray() "long" -> (this as List).toLongArray() - "java.lang.Class" -> (this as List).asClasses().toTypedArray() + "java.lang.Class" -> (this as List).asClasses(proxyClass).toTypedArray() "java.lang.String" -> (this as List).toTypedArray() else -> { // arrays of enums or annotations when { @@ -503,15 +503,15 @@ class KSTypeNotPresentException(val ksType: KSType, cause: Throwable) : RuntimeE class KSTypesNotPresentException(val ksTypes: List, cause: Throwable) : RuntimeException(cause) @KspExperimental -private fun KSType.asClass() = try { - Class.forName(this.declaration.qualifiedName!!.asString()) +private fun KSType.asClass(proxyClass: Class<*>) = try { + Class.forName(this.declaration.qualifiedName!!.asString(), true, proxyClass.classLoader) } catch (e: Exception) { throw KSTypeNotPresentException(this, e) } @KspExperimental -private fun List.asClasses() = try { - this.map(KSType::asClass) +private fun List.asClasses(proxyClass: Class<*>) = try { + this.map { type -> type.asClass(proxyClass) } } catch (e: Exception) { throw KSTypesNotPresentException(this, e) }