PagingSource type providers to use generalized binder
Replace existing PagingSourceQueryResultBinder with a generalized
version that can be used by multiple PagingSource-typed providers
by passing in the relevant class name
i.e. ListenableFuturePagingSource, PagingSource.
This allows reusing the same binder by other room-paging variants.
Test: ./gradlew :room:integration-tests:room-testapp-kotlin:cC
Test: ./gradles :room:room-compiler:test
Change-Id: Idaff63986f8eb456c1c605e4abf3a5680d99c726
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/PagingSourceQueryResultBinderProvider.kt b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/PagingSourceQueryResultBinderProvider.kt
index 13c9967..32add64 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/PagingSourceQueryResultBinderProvider.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/PagingSourceQueryResultBinderProvider.kt
@@ -25,8 +25,8 @@
import androidx.room.processor.ProcessorErrors
import androidx.room.solver.QueryResultBinderProvider
import androidx.room.solver.TypeAdapterExtras
+import androidx.room.solver.query.result.MultiTypedPagingSourceQueryResultBinder
import androidx.room.solver.query.result.ListQueryResultAdapter
-import androidx.room.solver.query.result.PagingSourceQueryResultBinder
import androidx.room.solver.query.result.QueryResultBinder
import com.squareup.javapoet.TypeName
@@ -63,10 +63,10 @@
(listAdapter?.accessedTableNames() ?: emptyList()) +
query.tables.map { it.name }
).toSet()
-
- return PagingSourceQueryResultBinder(
+ return MultiTypedPagingSourceQueryResultBinder(
listAdapter = listAdapter,
tableNames = tableNames,
+ className = RoomPagingTypeNames.LIMIT_OFFSET_PAGING_SOURCE
)
}
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/solver/query/result/PagingSourceQueryResultBinder.kt b/room/room-compiler/src/main/kotlin/androidx/room/solver/query/result/MultiTypedPagingSourceQueryResultBinder.kt
similarity index 79%
rename from room/room-compiler/src/main/kotlin/androidx/room/solver/query/result/PagingSourceQueryResultBinder.kt
rename to room/room-compiler/src/main/kotlin/androidx/room/solver/query/result/MultiTypedPagingSourceQueryResultBinder.kt
index 0220184..e88f895 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/solver/query/result/PagingSourceQueryResultBinder.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/solver/query/result/MultiTypedPagingSourceQueryResultBinder.kt
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2017 The Android Open Source Project
+ * Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,8 +20,8 @@
import androidx.room.ext.CommonTypeNames
import androidx.room.ext.L
import androidx.room.ext.N
-import androidx.room.ext.RoomPagingTypeNames
import androidx.room.solver.CodeGenScope
+import com.squareup.javapoet.ClassName
import com.squareup.javapoet.FieldSpec
import com.squareup.javapoet.MethodSpec
import com.squareup.javapoet.ParameterSpec
@@ -31,17 +31,21 @@
import javax.lang.model.element.Modifier
/**
- * This Binder uses room/room-paging artifact and binds queries directly to native Paging3
- * PagingSource through `LimitOffsetPagingSource`. Used solely by Paging3.
+ * This Binder binds queries directly to native Paging3
+ * PagingSource (i.e. [LimitOffsetPagingSource]) or its subclasses such as
+ * [LimitOffsetListenableFuturePagingSource]. Used solely by Paging3.
*/
-class PagingSourceQueryResultBinder(
+class MultiTypedPagingSourceQueryResultBinder(
private val listAdapter: ListQueryResultAdapter?,
private val tableNames: Set<String>,
+ className: ClassName
) : QueryResultBinder(listAdapter) {
+
private val itemTypeName: TypeName =
listAdapter?.rowAdapters?.firstOrNull()?.out?.typeName ?: TypeName.OBJECT
- private val limitOffsetPagingSourceTypeNam: ParameterizedTypeName = ParameterizedTypeName.get(
- RoomPagingTypeNames.LIMIT_OFFSET_PAGING_SOURCE, itemTypeName
+
+ private val pagingSourceTypeName: ParameterizedTypeName = ParameterizedTypeName.get(
+ className, itemTypeName
)
override fun convertAndReturn(
@@ -53,16 +57,16 @@
) {
scope.builder().apply {
val tableNamesList = tableNames.joinToString(", ") { "\"$it\"" }
- val limitOffsetPagingSourceSpec = TypeSpec.anonymousClassBuilder(
+ val pagingSourceSpec = TypeSpec.anonymousClassBuilder(
"$L, $N, $L",
roomSQLiteQueryVar,
dbField,
tableNamesList
).apply {
- addSuperinterface(limitOffsetPagingSourceTypeNam)
+ addSuperinterface(pagingSourceTypeName)
addMethod(createConvertRowsMethod(scope))
}.build()
- addStatement("return $L", limitOffsetPagingSourceSpec)
+ addStatement("return $L", pagingSourceSpec)
}
}
diff --git a/room/room-compiler/src/test/kotlin/androidx/room/solver/TypeAdapterStoreTest.kt b/room/room-compiler/src/test/kotlin/androidx/room/solver/TypeAdapterStoreTest.kt
index 5a958c7..3208dfa 100644
--- a/room/room-compiler/src/test/kotlin/androidx/room/solver/TypeAdapterStoreTest.kt
+++ b/room/room-compiler/src/test/kotlin/androidx/room/solver/TypeAdapterStoreTest.kt
@@ -21,6 +21,7 @@
import androidx.paging.PagingSource
import androidx.room.Dao
import androidx.room.compiler.processing.XProcessingEnv
+import androidx.room.compiler.processing.XRawType
import androidx.room.compiler.processing.isTypeElement
import androidx.room.compiler.processing.util.Source
import androidx.room.compiler.processing.util.XTestInvocation
@@ -49,7 +50,7 @@
import androidx.room.solver.binderprovider.PagingSourceQueryResultBinderProvider
import androidx.room.solver.binderprovider.RxQueryResultBinderProvider
import androidx.room.solver.query.parameter.CollectionQueryParameterAdapter
-import androidx.room.solver.query.result.PagingSourceQueryResultBinder
+import androidx.room.solver.query.result.MultiTypedPagingSourceQueryResultBinder
import androidx.room.solver.shortcut.binderprovider.GuavaListenableFutureDeleteOrUpdateMethodBinderProvider
import androidx.room.solver.shortcut.binderprovider.GuavaListenableFutureInsertMethodBinderProvider
import androidx.room.solver.shortcut.binderprovider.RxCallableDeleteOrUpdateMethodBinderProvider
@@ -861,7 +862,7 @@
}
@Test
- fun testNewPagingSourceBinder() {
+ fun testPagingSourceBinder() {
val inputSource =
Source.java(
qName = "foo.bar.MyDao",
@@ -897,7 +898,14 @@
val parsedDao = parser.process()
val binder = parsedDao.queryMethods.filterIsInstance<ReadQueryMethod>()
.first().queryResultBinder
- assertThat(binder is PagingSourceQueryResultBinder).isTrue()
+ assertThat(binder is MultiTypedPagingSourceQueryResultBinder).isTrue()
+
+ val pagingSourceXRawType: XRawType? = invocation.context.processingEnv
+ .findType(PagingTypeNames.PAGING_SOURCE)?.rawType
+ val returnedXRawType = parsedDao.queryMethods
+ .filterIsInstance<ReadQueryMethod>().first().returnType.rawType
+ // make sure returned type is the original PagingSource
+ assertThat(returnedXRawType).isEqualTo(pagingSourceXRawType)
}
}