1 /*
<lambda>null2  * 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.text
18 
19 import com.android.tools.metalava.model.ClassKind
20 import com.android.tools.metalava.model.ClassTypeItem
21 import com.android.tools.metalava.model.DefaultModifierList
22 import com.android.tools.metalava.model.bestGuessAtFullName
23 
24 /**
25  * A builder for stub classes, i.e. [TextClassItem]s fabricated because [ApiFile] has no definition
26  * of the class but a [TextClassItem] is still needed.
27  */
28 internal class StubClassBuilder(
29     val codebase: TextCodebase,
30     val qualifiedName: String,
31 ) {
32     /**
33      * The full name can be ambiguous in theory, but where the naming conventions for packages and
34      * classes are followed it is not. So, assuming that the conventions are followed then produce
35      * the best guess for the full name. This is not really that important as the full name only
36      * really affects the partial ordering of some classes, like in a `throws` list.
37      */
38     val fullName: String = bestGuessAtFullName(qualifiedName)
39 
40     /** The default [ClassKind] can be modified. */
41     var classKind = ClassKind.CLASS
42 
43     /** The modifiers are set to `public` because otherwise there is no point in creating it. */
44     val modifiers = DefaultModifierList(codebase, DefaultModifierList.PUBLIC)
45 
46     var superClassType: ClassTypeItem? = null
47 
48     private fun build(): TextClassItem =
49         TextClassItem(
50                 codebase = codebase,
51                 qualifiedName = qualifiedName,
52                 fullName = fullName,
53                 classKind = classKind,
54                 modifiers = modifiers,
55             )
56             .also { item -> item.setSuperClassType(superClassType) }
57 
58     companion object {
59         /**
60          * Create a [TextClassItem] in the specified [codebase] and with the specific
61          * [qualifiedName], after applying the specified mutator.
62          */
63         fun build(
64             codebase: TextCodebase,
65             qualifiedName: String,
66             mutator: StubClassBuilder.() -> Unit
67         ): TextClassItem {
68             val builder = StubClassBuilder(codebase, qualifiedName)
69             builder.mutator()
70             return builder.build()
71         }
72     }
73 }
74