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.TypeName
20 import com.squareup.kotlinpoet.metadata.classinspectors.ClassInspectorUtil
21 
22 /**
23  * Represents relevant information on a constructor used for [ClassInspector]. Should only be
24  * associated with constructors of a [ClassData].
25  *
26  * @param annotations declared annotations on this constructor.
27  * @property parameterAnnotations a mapping of parameter indices to annotations on them.
28  * @property isSynthetic indicates if this constructor is synthetic or not.
29  * @property jvmModifiers set of [JvmMethodModifiers][JvmMethodModifier] on this constructor.
30  * @property exceptions list of exceptions thrown by this constructor.
31  */
32 public data class ConstructorData(
33   private val annotations: List<AnnotationSpec>,
34   val parameterAnnotations: Map<Int, Collection<AnnotationSpec>>,
35   val isSynthetic: Boolean,
36   val jvmModifiers: Set<JvmMethodModifier>,
37   val exceptions: List<TypeName>,
38 ) {
39 
40   /**
41    * A collection of all annotations on this constructor, including any derived from [jvmModifiers],
42    * [isSynthetic], and [exceptions].
43    */
<lambda>null44   val allAnnotations: Collection<AnnotationSpec> = ClassInspectorUtil.createAnnotations {
45     addAll(annotations)
46     if (isSynthetic) {
47       add(ClassInspectorUtil.JVM_SYNTHETIC_SPEC)
48     }
49     addAll(jvmModifiers.mapNotNull { it.annotationSpec() })
50     exceptions.takeIf { it.isNotEmpty() }
51       ?.let {
52         add(ClassInspectorUtil.createThrowsSpec(it))
53       }
54   }
55 
56   public companion object {
57     public val EMPTY: ConstructorData = ConstructorData(
58       annotations = emptyList(),
59       parameterAnnotations = emptyMap(),
60       isSynthetic = false,
61       jvmModifiers = emptySet(),
62       exceptions = emptyList(),
63     )
64   }
65 }
66