• 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.type
18 
19 import com.android.tools.metalava.model.AnnotationItem
20 import com.android.tools.metalava.model.TypeItem
21 import com.android.tools.metalava.model.TypeModifiers
22 import com.android.tools.metalava.model.TypeNullability
23 
24 /** Modifiers for a [TypeItem]. */
25 class DefaultTypeModifiers(
26     override val annotations: List<AnnotationItem>,
27     override val nullability: TypeNullability,
28 ) : TypeModifiers {
29 
substitutenull30     override fun substitute(
31         nullability: TypeNullability,
32         annotations: List<AnnotationItem>,
33     ): TypeModifiers =
34         if (nullability != this.nullability || annotations != this.annotations)
35             DefaultTypeModifiers(annotations, nullability)
36         else this
37 
38     companion object {
39         /** A set of empty, non-null [TypeModifiers] for sharing. */
40         val emptyNonNullModifiers: TypeModifiers =
41             DefaultTypeModifiers(emptyList(), TypeNullability.NONNULL)
42 
43         /** A set of empty, nullable [TypeModifiers] for sharing. */
44         val emptyNullableModifiers: TypeModifiers =
45             DefaultTypeModifiers(emptyList(), TypeNullability.NULLABLE)
46 
47         /** A set of empty, platform [TypeModifiers] for sharing. */
48         val emptyPlatformModifiers: TypeModifiers =
49             DefaultTypeModifiers(emptyList(), TypeNullability.PLATFORM)
50 
51         /** A set of empty, undefined [TypeModifiers] for sharing. */
52         val emptyUndefinedModifiers: TypeModifiers =
53             DefaultTypeModifiers(emptyList(), TypeNullability.UNDEFINED)
54 
55         /**
56          * Create a [DefaultTypeModifiers].
57          *
58          * If [knownNullability] is `null` then this will compute nullability from the
59          * [annotations], if any, and if not then default to platform nullness.
60          */
61         fun create(
62             annotations: List<AnnotationItem>,
63             knownNullability: TypeNullability? = null,
64         ): TypeModifiers {
65             // Use the known nullability, or find if there is a nullness annotation on the type,
66             // defaulting to platform nullness if not.
67             val nullability =
68                 knownNullability
69                     ?: annotations
70                         .firstOrNull { it.isNullnessAnnotation() }
71                         ?.let { TypeNullability.ofAnnotation(it) }
72                         ?: TypeNullability.PLATFORM
73 
74             // If the annotations are empty then use one of the predefined instances.
75             if (annotations.isEmpty()) {
76                 return when (nullability) {
77                     TypeNullability.NONNULL -> emptyNonNullModifiers
78                     TypeNullability.NULLABLE -> emptyNullableModifiers
79                     TypeNullability.PLATFORM -> emptyPlatformModifiers
80                     TypeNullability.UNDEFINED -> emptyUndefinedModifiers
81                 }
82             }
83 
84             return DefaultTypeModifiers(annotations, nullability)
85         }
86     }
87 }
88