• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.text
18 
19 import com.android.tools.metalava.model.ClassItem
20 import com.android.tools.metalava.model.ClassKind
21 import com.android.tools.metalava.model.ClassOrigin
22 import com.android.tools.metalava.model.ClassTypeItem
23 import com.android.tools.metalava.model.PackageItem
24 import com.android.tools.metalava.model.TypeParameterList
25 import com.android.tools.metalava.model.VisibilityLevel
26 import com.android.tools.metalava.model.createImmutableModifiers
27 import com.android.tools.metalava.model.item.DefaultClassItem
28 import com.android.tools.metalava.reporter.FileLocation
29 
30 /**
31  * A builder for stub classes, i.e. [DefaultClassItem]s fabricated because [ApiFile] has no
32  * definition of the class but a [DefaultClassItem] is still needed.
33  */
34 internal class StubClassBuilder(
35     internal val assembler: TextCodebaseAssembler,
36     internal val qualifiedName: String,
37     private val containingClass: ClassItem?,
38     val containingPackage: PackageItem,
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 = createImmutableModifiers(VisibilityLevel.PUBLIC)
45 
46     var superClassType: ClassTypeItem? = null
47 
buildnull48     private fun build(): DefaultClassItem =
49         assembler.itemFactory.createClassItem(
50             fileLocation = FileLocation.UNKNOWN,
51             modifiers = modifiers,
52             classKind = classKind,
53             containingClass = containingClass,
54             containingPackage = containingPackage,
55             qualifiedName = qualifiedName,
56             typeParameterList = TypeParameterList.NONE,
57             // Always treat stubs as if they are from the class path. While that is not strictly
58             // true stubs classes should be treated as if they did come from there, i.e. they can be
59             // referenced but not emitted.
60             origin = ClassOrigin.CLASS_PATH,
61             superClassType = superClassType,
62             interfaceTypes = emptyList(),
63         )
64 
65     companion object {
66         /**
67          * Create a [DefaultClassItem] in the specified [codebase] and with the specific
68          * [qualifiedName], after applying the specified mutator.
69          */
70         fun build(
71             assembler: TextCodebaseAssembler,
72             qualifiedName: String,
73             containingClass: ClassItem?,
74             containingPackage: PackageItem,
75             mutator: StubClassBuilder.() -> Unit,
76         ): DefaultClassItem {
77             val builder =
78                 StubClassBuilder(
79                     assembler = assembler,
80                     qualifiedName = qualifiedName,
81                     containingClass = containingClass,
82                     containingPackage = containingPackage,
83                 )
84             builder.mutator()
85             return builder.build()
86         }
87     }
88 }
89