-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Open
Description
Hello,
I'm wanting to use Kotlin in a project that uses Lombok with Java.
However, while the IDE shows code as valid, the Kotlin compiler can not see Lombok generated code.
This makes mixing Java+Lombok with Kotlin impossible.
Would it be possible to also apply the Lombok processing to the Kotlin compiler so we can have harmony?
pavlo-v-chernykh, Zverijo, arvindkumarc, BenTilbrook, wszymeczko and 66 morepavel-kiper
Activity
dodgex commentedon Mar 18, 2017
For my Android App I'm currently playing with Kotlin and was able to let Kotlin see Lombok generated getters by adding the lombok processor to
kapt
.This is from a gradle build but should be also possible with maven. :)
vojkny commentedon May 23, 2017
How can this be done via Maven? Also how can I use
kapt
in IDE?vojkny commentedon May 24, 2017
I tried using
kapt
with lombok annotation processor as follows:With no effect.
ubaierbhat commentedon Jul 12, 2017
I have the same problem. We are using Lombok in our Android apps and it looks like we can't use it along side with kotlin. @dodgex suggestion did not work for me.
taejungkim commentedon Aug 17, 2017
@dodgex I added kapt plugin with kapt "org.projectlombok:lombok:1.16.14" .
But impossible access to getter ( generated by lombok )
Help me!
faizanahemad commentedon Oct 12, 2017
Any solution found to this? I have the same issue. We use Intellij with Java and lombok. and build with maven.
jtonic commentedon Oct 12, 2017
Hi,
The only solution I found quite satisfactory was to move the lombok annotated java file in a separate maven module. But in our case lombok annotations were only used for DTOs (model), so it did make sense to approach this way.
Kind regards
Tony
rojiani commentedon Jan 23, 2018
For anyone looking for a solution, I've created a demo app illustrating the separate module idea mentioned by @jtonic (except using Gradle rather than Maven).
mouyang commentedon Feb 18, 2018
@dodgex do you have a working solution or a minimal proof-of-concept that you are able to post? All of the comments here suggest that this does not work.
dodgex commentedon Feb 18, 2018
Sorry but currently I don't have a project using lombok & kotlin.
mouyang commentedon Feb 23, 2018
Hmmm. I'm probably not understanding the issue but it seems like this is possible as of Kotlin 1.2.21. All I did was blindly follow the instructions on the Kotlin Web site ...
https://kotlinlang.org/docs/reference/using-maven.html#compiling-kotlin-and-java-sources
https://kotlinlang.org/docs/reference/using-gradle.html
and I was able to see Lombok-generated methods in Kotlin without creating any separate modules ...
https://github.com/mouyang/kotlin-lombok/tree/master/gradle
https://github.com/mouyang/kotlin-lombok/tree/master/maven
Again, I'm probably misunderstanding the issue completely so any clarification would be appreciated.
jtonic commentedon Feb 24, 2018
Hi,
I am not a specialist but I am trying to shed a light on the problem.
There are some popular ATP libraries to generate useful java sources (related to hibernate, spring boot properties, java bean mappers - MapStruct) and there is no problem with them in java/kotlin module, thanks to kapt. But on the other hand because lombok modifies the bytecode during the compilation kapt can do nothing about it.
This is the problem I came across and if, I remember well, there was a thread, I suppose on stackoverflow, where the kotlin language architect told about this issue, explaining things more in depth.
Kind regards.
mouyang commentedon Feb 27, 2018
@jtonic, thank you. Your explanation makes a lot of sense.
@dodgex If you have time, can you take a look at a sample branch I have? It's an minimal unsuccessful attempt to use Lombok in Kotlin, and I was wondering what you did to make it work.
I did not use "annotationProcessor" since I'm not working on Android, but I don't know if that actually matters or not. https://github.com/mouyang/kotlin-lombok/tree/broken-gradle-kapt
jtonic commentedon Feb 27, 2018
@mouyang
I looked a bit over your example (gradle based one) and I have some recommendations.
Note: I remember I read on some stack overflow thread another approach - smth like put some generated lomboked classes in a gen folder and then refer it in the kotlin compilation one. This could work but for me it was an additional burden on the build tool.
I really hope all these help you figured out how to approach the kotlin/java/lombok issue.
ericytsang commentedon Jun 18, 2018
I've been trying to look for a solution for this problem to try to get my ancient team to migrate to a new language. we're so ancient that the leads complain about using java8's lambdas. i ended up making a
pom.xml
that delomboks the code insrc/main/java
then compiles the delomboked code with the kotlin plugin:i usually use gradle..im not too familiar with maven. improvements are welcome. i don't think it supports incremental compilation...i also imagine that having to modify every plugin originally using
src/main/java
to now use${project.build.directory}/delombok-main
would be a pain. debugging also becomes odd since the compiled code is the delomboked code instead of the code insrc/main/java
.https://stackoverflow.com/a/50914110/2898715
DRSchlaubi commentedon Apr 27, 2021
Seems like Kotlin themselves is working on a Lombok integration: https://github.com/JetBrains/kotlin/tree/master/plugins/lombok/lombok-compiler-plugin
Rawi01 commentedon Apr 27, 2021
That's fascinating, I had a quick look at the code and it seems like it adds some kind of method stubs to make them visible for Kotlin. If I have not missed anything that requires a basic reimplementation of all lombok handlers, seems to be a bunch of work 😄
I also tried to understand the Kotlin compilation process and I think it still invokes
javac
to compile the Java files. If it would be possible to run an annotation processor at that moment lombok should work. IIRC I did something similar some time ago for an android project: #2412 (comment)zveznicht commentedon Jun 15, 2021
kotlin+java projects are compiled like this:
So to support lombok you need to provide all symbols lombok generates to kotlin on (1). Thats what new plugin does. You may call it stubs, they are basically declarations of parts lombok will generate later.
During (2) you simply enable lombok as annotation processor and it generates everything as usual.
it will work as long as you don't refer any lombok-generated symbols from kotlin code.
Rawi01 commentedon Jun 15, 2021
@zveznicht Thanks for the additional details. Does kotlinc uses its own parser or does it use javac to generate the symbols/stubs? If it is the former the plugin is the only solution but if it is the latter it might be possible to invoke lombok.
zveznicht commentedon Jun 15, 2021
kotlinc uses its own parser
CharlyLafon37 commentedon Jul 4, 2021
Check https://kotlinlang.org/docs/lombok.html. Seems to work for me.
rzwitserloot commentedon Jan 23, 2022
We're still seeing some issue traffic on this. Does the kotlin plugin for lombok as linked by @CharlyLafon37 above work as expected? To be clear, this is what I expect, and this is what we will (attempt to) fix:
.java
as well as.kt
source files in one go usingkotlinc
, that any lombok annotations used in your java code works totally fine, and that your.kt
code sees the effects (as in, if your java source files use@Getter
, then [A] these compile fine, [B] those getters are indeed generated and in the class files thatkotlinc
spits out, and [C] your.kt
code sees them. – and that this all works if the code is tightly bound (Foo.java
needsBar.kt
and vice versa, so compiling them out separately does not work unless you make stubs).What we won't fix is:
.kt
files, such as putting@Slf4j
on your kotlin class and expecting lombok to stick aLOGGER
field in your kotlin file. We don't do that, and don't intend to. We won't accept PRs unless you bring your own support team, as we are not in a position to provide the maintenance of such a feature. I'm not sure it's a good idea in the first place. Sounds like that should be a separate project, run by different folks. We'd love to work together with such a project to try and use the same names and adopt the same general meanings of things as much as possible, but it would end there.rzwitserloot commentedon Jan 23, 2022
Assuming that plugin works, can somebody tell me what happens if that plugin isn't around? Does lombok just silently not run, or is it more like #2428 describes: That lombok does run as annotation processor, fails to detect environment, says so in an error log, and throws in the towel?
Assuming the kotlinlang plugin works, we need to update our docs to refer to it. If lombok does run but fails to detect environment, we should make it detect environment and adjust our error message to refer to the plugin.
YASMINCOS commentedon Oct 8, 2024
alguma atualização? estou tendo esse problema