xref: /aosp_15_r20/external/kotlinpoet/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Annotatable.kt (revision 3c321d951dd070fb96f8ba59e952ffc3131379a0)
1 /*
2  * Copyright (C) 2023 Square, Inc.
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  * https://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 com.squareup.kotlinpoet
17 
18 import kotlin.reflect.KClass
19 
20 /** A spec or type which can be annotated. */
21 public interface Annotatable {
22   public val annotations: List<AnnotationSpec>
23 
24   public interface Builder<out T : Builder<T>> {
25     public val annotations: MutableList<AnnotationSpec>
26 
27     @Suppress("UNCHECKED_CAST")
<lambda>null28     public fun addAnnotation(annotationSpec: AnnotationSpec): T = apply {
29       annotations += annotationSpec
30     } as T
31 
32     @Suppress("UNCHECKED_CAST")
addAnnotationsnull33     public fun addAnnotations(annotationSpecs: Iterable<AnnotationSpec>): T = apply {
34       for (annotationSpec in annotationSpecs) {
35         addAnnotation(annotationSpec)
36       }
37     } as T
38 
addAnnotationnull39     public fun addAnnotation(annotation: ClassName): T = addAnnotation(AnnotationSpec.builder(annotation).build())
40 
41     @DelicateKotlinPoetApi(
42       message = "Java reflection APIs don't give complete information on Kotlin types. Consider " +
43         "using the kotlinpoet-metadata APIs instead.",
44     )
45     public fun addAnnotation(annotation: Class<*>): T = addAnnotation(annotation.asClassName())
46 
47     public fun addAnnotation(annotation: KClass<*>): T = addAnnotation(annotation.asClassName())
48   }
49 }
50