Add mechanism to pass extra information from processors to type adapter store.
For now only methods pertaining to queries in the type adapter store will pipe the extras, if need be other methods can be updated later if needed.
Test: ./gradlew room:room-compiler:test
Change-Id: I53ef1aedf07f167cb2b2112ef5ccda64ef932b5f
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/processor/MethodProcessorDelegate.kt b/room/room-compiler/src/main/kotlin/androidx/room/processor/MethodProcessorDelegate.kt
index 0c727f7..e7443fb 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/processor/MethodProcessorDelegate.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/processor/MethodProcessorDelegate.kt
@@ -28,6 +28,7 @@
import androidx.room.ext.RoomCoroutinesTypeNames
import androidx.room.ext.T
import androidx.room.parser.ParsedQuery
+import androidx.room.solver.TypeAdapterExtras
import androidx.room.solver.prepared.binder.CallablePreparedQueryResultBinder.Companion.createPreparedBinder
import androidx.room.solver.prepared.binder.PreparedQueryResultBinder
import androidx.room.solver.query.result.CoroutineResultBinder
@@ -71,7 +72,11 @@
}
}
- abstract fun findResultBinder(returnType: XType, query: ParsedQuery): QueryResultBinder
+ abstract fun findResultBinder(
+ returnType: XType,
+ query: ParsedQuery,
+ extrasCreator: TypeAdapterExtras.() -> Unit
+ ): QueryResultBinder
abstract fun findPreparedResultBinder(
returnType: XType,
@@ -136,8 +141,11 @@
override fun extractParams() = executableElement.parameters
- override fun findResultBinder(returnType: XType, query: ParsedQuery) =
- context.typeAdapterStore.findQueryResultBinder(returnType, query)
+ override fun findResultBinder(
+ returnType: XType,
+ query: ParsedQuery,
+ extrasCreator: TypeAdapterExtras.() -> Unit
+ ) = context.typeAdapterStore.findQueryResultBinder(returnType, query, extrasCreator)
override fun findPreparedResultBinder(
returnType: XType,
@@ -185,10 +193,15 @@
it == continuationParam
}
- override fun findResultBinder(returnType: XType, query: ParsedQuery) =
+ override fun findResultBinder(
+ returnType: XType,
+ query: ParsedQuery,
+ extrasCreator: TypeAdapterExtras.() -> Unit
+ ) =
CoroutineResultBinder(
typeArg = returnType,
- adapter = context.typeAdapterStore.findQueryResultAdapter(returnType, query),
+ adapter =
+ context.typeAdapterStore.findQueryResultAdapter(returnType, query, extrasCreator),
continuationParamName = continuationParam.name
)
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/processor/QueryMethodProcessor.kt b/room/room-compiler/src/main/kotlin/androidx/room/processor/QueryMethodProcessor.kt
index ebd0e81..55b23e7 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/processor/QueryMethodProcessor.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/processor/QueryMethodProcessor.kt
@@ -201,7 +201,7 @@
returnType: XType,
query: ParsedQuery
): QueryMethod {
- val resultBinder = delegate.findResultBinder(returnType, query)
+ val resultBinder = delegate.findResultBinder(returnType, query) { }
context.checker.check(
resultBinder.adapter != null,
executableElement,
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/processor/RawQueryMethodProcessor.kt b/room/room-compiler/src/main/kotlin/androidx/room/processor/RawQueryMethodProcessor.kt
index 39377be8..b8f99b74 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/processor/RawQueryMethodProcessor.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/processor/RawQueryMethodProcessor.kt
@@ -52,7 +52,7 @@
val observedTableNames = processObservedTables()
val query = SqlParser.rawQueryForTables(observedTableNames)
// build the query but don't calculate result info since we just guessed it.
- val resultBinder = delegate.findResultBinder(returnType, query)
+ val resultBinder = delegate.findResultBinder(returnType, query) { }
val runtimeQueryParam = findRuntimeQueryParameter(delegate.extractParams())
val inTransaction = executableElement.hasAnnotation(Transaction::class)
val rawQueryMethod = RawQueryMethod(
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/solver/ObservableQueryResultBinderProvider.kt b/room/room-compiler/src/main/kotlin/androidx/room/solver/ObservableQueryResultBinderProvider.kt
index 6d46bed..3b09a3d 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/solver/ObservableQueryResultBinderProvider.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/solver/ObservableQueryResultBinderProvider.kt
@@ -16,8 +16,8 @@
package androidx.room.solver
-import androidx.room.parser.ParsedQuery
import androidx.room.compiler.processing.XType
+import androidx.room.parser.ParsedQuery
import androidx.room.processor.Context
import androidx.room.processor.ProcessorErrors
import androidx.room.solver.query.result.QueryResultAdapter
@@ -35,9 +35,13 @@
tableNames: Set<String>
): QueryResultBinder
- final override fun provide(declared: XType, query: ParsedQuery): QueryResultBinder {
+ final override fun provide(
+ declared: XType,
+ query: ParsedQuery,
+ extras: TypeAdapterExtras
+ ): QueryResultBinder {
val typeArg = extractTypeArg(declared)
- val adapter = context.typeAdapterStore.findQueryResultAdapter(typeArg, query)
+ val adapter = context.typeAdapterStore.findQueryResultAdapter(typeArg, query, extras)
val tableNames = (
(adapter?.accessedTableNames() ?: emptyList()) +
query.tables.map { it.name }
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/solver/QueryResultBinderProvider.kt b/room/room-compiler/src/main/kotlin/androidx/room/solver/QueryResultBinderProvider.kt
index 696433e..cd5bc03 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/solver/QueryResultBinderProvider.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/solver/QueryResultBinderProvider.kt
@@ -21,6 +21,11 @@
import androidx.room.solver.query.result.QueryResultBinder
interface QueryResultBinderProvider {
- fun provide(declared: XType, query: ParsedQuery): QueryResultBinder
+ fun provide(
+ declared: XType,
+ query: ParsedQuery,
+ extras: TypeAdapterExtras
+ ): QueryResultBinder
+
fun matches(declared: XType): Boolean
}
\ No newline at end of file
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/solver/TypeAdapterExtras.kt b/room/room-compiler/src/main/kotlin/androidx/room/solver/TypeAdapterExtras.kt
new file mode 100644
index 0000000..c485ff5
--- /dev/null
+++ b/room/room-compiler/src/main/kotlin/androidx/room/solver/TypeAdapterExtras.kt
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2021 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.room.solver
+
+import kotlin.reflect.KClass
+
+/**
+ * Bag of objects to pass additional information to the [TypeAdapterStore].
+ */
+class TypeAdapterExtras {
+ private val data = mutableMapOf<KClass<*>, Any>()
+
+ fun <T : Any> getData(key: KClass<T>): T? {
+ @Suppress("UNCHECKED_CAST")
+ return data[key] as T?
+ }
+
+ fun <T : Any> putData(key: KClass<T>, value: T) {
+ data[key] = value
+ }
+}
\ No newline at end of file
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/solver/TypeAdapterStore.kt b/room/room-compiler/src/main/kotlin/androidx/room/solver/TypeAdapterStore.kt
index ae7a141..4d19b49 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/solver/TypeAdapterStore.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/solver/TypeAdapterStore.kt
@@ -397,10 +397,22 @@
}.provide(typeMirror, params)
}
- fun findQueryResultBinder(typeMirror: XType, query: ParsedQuery): QueryResultBinder {
+ fun findQueryResultBinder(
+ typeMirror: XType,
+ query: ParsedQuery,
+ extrasCreator: TypeAdapterExtras.() -> Unit = { }
+ ): QueryResultBinder {
+ return findQueryResultBinder(typeMirror, query, TypeAdapterExtras().apply(extrasCreator))
+ }
+
+ fun findQueryResultBinder(
+ typeMirror: XType,
+ query: ParsedQuery,
+ extras: TypeAdapterExtras
+ ): QueryResultBinder {
return queryResultBinderProviders.first {
it.matches(typeMirror)
- }.provide(typeMirror, query)
+ }.provide(typeMirror, query, extras)
}
fun findPreparedQueryResultBinder(
@@ -426,7 +438,19 @@
return InsertMethodAdapter.create(typeMirror, params)
}
- fun findQueryResultAdapter(typeMirror: XType, query: ParsedQuery): QueryResultAdapter? {
+ fun findQueryResultAdapter(
+ typeMirror: XType,
+ query: ParsedQuery,
+ extrasCreator: TypeAdapterExtras.() -> Unit = { }
+ ): QueryResultAdapter? {
+ return findQueryResultAdapter(typeMirror, query, TypeAdapterExtras().apply(extrasCreator))
+ }
+
+ fun findQueryResultAdapter(
+ typeMirror: XType,
+ query: ParsedQuery,
+ extras: TypeAdapterExtras
+ ): QueryResultAdapter? {
if (typeMirror.isError()) {
return null
}
@@ -485,7 +509,7 @@
keyTypeArg,
valueTypeArg
)
- val resultAdapter = findQueryResultAdapter(mapType, query = query) ?: return null
+ val resultAdapter = findQueryResultAdapter(mapType, query, extras) ?: return null
return ImmutableMapQueryResultAdapter(
keyTypeArg = keyTypeArg,
valueTypeArg = valueTypeArg,
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/CoroutineFlowResultBinderProvider.kt b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/CoroutineFlowResultBinderProvider.kt
index 00807f1..93a742d 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/CoroutineFlowResultBinderProvider.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/CoroutineFlowResultBinderProvider.kt
@@ -23,6 +23,7 @@
import androidx.room.processor.Context
import androidx.room.processor.ProcessorErrors
import androidx.room.solver.QueryResultBinderProvider
+import androidx.room.solver.TypeAdapterExtras
import androidx.room.solver.query.result.CoroutineFlowResultBinder
import androidx.room.solver.query.result.QueryResultBinder
@@ -47,9 +48,13 @@
)
}
- override fun provide(declared: XType, query: ParsedQuery): QueryResultBinder {
+ override fun provide(
+ declared: XType,
+ query: ParsedQuery,
+ extras: TypeAdapterExtras
+ ): QueryResultBinder {
val typeArg = declared.typeArguments.first()
- val adapter = context.typeAdapterStore.findQueryResultAdapter(typeArg, query)
+ val adapter = context.typeAdapterStore.findQueryResultAdapter(typeArg, query, extras)
val tableNames = (
(adapter?.accessedTableNames() ?: emptyList()) +
query.tables.map { it.name }
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/CursorQueryResultBinderProvider.kt b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/CursorQueryResultBinderProvider.kt
index 20f1bb7..fb889c4 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/CursorQueryResultBinderProvider.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/CursorQueryResultBinderProvider.kt
@@ -21,11 +21,16 @@
import androidx.room.parser.ParsedQuery
import androidx.room.processor.Context
import androidx.room.solver.QueryResultBinderProvider
+import androidx.room.solver.TypeAdapterExtras
import androidx.room.solver.query.result.CursorQueryResultBinder
import androidx.room.solver.query.result.QueryResultBinder
class CursorQueryResultBinderProvider(val context: Context) : QueryResultBinderProvider {
- override fun provide(declared: XType, query: ParsedQuery): QueryResultBinder {
+ override fun provide(
+ declared: XType,
+ query: ParsedQuery,
+ extras: TypeAdapterExtras
+ ): QueryResultBinder {
return CursorQueryResultBinder()
}
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceFactoryQueryResultBinderProvider.kt b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceFactoryQueryResultBinderProvider.kt
index fa97042..88dabbe 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceFactoryQueryResultBinderProvider.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceFactoryQueryResultBinderProvider.kt
@@ -23,6 +23,7 @@
import androidx.room.processor.Context
import androidx.room.processor.ProcessorErrors
import androidx.room.solver.QueryResultBinderProvider
+import androidx.room.solver.TypeAdapterExtras
import androidx.room.solver.query.result.DataSourceFactoryQueryResultBinder
import androidx.room.solver.query.result.ListQueryResultAdapter
import androidx.room.solver.query.result.PositionalDataSourceQueryResultBinder
@@ -33,7 +34,11 @@
context.processingEnv.findType(PagingTypeNames.DATA_SOURCE_FACTORY)?.rawType
}
- override fun provide(declared: XType, query: ParsedQuery): QueryResultBinder {
+ override fun provide(
+ declared: XType,
+ query: ParsedQuery,
+ extras: TypeAdapterExtras
+ ): QueryResultBinder {
if (query.tables.isEmpty()) {
context.logger.e(ProcessorErrors.OBSERVABLE_QUERY_NOTHING_TO_OBSERVE)
}
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceQueryResultBinderProvider.kt b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceQueryResultBinderProvider.kt
index 1250b87..a3c2db3f 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceQueryResultBinderProvider.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceQueryResultBinderProvider.kt
@@ -16,13 +16,14 @@
package androidx.room.solver.binderprovider
-import androidx.room.ext.PagingTypeNames
-import androidx.room.parser.ParsedQuery
import androidx.room.compiler.processing.XRawType
import androidx.room.compiler.processing.XType
+import androidx.room.ext.PagingTypeNames
+import androidx.room.parser.ParsedQuery
import androidx.room.processor.Context
import androidx.room.processor.ProcessorErrors
import androidx.room.solver.QueryResultBinderProvider
+import androidx.room.solver.TypeAdapterExtras
import androidx.room.solver.query.result.ListQueryResultAdapter
import androidx.room.solver.query.result.PositionalDataSourceQueryResultBinder
import androidx.room.solver.query.result.QueryResultBinder
@@ -36,7 +37,11 @@
context.processingEnv.findType(PagingTypeNames.POSITIONAL_DATA_SOURCE)?.rawType
}
- override fun provide(declared: XType, query: ParsedQuery): QueryResultBinder {
+ override fun provide(
+ declared: XType,
+ query: ParsedQuery,
+ extras: TypeAdapterExtras
+ ): QueryResultBinder {
if (query.tables.isEmpty()) {
context.logger.e(ProcessorErrors.OBSERVABLE_QUERY_NOTHING_TO_OBSERVE)
}
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/GuavaListenableFutureQueryResultBinderProvider.kt b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/GuavaListenableFutureQueryResultBinderProvider.kt
index 783bed2..967a583 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/GuavaListenableFutureQueryResultBinderProvider.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/GuavaListenableFutureQueryResultBinderProvider.kt
@@ -23,6 +23,7 @@
import androidx.room.processor.Context
import androidx.room.processor.ProcessorErrors
import androidx.room.solver.QueryResultBinderProvider
+import androidx.room.solver.TypeAdapterExtras
import androidx.room.solver.query.result.GuavaListenableFutureQueryResultBinder
import androidx.room.solver.query.result.QueryResultBinder
@@ -45,11 +46,15 @@
*
* <p>Emits a compiler error if the Guava Room extension library is not linked.
*/
- override fun provide(declared: XType, query: ParsedQuery): QueryResultBinder {
+ override fun provide(
+ declared: XType,
+ query: ParsedQuery,
+ extras: TypeAdapterExtras
+ ): QueryResultBinder {
// Use the type T inside ListenableFuture<T> as the type to adapt and to pass into
// the binder.
val adapter = context.typeAdapterStore.findQueryResultAdapter(
- declared.typeArguments.first(), query
+ declared.typeArguments.first(), query, extras
)
return GuavaListenableFutureQueryResultBinder(
declared.typeArguments.first(), adapter
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/InstantQueryResultBinderProvider.kt b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/InstantQueryResultBinderProvider.kt
index 7883db5..f799fd3 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/InstantQueryResultBinderProvider.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/InstantQueryResultBinderProvider.kt
@@ -20,13 +20,18 @@
import androidx.room.parser.ParsedQuery
import androidx.room.processor.Context
import androidx.room.solver.QueryResultBinderProvider
+import androidx.room.solver.TypeAdapterExtras
import androidx.room.solver.query.result.InstantQueryResultBinder
import androidx.room.solver.query.result.QueryResultBinder
class InstantQueryResultBinderProvider(val context: Context) : QueryResultBinderProvider {
- override fun provide(declared: XType, query: ParsedQuery): QueryResultBinder {
+ override fun provide(
+ declared: XType,
+ query: ParsedQuery,
+ extras: TypeAdapterExtras
+ ): QueryResultBinder {
return InstantQueryResultBinder(
- context.typeAdapterStore.findQueryResultAdapter(declared, query)
+ context.typeAdapterStore.findQueryResultAdapter(declared, query, extras)
)
}
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 a183b92..13c9967 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
@@ -24,6 +24,7 @@
import androidx.room.processor.Context
import androidx.room.processor.ProcessorErrors
import androidx.room.solver.QueryResultBinderProvider
+import androidx.room.solver.TypeAdapterExtras
import androidx.room.solver.query.result.ListQueryResultAdapter
import androidx.room.solver.query.result.PagingSourceQueryResultBinder
import androidx.room.solver.query.result.QueryResultBinder
@@ -46,7 +47,11 @@
context.processingEnv.findType(PagingTypeNames.PAGING_SOURCE)?.rawType
}
- override fun provide(declared: XType, query: ParsedQuery): QueryResultBinder {
+ override fun provide(
+ declared: XType,
+ query: ParsedQuery,
+ extras: TypeAdapterExtras
+ ): QueryResultBinder {
if (query.tables.isEmpty()) {
context.logger.e(ProcessorErrors.OBSERVABLE_QUERY_NOTHING_TO_OBSERVE)
}
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/QueryResultBinderProviderWithRequiredArtifact.kt b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/QueryResultBinderProviderWithRequiredArtifact.kt
index 4965800..c8f99a7 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/QueryResultBinderProviderWithRequiredArtifact.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/QueryResultBinderProviderWithRequiredArtifact.kt
@@ -19,6 +19,7 @@
import androidx.room.parser.ParsedQuery
import androidx.room.processor.Context
import androidx.room.solver.QueryResultBinderProvider
+import androidx.room.solver.TypeAdapterExtras
import androidx.room.solver.query.result.QueryResultBinder
import com.squareup.javapoet.TypeName
@@ -46,8 +47,12 @@
context.processingEnv.findTypeElement(requiredType) != null
}
- override fun provide(declared: XType, query: ParsedQuery): QueryResultBinder {
- return delegate.provide(declared, query)
+ override fun provide(
+ declared: XType,
+ query: ParsedQuery,
+ extras: TypeAdapterExtras
+ ): QueryResultBinder {
+ return delegate.provide(declared, query, extras)
}
override fun matches(declared: XType): Boolean {
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/RxCallableQueryResultBinderProvider.kt b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/RxCallableQueryResultBinderProvider.kt
index cbe657e..57a31c5 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/RxCallableQueryResultBinderProvider.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/solver/binderprovider/RxCallableQueryResultBinderProvider.kt
@@ -21,6 +21,7 @@
import androidx.room.processor.Context
import androidx.room.solver.QueryResultBinderProvider
import androidx.room.solver.RxType
+import androidx.room.solver.TypeAdapterExtras
import androidx.room.solver.query.result.QueryResultBinder
import androidx.room.solver.query.result.RxCallableQueryResultBinder
@@ -28,9 +29,13 @@
val context: Context,
private val rxType: RxType
) : QueryResultBinderProvider {
- override fun provide(declared: XType, query: ParsedQuery): QueryResultBinder {
+ override fun provide(
+ declared: XType,
+ query: ParsedQuery,
+ extras: TypeAdapterExtras
+ ): QueryResultBinder {
val typeArg = declared.typeArguments.first()
- val adapter = context.typeAdapterStore.findQueryResultAdapter(typeArg, query)
+ val adapter = context.typeAdapterStore.findQueryResultAdapter(typeArg, query, extras)
return RxCallableQueryResultBinder(rxType, typeArg, adapter)
}