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.ExceptionTypeItem
22 import com.android.tools.metalava.model.Item
23 import com.android.tools.metalava.model.MethodItem
24 import com.android.tools.metalava.model.ParameterItem
25 import com.android.tools.metalava.model.TypeItem
26 import com.android.tools.metalava.model.TypeParameterList
27 import com.android.tools.metalava.model.computeSuperMethods
28 import com.android.tools.metalava.model.updateCopiedMethodState
29 import com.android.tools.metalava.reporter.FileLocation
30 import java.util.function.Predicate
31 
32 internal open class TextMethodItem(
33     codebase: TextCodebase,
34     name: String,
35     containingClass: ClassItem,
36     modifiers: DefaultModifierList,
37     private val returnType: TypeItem,
38     private val parameters: List<TextParameterItem>,
39     fileLocation: FileLocation,
40 ) :
41     TextMemberItem(codebase, name, containingClass, fileLocation, modifiers = modifiers),
42     MethodItem {
43     init {
<lambda>null44         parameters.forEach { it.containingMethod = this }
45     }
46 
equalsnull47     override fun equals(other: Any?): Boolean {
48         if (this === other) return true
49         if (other !is MethodItem) return false
50 
51         if (name() != other.name()) {
52             return false
53         }
54 
55         if (containingClass() != other.containingClass()) {
56             return false
57         }
58 
59         val parameters1 = parameters()
60         val parameters2 = other.parameters()
61 
62         if (parameters1.size != parameters2.size) {
63             return false
64         }
65 
66         for (i in parameters1.indices) {
67             val parameter1 = parameters1[i]
68             val parameter2 = parameters2[i]
69             if (parameter1.type() != parameter2.type()) {
70                 return false
71             }
72         }
73 
74         val typeParameters1 = typeParameterList
75         val typeParameters2 = other.typeParameterList
76 
77         if (typeParameters1.size != typeParameters2.size) {
78             return false
79         }
80 
81         for (i in typeParameters1.indices) {
82             val typeParameter1 = typeParameters1[i]
83             val typeParameter2 = typeParameters2[i]
84             if (typeParameter1.typeBounds() != typeParameter2.typeBounds()) {
85                 return false
86             }
87         }
88         return true
89     }
90 
hashCodenull91     override fun hashCode(): Int {
92         return name().hashCode()
93     }
94 
isConstructornull95     override fun isConstructor(): Boolean = false
96 
97     override fun returnType(): TypeItem = returnType
98 
99     override fun superMethods(): List<MethodItem> {
100         return computeSuperMethods()
101     }
102 
findMainDocumentationnull103     override fun findMainDocumentation(): String = documentation
104 
105     override fun findPredicateSuperMethod(predicate: Predicate<Item>): MethodItem? = null
106 
107     override var typeParameterList: TypeParameterList = TypeParameterList.NONE
108         internal set
109 
110     override fun duplicate(targetContainingClass: ClassItem): MethodItem {
111         val typeVariableMap = targetContainingClass.mapTypeVariables(containingClass())
112         val duplicated =
113             TextMethodItem(
114                 codebase,
115                 name(),
116                 targetContainingClass,
117                 modifiers.duplicate(),
118                 returnType.convertType(typeVariableMap),
119                 parameters.map { it.duplicate(typeVariableMap) },
120                 fileLocation
121             )
122         duplicated.inheritedFrom = containingClass()
123 
124         // Preserve flags that may have been inherited (propagated) from surrounding packages
125         if (targetContainingClass.hidden) {
126             duplicated.hidden = true
127         }
128         if (targetContainingClass.removed) {
129             duplicated.removed = true
130         }
131         if (targetContainingClass.docOnly) {
132             duplicated.docOnly = true
133         }
134 
135         duplicated.annotationDefault = annotationDefault
136         duplicated.throwsTypes = this.throwsTypes
137         duplicated.typeParameterList = typeParameterList
138 
139         duplicated.updateCopiedMethodState()
140 
141         return duplicated
142     }
143 
144     private var throwsTypes: List<ExceptionTypeItem> = emptyList()
145 
throwsTypesnull146     override fun throwsTypes(): List<ExceptionTypeItem> = this.throwsTypes
147 
148     fun setThrowsTypes(throwsClasses: List<ExceptionTypeItem>) {
149         this.throwsTypes = throwsClasses
150     }
151 
parametersnull152     override fun parameters(): List<ParameterItem> = parameters
153 
154     override fun isExtensionMethod(): Boolean = codebase.unsupported()
155 
156     override var inheritedFrom: ClassItem? = null
157 
158     @Deprecated("This property should not be accessed directly.")
159     override var _requiresOverride: Boolean? = null
160 
161     private var annotationDefault = ""
162 
163     fun setAnnotationDefault(default: String) {
164         annotationDefault = default
165     }
166 
defaultValuenull167     override fun defaultValue(): String {
168         return annotationDefault
169     }
170 }
171