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

v79

07/17/2022, 7:51 AM
Very new to #ksp - not even sure if this is what I want. Say I have a project with two modules, A and B. A contains the
main
function. B contains a class with a DSL that I'm very interested in interrogating. Is that something #ksp can do for me? So when A
main
runs, it can iterate over some values in the DSL class in B? I'm probably not even clear what I want...
I don't think I can do what I want with Reflections, pretty certain it has to be compile time.
d

David Rawson

07/17/2022, 8:42 AM
If you are talking about a program (for consumption in A) that takes as its input another program (a DSL in B) then that is metaprogramming and both reflection and KSP should be able to do this. There are of course some limitations (like function body information not being available)
v

v79

07/17/2022, 8:46 AM
If function body information isn't available, does suggest that KSP wouldn't be able to process code written with a Kotlin DSL?
d

David Rawson

07/17/2022, 8:49 AM
You can look at “how KSP models Kotlin code” in the docs to see what info is available: https://kotlinlang.org/docs/ksp-additional-details.html
So if you are talking about inspecting an EDSL (type-safe builder) Kotlin class, then the function names, function return types, property names and return types, nested declarations etc. will be available to KSP.
v

v79

07/17/2022, 8:54 AM
As is often the case, I suspect my reach is beyond my abilities, but I'll persevere. Just watched you on YouTube demoing KSP.
d

David Rawson

07/17/2022, 8:55 AM
😬 that video has some cringey mistakes in it 😬 but I guess it will help give an idea of how to use the APIs and what info is available
v

v79

07/17/2022, 10:55 AM
I've been experimenting and I am not sure KSP is what I need. When module A runs, it needs to be able to interrogate the compiled code of module B. (Specifically, I'm trying to get a list of lambda functions with particular patterns). Perhaps with KSP, when module B compiles I can generate a text file with the information I need, when A will then pick up when it runs. I don't need code generation. If I use reflection, of course, then that's always runtime - as module A runs, it cannot interrogate module B, as module B isn't running.
And finally, I know understand that as KSP doesn't parse expressions, it won't work for me. I can't get it to parse code like this:
Copy code
class MyRouter(private val name: String) {
	val router = Routing().apply {
		@Router
		get("/hello")
	}
}
(I'm trying to get all the elements with the
@Router
annotation, or better still, all the member expressions of the `router`field).
d

David Rawson

07/18/2022, 9:41 AM
sounds like a problem that could be solved by exposing some public class in B that A can access?
v

v79

07/18/2022, 9:46 AM
Module A only runs once its deployed to AWS (it's a set of AWS lambda functions). Module B needs to know the contents of module A at compile time. But A can't report to B because A isn't running. I've been trying to build something like Jetbrains Kotless and looking at how they do it. Turns out they are using internal Jetbrains compiler magic to parse the source code (I think). I'm increasingly sure that neither KSP nor KAPT can do what I want.
3 Views