Open
Description
TLDR: How to (consistently) identify that a type is actually an aliased import?
Given the following code block
import kotlin.Double as FP
interface Mapper {
fun fromFP(fp: FP): Int
fun fromString(str: String?): Int?
}
And given the following simplified KSP code
ksClassDeclaration
.getAllFunctions()
.map {
isAlias(it.parameters.first().type)
}
My current (frankly ugly and not optimal) way to identify if it is an alias:
fun isAlias(typeReference: KSTypeReference): Boolean {
val resolvedType = typeReference.resolve()
return typeReference.toString().takeWhile { it != '<' } != resolvedType.makeNotNullable().toString().takeWhile { it != '<' }
}
Sadly this is not working anymore since 2.1.20-1.0.32, as the typeReference
is not anymore an instance of KSTypeReferenceImpl
(1.0.31) but of KSTypeReferenceResolvedImpl
(1.0.32), which has a different result for toString()
.
For fromFP
:
- With 1.0.31 the type is an instance of
KSTypeReferenceImpl
withtoString() == "FP"
- With 1.0.32 the type is an instance of
KSTypeReferenceResolvedImpl
withtoString() == "Double"
I could amend the already ugly isAlias
logic, but then I directly stumble upon similar problems with nullable types (see fromString
) or even worse: a combination of nullable aliased import types with generics 😵
For fromString
:
- With 1.0.31 the type is an instance of
KSTypeReferenceImpl
withtoString() == "String"
- With 1.0.32 the type is an instance of
KSTypeReferenceResolvedImpl
withtoString() == "String?"
Metadata
Metadata
Assignees
Labels
No labels