1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.room.solver.binderprovider 18 19 import androidx.room.ext.RoomRxJava2TypeNames 20 import androidx.room.ext.typeName 21 import androidx.room.parser.ParsedQuery 22 import androidx.room.processor.Context 23 import androidx.room.processor.ProcessorErrors 24 import androidx.room.solver.QueryResultBinderProvider 25 import androidx.room.solver.query.result.QueryResultBinder 26 import androidx.room.solver.query.result.RxCallableQueryResultBinder 27 import javax.lang.model.type.DeclaredType 28 29 sealed class RxCallableQueryResultBinderProvider(val context: Context, 30 val rxType: RxCallableQueryResultBinder.RxType) 31 : QueryResultBinderProvider { <lambda>null32 private val hasRxJava2Artifact by lazy { 33 context.processingEnv.elementUtils 34 .getTypeElement(RoomRxJava2TypeNames.RX_ROOM.toString()) != null 35 } 36 providenull37 override fun provide(declared: DeclaredType, query: ParsedQuery): QueryResultBinder { 38 val typeArg = declared.typeArguments.first() 39 val adapter = context.typeAdapterStore.findQueryResultAdapter(typeArg, query) 40 return RxCallableQueryResultBinder(rxType, typeArg, adapter) 41 } 42 matchesnull43 override fun matches(declared: DeclaredType): Boolean = 44 declared.typeArguments.size == 1 && matchesRxType(declared) 45 46 private fun matchesRxType(declared: DeclaredType): Boolean { 47 val erasure = context.processingEnv.typeUtils.erasure(declared) 48 val match = erasure.typeName() == rxType.className 49 if (match && !hasRxJava2Artifact) { 50 context.logger.e(ProcessorErrors.MISSING_ROOM_RXJAVA2_ARTIFACT) 51 } 52 return match 53 } 54 } 55 56 class RxSingleQueryResultBinderProvider(context: Context) 57 : RxCallableQueryResultBinderProvider(context, RxCallableQueryResultBinder.RxType.SINGLE) 58 59 class RxMaybeQueryResultBinderProvider(context: Context) 60 : RxCallableQueryResultBinderProvider(context, RxCallableQueryResultBinder.RxType.MAYBE) 61