Kapt: Support String[] in annotation proxy
(cherry picked from commit 5e9eab9)
This commit is contained in:
committed by
Yan Zhulanow
parent
0042a59fb6
commit
cd44540965
@@ -0,0 +1,2 @@
|
|||||||
|
@Suppress("Tom", "Mary")
|
||||||
|
class Test
|
||||||
+8
-6
@@ -64,6 +64,8 @@ class KotlinAnnotationProxyMaker(val annotation: PsiAnnotation, val annotationCl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val JAVA_LANG_STRING = "java.lang.String"
|
||||||
|
|
||||||
private fun getConstantValue(
|
private fun getConstantValue(
|
||||||
psiValue: PsiAnnotationMemberValue,
|
psiValue: PsiAnnotationMemberValue,
|
||||||
returnType: PsiType,
|
returnType: PsiType,
|
||||||
@@ -74,7 +76,7 @@ private fun getConstantValue(
|
|||||||
|
|
||||||
when {
|
when {
|
||||||
returnType == PsiType.NULL || returnType == PsiType.VOID -> unexpectedType("void")
|
returnType == PsiType.NULL || returnType == PsiType.VOID -> unexpectedType("void")
|
||||||
returnType.fqName == "java.lang.String" -> return (psiValue as? PsiExpression)?.calcConstantValue(evaluator)
|
returnType.fqName == JAVA_LANG_STRING -> return (psiValue as? PsiExpression)?.calcConstantValue(evaluator)
|
||||||
jReturnType.isAnnotation -> {
|
jReturnType.isAnnotation -> {
|
||||||
if (psiValue !is PsiAnnotation) error("psiValue is not a PsiAnnotation")
|
if (psiValue !is PsiAnnotation) error("psiValue is not a PsiAnnotation")
|
||||||
val annotationClass = PsiTypesUtil.getPsiClass(returnType) ?: error("Can't resolve type $returnType")
|
val annotationClass = PsiTypesUtil.getPsiClass(returnType) ?: error("Can't resolve type $returnType")
|
||||||
@@ -91,16 +93,16 @@ private fun getConstantValue(
|
|||||||
else -> listOf(psiValue)
|
else -> listOf(psiValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!jComponentType.isPrimitive && !jComponentType.isAnnotation) {
|
if (jComponentType.isPrimitive || jComponentType.isAnnotation || jComponentType.canonicalName == JAVA_LANG_STRING) {
|
||||||
val typeMirrors = arrayValues.map { getObjectType(it).toJeType(manager) }
|
|
||||||
return MirroredTypesExceptionProxy(Collections.unmodifiableList(typeMirrors))
|
|
||||||
} else {
|
|
||||||
val arr = Array.newInstance(jComponentType, arrayValues.size)
|
val arr = Array.newInstance(jComponentType, arrayValues.size)
|
||||||
arrayValues.forEachIndexed { i, componentPsiValue ->
|
arrayValues.forEachIndexed { i, componentPsiValue ->
|
||||||
val componentValue = getConstantValue(componentPsiValue, returnType.componentType, jComponentType, evaluator)
|
val componentValue = getConstantValue(componentPsiValue, returnType.componentType, jComponentType, evaluator)
|
||||||
try { Array.set(arr, i, componentValue) } catch (e: IllegalArgumentException) { return null }
|
try { Array.set(arr, i, componentValue) } catch (e: IllegalArgumentException) { return null }
|
||||||
}
|
}
|
||||||
return arr
|
return arr
|
||||||
|
} else {
|
||||||
|
val typeMirrors = arrayValues.map { getObjectType(it).toJeType(manager) }
|
||||||
|
return MirroredTypesExceptionProxy(Collections.unmodifiableList(typeMirrors))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
jReturnType.isEnum -> {
|
jReturnType.isEnum -> {
|
||||||
|
|||||||
+8
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.kotlin.annotation.processing.test.processor
|
package org.jetbrains.kotlin.annotation.processing.test.processor
|
||||||
|
|
||||||
import org.jetbrains.kotlin.java.model.elements.*
|
import org.jetbrains.kotlin.java.model.elements.*
|
||||||
|
import org.jetbrains.kotlin.java.model.types.JeDeclaredType
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
|
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
|
||||||
import javax.lang.model.element.AnnotationMirror
|
import javax.lang.model.element.AnnotationMirror
|
||||||
|
|
||||||
@@ -111,4 +112,11 @@ class ProcessorTests : AbstractProcessorTest() {
|
|||||||
assertTrue(anno2.getParam("d") is JeTypeAnnotationValue)
|
assertTrue(anno2.getParam("d") is JeTypeAnnotationValue)
|
||||||
assertTrue((anno2.getParam("e") as JeArrayAnnotationValue).value.first() is JeTypeAnnotationValue)
|
assertTrue((anno2.getParam("e") as JeArrayAnnotationValue).value.first() is JeTypeAnnotationValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testStringArray() = test("StringArray", "*") { set, roundEnv, env ->
|
||||||
|
val testClass = env.findClass("Test")
|
||||||
|
val suppress = testClass.getAnnotation(Suppress::class.java)
|
||||||
|
assertNotNull(suppress)
|
||||||
|
assertEquals(listOf("Tom", "Mary"), suppress!!.names.toList())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user