• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.item
18 
19 import com.android.tools.metalava.model.ArrayTypeItem
20 import com.android.tools.metalava.model.BaseModifierList
21 import com.android.tools.metalava.model.CallableItem
22 import com.android.tools.metalava.model.Codebase
23 import com.android.tools.metalava.model.DefaultItem
24 import com.android.tools.metalava.model.ItemDocumentation
25 import com.android.tools.metalava.model.ItemLanguage
26 import com.android.tools.metalava.model.ParameterItem
27 import com.android.tools.metalava.model.TypeItem
28 import com.android.tools.metalava.model.TypeParameterBindings
29 import com.android.tools.metalava.reporter.FileLocation
30 
31 open class DefaultParameterItem(
32     codebase: Codebase,
33     fileLocation: FileLocation,
34     itemLanguage: ItemLanguage,
35     modifiers: BaseModifierList,
36     private val name: String,
37     protected val publicNameProvider: PublicNameProvider,
38     private val containingCallable: CallableItem,
39     override val parameterIndex: Int,
40     private var type: TypeItem,
41     defaultValueFactory: ParameterDefaultValueFactory,
42 ) :
43     DefaultItem(
44         codebase = codebase,
45         fileLocation = fileLocation,
46         itemLanguage = itemLanguage,
47         modifiers = modifiers,
48         documentationFactory = ItemDocumentation.NONE_FACTORY,
49     ),
50     ParameterItem {
51 
52     init {
53         // Set the varargs modifier to true if the type is a varargs.
<lambda>null54         type.let { if (it is ArrayTypeItem && it.isVarargs) mutateModifiers { setVarArg(true) } }
55     }
56 
57     /**
58      * Create the [ParameterDefaultValue] during initialization of this parameter to allow it to
59      * contain an immutable reference to this object.
60      */
61     final override val defaultValue = defaultValueFactory(this)
62 
namenull63     final override fun name(): String = name
64 
65     final override fun publicName(): String? = publicNameProvider(this)
66 
67     final override fun containingCallable(): CallableItem = containingCallable
68 
69     final override fun type(): TypeItem = type
70 
71     final override fun setType(type: TypeItem) {
72         this.type = type
73     }
74 
hasDefaultValuenull75     final override fun hasDefaultValue(): Boolean = defaultValue.hasDefaultValue()
76 
77     override fun duplicate(
78         containingCallable: CallableItem,
79         typeVariableMap: TypeParameterBindings,
80     ) =
81         DefaultParameterItem(
82             codebase,
83             fileLocation,
84             itemLanguage,
85             modifiers,
86             name(),
87             publicNameProvider,
88             containingCallable,
89             parameterIndex,
90             type().convertType(typeVariableMap),
91             defaultValue::duplicate,
92         )
93 }
94