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.text 18 19 import com.android.tools.metalava.model.ArrayTypeItem 20 import com.android.tools.metalava.model.ClassTypeItem 21 import com.android.tools.metalava.model.JAVA_LANG_ANNOTATION 22 import com.android.tools.metalava.model.JAVA_LANG_ENUM 23 import com.android.tools.metalava.model.JAVA_LANG_OBJECT 24 import com.android.tools.metalava.model.TypeItem 25 import com.android.tools.metalava.model.TypeParameterScope 26 import com.android.tools.metalava.model.type.ContextNullability 27 import com.android.tools.metalava.model.type.DefaultTypeItemFactory 28 29 internal class TextTypeItemFactory( 30 private val assembler: TextCodebaseAssembler, 31 private val typeParser: TextTypeParser, 32 typeParameterScope: TypeParameterScope = TypeParameterScope.empty, 33 ) : DefaultTypeItemFactory<String, TextTypeItemFactory>(typeParameterScope) { 34 35 /** A [JAVA_LANG_ANNOTATION] suitable for use as a super type. */ 36 val superAnnotationType 37 get() = getInterfaceType(JAVA_LANG_ANNOTATION) 38 39 /** A [JAVA_LANG_ENUM] suitable for use as a super type. */ 40 val superEnumType 41 get() = getSuperClassType(JAVA_LANG_ENUM) 42 43 /** A [JAVA_LANG_OBJECT] suitable for use as a super type. */ 44 val superObjectType 45 get() = getSuperClassType(JAVA_LANG_OBJECT) 46 47 override fun self() = this 48 49 override fun createNestedFactory(scope: TypeParameterScope) = 50 TextTypeItemFactory(assembler, typeParser, scope) 51 52 override fun getType( 53 underlyingType: String, 54 contextNullability: ContextNullability, 55 isVarArg: Boolean 56 ): TypeItem { 57 var typeItem = 58 typeParser.obtainTypeFromString( 59 underlyingType, 60 typeParameterScope, 61 contextNullability, 62 ) 63 64 // Check if the type is an array and its component nullability needs to be updated based on 65 // the context. 66 val forcedComponentNullability = contextNullability.forcedComponentNullability 67 if ( 68 typeItem is ArrayTypeItem && 69 forcedComponentNullability != null && 70 forcedComponentNullability != typeItem.componentType.modifiers.nullability 71 ) { 72 typeItem = 73 typeItem.substitute( 74 componentType = typeItem.componentType.substitute(forcedComponentNullability), 75 ) 76 } 77 78 // Check if the type's nullability needs to be updated based on the context. 79 val typeNullability = typeItem.modifiers.nullability 80 val actualTypeNullability = 81 contextNullability.compute(typeNullability, typeItem.modifiers.annotations) 82 return if (actualTypeNullability != typeNullability) { 83 typeItem.substitute(actualTypeNullability) 84 } else typeItem 85 } 86 87 override fun getExceptionType(underlyingType: String) = 88 super.getExceptionType(underlyingType).also { exceptionTypeItem -> 89 if (exceptionTypeItem is ClassTypeItem) { 90 assembler.requireStubKindFor(exceptionTypeItem, StubKind.THROWABLE) 91 } 92 } 93 94 override fun getInterfaceType(underlyingType: String) = 95 super.getInterfaceType(underlyingType).also { classTypeItem -> 96 assembler.requireStubKindFor(classTypeItem, StubKind.INTERFACE) 97 } 98 } 99