1 /* 2 * Copyright (C) 2017 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 import com.android.tools.metalava.model.item.ParameterDefaultValue 20 21 @MetalavaApi 22 interface ParameterItem : ClassContentItem, Item { 23 /** The name of this field */ namenull24 fun name(): String 25 26 /** The type of this field */ 27 @MetalavaApi override fun type(): TypeItem 28 29 override fun findCorrespondingItemIn( 30 codebase: Codebase, 31 superMethods: Boolean, 32 duplicate: Boolean, 33 ) = 34 containingCallable() 35 .findCorrespondingItemIn(codebase, superMethods = superMethods, duplicate = duplicate) 36 ?.parameters() 37 ?.getOrNull(parameterIndex) 38 39 /** The containing callable. */ 40 fun containingCallable(): CallableItem 41 42 /** The possible containing method, returns null if this is a constructor parameter. */ 43 fun possibleContainingMethod(): MethodItem? = containingCallable().let { it as? MethodItem } 44 45 /** Index of this parameter in the parameter list (0-based) */ 46 val parameterIndex: Int 47 48 /** 49 * The public name of this parameter. In Kotlin, names are part of the public API; in Java they 50 * are not. 51 */ publicNamenull52 fun publicName(): String? 53 54 /** 55 * Returns whether this parameter has a default value. In Kotlin, this is supported directly; in 56 * Java, it's supported via a special annotation, {@literal @DefaultValue("source"). This does 57 * not necessarily imply that the default value is accessible, and we know the body of the 58 * default value. 59 */ 60 fun hasDefaultValue(): Boolean 61 62 /** The default value of this [ParameterItem]. */ 63 val defaultValue: ParameterDefaultValue 64 65 /** Whether this is a varargs parameter */ 66 fun isVarArgs(): Boolean = modifiers.isVarArg() 67 68 /** The property declared by this parameter; inverse of [PropertyItem.constructorParameter] */ 69 val property: PropertyItem? 70 get() = null 71 72 override fun parent(): CallableItem? = containingCallable() 73 74 override val effectivelyDeprecated: Boolean 75 get() = originallyDeprecated || containingCallable().effectivelyDeprecated 76 77 override fun baselineElementId() = 78 containingCallable().baselineElementId() + " parameter #" + parameterIndex 79 80 override fun accept(visitor: ItemVisitor) { 81 visitor.visit(this) 82 } 83 84 /** 85 * Returns whether this parameter is SAM convertible or a Kotlin lambda. If this parameter is 86 * the last parameter, it also means that it could be called in Kotlin using the trailing lambda 87 * syntax. 88 * 89 * Specifically this will attempt to handle the follow cases: 90 * - Java SAM interface = true 91 * - Kotlin SAM interface = false // Kotlin (non-fun) interfaces are not SAM convertible 92 * - Kotlin fun interface = true 93 * - Kotlin lambda = true 94 * - Any other type = false 95 */ isSamCompatibleOrKotlinLambdanull96 fun isSamCompatibleOrKotlinLambda(): Boolean = 97 // TODO(b/354889186): Implement correctly 98 false 99 100 /** 101 * Create a duplicate of this for [containingCallable]. 102 * 103 * The duplicate's [type] must have applied the [typeVariableMap] substitutions by using 104 * [TypeItem.convertType]. 105 * 106 * This is called from within the constructor of the [containingCallable] so must only access 107 * its `name` and its reference. In particularly it must not access its 108 * [CallableItem.parameters] property as this is called during its initialization. 109 */ 110 fun duplicate( 111 containingCallable: CallableItem, 112 typeVariableMap: TypeParameterBindings, 113 ): ParameterItem 114 115 override fun equalsToItem(other: Any?): Boolean { 116 if (this === other) return true 117 if (other !is ParameterItem) return false 118 119 return parameterIndex == other.parameterIndex && 120 containingCallable() == other.containingCallable() 121 } 122 hashCodeForItemnull123 override fun hashCodeForItem(): Int { 124 return name().hashCode() 125 } 126 toStringForItemnull127 override fun toStringForItem() = "parameter ${name()}" 128 129 override fun containingClass(): ClassItem = containingCallable().containingClass() 130 131 override fun containingPackage(): PackageItem? = containingCallable().containingPackage() 132 133 // TODO: modifier list 134 } 135