How to get the resolved type of property getter us...
# k2-adopters
a
How to get the resolved type of property getter using delegate using
analyze
apis? Earlier using
bindingContext
below code was working
Copy code
val property = this?.parent as? KtProperty ?: return false
val propertyDescriptor =
    bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, property] as? PropertyDescriptor
return propertyDescriptor?.getter?.let {
    bindingContext[BindingContext.DELEGATED_PROPERTY_RESOLVED_CALL, it]
        ?.resultingDescriptor
        ?.returnType
closest I could get is below
Copy code
val delegate = this ?: return true
return analyze(delegate) {
    val functionSymbol = delegate
        .mainReference
        ?.resolveToSymbols()
        ?.filterIsInstance<KaFunctionSymbol>()
        ?.firstOrNull {
            it.callableId?.callableName == OperatorNameConventions.GET_VALUE
        }
    functionSymbol?.returnType?.nullability == KaTypeNullability.NULLABLE
but this gives the type of the actual implementation not the runtime resolved type(few examples in the đź§µ)
Copy code
// example 1
class A {
    val a: Int? by lazy {
        5
    }
}
// using bindingContext it returns `Int`
// using analyze api it returns `V`(this is generic type by which lazy is implemented not the actual type)

// example 2
class A(private var aDelegate: Int?) {
    private var a: Int? by this::aDelegate
}
// using bindingContext it returns `Int?`
// using the analyze api it returns `V`
In short is there any equivalent of
BindingContext.DELEGATED_PROPERTY_RESOLVED_CALL
in
analyze
api
r
Hi! If I understand your request correctly, there’s no way to do what you want with Analysis API at the moment You would have to wait for KT-66039 to address those issues
Let me ask you this: why exactly do you need to know the type of the
getter
of the delegated property? Are you talking about
getValue
operator specifically? Wouldn’t it be enough to simply know the type of the property itself in that case?
a
Hi Thanks for the answer! I was working on migrating(PR) the Detekt rule
CanBeNonNullable
It warns you for some property which is declared as nullable(maybe by mistake) but can be declared as non null For this usecase, I need to know the type of getter body of delegated property. In my migration PR I was able to pass all the TCs except the above mentioned cases
👌 1