google / ksp

Kotlin Symbol Processing API
https://github.com/google/ksp
Apache License 2.0
2.84k stars 266 forks source link

Request for guidance on accessing const property values in KSP #1771

Open niorgai opened 7 months ago

niorgai commented 7 months ago

Description

I am currently working with Kotlin Symbol Processing (KSP) and I have encountered a scenario where I need to access the value of a const property within an object declaration. However, I am unable to find a straightforward way to retrieve the value of a const property using KSP's API.

Example

To illustrate, I have the following Kotlin object with a const property:

object SharedPreferenceKey {
    const val USER_MAX_NUM = "11112"
}

I would like to access the value "11112" of the property USER_MAX_NUM during symbol processing.

Question

Is there a way to retrieve the value of a const property from a Kotlin object during KSP processing? If so, could you provide an example or point me to the relevant documentation or API?

I am aware that KSP focuses on code structure and symbol processing rather than code execution, but since const properties are compile-time constants, I was hoping there might be a way to access their values.

neetopia commented 7 months ago

It is not currently supported to evaluate expressions on property initializers, if you can copy this compile constant value to an annotation value (might be easier to just put it on the const val in your snippet), then you should be able to get the value computed by KSP via said annotation.

niorgai commented 7 months ago

@neetopia Thank you for the information. I understand that currently expressions on property initializers are not supported for evaluation. Since this feature is available in APT, I was wondering if there are any plans to support this in KSP in the future? It would be beneficial to have parity with APT in this regard.