Skip to content

Commit 01a5345

Browse files
committed
Reimplement Deprecated annotation conversion suitable for older Kotlin versions
1 parent 8ca05d8 commit 01a5345

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -487,15 +487,42 @@ open class KotlinFileExtractor(
487487
extractDeclInitializers(c.declarations, false) { Pair(blockId, obinitId) }
488488
}
489489

490-
private fun extractAnnotations(annotations: List<IrConstructorCall>, parent: Label<out DbExprparent>, extractEnumTypeAccesses: Boolean) {
491-
val groupedAnnotations = metaAnnotationSupport.groupRepeatableAnnotations(annotations)
490+
private val javaLangDeprecated by lazy { referenceExternalClass("java.lang.Deprecated") }
491+
492+
private val javaLangDeprecatedConstructor by lazy { javaLangDeprecated?.constructors?.singleOrNull() }
493+
494+
private fun replaceKotlinDeprecatedAnnotation(annotations: List<IrConstructorCall>): List<IrConstructorCall> {
495+
val shouldReplace =
496+
annotations.any { (it.type as? IrSimpleType)?.classFqName?.asString() == "kotlin.Deprecated" } &&
497+
annotations.none { (it.type as? IrSimpleType)?.classFqName?.asString() == "java.lang.Deprecated" }
498+
val jldConstructor = javaLangDeprecatedConstructor
499+
if (!shouldReplace || jldConstructor == null)
500+
return annotations
501+
return annotations.filter { (it.type as? IrSimpleType)?.classFqName?.asString() != "kotlin.Deprecated" } +
502+
// Note we lose any arguments to @java.lang.Deprecated that were written in source.
503+
IrConstructorCallImpl.fromSymbolOwner(
504+
UNDEFINED_OFFSET, UNDEFINED_OFFSET, jldConstructor.returnType, jldConstructor.symbol, 0
505+
)
506+
}
507+
508+
private fun extractAnnotations(c: IrAnnotationContainer, annotations: List<IrConstructorCall>, parent: Label<out DbExprparent>, extractEnumTypeAccesses: Boolean) {
509+
val origin = when(c) {
510+
is IrDeclaration -> c.origin
511+
else -> null
512+
}
513+
val replacedAnnotations =
514+
if (origin == IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB)
515+
replaceKotlinDeprecatedAnnotation(annotations)
516+
else
517+
annotations
518+
val groupedAnnotations = metaAnnotationSupport.groupRepeatableAnnotations(replacedAnnotations)
492519
for ((idx, constructorCall: IrConstructorCall) in groupedAnnotations.sortedBy { v -> v.type.classFqName?.asString() }.withIndex()) {
493520
extractAnnotation(constructorCall, parent, idx, extractEnumTypeAccesses)
494521
}
495522
}
496523

497524
private fun extractAnnotations(c: IrAnnotationContainer, parent: Label<out DbExprparent>, extractEnumTypeAccesses: Boolean) {
498-
extractAnnotations(c.annotations, parent, extractEnumTypeAccesses)
525+
extractAnnotations(c, c.annotations, parent, extractEnumTypeAccesses)
499526
}
500527

501528
private fun extractAnnotation(
@@ -505,19 +532,8 @@ open class KotlinFileExtractor(
505532
extractEnumTypeAccesses: Boolean,
506533
contextLabel: String? = null
507534
): Label<out DbExpr> {
508-
val isConvertedJavaDeprecatedAnnotation = (constructorCall.type as? IrSimpleType)?.classFqName?.asString() == "kotlin.Deprecated" &&
509-
constructorCall.source is JavaSourceElement
510-
511-
val extractType =
512-
(
513-
if (isConvertedJavaDeprecatedAnnotation)
514-
pluginContext.referenceClass(FqName("java.lang.Deprecated"))?.defaultType
515-
else
516-
null
517-
) ?: erase(constructorCall.type)
518-
519535
// Erase the type here because the JVM lowering erases the annotation type, and so the Java extractor will see it in erased form.
520-
val t = useType(extractType)
536+
val t = useType(erase(constructorCall.type))
521537
val annotationContextLabel = contextLabel ?: "{${t.javaResult.id}}"
522538
val id = tw.getLabelFor<DbDeclannotation>("@\"annotation;{$parent};$annotationContextLabel\"")
523539
tw.writeExprs_declannotation(id, t.javaResult.id, parent, idx)
@@ -526,11 +542,7 @@ open class KotlinFileExtractor(
526542
val locId = tw.getLocation(constructorCall)
527543
tw.writeHasLocation(id, locId)
528544

529-
// If this is `java.lang.Deprecated`, extract an annotation without parameters -- whatever the original source
530-
// may have supplied has been lost.
531-
val paramCount = if (isConvertedJavaDeprecatedAnnotation) 0 else constructorCall.valueArgumentsCount
532-
533-
for (i in 0 until paramCount) {
545+
for (i in 0 until constructorCall.valueArgumentsCount) {
534546
val param = constructorCall.symbol.owner.valueParameters[i]
535547
val prop = constructorCall.symbol.owner.parentAsClass.declarations
536548
.filterIsInstance<IrProperty>()
@@ -702,7 +714,7 @@ open class KotlinFileExtractor(
702714
else
703715
listOf()
704716

705-
extractAnnotations(c.annotations + additionalAnnotations, id, extractFunctionBodies)
717+
extractAnnotations(c, c.annotations + additionalAnnotations, id, extractFunctionBodies)
706718

707719
if (extractFunctionBodies && !c.isAnonymousObject && !c.isLocal)
708720
externalClassExtractor.writeStubTrapFile(c)
@@ -900,7 +912,7 @@ open class KotlinFileExtractor(
900912
else -> null
901913
}
902914
val extraAnnotations = listOfNotNull(getNullabilityAnnotation(vp.type, vp.origin, vp.annotations, javaParameter?.annotations))
903-
extractAnnotations(vp.annotations + extraAnnotations, id, extractTypeAccess)
915+
extractAnnotations(vp, vp.annotations + extraAnnotations, id, extractTypeAccess)
904916
return extractValueParameter(id, substitutedType, vp.name.asString(), location, parent, idx, useValueParameter(vp, parentSourceDeclaration), syntheticParameterNames, vp.isVararg, vp.isNoinline, vp.isCrossinline)
905917
}
906918
}
@@ -1468,7 +1480,7 @@ open class KotlinFileExtractor(
14681480

14691481
if (extractAnnotations) {
14701482
val extraAnnotations = listOfNotNull(getNullabilityAnnotation(f.returnType, f.origin, f.annotations, getJavaCallable(f)?.annotations))
1471-
extractAnnotations(f.annotations + extraAnnotations, id, extractMethodAndParameterTypeAccesses)
1483+
extractAnnotations(f, f.annotations + extraAnnotations, id, extractMethodAndParameterTypeAccesses)
14721484
}
14731485

14741486
return id

0 commit comments

Comments
 (0)