1 /*
2 * Copyright 2020 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 package androidx.room.solver.binderprovider
17
18 import androidx.room.compiler.codegen.XClassName
19 import androidx.room.compiler.processing.XType
20 import androidx.room.parser.ParsedQuery
21 import androidx.room.processor.Context
22 import androidx.room.solver.QueryResultBinderProvider
23 import androidx.room.solver.TypeAdapterExtras
24 import androidx.room.solver.query.result.QueryResultBinder
25
26 /** Common functionality for binder providers that require an additional artifact */
QueryResultBinderProvidernull27 fun QueryResultBinderProvider.requireArtifact(
28 context: Context,
29 requiredType: XClassName,
30 missingArtifactErrorMsg: String
31 ): QueryResultBinderProvider =
32 QueryResultBinderProviderWithRequiredArtifact(
33 context = context,
34 requiredType = requiredType,
35 missingArtifactErrorMsg = missingArtifactErrorMsg,
36 delegate = this
37 )
38
39 private class QueryResultBinderProviderWithRequiredArtifact(
40 val context: Context,
41 val requiredType: XClassName,
42 val missingArtifactErrorMsg: String,
43 val delegate: QueryResultBinderProvider
44 ) : QueryResultBinderProvider {
45 private val hasRequiredArtifact by
46 lazy(LazyThreadSafetyMode.NONE) {
47 context.processingEnv.findTypeElement(requiredType.canonicalName) != null
48 }
49
50 override fun provide(
51 declared: XType,
52 query: ParsedQuery,
53 extras: TypeAdapterExtras
54 ): QueryResultBinder {
55 return delegate.provide(declared, query, extras)
56 }
57
58 override fun matches(declared: XType): Boolean {
59 val result = delegate.matches(declared)
60 if (result && !hasRequiredArtifact) {
61 context.logger.e(missingArtifactErrorMsg)
62 }
63 return result
64 }
65 }
66