Kapt: Support nested annotations property.
Also do not rely on Java class comparison, check against qualified names. (cherry picked from commit 7610945)
This commit is contained in:
committed by
Yan Zhulanow
parent
49bd303988
commit
a8b577cd09
+8
-8
@@ -47,22 +47,22 @@ fun JeAnnotationValue(psi: PsiAnnotationMemberValue): AnnotationValue {
|
||||
return annotationValue
|
||||
}
|
||||
|
||||
internal class JeAnnotationAnnotationValue(val psi: PsiAnnotation) : AnnotationValue {
|
||||
class JeAnnotationAnnotationValue(val psi: PsiAnnotation) : AnnotationValue {
|
||||
override fun getValue() = JeAnnotationMirror(psi)
|
||||
override fun <R : Any?, P : Any?> accept(v: AnnotationValueVisitor<R, P>, p: P) = v.visitAnnotation(value, p)
|
||||
}
|
||||
|
||||
internal class JeEnumValueAnnotationValue(val psi: PsiEnumConstant) : AnnotationValue {
|
||||
class JeEnumValueAnnotationValue(val psi: PsiEnumConstant) : AnnotationValue {
|
||||
override fun getValue() = JeVariableElement(psi)
|
||||
override fun <R : Any?, P : Any?> accept(v: AnnotationValueVisitor<R, P>, p: P) = v.visitEnumConstant(value, p)
|
||||
}
|
||||
|
||||
internal class JeTypeAnnotationValue(val psi: PsiClassObjectAccessExpression) : AnnotationValue {
|
||||
class JeTypeAnnotationValue(val psi: PsiClassObjectAccessExpression) : AnnotationValue {
|
||||
override fun getValue() = psi.operand.type.toJeType(psi.manager)
|
||||
override fun <R : Any?, P : Any?> accept(v: AnnotationValueVisitor<R, P>, p: P) = v.visitType(value, p)
|
||||
}
|
||||
|
||||
internal abstract class JePrimitiveAnnotationValue : AnnotationValue {
|
||||
abstract class JePrimitiveAnnotationValue : AnnotationValue {
|
||||
override fun <R : Any?, P : Any?> accept(v: AnnotationValueVisitor<R, P>, p: P): R {
|
||||
val value = this.value
|
||||
return when (value) {
|
||||
@@ -80,20 +80,20 @@ internal abstract class JePrimitiveAnnotationValue : AnnotationValue {
|
||||
}
|
||||
}
|
||||
|
||||
internal class JeLiteralAnnotationValue(val psi: PsiLiteral) : JePrimitiveAnnotationValue() {
|
||||
class JeLiteralAnnotationValue(val psi: PsiLiteral) : JePrimitiveAnnotationValue() {
|
||||
override fun getValue() = psi.value
|
||||
}
|
||||
|
||||
internal class JeExpressionAnnotationValue(val psi: PsiExpression) : JePrimitiveAnnotationValue() {
|
||||
class JeExpressionAnnotationValue(val psi: PsiExpression) : JePrimitiveAnnotationValue() {
|
||||
override fun getValue() = psi.calcConstantValue()
|
||||
}
|
||||
|
||||
internal class JeArrayAnnotationValue(val psi: PsiArrayInitializerMemberValue) : AnnotationValue {
|
||||
class JeArrayAnnotationValue(val psi: PsiArrayInitializerMemberValue) : AnnotationValue {
|
||||
override fun getValue() = psi.initializers.map(::JeAnnotationValue)
|
||||
override fun <R : Any?, P : Any?> accept(v: AnnotationValueVisitor<R, P>, p: P) = v.visitArray(value, p)
|
||||
}
|
||||
|
||||
internal class JeErrorAnnotationValue(val psi: PsiElement) : AnnotationValue {
|
||||
class JeErrorAnnotationValue(val psi: PsiElement) : AnnotationValue {
|
||||
override fun <R : Any?, P : Any?> accept(v: AnnotationValueVisitor<R, P>, p: P) = v.visitString(psi.text, p)
|
||||
override fun getValue() = null
|
||||
}
|
||||
+18
-7
@@ -17,7 +17,7 @@
|
||||
package org.jetbrains.kotlin.java.model.internal
|
||||
|
||||
import com.intellij.psi.*
|
||||
import org.jetbrains.kotlin.asJava.elements.KtLightAnnotation
|
||||
import com.intellij.psi.util.PsiTypesUtil
|
||||
import org.jetbrains.kotlin.java.model.types.toJeType
|
||||
import sun.reflect.annotation.AnnotationParser
|
||||
import sun.reflect.annotation.ExceptionProxy
|
||||
@@ -74,10 +74,13 @@ private fun getConstantValue(
|
||||
|
||||
when {
|
||||
returnType == PsiType.NULL || returnType == PsiType.VOID -> unexpectedType("void")
|
||||
jReturnType == String::class.java -> return (psiValue as? PsiExpression)?.calcConstantValue(evaluator)
|
||||
jReturnType == Class::class.java -> {
|
||||
val type = getObjectType(psiValue).toJeType(manager)
|
||||
return MirroredTypeExceptionProxy(type)
|
||||
returnType.fqName == "java.lang.String" -> return (psiValue as? PsiExpression)?.calcConstantValue(evaluator)
|
||||
jReturnType.isAnnotation -> {
|
||||
if (psiValue !is PsiAnnotation) error("psiValue is not a PsiAnnotation")
|
||||
val annotationClass = PsiTypesUtil.getPsiClass(returnType) ?: error("Can't resolve type $returnType")
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val annotation = KotlinAnnotationProxyMaker(psiValue, annotationClass, jReturnType as Class<out Annotation>)
|
||||
return jReturnType.cast(annotation.generate())
|
||||
}
|
||||
jReturnType.isArray -> {
|
||||
val jComponentType = jReturnType.componentType ?: unexpectedType("no component type for $jReturnType")
|
||||
@@ -88,7 +91,7 @@ private fun getConstantValue(
|
||||
else -> listOf(psiValue)
|
||||
}
|
||||
|
||||
if (jComponentType == Class::class.java) {
|
||||
if (!jComponentType.isPrimitive && !jComponentType.isAnnotation) {
|
||||
val typeMirrors = arrayValues.map { getObjectType(it).toJeType(manager) }
|
||||
return MirroredTypesExceptionProxy(Collections.unmodifiableList(typeMirrors))
|
||||
} else {
|
||||
@@ -105,10 +108,18 @@ private fun getConstantValue(
|
||||
?: error("$psiValue can not be resolved to enum constant")
|
||||
return AnnotationUtil.createEnumValue(jReturnType, enumConstant.name)
|
||||
}
|
||||
else -> return castPrimitiveValue(returnType, (psiValue as? PsiExpression)?.calcConstantValue(evaluator))
|
||||
else -> return if (returnType is PsiClassType) {
|
||||
val type = getObjectType(psiValue).toJeType(manager)
|
||||
MirroredTypeExceptionProxy(type)
|
||||
} else {
|
||||
castPrimitiveValue(returnType, (psiValue as? PsiExpression)?.calcConstantValue(evaluator))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val PsiType.fqName: String?
|
||||
get() = (this as? PsiClassType)?.resolve()?.qualifiedName
|
||||
|
||||
private fun getObjectType(value: PsiAnnotationMemberValue): PsiType {
|
||||
when (value) {
|
||||
is PsiClassObjectAccessExpression -> return value.operand.type
|
||||
|
||||
Reference in New Issue
Block a user