Kotlin: annotation properties should be java.lang.Class not KClass

As documented at https://kotlinlang.org/docs/annotations.html#constructors, annotation properties of type KClass get rewritten when targeting the JVM.
This commit is contained in:
Chris Smowton
2022-07-14 18:24:23 +01:00
parent a8197b27aa
commit f774467892
7 changed files with 192 additions and 7 deletions

View File

@@ -628,14 +628,16 @@ open class KotlinFileExtractor(
private fun extractValueParameter(vp: IrValueParameter, parent: Label<out DbCallable>, idx: Int, typeSubstitution: TypeSubstitution?, parentSourceDeclaration: Label<out DbCallable>, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>?, extractTypeAccess: Boolean, locOverride: Label<DbLocation>? = null): TypeResults {
with("value parameter", vp) {
val location = locOverride ?: getLocation(vp, classTypeArgsIncludingOuterClasses)
val maybeErasedType = (vp.parent as? IrFunction)?.let {
val maybeAlteredType = (vp.parent as? IrFunction)?.let {
if (overridesCollectionsMethodWithAlteredParameterTypes(it))
eraseCollectionsMethodParameterType(vp.type, it.name.asString(), idx)
else if ((vp.parent as? IrConstructor)?.parentClassOrNull?.kind == ClassKind.ANNOTATION_CLASS)
kClassToJavaClass(vp.type)
else
null
} ?: vp.type
val javaType = (vp.parent as? IrFunction)?.let { getJavaCallable(it)?.let { jCallable -> getJavaValueParameterType(jCallable, idx) } }
val typeWithWildcards = addJavaLoweringWildcards(maybeErasedType, !hasWildcardSuppressionAnnotation(vp), javaType)
val typeWithWildcards = addJavaLoweringWildcards(maybeAlteredType, !hasWildcardSuppressionAnnotation(vp), javaType)
val substitutedType = typeSubstitution?.let { it(typeWithWildcards, TypeContext.OTHER, pluginContext) } ?: typeWithWildcards
val id = useValueParameter(vp, parent)
if (extractTypeAccess) {
@@ -734,14 +736,17 @@ open class KotlinFileExtractor(
val initializer: IrExpressionBody?
val lhsType: TypeResults?
val vId: Label<out DbVariable>?
val isAnnotationClassField: Boolean
if (f is IrField) {
static = f.isStatic
initializer = f.initializer
lhsType = useType(f.type)
isAnnotationClassField = isAnnotationClassField(f)
lhsType = useType(if (isAnnotationClassField) kClassToJavaClass(f.type) else f.type)
vId = useField(f)
} else if (f is IrEnumEntry) {
static = true
initializer = f.initializerExpression
isAnnotationClassField = false
lhsType = getEnumEntryType(f)
if (lhsType == null) {
return
@@ -762,7 +767,7 @@ open class KotlinFileExtractor(
tw.writeStmts_exprstmt(stmtId, blockAndFunctionId.first, idx++, blockAndFunctionId.second)
tw.writeHasLocation(stmtId, declLocId)
val assignmentId = tw.getFreshIdLabel<DbAssignexpr>()
val type = useType(expr.type)
val type = useType(if (isAnnotationClassField) kClassToJavaClass(expr.type) else expr.type)
tw.writeExprs_assignexpr(assignmentId, type.javaResult.id, stmtId, 0)
tw.writeExprsKotlinType(assignmentId, type.kotlinResult.id)
tw.writeHasLocation(assignmentId, declLocId)
@@ -936,7 +941,8 @@ open class KotlinFileExtractor(
with("field", f) {
DeclarationStackAdjuster(f).use {
val fNameSuffix = getExtensionReceiverType(f)?.let { it.classFqName?.asString()?.replace(".", "$$") } ?: ""
return extractField(useField(f), "${f.name.asString()}$fNameSuffix", f.type, parentId, tw.getLocation(f), f.visibility, f, isExternalDeclaration(f), f.isFinal)
val extractType = if (isAnnotationClassField(f)) kClassToJavaClass(f.type) else f.type
return extractField(useField(f), "${f.name.asString()}$fNameSuffix", extractType, parentId, tw.getLocation(f), f.visibility, f, isExternalDeclaration(f), f.isFinal)
}
}
}
@@ -2917,14 +2923,17 @@ open class KotlinFileExtractor(
if (owner is IrValueParameter && owner.index == -1 && !owner.isExtensionReceiver()) {
extractThisAccess(e, exprParent, callable)
} else {
extractVariableAccess(useValueDeclaration(owner), e.type, tw.getLocation(e), exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt)
val isAnnotationClassParameter = ((owner as? IrValueParameter)?.parent as? IrConstructor)?.parentClassOrNull?.kind == ClassKind.ANNOTATION_CLASS
val extractType = if (isAnnotationClassParameter) kClassToJavaClass(e.type) else e.type
extractVariableAccess(useValueDeclaration(owner), extractType, tw.getLocation(e), exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt)
}
}
is IrGetField -> {
val exprParent = parent.expr(e, callable)
val owner = tryReplaceAndroidSyntheticField(e.symbol.owner)
val locId = tw.getLocation(e)
extractVariableAccess(useField(owner), e.type, locId, exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt).also { id ->
val fieldType = if (isAnnotationClassField(owner)) kClassToJavaClass(e.type) else e.type
extractVariableAccess(useField(owner), fieldType, locId, exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt).also { id ->
val receiver = e.receiver
if (receiver != null) {
extractExpressionExpr(receiver, callable, id, -1, exprParent.enclosingStmt)