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