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. */ 25 val annotations: List<AnnotationItem> 26 27 /** The nullability of the type. */ 28 val nullability: TypeNullability 29 30 /** 31 * Return a [TypeModifiers] instance identical to this one except its 32 * [TypeModifiers.nullability] and [TypeModifiers.annotations] properties are the same as the 33 * [nullability] and [annotations] parameters respectively. 34 * 35 * If the parameters are the same as this instance's properties then it will just return this 36 * instance, otherwise it will return a new instance. 37 */ substitutenull38 fun substitute( 39 nullability: TypeNullability = this.nullability, 40 annotations: List<AnnotationItem> = this.annotations, 41 ): TypeModifiers 42 43 /** Whether the [nullability] is [TypeNullability.NULLABLE]. */ 44 val isNullable 45 get() = nullability == TypeNullability.NULLABLE 46 47 /** Whether the [nullability] is [TypeNullability.NONNULL]. */ 48 val isNonNull 49 get() = nullability == TypeNullability.NONNULL 50 51 /** Whether the [nullability] is [TypeNullability.PLATFORM]. */ 52 val isPlatformNullability 53 get() = nullability == TypeNullability.PLATFORM 54 } 55 56 /** An enum representing the possible nullness values of a type. */ 57 enum class TypeNullability( 58 /** Kotlin nullability suffix. */ 59 val suffix: String, 60 ) { 61 /** 62 * Nullability for a type that is annotated non-null, is primitive, or defined as non-null in 63 * Kotlin. 64 */ 65 NONNULL(""), 66 /** Nullability for a type that is annotated nullable or defined as nullable in Kotlin. */ 67 NULLABLE("?"), 68 /** Nullability for a Java type without a specified nullability. */ 69 PLATFORM("!"), 70 /** 71 * The nullability for a type without defined nullness. Examples include: 72 * - A Kotlin type variable with inherited nullability. 73 * - Wildcard types (nullness is defined through the bounds of the wildcard). 74 */ 75 UNDEFINED(""); 76 77 companion object { 78 /** Given a nullness [annotation], returns the corresponding [TypeNullability]. */ 79 fun ofAnnotation(annotation: AnnotationItem): TypeNullability { 80 return if (isNullableAnnotation(annotation.qualifiedName.orEmpty())) { 81 NULLABLE 82 } else if (isNonNullAnnotation(annotation.qualifiedName.orEmpty())) { 83 NONNULL 84 } else { 85 throw IllegalStateException("Not a nullness annotation: $annotation") 86 } 87 } 88 } 89 } 90