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.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 private val annotations: MutableList<AnnotationItem>, 27 private var nullability: TypeNullability, 28 /** 29 * If non-null then this causes this instance to be treated as immutable and any attempt to 30 * mutate it will throw an exception with this as the reason. 31 */ 32 private val immutableReason: String? = null, 33 ) : TypeModifiers { 34 35 private fun ensureMutable() { 36 immutableReason?.let { reason -> error(reason) } 37 } 38 39 override fun annotations(): List<AnnotationItem> = annotations 40 41 override fun addAnnotation(annotation: AnnotationItem) { 42 ensureMutable() 43 annotations.add(annotation) 44 } 45 46 override fun removeAnnotation(annotation: AnnotationItem) { 47 ensureMutable() 48 annotations.remove(annotation) 49 } 50 51 override fun nullability(): TypeNullability { 52 return nullability 53 } 54 55 override fun setNullability(newNullability: TypeNullability) { 56 if (newNullability == nullability) return 57 ensureMutable() 58 nullability = newNullability 59 } 60 61 override fun duplicate(withNullability: TypeNullability?) = 62 DefaultTypeModifiers(annotations.toMutableList(), withNullability ?: nullability) 63 64 companion object { 65 /** A set of empty, non-null [TypeModifiers] for sharing. */ 66 val emptyNonNullModifiers = 67 create(emptyList(), TypeNullability.NONNULL, "emptyNonNullModifiers is shared") 68 69 /** 70 * Create a [DefaultTypeModifiers]. 71 * 72 * If [knownNullability] is `null` then this will compute nullability from the 73 * [annotations], if any, and if not then default to platform nullness. 74 */ 75 fun create( 76 annotations: List<AnnotationItem>, 77 knownNullability: TypeNullability? = null, 78 immutableReason: String? = null, 79 ): TypeModifiers { 80 // Use the known nullability, or find if there is a nullness annotation on the type, 81 // defaulting to platform nullness if not. 82 val nullability = 83 knownNullability 84 ?: annotations 85 .firstOrNull { it.isNullnessAnnotation() } 86 ?.let { TypeNullability.ofAnnotation(it) } 87 ?: TypeNullability.PLATFORM 88 return DefaultTypeModifiers(annotations.toMutableList(), nullability, immutableReason) 89 } 90 } 91 } 92