xref: /aosp_15_r20/tools/metalava/metalava-model/src/main/java/com/android/tools/metalava/model/item/DefaultFieldItem.kt (revision 115816f9299ab6ddd6b9673b81f34e707f6bacab)
1 /*
<lambda>null2  * Copyright (C) 2017 The Android Open Source Project
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  *      http://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 
17 package com.android.tools.metalava.model.item
18 
19 import com.android.tools.metalava.model.ApiVariantSelectorsFactory
20 import com.android.tools.metalava.model.BaseModifierList
21 import com.android.tools.metalava.model.ClassItem
22 import com.android.tools.metalava.model.Codebase
23 import com.android.tools.metalava.model.FieldItem
24 import com.android.tools.metalava.model.ItemDocumentationFactory
25 import com.android.tools.metalava.model.ItemLanguage
26 import com.android.tools.metalava.model.TypeItem
27 import com.android.tools.metalava.reporter.FileLocation
28 
29 open class DefaultFieldItem(
30     codebase: Codebase,
31     fileLocation: FileLocation,
32     itemLanguage: ItemLanguage,
33     variantSelectorsFactory: ApiVariantSelectorsFactory,
34     modifiers: BaseModifierList,
35     documentationFactory: ItemDocumentationFactory,
36     name: String,
37     containingClass: ClassItem,
38     private var type: TypeItem,
39     private val isEnumConstant: Boolean,
40     override val fieldValue: FieldValue?,
41 ) :
42     DefaultMemberItem(
43         codebase = codebase,
44         fileLocation = fileLocation,
45         itemLanguage = itemLanguage,
46         modifiers = modifiers,
47         documentationFactory = documentationFactory,
48         variantSelectorsFactory = variantSelectorsFactory,
49         name = name,
50         containingClass = containingClass,
51     ),
52     FieldItem {
53 
54     final override var inheritedFrom: ClassItem? = null
55 
56     final override fun type(): TypeItem = type
57 
58     final override fun setType(type: TypeItem) {
59         this.type = type
60     }
61 
62     override fun duplicate(targetContainingClass: ClassItem) =
63         DefaultFieldItem(
64                 codebase = codebase,
65                 fileLocation = fileLocation,
66                 itemLanguage = itemLanguage,
67                 variantSelectorsFactory = variantSelectors::duplicate,
68                 modifiers = modifiers,
69                 documentationFactory = documentation::duplicate,
70                 name = name(),
71                 containingClass = targetContainingClass,
72                 type = type,
73                 isEnumConstant = isEnumConstant,
74                 fieldValue = fieldValue,
75             )
76             .also { duplicated -> duplicated.inheritedFrom = containingClass() }
77 
78     final override fun initialValue(requireConstant: Boolean) =
79         fieldValue?.initialValue(requireConstant)
80 
81     final override fun isEnumConstant(): Boolean = isEnumConstant
82 }
83