1 /*
2  * Copyright (C) 2021 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.metadata.specs
17 
18 import com.squareup.kotlinpoet.AnnotationSpec
19 import com.squareup.kotlinpoet.ClassName
20 import kotlin.metadata.KmClass
21 import kotlin.metadata.KmConstructor
22 import kotlin.metadata.KmDeclarationContainer
23 import kotlin.metadata.KmFunction
24 import kotlin.metadata.KmPackage
25 import kotlin.metadata.KmProperty
26 
27 /**
28  * Represents relevant information on a declaration container used for [ClassInspector]. Can only
29  * ever be applied on a Kotlin type (i.e. is annotated with [Metadata]).
30  *
31  * @property declarationContainer the [KmDeclarationContainer] as parsed from the class's
32  *           [@Metadata][Metadata] annotation.
33  * @property annotations declared annotations on this class.
34  * @property properties the mapping of [declarationContainer]'s properties to parsed [PropertyData].
35  * @property methods the mapping of [declarationContainer]'s methods to parsed [MethodData].
36  */
37 public interface ContainerData {
38   public val declarationContainer: KmDeclarationContainer
39   public val annotations: Collection<AnnotationSpec>
40   public val properties: Map<KmProperty, PropertyData>
41   public val methods: Map<KmFunction, MethodData>
42 }
43 
44 /**
45  * Represents relevant information on a Kotlin class used for [ClassInspector]. Can only ever be
46  * applied on a class and not file facades.
47  *
48  * @property declarationContainer the [KmClass] as parsed from the class's
49  *           [@Metadata][Metadata] annotation.
50  * @property className the KotlinPoet [ClassName] of the class.
51  * @property constructors the mapping of [declarationContainer]'s constructors to parsed
52  * [ConstructorData].
53  */
54 public data class ClassData(
55   override val declarationContainer: KmClass,
56   val className: ClassName,
57   override val annotations: Collection<AnnotationSpec>,
58   override val properties: Map<KmProperty, PropertyData>,
59   val constructors: Map<KmConstructor, ConstructorData>,
60   override val methods: Map<KmFunction, MethodData>,
61 ) : ContainerData
62 
63 /**
64  * Represents relevant information on a file facade used for [ClassInspector].
65  *
66  * @property declarationContainer the [KmClass] as parsed from the class's
67  *           [@Metadata][Metadata] annotation.
68  * @property className the KotlinPoet [ClassName] of the underlying facade class in JVM.
69  * @property jvmName the `@JvmName` of the class or null if it does not have a custom name.
70  *           Default will try to infer from the [className].
71  */
72 public data class FileData(
73   override val declarationContainer: KmPackage,
74   override val annotations: Collection<AnnotationSpec>,
75   override val properties: Map<KmProperty, PropertyData>,
76   override val methods: Map<KmFunction, MethodData>,
77   val className: ClassName,
78   val jvmName: String? =
79     if (!className.simpleName.endsWith("Kt")) className.simpleName else null,
80 ) : ContainerData {
81 
82   /**
83    * The file name of the container, defaults to [className]'s simple name + "Kt". If a [jvmName] is
84    * specified, it will always defer to that.
85    */
86   val fileName: String = jvmName ?: className.simpleName.removeSuffix("Kt")
87 }
88 
89 /**
90  * Represents relevant information on a Kotlin enum entry.
91  *
92  * @property declarationContainer the [KmClass] as parsed from the entry's
93  * [@Metadata][Metadata] annotation.
94  * @property annotations the annotations for the entry
95  */
96 public data class EnumEntryData(
97   val declarationContainer: KmClass?,
98   val annotations: Collection<AnnotationSpec>,
99 )
100