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.doclava1.SourcePositionInfo
20 import com.android.tools.metalava.doclava1.TextCodebase
21 import com.android.tools.metalava.model.ClassItem
22 import com.android.tools.metalava.model.FieldItem
23 import com.android.tools.metalava.model.TypeItem
24 
25 class TextFieldItem(
26     codebase: TextCodebase,
27     name: String,
28     containingClass: TextClassItem,
29     modifiers: TextModifiers,
30     private val type: TextTypeItem,
31     private val constantValue: Any?,
32     position: SourcePositionInfo
33 ) : TextMemberItem(codebase, name, containingClass, position, modifiers), FieldItem {
34 
35     init {
36         modifiers.setOwner(this)
37     }
38 
equalsnull39     override fun equals(other: Any?): Boolean {
40         if (this === other) return true
41         if (other !is FieldItem) return false
42 
43         if (name() != other.name()) {
44             return false
45         }
46 
47         return containingClass() == other.containingClass()
48     }
49 
hashCodenull50     override fun hashCode(): Int = name().hashCode()
51 
52     override fun type(): TypeItem = type
53 
54     override fun initialValue(requireConstant: Boolean): Any? = constantValue
55 
56     override fun toString(): String = "field ${containingClass().fullName()}.${name()}"
57 
58     override fun duplicate(targetContainingClass: ClassItem): TextFieldItem {
59         val duplicated = TextFieldItem(
60             codebase, name(), targetContainingClass as TextClassItem,
61             modifiers.duplicate(), type, constantValue, position
62         )
63         duplicated.inheritedFrom = containingClass()
64         duplicated.inheritedField = inheritedField
65 
66         // Preserve flags that may have been inherited (propagated) from surrounding packages
67         if (targetContainingClass.hidden) {
68             duplicated.hidden = true
69         }
70         if (targetContainingClass.removed) {
71             duplicated.removed = true
72         }
73         if (targetContainingClass.docOnly) {
74             duplicated.docOnly = true
75         }
76 
77         return duplicated
78     }
79 
80     override var inheritedFrom: ClassItem? = null
81     override var inheritedField: Boolean = false
82 
83     private var isEnumConstant = false
isEnumConstantnull84     override fun isEnumConstant(): Boolean = isEnumConstant
85     fun setEnumConstant(isEnumConstant: Boolean) {
86         this.isEnumConstant = isEnumConstant
87     }
88 }