Skip to content

Commit 65b595c

Browse files
authored
Merge pull request #24 from skydoves/feature/StabilityInferred
Handle StabilityInferred annotated composables
2 parents 309b3d1 + 32eba91 commit 65b595c

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

compose-stability-analyzer-idea/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ All notable changes to the IntelliJ IDEA plugin will be documented in this file.
77
### Added
88
- New setting: "Show in test source sets" for gutter icons (Issue #21)
99
- Gutter icons are now hidden in test directories by default (can be enabled in settings)
10+
- Support for reading @StabilityInferred annotation parameters for cross-module stability detection (Issue #18)
1011

1112
### Fixed
1213
- Fixed typealias detection for Composable function types (Issue #16)
1314
- Typealiases like `typealias SettingsButtons = @Composable (PlayerUiState) -> Unit` now correctly expand to their underlying function types before stability analysis
1415
- Fixed ImmutableList/ImmutableSet/ImmutableMap showing as unstable in test code (Issue #21)
1516
- Added fallback type resolution by simple name for immutable collections when FQN resolution fails in test source sets
17+
- Improved cross-module stability detection by reading @StabilityInferred(parameters) annotation (Issue #18)
18+
- Classes from other modules now correctly marked as UNSTABLE unless annotated with @Stable/@Immutable or @StabilityInferred(parameters=0)
1619

1720
---
1821

compose-stability-analyzer-idea/src/main/kotlin/com/skydoves/compose/stability/idea/k2/KtStabilityInferencer.kt

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,12 +598,44 @@ internal class KtStabilityInferencer {
598598
}
599599

600600
/**
601-
* TODO: Read @StabilityInferred parameters field using K2 Analysis API.
602-
* Returns null (conservative RUNTIME) until reliable API is found.
601+
* Reads the @StabilityInferred annotation's parameters field.
602+
*
603+
* @StabilityInferred is added by the Compose compiler to classes from other modules
604+
* to indicate their stability:
605+
* - parameters = 0: Class is stable
606+
* - parameters > 0: Class needs runtime stability check
607+
* - null: Annotation not present
608+
*
609+
* This is crucial for cross-module stability: classes from other modules should be
610+
* UNSTABLE unless annotated with @Stable/@Immutable or @StabilityInferred(parameters=0).
603611
*/
604612
context(KaSession)
605613
private fun KaClassSymbol.getStabilityInferredParameters(): Int? {
606-
return null
614+
val stabilityInferredFqName = "androidx.compose.runtime.internal.StabilityInferred"
615+
val annotation = annotations.firstOrNull { annotation ->
616+
annotation.classId?.asSingleFqName()?.asString() == stabilityInferredFqName
617+
} ?: return null
618+
619+
val parametersArgument = annotation.arguments.firstOrNull { arg ->
620+
arg.name.asString() == "parameters"
621+
}
622+
623+
// Extract the Int value from the constant expression
624+
return try {
625+
when (val expression = parametersArgument?.expression) {
626+
is org.jetbrains.kotlin.analysis.api.annotations.KaAnnotationValue.ConstantValue -> {
627+
// Get the constant value as Int
628+
(expression.value.value as? Int) ?: run {
629+
null
630+
}
631+
}
632+
else -> {
633+
null
634+
}
635+
}
636+
} catch (e: Exception) {
637+
null
638+
}
607639
}
608640

609641
/**

0 commit comments

Comments
 (0)