1 /* 2 * Copyright (C) 2023 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.turbine 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 TurbineFieldItem( 26 codebase: TurbineBasedCodebase, 27 fileLocation: FileLocation, 28 private val name: String, 29 containingClass: ClassItem, 30 private val type: TypeItem, 31 modifiers: DefaultModifierList, 32 documentation: String, 33 private val isEnumConstant: Boolean, 34 private val fieldValue: TurbineFieldValue?, 35 ) : 36 TurbineMemberItem(codebase, fileLocation, modifiers, documentation, containingClass), 37 FieldItem { 38 39 override var inheritedFrom: ClassItem? = null 40 namenull41 override fun name(): String = name 42 43 override fun equals(other: Any?): Boolean { 44 if (this === other) { 45 return true 46 } 47 return other is FieldItem && 48 name() == other.name() && 49 containingClass() == other.containingClass() 50 } 51 hashCodenull52 override fun hashCode(): Int = name().hashCode() 53 54 override fun type(): TypeItem = type 55 56 override fun duplicate(targetContainingClass: ClassItem): FieldItem { 57 val duplicated = 58 TurbineFieldItem( 59 codebase, 60 fileLocation, 61 name(), 62 targetContainingClass, 63 type.duplicate(), 64 modifiers.duplicate(), 65 documentation, 66 isEnumConstant, 67 fieldValue, 68 ) 69 duplicated.inheritedFrom = containingClass() 70 71 // Preserve flags that may have been inherited (propagated) from surrounding packages 72 if (targetContainingClass.hidden) { 73 duplicated.hidden = true 74 } 75 if (targetContainingClass.removed) { 76 duplicated.removed = true 77 } 78 if (targetContainingClass.docOnly) { 79 duplicated.docOnly = true 80 } 81 82 return duplicated 83 } 84 initialValuenull85 override fun initialValue(requireConstant: Boolean) = fieldValue?.initialValue(requireConstant) 86 87 override fun isEnumConstant(): Boolean = isEnumConstant 88 } 89 90 /** Provides access to the initial values of a field. */ 91 class TurbineFieldValue( 92 private var initialValueWithRequiredConstant: Any?, 93 private var initialValueWithoutRequiredConstant: Any?, 94 ) { 95 96 fun initialValue(requireConstant: Boolean) = 97 if (requireConstant) initialValueWithRequiredConstant 98 else initialValueWithoutRequiredConstant 99 } 100