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.AnnotationItem
20 import com.android.tools.metalava.model.DefaultModifierList
21 import com.android.tools.metalava.model.MethodItem
22 import com.android.tools.metalava.model.ParameterItem
23 import com.android.tools.metalava.model.TypeItem
24 import com.android.tools.metalava.model.TypeParameterBindings
25 import com.android.tools.metalava.model.findAnnotation
26 import com.android.tools.metalava.model.hasAnnotation
27 import com.android.tools.metalava.reporter.FileLocation
28 
29 internal class TurbineParameterItem(
30     codebase: TurbineBasedCodebase,
31     fileLocation: FileLocation,
32     private val name: String,
33     private val containingMethod: MethodItem,
34     override val parameterIndex: Int,
35     private val type: TypeItem,
36     modifiers: DefaultModifierList,
37 ) : TurbineItem(codebase, fileLocation, modifiers, ""), ParameterItem {
38 
namenull39     override fun name(): String = name
40 
41     override fun publicName(): String? {
42         // Java: Look for @ParameterName annotation
43         val annotation = modifiers.findAnnotation(AnnotationItem::isParameterName)
44         return annotation?.attributes?.firstOrNull()?.value?.value()?.toString()
45     }
46 
containingMethodnull47     override fun containingMethod(): MethodItem = containingMethod
48 
49     override fun hasDefaultValue(): Boolean = isDefaultValueKnown()
50 
51     override fun isDefaultValueKnown(): Boolean {
52         return modifiers.hasAnnotation(AnnotationItem::isDefaultValue)
53     }
54 
defaultValuenull55     override fun defaultValue(): String? {
56         val annotation = modifiers.findAnnotation(AnnotationItem::isDefaultValue)
57         return annotation?.attributes?.firstOrNull()?.value?.value()?.toString()
58     }
59 
equalsnull60     override fun equals(other: Any?): Boolean {
61         if (this === other) {
62             return true
63         }
64         return other is ParameterItem &&
65             parameterIndex == other.parameterIndex &&
66             containingMethod == other.containingMethod()
67     }
68 
hashCodenull69     override fun hashCode(): Int = parameterIndex
70 
71     override fun type(): TypeItem = type
72 
73     override fun isVarArgs(): Boolean = modifiers.isVarArg()
74 
75     companion object {
76         internal fun duplicate(
77             codebase: TurbineBasedCodebase,
78             containingMethod: MethodItem,
79             parameter: ParameterItem,
80             typeParameterBindings: TypeParameterBindings,
81         ): TurbineParameterItem {
82             val type = parameter.type().convertType(typeParameterBindings)
83             val mods = (parameter.modifiers as DefaultModifierList).duplicate()
84             return TurbineParameterItem(
85                 codebase,
86                 FileLocation.UNKNOWN,
87                 parameter.name(),
88                 containingMethod,
89                 parameter.parameterIndex,
90                 type,
91                 mods
92             )
93         }
94     }
95 }
96