Is it possible to use Lombok with Kotlin?

时间秒杀一切 提交于 2019-12-10 00:42:29

问题


I have a Kotlin Gradle project. I added Lombok as a dependency and also registred it with kapt

compileOnly("org.projectlombok:lombok:$lombokVersion")
kapt("org.projectlombok:lombok:$lombokVersion")

I would like to use just @Slf4j anotation for automatic log generation. It works for Java classes but not for the Kotlin ones.

Is using Kotling and Lombok together even possible as of now ?

EDIT: Adding more details

If I annotate a Kotlin classs with @Slf4j and use log inside it I get

Unresolved reference: log

Evidently no annotation processing is applied.


回答1:


Lombok does not run on your source code, but on the AST. Anyway, it is an annotation processor that is run at compile-time by the Java compiler. The Kotlin compiler does not use these annotation processors. See also the answer https://stackoverflow.com/a/35530223/2621917 straight from the horse’s mouth.




回答2:


You cannot use annotation @Slf4j, but manually create its object in the class required.

Refer https://www.reddit.com/r/Kotlin/comments/8gbiul/slf4j_loggers_in_3_ways/




回答3:


It's not supported and, by the looks of things, it isn't going to be.




回答4:


If all you want to use Lombok for is @Slf4j, then I'd suggest using kotlin-logging instead: https://github.com/MicroUtils/kotlin-logging




回答5:


I can't see how it would work without additional support from the lombok team. Lombok is based on annotation processing so it runs during compilation time and runs on your source code, so I guess it assumes Java's syntax.



来源:https://stackoverflow.com/questions/46027391/is-it-possible-to-use-lombok-with-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!