https://kotlinlang.org logo
#ksp
Title
# ksp
r

Rafael Costa

03/06/2024, 3:07 PM
Hi guys! I know this may seem a bit crazy, but is it possible to get the KSType here corresponding to
FirstType
and
SecondType
? I am able to get the KSType corresponding to
AbcType
, but not the other two.
ExternalModuleDestinations
is an
annotation class ExternalModuleDestinations<T: SomeType>
and
ExternalDestination
is also
annotation class ExternalDestination<T: OtherType>
.
Copy code
@ExternalModuleDestinations<AbcType>(
        overriding = [
            ExternalDestination<FirstType>(),
            ExternalDestination<SecondType>(),
        ]
    )
    class SomeClass
When I try, I just get
T
, not the actual types used here (
FirstType
and
SecondType
)
j

Jiaxiang

03/06/2024, 8:58 PM
Can you try to read the annotation value of
ExternalModuleDestinations
?
r

Rafael Costa

03/06/2024, 9:19 PM
Hmm how exactly do I do that? 😅 Sorry I’m not familiar with the term
j

Jiaxiang

03/06/2024, 9:21 PM
say you got the
KSClassDeclaration
of
SomeClass
, you can get the list of its annotations via
KSClassDeclaration.annotations
, iterate through the annotation list, find the annotation with type
ExternalModuleDestinations
, read the annotation values by
KSAnnotation.arguments
r

Rafael Costa

03/06/2024, 9:25 PM
Im afk now, but I think that’s what I’m doing, yes. Then I get the argument “overriding” and I get an ArrayLisf<KSAnnotation>. Then I try to get the type argument with something like (will confirm shortly)
annotationType.arguments.first().resolve()
I think I’m missing something there, I’ll check it in 5 min
j

Jiaxiang

03/06/2024, 9:26 PM
don’t resolve, by resolving you get the declaration of the types, which surely only contains type parameter.
🤔 1
r

Rafael Costa

03/06/2024, 9:37 PM
so this is what I actually do (sorry)
Copy code
it.annotationType.resolve().arguments.first().type!!.resolve()
ìt
is a
KSAnnotation
from the
ArrayList<KSAnnotation>
I got from "overriding" argument.
if I don't
resolve
then I get a
KSTypeReference
.. how can I convert that into
KSType
?
I guess I would expect to get
T
if I was using
declaration
, like:
Copy code
it.annotationType.resolve().declaration.typeParameters.first()
Since then I was actually going to the declaration of the annotation type (
ExternalDestination
)
also, I use the exact same code to get the type argument of
@ExternalModuleDestinations<AbcType>
. In that case I do get
AbcType
with
Copy code
ksAnnotation.annotationType.resolve().arguments.first().type!!.resolve()
I get that in this case it is actually annotating, while on the other case I'm passing an "instance" of the annotation class to the "overriding" argument.
@Jiaxiang sorry to tag you, but if you could follow up on this 🙏 I already have a workaround, but I'm still interested in improving things if this is possible.
j

Jiaxiang

03/07/2024, 9:35 PM
sorry, your case might be a little bit convoluted to be communicated via chat, can you put your case in a mini repository so I can take a look?