Skip to content

Commit 8926403

Browse files
authored
Support StabilityInferred annotation for Gradle plugin (#112)
1 parent f8b5a8e commit 8926403

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

stability-compiler/src/main/kotlin/com/skydoves/compose/stability/compiler/lower/StabilityAnalyzerTransformer.kt

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,9 @@ public class StabilityAnalyzerTransformer(
443443
}
444444

445445
// 17. Cross-module types require explicit @Stable/@Immutable/@StabilityInferred
446-
if (isFromDifferentModule(clazz) && !type.hasStableAnnotation()) {
446+
if (isFromDifferentModule(clazz) && !type.hasStableAnnotation() &&
447+
!type.hasStabilityInferredAnnotation()
448+
) {
447449
return ParameterStability.UNSTABLE
448450
}
449451

@@ -588,11 +590,33 @@ public class StabilityAnalyzerTransformer(
588590
}
589591

590592
/**
591-
* TODO: Read @StabilityInferred parameters field without deprecated IR APIs.
592-
* Returns null (conservative RUNTIME) until stable API is available.
593+
* Read the `parameters` field from @StabilityInferred annotation.
594+
* Returns 0 when all type parameters are stable, non-zero bitmask otherwise.
595+
* Returns null if the annotation is not present.
593596
*/
594597
private fun IrType.getStabilityInferredParameters(): Int? {
595-
return null
598+
val classSymbol = this.classOrNull ?: return null
599+
val clazz = classSymbol.owner
600+
val stabilityInferredFqName =
601+
FqName("androidx.compose.runtime.internal.StabilityInferred")
602+
603+
val annotation = clazz.annotations.find { annot ->
604+
try {
605+
val annotationClass = annot.symbol.owner.parent as? IrClass
606+
annotationClass?.kotlinFqName == stabilityInferredFqName
607+
} catch (e: Exception) {
608+
false
609+
}
610+
} ?: return null
611+
612+
val annotationClass = annotation.symbol.owner
613+
val paramNameToIndex = annotationClass.parameters
614+
.mapIndexed { index, param -> param.name.asString() to index }
615+
.toMap()
616+
617+
val parametersIndex = paramNameToIndex["parameters"] ?: return null
618+
val value = annotation.arguments.getOrNull(parametersIndex) ?: return null
619+
return extractConstIntValue(value)
596620
}
597621

598622
private fun IrType.isCollection(): Boolean {

0 commit comments

Comments
 (0)