• 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
18 
19 /**
20  * Modifiers for a [TypeItem], analogous to [ModifierList]s for [Item]s. Contains type-use
21  * annotation information.
22  */
23 interface TypeModifiers {
24     /** The type-use annotations applied to the owning type. */
annotationsnull25     fun annotations(): List<AnnotationItem>
26 
27     /** Adds the [annotation] to the list of annotations for the type. */
28     fun addAnnotation(annotation: AnnotationItem)
29 
30     /** Removes the [annotation] from the list of annotations for the type, if it was present. */
31     fun removeAnnotation(annotation: AnnotationItem)
32 
33     /** The nullability of the type. */
34     fun nullability(): TypeNullability
35 
36     /**
37      * Updates the nullability of the type to [newNullability]. Does not add or remove any nullness
38      * annotations, so those should be handled separately through [addAnnotation] and/or
39      * [removeAnnotation], if needed.
40      */
41     fun setNullability(newNullability: TypeNullability)
42 
43     /** Create a copy of this to which modifications can be made. */
44     fun duplicate(withNullability: TypeNullability? = null): TypeModifiers
45 
46     /** Whether the [nullability] is [TypeNullability.NULLABLE]. */
47     val isNullable
48         get() = nullability() == TypeNullability.NULLABLE
49 
50     /** Whether the [nullability] is [TypeNullability.NONNULL]. */
51     val isNonNull
52         get() = nullability() == TypeNullability.NONNULL
53 
54     /** Whether the [nullability] is [TypeNullability.PLATFORM]. */
55     val isPlatformNullability
56         get() = nullability() == TypeNullability.PLATFORM
57 }
58 
59 /** An enum representing the possible nullness values of a type. */
60 enum class TypeNullability(
61     /** Kotlin nullability suffix. */
62     val suffix: String,
63 ) {
64     /**
65      * Nullability for a type that is annotated non-null, is primitive, or defined as non-null in
66      * Kotlin.
67      */
68     NONNULL(""),
69     /** Nullability for a type that is annotated nullable or defined as nullable in Kotlin. */
70     NULLABLE("?"),
71     /** Nullability for a Java type without a specified nullability. */
72     PLATFORM("!"),
73     /**
74      * The nullability for a type without defined nullness. Examples include:
75      * - A Kotlin type variable with inherited nullability.
76      * - Wildcard types (nullness is defined through the bounds of the wildcard).
77      */
78     UNDEFINED("");
79 
80     companion object {
81         /** Given a nullness [annotation], returns the corresponding [TypeNullability]. */
82         fun ofAnnotation(annotation: AnnotationItem): TypeNullability {
83             return if (isNullableAnnotation(annotation.qualifiedName.orEmpty())) {
84                 NULLABLE
85             } else if (isNonNullAnnotation(annotation.qualifiedName.orEmpty())) {
86                 NONNULL
87             } else {
88                 throw IllegalStateException("Not a nullness annotation: $annotation")
89             }
90         }
91     }
92 }
93