1 /*
2  * 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.text
18 
19 import com.android.tools.metalava.model.ClassItem
20 import com.android.tools.metalava.model.DefaultModifierList
21 import com.android.tools.metalava.model.FieldItem
22 import com.android.tools.metalava.model.TypeItem
23 import com.android.tools.metalava.reporter.FileLocation
24 
25 internal class TextFieldItem(
26     codebase: TextCodebase,
27     name: String,
28     containingClass: TextClassItem,
29     modifiers: DefaultModifierList,
30     private val type: TypeItem,
31     private val constantValue: Any?,
32     fileLocation: FileLocation
33 ) : TextMemberItem(codebase, name, containingClass, fileLocation, modifiers), FieldItem {
34 
equalsnull35     override fun equals(other: Any?): Boolean {
36         if (this === other) return true
37         if (other !is FieldItem) return false
38 
39         if (name() != other.name()) {
40             return false
41         }
42 
43         return containingClass() == other.containingClass()
44     }
45 
hashCodenull46     override fun hashCode(): Int = name().hashCode()
47 
48     override fun type(): TypeItem = type
49 
50     override fun initialValue(requireConstant: Boolean): Any? = constantValue
51 
52     override fun duplicate(targetContainingClass: ClassItem): TextFieldItem {
53         val duplicated =
54             TextFieldItem(
55                 codebase,
56                 name(),
57                 targetContainingClass as TextClassItem,
58                 modifiers.duplicate(),
59                 type,
60                 constantValue,
61                 fileLocation
62             )
63         duplicated.inheritedFrom = containingClass()
64 
65         // Preserve flags that may have been inherited (propagated) from surrounding packages
66         if (targetContainingClass.hidden) {
67             duplicated.hidden = true
68         }
69         if (targetContainingClass.removed) {
70             duplicated.removed = true
71         }
72         if (targetContainingClass.docOnly) {
73             duplicated.docOnly = true
74         }
75 
76         return duplicated
77     }
78 
79     override var inheritedFrom: ClassItem? = null
80 
81     private var isEnumConstant = false
82 
isEnumConstantnull83     override fun isEnumConstant(): Boolean = isEnumConstant
84 
85     fun setEnumConstant(isEnumConstant: Boolean) {
86         this.isEnumConstant = isEnumConstant
87     }
88 }
89