cache KspTypeElement constructors
Test: tested with XTypeElementTest.
Change-Id: I24e7d1e40b0cb480e6fafa946940f9beeea72ace
diff --git a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspTypeElement.kt b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspTypeElement.kt
index a10ed56b..93b7fb7 100644
--- a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspTypeElement.kt
+++ b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspTypeElement.kt
@@ -181,6 +181,39 @@
}
}
+ private val _constructors by lazy {
+ if (isAnnotationClass()) {
+ emptyList()
+ } else {
+ val constructors = declaration.getConstructors().toList()
+ buildList {
+ addAll(
+ constructors.map { env.wrapFunctionDeclaration(it) as XConstructorElement }
+ )
+ constructors
+ .filter { it.hasOverloads() }
+ .forEach { addAll(enumerateSyntheticConstructors(it)) }
+
+ // To match KAPT if all params in the primary constructor have default values then
+ // synthesize a no-arg constructor if one is not already present.
+ val hasNoArgConstructor = constructors.any { it.parameters.isEmpty() }
+ if (!hasNoArgConstructor) {
+ declaration.primaryConstructor?.let {
+ if (!it.hasOverloads() && it.parameters.all { it.hasDefault }) {
+ add(
+ KspSyntheticConstructorElement(
+ env = env,
+ declaration = it,
+ valueParameters = emptyList()
+ )
+ )
+ }
+ }
+ }
+ }
+ }
+ }
+
private val _declaredFields by lazy {
_declaredProperties.filter {
it.declaration.hasBackingField
@@ -328,36 +361,7 @@
}
override fun getConstructors(): List<XConstructorElement> {
- if (isAnnotationClass()) {
- return emptyList()
- }
- val constructors = declaration.getConstructors().toList()
-
- return buildList {
- addAll(
- constructors.map { env.wrapFunctionDeclaration(it) as XConstructorElement }
- )
- constructors
- .filter { it.hasOverloads() }
- .forEach { addAll(enumerateSyntheticConstructors(it)) }
-
- // To match KAPT if all params in the primary constructor have default values then
- // synthesize a no-arg constructor if one is not already present.
- val hasNoArgConstructor = constructors.any { it.parameters.isEmpty() }
- if (!hasNoArgConstructor) {
- declaration.primaryConstructor?.let {
- if (!it.hasOverloads() && it.parameters.all { it.hasDefault }) {
- add(
- KspSyntheticConstructorElement(
- env = env,
- declaration = it,
- valueParameters = emptyList()
- )
- )
- }
- }
- }
- }
+ return _constructors
}
private fun enumerateSyntheticConstructors(