java 是否可以将 Lombok 与 Kotlin 一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46027391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:01:33  来源:igfitidea点击:

Is it possible to use Lombok with Kotlin?

javakotlinlombokannotation-processing

提问by ps-aux

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

我有一个 Kotlin Gradle 项目。我添加了 Lombok 作为依赖项,并用 kapt 注册了它

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

I would like to use just @Slf4janotation for automatic loggeneration. It works for Java classes but not for the Kotlin ones.

我只想使用@Slf4j注释进行自动log生成。它适用于 Java 类,但不适用于 Kotlin 类。

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

现在是否可以同时使用 Kotling 和 Lombok?

EDIT: Adding more details

编辑:添加更多细节

If I annotate a Kotlin classs with @Slf4jand use loginside it I get

如果我用 Kotlin 类注释@Slf4jlog在其中使用,我会得到

Unresolved reference: log

未解决的参考:日志

Evidently no annotation processing is applied.

显然没有应用注释处理。

采纳答案by Sahil Chhabra

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

您不能使用 annotation @Slf4j,而是在所需的类中手动创建其对象。

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

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

回答by Michael Piefel

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/2621917straight from the horse's mouth.

Lombok 不在您的源代码上运行,而是在 AST 上运行。总之,它是一个注解处理器,由 Java 编译器在编译时运行。Kotlin 编译器不使用这些注释处理器。另见答案https://stackoverflow.com/a/35530223/2621917直接从马嘴里说出来。

回答by YetAnotherMatt

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

如果您只想将 Lombok 用于 @Slf4j,那么我建议改用 kotlin-logging:https: //github.com/MicroUtils/kotlin-logging

回答by Michael

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

它不受支持,而且从表面上看,它不会是.

回答by pirho

For logging the best I could do - because @Slf4jdid not work - was like creating abstract log class like:

为了记录我能做的最好的事情 - 因为@Slf4j没有用 - 就像创建抽象日志类一样:

package org.example

import org.slf4j.LoggerFactory
import org.slf4j.Logger

abstract class Log {
    val log: Logger = LoggerFactory.getLogger(this.javaClass)
}

and usage:

和用法:

package org.example

class MyClass { 
    companion object : Log() {}
    @Test
    fun someFun() {
        log.info("Logging info")
    }
}

回答by Eyal

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.

如果没有 lombok 团队的额外支持,我无法理解它会如何工作。Lombok 基于注释处理,因此它在编译时运行并在您的源代码上运行,所以我猜它假定了 Java 的语法。