1 /*
2  * Copyright (C) 2024 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 java.lang.reflect.Type
19 import kotlin.reflect.KClass
20 
21 /** A spec which can contain [PropertySpec]s and [FunSpec]s. */
22 public interface MemberSpecHolder {
23   public val propertySpecs: List<PropertySpec>
24   public val funSpecs: List<FunSpec>
25 
26   public interface Builder<out T : Builder<T>> {
27     @Suppress("UNCHECKED_CAST")
<lambda>null28     public fun addProperties(propertySpecs: Iterable<PropertySpec>): T = apply {
29       propertySpecs.map(::addProperty)
30     } as T
31 
addPropertynull32     public fun addProperty(propertySpec: PropertySpec): T
33 
34     public fun addProperty(name: String, type: TypeName, vararg modifiers: KModifier): T =
35       addProperty(PropertySpec.builder(name, type, *modifiers).build())
36 
37     @DelicateKotlinPoetApi(
38       message = "Java reflection APIs don't give complete information on Kotlin types. Consider " +
39         "using the kotlinpoet-metadata APIs instead.",
40     )
41     public fun addProperty(name: String, type: Type, vararg modifiers: KModifier): T =
42       addProperty(name, type.asTypeName(), *modifiers)
43 
44     public fun addProperty(name: String, type: KClass<*>, vararg modifiers: KModifier): T =
45       addProperty(name, type.asTypeName(), *modifiers)
46 
47     public fun addProperty(name: String, type: TypeName, modifiers: Iterable<KModifier>): T =
48       addProperty(PropertySpec.builder(name, type, modifiers).build())
49 
50     @DelicateKotlinPoetApi(
51       message = "Java reflection APIs don't give complete information on Kotlin types. Consider " +
52         "using the kotlinpoet-metadata APIs instead.",
53     )
54     public fun addProperty(name: String, type: Type, modifiers: Iterable<KModifier>): T =
55       addProperty(name, type.asTypeName(), modifiers)
56 
57     public fun addProperty(name: String, type: KClass<*>, modifiers: Iterable<KModifier>): T =
58       addProperty(name, type.asTypeName(), modifiers)
59 
60     @Suppress("UNCHECKED_CAST")
61     public fun addFunctions(funSpecs: Iterable<FunSpec>): T = apply {
62       funSpecs.forEach(::addFunction)
63     } as T
64 
addFunctionnull65     public fun addFunction(funSpec: FunSpec): T
66   }
67 }
68