I'd like to generate an override for a class at co...
# getting-started
y
I'd like to generate an override for a class at compile time. for example, an override of
hashCode()
. is this possible using annotations?
j
You can do this using ksp plugin
1
y
oh lovely, that's exactly what I wanted
c
KSP, like Java’s annotation processor, cannot modify source code, it is only capable of generating new code. It cannot add functions/properties to existing classes, for example. While you can use KSP to generate extension functions which look like adding members to a class, it cannot be used to generate overrides like
hashCode()
Unlike a full-fledged compiler plugin, processors cannot modify the code. A compiler plugin that changes language semantics can sometimes be very confusing. KSP avoids that by treating the source programs as read-only.
https://kotlinlang.org/docs/ksp-overview.html#overview