Only convert KClass -> Class in annotation context

This commit is contained in:
Chris Smowton 2022-11-09 17:24:40 +00:00
Родитель 2fd8e61fe2
Коммит d7213d2d82
4 изменённых файлов: 14 добавлений и 13 удалений

Просмотреть файл

@ -512,14 +512,14 @@ open class KotlinFileExtractor(
is IrClassReference -> {
val classRefId = exprId()
val typeAccessId = tw.getLabelFor<DbUnannotatedtypeaccess>("@\"annotationExpr;{$classRefId};0\"")
extractClassReference(v, parent, idx, null, null, overrideId = classRefId, typeAccessOverrideId = typeAccessId, useUnboundType = true)
extractClassReference(v, parent, idx, null, null, overrideId = classRefId, typeAccessOverrideId = typeAccessId, useJavaLangClassType = true)
}
is IrConstructorCall -> {
extractAnnotation(v, parent, idx, contextLabel)
}
is IrVararg -> {
tw.getLabelFor<DbArrayinit>("@\"annotationarray;{${parent}};$contextLabel\"").also { arrayId ->
val type = useType(v.type)
val type = useType(kClassToJavaClass(v.type))
tw.writeExprs_arrayinit(arrayId, type.javaResult.id, parent, idx)
tw.writeExprsKotlinType(arrayId, type.kotlinResult.id)
tw.writeHasLocation(arrayId, tw.getLocation(v))
@ -4195,11 +4195,12 @@ open class KotlinFileExtractor(
enclosingStmt: Label<out DbStmt>?,
overrideId: Label<out DbExpr>? = null,
typeAccessOverrideId: Label<out DbExpr>? = null,
useUnboundType: Boolean = false
useJavaLangClassType: Boolean = false
) =
exprIdOrFresh<DbTypeliteral>(overrideId).also { id ->
val locId = tw.getLocation(e)
val type = useType(if (useUnboundType) toUnbound(e.type) else e.type)
val jlcType = if (useJavaLangClassType) this.javaLangClass?.let { it.typeWith() } else null
val type = useType(jlcType ?: e.type)
tw.writeExprs_typeliteral(id, type.javaResult.id, parent, idx)
tw.writeExprsKotlinType(id, type.kotlinResult.id)
extractExprContext(id, locId, enclosingCallable, enclosingStmt)

Просмотреть файл

@ -120,10 +120,4 @@ fun getContainingClassOrSelf(decl: IrDeclaration): IrClass? {
}
fun getJavaEquivalentClassId(c: IrClass) =
c.fqNameWhenAvailable?.toUnsafe()?.let {
if (it.asString() == "kotlin.reflect.KClass") {
ClassId.fromString("java.lang.Class")
} else {
JavaToKotlinClassMap.mapKotlinToJava(it)
}
}
c.fqNameWhenAvailable?.toUnsafe()?.let { JavaToKotlinClassMap.mapKotlinToJava(it) }

Просмотреть файл

@ -28,6 +28,7 @@ test.kt:
# 5| 2: [Method] w
# 5| 3: [Method] v
# 5| 4: [Method] u
# 5| 5: [Method] t
# 7| 3: [Interface] Ann3
# 7| 1: [Method] a
# 9| 4: [Class] Annotated
@ -47,6 +48,11 @@ test.kt:
# 0| 1: [IntegerLiteral] 1
# 0| 1: [Annotation] Ann3
# 0| 1: [IntegerLiteral] 2
# 0| 1: [ArrayInit] {...}
# 0| 1: [TypeLiteral] String.class
# 0| 0: [TypeAccess] String
# 0| 1: [TypeLiteral] int.class
# 0| 0: [TypeAccess] int
# 10| 1: [Constructor] Annotated
# 9| 5: [BlockStmt] { ... }
# 9| 0: [SuperConstructorInvocationStmt] super(...)

Просмотреть файл

@ -2,9 +2,9 @@ import kotlin.reflect.KClass
annotation class Ann1(val x: Int, val y: Ann2) { }
annotation class Ann2(val z: String, val w: KClass<*>, val v: IntArray, val u: Array<Ann3>) { }
annotation class Ann2(val z: String, val w: KClass<*>, val v: IntArray, val u: Array<Ann3>, val t: Array<KClass<*>>) { }
annotation class Ann3(val a: Int) { }
@Ann1(1, Ann2("Hello", String::class, intArrayOf(1, 2, 3), arrayOf(Ann3(1), Ann3(2))))
@Ann1(1, Ann2("Hello", String::class, intArrayOf(1, 2, 3), arrayOf(Ann3(1), Ann3(2)), arrayOf(String::class, Int::class)))
class Annotated { }