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

Shreyash Saitwal

09/01/2022, 4:48 PM
Hi there, is it possible to get the name of the constants an enum declares? This isn't possible with the traditional Java annotation processing but I was curious if it's something KSP might support.
j

Jiaxiang

09/01/2022, 4:51 PM
what do you mean by “name of the constants”?
s

Shreyash Saitwal

09/01/2022, 4:55 PM
Say I have an enum like this:
Copy code
enum class Foo { bar, bazz }
Then I want to be able to get the values bar and bazz given that I have KSDeclaration of Foo
I can do this using runtime reflection but since this is an annotation processor, I can't really do that here. 🤷‍♂️
j

Jiaxiang

09/01/2022, 5:39 PM
if you already have the enum class, you can get the enum entries by
KSClassDeclaration.declarations
s

Shreyash Saitwal

09/01/2022, 5:58 PM
Will this work on enums that are not part of the processor itself but of the code which is being processed? This enum will be user-defined and my annotation processor won't know much about it other than the fact that it is the return type (or parameter type) of the method annotated by one of the annotations it is processing.
b

Big Chungus

09/01/2022, 5:59 PM
It will only work for entities being processed (i.e. user enums)
j

Jiaxiang

09/01/2022, 6:00 PM
it will work
s

Shreyash Saitwal

09/01/2022, 6:01 PM
Okay, thanks a lot. I will give it a shot 👍
I've one more question @Jiaxiang, can KSP process Java only sources?
b

Big Chungus

09/01/2022, 6:03 PM
No
s

Shreyash Saitwal

09/01/2022, 6:04 PM
Thanks for your reply @Big Chungus
Nice username btw 😅
b

Big Chungus

09/01/2022, 6:05 PM
Thanks, I'm very proud of it!
j

Jiaxiang

09/01/2022, 6:06 PM
It actually can, you will need to add
-Xallow-no-source-files
flag, as otherwise compiler will simply return upon no kotlin source case. But that will requires you to run KSP in commandline mode which can be tricky to figure out how to do that without the help of gradle.
b

Big Chungus

09/01/2022, 6:07 PM
Wait what? Is this recent?
j

Jiaxiang

09/01/2022, 6:07 PM
no it’s not, that was a compiler flag.
b

Big Chungus

09/01/2022, 6:08 PM
No I know the flag, I meant ksp being able to process java files
j

Jiaxiang

09/01/2022, 6:12 PM
oh KSP has been able to process java files since day 1…
b

Big Chungus

09/01/2022, 6:13 PM
Oh boy... I've lied to so many people then!..
s

Shreyash Saitwal

09/01/2022, 6:13 PM
That's great to hear @Jiaxiang. Also, I'm using kotlinc from the command-line (in a way), so it would it be pretty straightforward to implement this.
b

Big Chungus

09/01/2022, 6:14 PM
I don't recall where I read it, but I was so sure it can only work with kotlin files. Or maybe I got it confused with ksp not being able to cooperate with JAP
@Shreyash Saitwal apologies for misinformation
s

Shreyash Saitwal

09/01/2022, 6:15 PM
No worries, that was my initial thought as well since if you try to use kotlinc on Java-only sources you'd get the no source files errors. I was unaware of the flag mentioned by Jiaxiang.
@Jiaxiang, does that mean I can use kotlinc to compile Java sources as well if I add the
-Xallow-no-source-files
flag?
Turns out, I can 👍
j

Jiaxiang

09/01/2022, 6:21 PM
well, no, kotlinc only process kotlin files no matter what, you still need a separate javac call to compile java files. There might be some flags in kotlinc that will make kotlinc invoke Javac internally, but I can’t recall, and probably I won’t recommend doing so as it might be difficult dealing with javac arguments.
the reason ksp works with Java is because Kotlinc is java aware as information from java sources are necessary for processing kotlin sources.
s

Shreyash Saitwal

09/01/2022, 6:31 PM
Ah yes. It only produces the .kotlin_module files. I thought it also generated the .class files.
124 Views