David Herman
01/03/2024, 5:13 AMDavid Herman
01/03/2024, 5:17 AM// Case 1
abstract class Converter<I, O>
class NumericStringToNumberConverter : Converter<String, Int>
// Case 2
abstract class Converter<I, O>
abstract class ToStringConverter<I> : Converter<I, String>
abstract class DoubleToStringConverter : ToStringConverter<Double>
For case 1, when I visit a "NumericStringToNumberConverter" KSClassDeclaration, I want to know that it implements "Converter" and that its type parameters are bound to String and Int.
For case 2, when I visit a "DoubleToStringConverter", I want to know that it implements "Converter" as well, and that its type parameters are bound to Double and String.David Herman
01/03/2024, 5:21 AMwhatever.the.path.is.to.Converter
(*) but then I'm not sure how to check what its specific type parameters are.
(*) Side note: Should I just do a string check against the fully qualified name? Or should I somehow fetch the KSType of whatever.the.path.is.to.Converter
earlier and check against that?David Rawson
01/03/2024, 7:04 PMKSClassDeclaration
for`NumericStringToNumberConverter` you can find out the type arguments like this:
val converterTypeArguments = declaration.getAllSuperTypes().firstOrNull *{* it.declaration.qualifiedName.asString() == "Converter" *}*?.arguments
then:
_assert_(converterTypeArguments?.map { it.type.resolve() } == _listOf_(resolver.builtIns.stringType, resolver.builtIns.intType))
David Herman
01/03/2024, 7:05 PMDavid Herman
01/03/2024, 7:10 PMvisit
pass with a visitor whose purpose is to fetch the KSType
, and then pass that into a second round visitor?David Herman
01/03/2024, 7:13 PMit.resolve().declaration.qualifiedName.asString()
)David Rawson
01/03/2024, 7:14 PMgiven a fully qualified class name, can I easily ask KSP to get a KSType for it (or null if not found)?Would
resolver.getClassDeclarationByName()
work for that?
Once you have the KSClassDeclaration
you can easily get to its KSType
.David Herman
01/03/2024, 7:18 PMDavid Herman
01/04/2024, 12:45 AM