• 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.AnnotationSpec.UseSiteTarget
20 import com.squareup.kotlinpoet.TypeName
21 import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview
22 import com.squareup.kotlinpoet.metadata.classinspectors.ClassInspectorUtil
23 
24 /**
25  * Represents relevant information on a method used for [ClassInspector]. Should only be
26  * associated with methods of a [ClassData] or [PropertyData].
27  *
28  * @param annotations declared annotations on this method.
29  * @property parameterAnnotations a mapping of parameter indices to annotations on them.
30  * @property isSynthetic indicates if this method is synthetic or not.
31  * @property jvmModifiers set of [JvmMethodModifiers][JvmMethodModifier] on this method.
32  * @property isOverride indicates if this method overrides one in a supertype.
33  * @property exceptions list of exceptions thrown by this method.
34  */
35 @KotlinPoetMetadataPreview
36 public data class MethodData(
37   private val annotations: List<AnnotationSpec>,
38   val parameterAnnotations: Map<Int, Collection<AnnotationSpec>>,
39   val isSynthetic: Boolean,
40   val jvmModifiers: Set<JvmMethodModifier>,
41   val isOverride: Boolean,
42   val exceptions: List<TypeName>,
43 ) {
44 
45   /**
46    * A collection of all annotations on this method, including any derived from [jvmModifiers],
47    * [isSynthetic], and [exceptions].
48    *
49    * @param useSiteTarget an optional [UseSiteTarget] that all annotations on this method should
50    *        use.
51    * @param containsReifiedTypeParameter an optional boolean indicating if any type parameters on
52    *        this function are `reified`, which are implicitly synthetic.
53    */
allAnnotationsnull54   public fun allAnnotations(
55     useSiteTarget: UseSiteTarget? = null,
56     containsReifiedTypeParameter: Boolean = false,
57   ): Collection<AnnotationSpec> {
58     return ClassInspectorUtil.createAnnotations(
59       useSiteTarget,
60     ) {
61       addAll(annotations)
62       if (isSynthetic && !containsReifiedTypeParameter) {
63         add(ClassInspectorUtil.JVM_SYNTHETIC_SPEC)
64       }
65       addAll(jvmModifiers.mapNotNull(JvmMethodModifier::annotationSpec))
66       exceptions.takeIf { it.isNotEmpty() }
67         ?.let {
68           add(ClassInspectorUtil.createThrowsSpec(it, useSiteTarget))
69         }
70     }
71   }
72 
73   public companion object {
74     public val SYNTHETIC: MethodData = MethodData(
75       annotations = emptyList(),
76       parameterAnnotations = emptyMap(),
77       isSynthetic = true,
78       jvmModifiers = emptySet(),
79       isOverride = false,
80       exceptions = emptyList(),
81     )
82     public val EMPTY: MethodData = MethodData(
83       annotations = emptyList(),
84       parameterAnnotations = emptyMap(),
85       isSynthetic = false,
86       jvmModifiers = emptySet(),
87       isOverride = false,
88       exceptions = emptyList(),
89     )
90   }
91 }
92