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

Ahmed na

02/28/2023, 12:12 AM
Hi Can we use generated classes in original annotated class ?
Copy code
@MyAnnotatoin
data class A(){
   fun b(): MyAnnotatoinGeneratedClass {
       return MyAnnotatoinGeneratedClass()
   }
}
where
MyAnnotatoinGeneratedClass
is a class that was generated by
@MyAnnotation
Ofc i'm getting not resolved right now
b

Big Chungus

02/28/2023, 12:14 AM
This is a chicken and egg problem so no way to solve it without modifying existing code which ksp does not support.
You could achieve it with a full kotlin compiler plugin though.
y

yigit

02/28/2023, 12:14 AM
I think it should work, no? Validation will fail though so you would need to skip validation
I would avoid such design if possible though
a

Ahmed na

02/28/2023, 12:16 AM
I know it's a deadLock 😬 , but wondering how they did it here , they used kpat i think
b

Big Chungus

02/28/2023, 12:37 AM
Multi-round processing in play I guess. Ksp kinda has that too but it might have different limitations. Do you get unresolved errors during gradle build or just in IDE? (never trust ide errors, always check if it happens when building from cli)
a

Ahmed na

02/28/2023, 12:46 AM
in the cli too , actually how i ended up here is bc i was using the generated code in the class , and it worked fine
Copy code
@MyAnnotatoin
data class A(){
   fun b(): String {
       val c = MyAnnotatoinGeneratedClass()
       return c.process()
   }
}
but when i used it as a return type i hit this wall
j

Jiaxiang

02/28/2023, 12:58 AM
For your working case, it works fine because you have explicit return type declared for the function, and KSP does not care about function body expressions, so you are not getting error types because they are not touched by KSP. When you changed it to return type, you are getting unresolved because now KSP need to resolve the type for the yet non exist type. As Yigit suggested, you can ignore the validation part for now. In your case, I don’t have full picture of what you want to do here, but from the code snippet, it seems not hurt to just defer the processing of
data class A
to next round where (ideally) the generated class
MyAnnotationGeneratedClass
should be available, am I misunderstanding anything here?
a

Ahmed na

02/28/2023, 1:07 AM
@Jiaxiang yes i'll give it a try , how can i ignore the validation for a fun/class i used
@Suppress
seams only for lintting
j

Jiaxiang

02/28/2023, 1:09 AM
So, after reading your snippet again, actually maybe not about ignoring validation, in your processor, if you see unresolvable type (MyAnnotationGeneratedClass) in this case, you can just defer the symbol
A
with the return result of
SymbolProcessor.process()
, and therefore KSP will grab the deferred symbol in the next round, and if
MyAnnotationGeneratedClass
is generated in the first round, it should be able to unblock the processing of
A
in second round.
a

Ahmed na

02/28/2023, 1:16 AM
ok i think i got it , i'm reading this https://kotlinlang.org/docs/ksp-multi-round.html#changes-to-your-processor i'll try to edit the processor
I got it working by ignoring the validation
Copy code
val validatedSymbols = resolved.filter {
    it.validate()
    true // <--- ignore validation
}.toList()
as for multi round/deferring i'll give another shot later Thank you !
12 Views