1 /* 2 * Copyright (C) 2024 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.ApiVariantSelectorsFactory 20 import com.android.tools.metalava.model.BaseModifierList 21 import com.android.tools.metalava.model.CallableBody 22 import com.android.tools.metalava.model.CallableBodyFactory 23 import com.android.tools.metalava.model.CallableItem 24 import com.android.tools.metalava.model.ClassItem 25 import com.android.tools.metalava.model.ClassKind 26 import com.android.tools.metalava.model.ClassOrigin 27 import com.android.tools.metalava.model.ClassTypeItem 28 import com.android.tools.metalava.model.ConstructorItem 29 import com.android.tools.metalava.model.ExceptionTypeItem 30 import com.android.tools.metalava.model.FieldItem 31 import com.android.tools.metalava.model.Item 32 import com.android.tools.metalava.model.ItemDocumentation 33 import com.android.tools.metalava.model.ItemDocumentationFactory 34 import com.android.tools.metalava.model.ItemLanguage 35 import com.android.tools.metalava.model.MethodItem 36 import com.android.tools.metalava.model.PackageItem 37 import com.android.tools.metalava.model.ParameterItem 38 import com.android.tools.metalava.model.PropertyItem 39 import com.android.tools.metalava.model.SourceFile 40 import com.android.tools.metalava.model.TypeAliasItem 41 import com.android.tools.metalava.model.TypeItem 42 import com.android.tools.metalava.model.TypeParameterList 43 import com.android.tools.metalava.reporter.FileLocation 44 45 /** 46 * A lambda that when passed the [Item] will return the public name, or null if there is not one. 47 */ 48 typealias PublicNameProvider = (Item) -> String? 49 50 /** A factory for creating [Item] instances suitable for use by many models. */ 51 class DefaultItemFactory( 52 /** The [DefaultCodebase] to which returned [Item]s will belong. */ 53 private val codebase: DefaultCodebase, 54 55 /** The default language for [Item]s created by this. */ 56 private val defaultItemLanguage: ItemLanguage, 57 58 /** The default [ApiVariantSelectorsFactory] for [Item]s created by this. */ 59 private val defaultVariantSelectorsFactory: ApiVariantSelectorsFactory, 60 ) { 61 /** Create a [PackageItem]. */ createPackageItemnull62 fun createPackageItem( 63 fileLocation: FileLocation, 64 modifiers: BaseModifierList, 65 documentationFactory: ItemDocumentationFactory, 66 qualifiedName: String, 67 containingPackage: PackageItem?, 68 overviewDocumentation: ResourceFile?, 69 ): DefaultPackageItem { 70 return DefaultPackageItem( 71 codebase, 72 fileLocation, 73 defaultItemLanguage, 74 modifiers, 75 documentationFactory, 76 defaultVariantSelectorsFactory, 77 qualifiedName, 78 containingPackage, 79 overviewDocumentation, 80 ) 81 } 82 83 /** Create a [ConstructorItem]. */ createClassItemnull84 fun createClassItem( 85 fileLocation: FileLocation, 86 itemLanguage: ItemLanguage = defaultItemLanguage, 87 modifiers: BaseModifierList, 88 documentationFactory: ItemDocumentationFactory = ItemDocumentation.NONE_FACTORY, 89 source: SourceFile? = null, 90 classKind: ClassKind, 91 containingClass: ClassItem?, 92 containingPackage: PackageItem, 93 qualifiedName: String = "", 94 typeParameterList: TypeParameterList, 95 origin: ClassOrigin, 96 superClassType: ClassTypeItem?, 97 interfaceTypes: List<ClassTypeItem>, 98 ) = 99 DefaultClassItem( 100 codebase, 101 fileLocation, 102 itemLanguage, 103 modifiers, 104 documentationFactory, 105 defaultVariantSelectorsFactory, 106 source, 107 classKind, 108 containingClass, 109 containingPackage, 110 qualifiedName, 111 typeParameterList, 112 origin, 113 superClassType, 114 interfaceTypes, 115 ) 116 117 /** Create a [ConstructorItem]. */ 118 fun createConstructorItem( 119 fileLocation: FileLocation, 120 itemLanguage: ItemLanguage = defaultItemLanguage, 121 modifiers: BaseModifierList, 122 documentationFactory: ItemDocumentationFactory, 123 name: String, 124 containingClass: ClassItem, 125 typeParameterList: TypeParameterList, 126 returnType: ClassTypeItem, 127 parameterItemsFactory: ParameterItemsFactory, 128 throwsTypes: List<ExceptionTypeItem>, 129 callableBodyFactory: CallableBodyFactory = CallableBody.UNAVAILABLE_FACTORY, 130 implicitConstructor: Boolean, 131 isPrimary: Boolean = false, 132 ): ConstructorItem = 133 DefaultConstructorItem( 134 codebase, 135 fileLocation, 136 itemLanguage, 137 modifiers, 138 documentationFactory, 139 defaultVariantSelectorsFactory, 140 name, 141 containingClass, 142 typeParameterList, 143 returnType, 144 parameterItemsFactory, 145 throwsTypes, 146 callableBodyFactory, 147 implicitConstructor, 148 isPrimary, 149 ) 150 151 /** Create a [FieldItem]. */ 152 fun createFieldItem( 153 fileLocation: FileLocation, 154 itemLanguage: ItemLanguage = defaultItemLanguage, 155 modifiers: BaseModifierList, 156 documentationFactory: ItemDocumentationFactory, 157 name: String, 158 containingClass: ClassItem, 159 type: TypeItem, 160 isEnumConstant: Boolean, 161 fieldValue: FieldValue?, 162 ): FieldItem = 163 DefaultFieldItem( 164 codebase, 165 fileLocation, 166 itemLanguage, 167 defaultVariantSelectorsFactory, 168 modifiers, 169 documentationFactory, 170 name, 171 containingClass, 172 type, 173 isEnumConstant, 174 fieldValue, 175 ) 176 177 /** Create a [MethodItem]. */ 178 fun createMethodItem( 179 fileLocation: FileLocation, 180 itemLanguage: ItemLanguage = defaultItemLanguage, 181 modifiers: BaseModifierList, 182 documentationFactory: ItemDocumentationFactory, 183 name: String, 184 containingClass: ClassItem, 185 typeParameterList: TypeParameterList, 186 returnType: TypeItem, 187 parameterItemsFactory: ParameterItemsFactory, 188 throwsTypes: List<ExceptionTypeItem>, 189 callableBodyFactory: CallableBodyFactory = CallableBody.UNAVAILABLE_FACTORY, 190 annotationDefault: String, 191 ): MethodItem = 192 DefaultMethodItem( 193 codebase, 194 fileLocation, 195 itemLanguage, 196 modifiers, 197 documentationFactory, 198 defaultVariantSelectorsFactory, 199 name, 200 containingClass, 201 typeParameterList, 202 returnType, 203 parameterItemsFactory, 204 throwsTypes, 205 callableBodyFactory, 206 annotationDefault, 207 ) 208 209 /** Create a [ParameterItem]. */ 210 fun createParameterItem( 211 fileLocation: FileLocation, 212 itemLanguage: ItemLanguage = defaultItemLanguage, 213 modifiers: BaseModifierList, 214 name: String, 215 publicNameProvider: PublicNameProvider, 216 containingCallable: CallableItem, 217 parameterIndex: Int, 218 type: TypeItem, 219 defaultValueFactory: ParameterDefaultValueFactory, 220 ): ParameterItem = 221 DefaultParameterItem( 222 codebase, 223 fileLocation, 224 itemLanguage, 225 modifiers, 226 name, 227 publicNameProvider, 228 containingCallable, 229 parameterIndex, 230 type, 231 defaultValueFactory, 232 ) 233 234 /** Create a [PropertyItem]. */ 235 fun createPropertyItem( 236 fileLocation: FileLocation, 237 itemLanguage: ItemLanguage = defaultItemLanguage, 238 documentationFactory: ItemDocumentationFactory = ItemDocumentation.NONE_FACTORY, 239 modifiers: BaseModifierList, 240 name: String, 241 containingClass: ClassItem, 242 type: TypeItem, 243 receiver: TypeItem?, 244 typeParameterList: TypeParameterList, 245 getter: MethodItem? = null, 246 setter: MethodItem? = null, 247 constructorParameter: ParameterItem? = null, 248 backingField: FieldItem? = null, 249 ): PropertyItem = 250 DefaultPropertyItem( 251 codebase, 252 fileLocation, 253 itemLanguage, 254 documentationFactory, 255 defaultVariantSelectorsFactory, 256 modifiers, 257 name, 258 containingClass, 259 type, 260 getter, 261 setter, 262 constructorParameter, 263 backingField, 264 receiver, 265 typeParameterList, 266 ) 267 268 /** Create a [TypeAliasItem]. */ 269 fun createTypeAliasItem( 270 fileLocation: FileLocation, 271 modifiers: BaseModifierList, 272 qualifiedName: String, 273 containingPackage: DefaultPackageItem, 274 aliasedType: TypeItem, 275 typeParameterList: TypeParameterList, 276 documentationFactory: ItemDocumentationFactory = ItemDocumentation.NONE_FACTORY, 277 ): TypeAliasItem = 278 DefaultTypeAliasItem( 279 codebase, 280 fileLocation, 281 modifiers, 282 documentationFactory, 283 defaultVariantSelectorsFactory, 284 aliasedType, 285 qualifiedName, 286 typeParameterList, 287 containingPackage 288 ) 289 290 /** 291 * Create a [DefaultTypeParameterItem]. 292 * 293 * This returns [DefaultTypeParameterItem] because access is needed to its 294 * [DefaultTypeParameterItem.bounds] after creation as full creation is a two stage process due 295 * to cyclical dependencies between [DefaultTypeParameterItem] in a type parameters list. 296 * 297 * TODO(b/351410134): Provide support in this factory for two stage initialization. 298 */ 299 fun createTypeParameterItem( 300 modifiers: BaseModifierList, 301 name: String, 302 isReified: Boolean, 303 ) = 304 DefaultTypeParameterItem( 305 codebase, 306 modifiers, 307 name, 308 isReified, 309 ) 310 } 311