Handle @Transient annotation in KSP
Similar to @JvmStatic, @Transient is an annotation in Kotlin. This CL
updates KspHasModifiers to check the annotation when reporting
transient.
Bug: 160322705
Test: XElementTest
Change-Id: Ic01caff24f062275bf40cad242540b2bc7fd386e
diff --git a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSAnnotatedExt.kt b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSAnnotatedExt.kt
index b7ec701..f9c71d0 100644
--- a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSAnnotatedExt.kt
+++ b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSAnnotatedExt.kt
@@ -29,6 +29,9 @@
internal fun KSAnnotated.hasJvmStaticAnnotation() = hasAnnotationWithQName("kotlin.jvm.JvmStatic")
+internal fun KSAnnotated.hasJvmTransientAnnotation() =
+ hasAnnotationWithQName("kotlin.jvm.Transient")
+
internal fun KSAnnotated.hasJvmFieldAnnotation() = hasAnnotationWithQName("kotlin.jvm.JvmField")
internal fun KSAnnotated.hasJvmDefaultAnnotation() = hasAnnotationWithQName("kotlin.jvm.JvmDefault")
diff --git a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSDeclarationExt.kt b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSDeclarationExt.kt
index 206f312..054c840 100644
--- a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSDeclarationExt.kt
+++ b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSDeclarationExt.kt
@@ -53,4 +53,8 @@
internal fun KSDeclaration.isStatic(): Boolean {
return modifiers.contains(Modifier.JAVA_STATIC) || hasJvmStaticAnnotation()
+}
+
+internal fun KSDeclaration.isTransient(): Boolean {
+ return modifiers.contains(Modifier.JAVA_TRANSIENT) || hasJvmTransientAnnotation()
}
\ No newline at end of file
diff --git a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspHasModifiers.kt b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspHasModifiers.kt
index dd40fc9..298222a 100644
--- a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspHasModifiers.kt
+++ b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspHasModifiers.kt
@@ -66,7 +66,7 @@
}
override fun isTransient(): Boolean {
- return declaration.modifiers.contains(Modifier.JAVA_TRANSIENT)
+ return declaration.isTransient()
}
override fun isFinal(): Boolean {
diff --git a/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XElementTest.kt b/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XElementTest.kt
index ef5faf8..1f0a254 100644
--- a/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XElementTest.kt
+++ b/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XElementTest.kt
@@ -35,6 +35,29 @@
@RunWith(JUnit4::class)
class XElementTest {
@Test
+ fun kotlinAnnotationModifierrs() {
+ val src = Source.kotlin(
+ "Subject.kt",
+ """
+ object Subject {
+ @Transient val transientProp:Int = 0
+ @JvmStatic val staticProp:Int = 0
+ }
+ """.trimIndent()
+ )
+ runProcessorTest(sources = listOf(src)) { invocation ->
+ invocation.processingEnv.requireTypeElement("Subject").let {
+ assertThat(
+ it.getField("transientProp").isTransient()
+ ).isTrue()
+ assertThat(
+ it.getField("staticProp").isStatic()
+ ).isTrue()
+ }
+ }
+ }
+
+ @Test
fun modifiers() {
runProcessorTest(
listOf(