Hi there, is it possible to get the name of the co...
# ksp
s
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
what do you mean by “name of the constants”?
s
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
if you already have the enum class, you can get the enum entries by
KSClassDeclaration.declarations
s
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
It will only work for entities being processed (i.e. user enums)
j
it will work
s
Okay, thanks a lot. I will give it a shot 👍
I've one more question @Jiaxiang, can KSP process Java only sources?
b
No
s
Thanks for your reply @Big Chungus
Nice username btw 😅
b
Thanks, I'm very proud of it!
j
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
Wait what? Is this recent?
j
no it’s not, that was a compiler flag.
b
No I know the flag, I meant ksp being able to process java files
j
oh KSP has been able to process java files since day 1…
b
Oh boy... I've lied to so many people then!..
s
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
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
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
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
Ah yes. It only produces the .kotlin_module files. I thought it also generated the .class files.
367 Views