Merge "Use immutable collection for KSP" into androidx-main
diff --git a/room/compiler/src/main/kotlin/androidx/room/processor/Context.kt b/room/compiler/src/main/kotlin/androidx/room/processor/Context.kt
index 5bbeea0..484baaa 100644
--- a/room/compiler/src/main/kotlin/androidx/room/processor/Context.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/processor/Context.kt
@@ -101,8 +101,12 @@
         val STRING: XType by lazy {
             processingEnv.requireType("java.lang.String")
         }
-        val COLLECTION: XType by lazy {
-            processingEnv.requireType("java.util.Collection")
+        val READONLY_COLLECTION: XType by lazy {
+            if (processingEnv.backend == XProcessingEnv.Backend.KSP) {
+                processingEnv.requireType("kotlin.collections.Collection")
+            } else {
+                processingEnv.requireType("java.util.Collection")
+            }
         }
     }
 
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/TypeAdapterStore.kt b/room/compiler/src/main/kotlin/androidx/room/solver/TypeAdapterStore.kt
index b279c68..f12c2b1 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/TypeAdapterStore.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/TypeAdapterStore.kt
@@ -541,7 +541,7 @@
         typeMirror: XType,
         isMultipleParameter: Boolean
     ): QueryParameterAdapter? {
-        if (context.COMMON_TYPES.COLLECTION.rawType.isAssignableFrom(typeMirror)) {
+        if (context.COMMON_TYPES.READONLY_COLLECTION.rawType.isAssignableFrom(typeMirror)) {
             val typeArg = typeMirror.typeArguments.first().extendsBoundOrSelf()
             // An adapter for the collection type arg wrapped in the built-in collection adapter.
             val wrappedCollectionAdapter = findStatementValueBinder(typeArg, null)?.let {
diff --git a/room/compiler/src/test/kotlin/androidx/room/solver/TypeAdapterStoreTest.kt b/room/compiler/src/test/kotlin/androidx/room/solver/TypeAdapterStoreTest.kt
index 1632335..be4fd45 100644
--- a/room/compiler/src/test/kotlin/androidx/room/solver/TypeAdapterStoreTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/solver/TypeAdapterStoreTest.kt
@@ -42,6 +42,7 @@
 import androidx.room.solver.binderprovider.LiveDataQueryResultBinderProvider
 import androidx.room.solver.binderprovider.PagingSourceQueryResultBinderProvider
 import androidx.room.solver.binderprovider.RxQueryResultBinderProvider
+import androidx.room.solver.query.parameter.CollectionQueryParameterAdapter
 import androidx.room.solver.shortcut.binderprovider.GuavaListenableFutureDeleteOrUpdateMethodBinderProvider
 import androidx.room.solver.shortcut.binderprovider.GuavaListenableFutureInsertMethodBinderProvider
 import androidx.room.solver.shortcut.binderprovider.RxCallableDeleteOrUpdateMethodBinderProvider
@@ -843,6 +844,42 @@
         }
     }
 
+    @Test
+    fun findQueryParameterAdapter_collections() {
+        runProcessorTest { invocation ->
+            val store = TypeAdapterStore.create(
+                context = invocation.context
+            )
+            val javacCollectionTypes = listOf(
+                "java.util.Set",
+                "java.util.List",
+                "java.util.ArrayList"
+            )
+            val kotlinCollectionTypes = listOf(
+                "kotlin.collections.List",
+                "kotlin.collections.MutableList"
+            )
+            val collectionTypes = if (invocation.isKsp) {
+                javacCollectionTypes + kotlinCollectionTypes
+            } else {
+                javacCollectionTypes
+            }
+            collectionTypes.map { collectionType ->
+                invocation.processingEnv.getDeclaredType(
+                    invocation.processingEnv.requireTypeElement(collectionType),
+                    invocation.processingEnv.requireType(TypeName.INT).boxed()
+                )
+            }.forEach { type ->
+                val adapter = store.findQueryParameterAdapter(
+                    typeMirror = type,
+                    isMultipleParameter = true
+                )
+                assertThat(adapter).isNotNull()
+                assertThat(adapter).isInstanceOf(CollectionQueryParameterAdapter::class.java)
+            }
+        }
+    }
+
     private fun createIntListToStringBinders(invocation: XTestInvocation): List<TypeConverter> {
         val intType = invocation.processingEnv.requireType(Integer::class)
         val listElement = invocation.processingEnv.requireTypeElement(java.util.List::class)