https://kotlinlang.org logo
#kapt
Title
# kapt
t

Timo Drick

04/11/2021, 12:18 AM
Hi, is it possible to get the source file of the code where an annotation is used inside of an annotation Processor? So e.g. i do have the file main.kt and there is my annotation attached to the main function. Is it posiible to get the path and file name of that file inside of my Processor?
y

yigit

04/11/2021, 12:33 AM
I don't think there is an api for that but you probably can via reflection. We do something similar in room where we append element name to an error message if the element is from a .class file. (Otherwise, there is no location on the message) https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:room/compiler-processing/src/main/java/androidx/room/compiler/processing/javac/JavacProcessingEnvMessager.kt;l=49
t

Timo Drick

04/11/2021, 12:45 AM
@yigit thank you for this fast answer. I will look at it. Yes i am able to get the file class (Not sure it is really called like that.) and from this name i know the file name of the source. But i do not get the path. It could be anywhere right? But yes would be one solution to just search the source directory for the file.
But i am writing a library that can be used from other projects. And i want to know the location of the file where my annotation is used. I need the exact path. For now the user has to provide this path by hand.
y

yigit

04/11/2021, 12:55 AM
If it is a public library, i would say don't do it. At least if it is core functionality. Maybe annotation processor is not what you want. Maybe this? https://projectlombok.org/ Also, with gradlew tasts etc, the file you find may not be the actual source file.
t

Timo Drick

04/11/2021, 12:59 AM
I am working on an hot reload for compose for desktop and i have to observe file changes during runtime. So it will be only used during development. I know that i can get the file name and line number by analyzing the stacktrace. But the path is missing.
I mean observing source code file changes.
So the program will hot reload itself when it detects that the source is changed.
And also compiles itself 😄
It is already working. But to use it you have to write some boilerplate code which i want to eliminate by generating this code with kapt.
y

yigit

04/11/2021, 1:02 AM
Interesting project👍 Where do you use the annotation processor? You may want to use a Gradle task to hook into the files. Also, might be better to use ksp. It has a location api :https://github.com/google/ksp/blob/master/api/src/main/kotlin/com/google/devtools/ksp/symbol/Location.kt
t

Timo Drick

04/11/2021, 1:03 AM
I do have a seperate module for the annotation processor for now. Because it is not compatible with compose. I will have a look at ksp. Thank you for the hint.
Looks interesting. I thought about writing a compiler plugin but looked a little bit too complicated to me. So maybe KSP is what i want 😄
I was able to port my KAPT to KSP and now i do have all information i need and also the kotlin types are now easier to handle. Thanks again @yigit If anyone is interested on this project here is the source: https://gitlab.com/compose1/livecomposable
y

yigit

04/12/2021, 4:17 PM
nice!
so this is hot reload on desktop?
t

Timo Drick

04/12/2021, 4:18 PM
It is limited but yes. Hotreload for a @Composable function for Compose for Desktop
I am preparing a demonstration video and fixing some issues. When i am finish i will post it on the compose-desktop channel.
y

yigit

04/12/2021, 5:34 PM
yep and a blog post would be great!
10 Views