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