1 /*
2  * Copyright 2024 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.appsearch.compiler.annotationwrapper
17 
18 import androidx.appsearch.compiler.IntrospectionHelper
19 import androidx.appsearch.compiler.ProcessingException
20 import com.squareup.javapoet.ClassName
21 import javax.lang.model.type.TypeMirror
22 
23 /** An instance of the `@Document.BlobHandleProperty` annotation. */
24 data class BlobHandlePropertyAnnotation(
25     override val name: String,
26     override val isRequired: Boolean,
27 ) :
28     DataPropertyAnnotation(
29         className = CLASS_NAME,
30         configClassName = CONFIG_CLASS,
31         genericDocGetterName = "getPropertyBlobHandle",
32         genericDocArrayGetterName = "getPropertyBlobHandleArray",
33         genericDocSetterName = "setPropertyBlobHandle",
34     ) {
35     companion object {
36         val CLASS_NAME: ClassName =
37             IntrospectionHelper.DOCUMENT_ANNOTATION_CLASS.nestedClass("BlobHandleProperty")
38 
39         val CONFIG_CLASS: ClassName =
40             IntrospectionHelper.APPSEARCH_SCHEMA_CLASS.nestedClass("BlobHandlePropertyConfig")
41 
42         /**
43          * Creates a [BlobHandlePropertyAnnotation] with given params.
44          *
45          * @param defaultName The name to use for the annotated property in case the annotation
46          *   params do not mention an explicit name.
47          * @throws ProcessingException If the annotation points to an Illegal serializer class.
48          */
49         @Throws(ProcessingException::class)
parsenull50         fun parse(
51             annotationParams: Map<String, Any?>,
52             defaultName: String
53         ): BlobHandlePropertyAnnotation {
54             val name = annotationParams["name"] as? String
55             return BlobHandlePropertyAnnotation(
56                 name = if (name.isNullOrEmpty()) defaultName else name,
57                 isRequired = annotationParams["required"] as Boolean,
58             )
59         }
60     }
61 
62     override val dataPropertyKind
63         get() = Kind.BLOB_HANDLE_PROPERTY
64 
getUnderlyingTypeWithinGenericDocnull65     override fun getUnderlyingTypeWithinGenericDoc(helper: IntrospectionHelper): TypeMirror =
66         helper.blobHandleType
67 }
68