Lombok的getter/setter在我的注解处理器中不可见。

huangapple go评论59阅读模式
英文:

Lombok getters/setters are not visible from my annotation processor

问题

我对 javax.annotation.processing.AbstractProcessor 进行了自定义实现,它能正常工作。但是我的处理器无法找到由 Lombok 生成的 getter、setter 和构造函数。

这是我的处理器(我需要创建一个最小示例吗?):
https://github.com/hohserg1/ElegantNetworking/blob/1.12.2-annotation-processor/src/main/java/hohserg/elegant/networking/annotation/processor/ElegantPacketProcessor.java#L62

示例类:

ElegantPacket //我的
@Value //lombok
public class Test implements ClientToServerPacket {
    int some; //它是可见的
    //int getSome() //由 Lombok 生成,但不可见
    //public Test(int some) //由 Lombok 生成,但不可见
}
英文:

I did custom implementation of javax.annotation.processing.AbstractProcessor and it work.
But my processor do not found getters, setters and constructors which generated by Lombok.

Here my propceesor(do I need to create minimal example?):
https://github.com/hohserg1/ElegantNetworking/blob/1.12.2-annotation-processor/src/main/java/hohserg/elegant/networking/annotation/processor/ElegantPacketProcessor.java#L62

Example class:

ElegantPacket //my
@Value //lombok
public class Test implements ClientToServerPacket {
    int some; //it visible
    //int getSome() //generated by Lombok, it invisible
    //public Test(int some) //generated by Lombok, it invisible
}

答案1

得分: 1

如果您希望同时运行 Lombok 和另一个注解处理器,您应该对您的代码进行反编译(delombok),然后对结果运行您的注解处理器。

这就是Checker Framework Gradle 插件的做法,正如在Checker Framework 手册中所解释的那样。

解释:
大多数注解处理器要么生成输出(比如发出警告),要么生成新的类。Lombok 是一个修改现有代码的注解处理器。它通过访问 javac 编译器的内部 API(也支持 eclipse)来实现这一点。这些操作会导致 javac 发出包含 Lombok 对您的类所做更改的字节码。然而,这些更改对于编译器的早期阶段(尤其是您的注解处理器)是不可见的。换句话说,Lombok 与其他注解处理器不太兼容。

英文:

If you wish to run both Lombok and another annotation processor, then you should delombok your code and run your annotation processor on the result.

This is what the Checker Framework Gradle Plugin does, as explained in the Checker Framework Manual.

Explanation:
Most annotation processors either produce output (say, issue warnings) or generate new classes. Lombok is an annotation processor that modifies existing code. It does so by accessing internal APIs of the javac compiler (it also supports eclipsec). These manipulations cause javac to emit bytecode that contains Lombok's changes to your classes. However, those changes are invisible to earlier phases of the compiler, notably your annotation processor. Another way of saying all this is that Lombok does not play well with other annotation processors.

答案2

得分: 0

好的,我将为您翻译代码部分:

dependencies {
    //gradle 4.6+
    annotationProcessor 'org.projectlombok:lombok:1.18.8', "io.gitlab.hohserg.elegant.networking:annotation-processor:2.8"
    ...
}

此外,并非所有的 lombok 更改都可以从我的注解处理器中看到。字段访问修饰符的更改是不可见的,但可以从 lombok 的注解中确定。例如,@Value 将包私有字段更改为私有。

同时,插件 apt 也可以在 Gradle 版本低于 4.6 的情况下使用:

buildscript {
    repositories {
        ...
        maven { url 'https://plugins.gradle.org/m2/' }
    }
    dependencies {
        ...
        classpath 'net.ltgt.gradle:gradle-apt-plugin:0.9'
    }
}
dependencies {
    apt 'org.projectlombok:lombok:1.18.8', "io.gitlab.hohserg.elegant.networking:annotation-processor:2.7"
    ...
}

如果您有任何其他翻译需求,请随时告诉我。

英文:

Ok, I solve this by using annotationProcessor of gradle dependency configuration:

dependencies {
    //gradle 4.6+
    annotationProcessor 'org.projectlombok:lombok:1.18.8', "io.gitlab.hohserg.elegant.networking:annotation-processor:2.8"
    ...
}

Also not all lombok changes visible from my annotation processor still. Changes of fields access modifiers is not visible, but it can be determine from lombok annotations. As example, @Value makes package-private fields to private.

Also plugin apt may be used on gradle less that 4.6

buildscript {
    repositories {
        ...
        maven { url 'https://plugins.gradle.org/m2/' }
    }
    dependencies {
        ...
        classpath 'net.ltgt.gradle:gradle-apt-plugin:0.9'
    }
}
dependencies {
    apt 'org.projectlombok:lombok:1.18.8', "io.gitlab.hohserg.elegant.networking:annotation-processor:2.7"
    ...
}

</details>



huangapple
  • 本文由 发表于 2020年9月26日 23:50:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64079635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定