https://kotlinlang.org logo
Title
l

leandro

03/22/2021, 7:12 PM
Apologies if this question isn’t appropriate to KSP, as my understanding of it is quite small. Let’s say I have several classes that inherit from an interface throughout my project, and would like to create some kind of output (CSV or so, not to be used in the project) that looks at all implementations of that interface together with its properties (e.g. a Map with simple types/enums). Would it be possible with KSP to do such a thing? Or should I look for a fully fledged compiler plugin?
t

Ting-Yuan Huang

03/22/2021, 7:24 PM
If the values (e.g., initializers) of the properties are not needed, then I think KSP could be a good fit.
l

leandro

03/22/2021, 7:43 PM
Awesome, thank you. A follow-up: I’m unsure what you mean by initializers though. Let’s say I have a class implementing this interface:
public class MyEvent(foo: MyEnum) : Event {
  override val name: String = "Event Name"
  override val properties: Map<String, Any> = mapOf("Foo" to foo)
}
Would KSP not be able to grab the values
name
and
properties
?
j

jw

03/22/2021, 7:53 PM
No. Those are only available at runtime inside instantiated instances of that type.
l

leandro

03/22/2021, 8:04 PM
Would my only option be a compiler plugin? Or not even?
y

yigit

03/22/2021, 8:18 PM
i think the compiler has some
constnat evaluation
logic that is used for annotation values but as jake mentioned, everything else in that sample is a runtime value. So even a compiler plugin cannot do that unless you implement some evaluation logic for those values (notice that, those values can be anything such that the classes used there may not be part of the compiler's own classpath)
f

Fabio

03/29/2021, 10:53 AM
I have a similar but not quite same problem. Pls see an example of DI for koin below:
class CoffeeApp : KoinComponent {

    val maker: CoffeeMaker by inject()
    val aaa = "aaa"
    val l by lazy {  "lazy" }
}
I'd like to find all properties that use
by inject()
. I don't need the value, I'd simply want to know whether
maker
's value is going to be provided by DI, so I put that in a list. So far all I can do is is
isDelegated
, but that's also
true
for
l by lazy
.
j

jw

03/29/2021, 12:28 PM
You cannot. Those are implementation detail behind the properties. Also, that's not DI it's service location because it lacks inversion of control.
f

Fabio

03/29/2021, 9:50 PM
fair enough, is this something annotation processors could do?
or maybe ByteBuddy or arrow-meta? what would be the recommended approach?
j

jw

03/30/2021, 2:12 AM
annotation processors cannot do this. bytebuddy is just a library so you would need to run it in the context of something, but i assume it would run as a bytecode transformation which does have access to everything but at which determining these things is more challenging. i have never looked at arrow-meta but a regular Kotlin compiler plugin would have access to it, yes.
👍 1