Is it possible to use Lombok with Kotlin?

2019-06-14 23:05发布

问题:

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:

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



回答3:

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/



回答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.