• 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.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.ClassItem
24 import com.android.tools.metalava.model.ClassTypeItem
25 import com.android.tools.metalava.model.Codebase
26 import com.android.tools.metalava.model.ConstructorItem
27 import com.android.tools.metalava.model.ExceptionTypeItem
28 import com.android.tools.metalava.model.ItemDocumentation
29 import com.android.tools.metalava.model.ItemDocumentationFactory
30 import com.android.tools.metalava.model.ItemLanguage
31 import com.android.tools.metalava.model.TypeItem
32 import com.android.tools.metalava.model.TypeParameterList
33 import com.android.tools.metalava.model.VisibilityLevel
34 import com.android.tools.metalava.model.createImmutableModifiers
35 import com.android.tools.metalava.reporter.FileLocation
36 
37 open class DefaultConstructorItem(
38     codebase: Codebase,
39     fileLocation: FileLocation,
40     itemLanguage: ItemLanguage,
41     modifiers: BaseModifierList,
42     documentationFactory: ItemDocumentationFactory,
43     variantSelectorsFactory: ApiVariantSelectorsFactory,
44     name: String,
45     containingClass: ClassItem,
46     typeParameterList: TypeParameterList,
47     returnType: ClassTypeItem,
48     parameterItemsFactory: ParameterItemsFactory,
49     throwsTypes: List<ExceptionTypeItem>,
50     callableBodyFactory: CallableBodyFactory,
51     private val implicitConstructor: Boolean,
52     override val isPrimary: Boolean = false,
53 ) :
54     DefaultCallableItem(
55         codebase = codebase,
56         fileLocation = fileLocation,
57         itemLanguage = itemLanguage,
58         modifiers = modifiers,
59         documentationFactory = documentationFactory,
60         variantSelectorsFactory = variantSelectorsFactory,
61         name = name,
62         containingClass = containingClass,
63         typeParameterList = typeParameterList,
64         returnType = returnType,
65         parameterItemsFactory = parameterItemsFactory,
66         throwsTypes = throwsTypes,
67         callableBodyFactory = callableBodyFactory,
68     ),
69     ConstructorItem {
70 
71     /** Override to specialize the return type. */
returnTypenull72     final override fun returnType() = super.returnType() as ClassTypeItem
73 
74     /** Override to make sure that [type] is a [ClassTypeItem]. */
75     final override fun setType(type: TypeItem) {
76         super.setType(type as ClassTypeItem)
77     }
78 
isImplicitConstructornull79     final override fun isImplicitConstructor() = implicitConstructor
80 
81     companion object {
82         fun createDefaultConstructor(
83             codebase: Codebase,
84             itemLanguage: ItemLanguage,
85             variantSelectorsFactory: ApiVariantSelectorsFactory,
86             containingClass: ClassItem,
87             visibility: VisibilityLevel,
88         ): ConstructorItem {
89             val name = containingClass.simpleName()
90             val modifiers = createImmutableModifiers(visibility)
91 
92             val ctorItem =
93                 DefaultConstructorItem(
94                     codebase = codebase,
95                     // Use the location of the containing class for the default constructor.
96                     fileLocation = containingClass.fileLocation,
97                     itemLanguage = itemLanguage,
98                     modifiers = modifiers,
99                     documentationFactory = ItemDocumentation.NONE_FACTORY,
100                     variantSelectorsFactory = variantSelectorsFactory,
101                     name = name,
102                     containingClass = containingClass,
103                     typeParameterList = TypeParameterList.NONE,
104                     returnType = containingClass.type(),
105                     parameterItemsFactory = { emptyList() },
106                     throwsTypes = emptyList(),
107                     callableBodyFactory = CallableBody.UNAVAILABLE_FACTORY,
108                     // This is not an implicit constructor as it was not created by the compiler.
109                     implicitConstructor = false,
110                 )
111             return ctorItem
112         }
113     }
114 }
115