• 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.FIELD
20 import com.squareup.kotlinpoet.CodeBlock
21 import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview
22 import com.squareup.kotlinpoet.metadata.classinspectors.ClassInspectorUtil
23 
24 /**
25  * Represents relevant information on a field used for [ClassInspector]. Should only be
26  * associated with a [PropertyData].
27  *
28  * @param annotations declared annotations on this field.
29  * @property isSynthetic indicates if this field is synthetic or not.
30  * @property jvmModifiers set of [JvmMethodModifiers][JvmMethodModifier] on this field.
31  * @property constant the constant value of this field, if available. Note that this is does not
32  *           strictly imply that the associated property is `const`.
33  */
34 @KotlinPoetMetadataPreview
35 public data class FieldData(
36   private val annotations: List<AnnotationSpec>,
37   val isSynthetic: Boolean,
38   val jvmModifiers: Set<JvmFieldModifier>,
39   val constant: CodeBlock?,
40 ) {
41 
42   /**
43    * A collection of all annotations on this method, including any derived from [jvmModifiers]
44    * and [isSynthetic].
45    */
46   val allAnnotations: Collection<AnnotationSpec> = ClassInspectorUtil.createAnnotations(
47     FIELD,
<lambda>null48   ) {
49     addAll(annotations)
50     if (isSynthetic) {
51       add(ClassInspectorUtil.JVM_SYNTHETIC_SPEC)
52     }
53     addAll(jvmModifiers.mapNotNull(JvmFieldModifier::annotationSpec))
54   }
55 
56   public companion object {
57     public val SYNTHETIC: FieldData = FieldData(
58       annotations = emptyList(),
59       isSynthetic = true,
60       jvmModifiers = emptySet(),
61       constant = null,
62     )
63   }
64 }
65