FIR IDE: API to determine if the given KtType is a functional interface type

This commit is contained in:
Jinseong Jeon
2021-08-19 22:49:28 -07:00
committed by Ilya Kirillov
parent b7a99eca55
commit dca8789659
2 changed files with 16 additions and 1 deletions
@@ -12,10 +12,17 @@ import org.jetbrains.kotlin.idea.frontend.api.types.*
import org.jetbrains.kotlin.name.ClassId
public abstract class KtTypeInfoProvider : KtAnalysisSessionComponent() {
public abstract fun isFunctionalInterfaceType(type: KtType): Boolean
public abstract fun canBeNull(type: KtType): Boolean
}
public interface KtTypeInfoProviderMixIn : KtAnalysisSessionMixIn {
/**
* Returns true if this type is a functional interface type, a.k.a. SAM type, e.g., Runnable.
*/
public val KtType.isFunctionalInterfaceType: Boolean
get() = analysisSession.typeInfoProvider.isFunctionalInterfaceType(this)
/**
* Returns true if a public value of this type can potentially be null. This means this type is not a subtype of [Any]. However, it does not
* mean one can assign `null` to a variable of this type because it may be unknown if this type can accept `null`. For example, a public value
@@ -5,6 +5,8 @@
package org.jetbrains.kotlin.idea.frontend.api.fir.components
import org.jetbrains.kotlin.fir.resolve.FirSamResolverImpl
import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.fir.types.canBeNull
import org.jetbrains.kotlin.idea.frontend.api.components.KtTypeInfoProvider
import org.jetbrains.kotlin.idea.frontend.api.fir.KtFirAnalysisSession
@@ -17,5 +19,11 @@ internal class KtFirTypeInfoProvider(
override val token: ValidityToken,
) : KtTypeInfoProvider(), KtFirAnalysisSessionComponent {
override fun isFunctionalInterfaceType(type: KtType): Boolean {
val coneType = (type as KtFirType).coneType
val samResolver = FirSamResolverImpl(analysisSession.rootModuleSession, ScopeSession())
return samResolver.getFunctionTypeForPossibleSamType(coneType) != null
}
override fun canBeNull(type: KtType): Boolean = (type as KtFirType).coneType.canBeNull
}
}