【问题标题】:lombok @NonNull on Field not readable using getAnnotationslombok @NonNull on Field not readable using getAnnotations
【发布时间】:2020-05-15 13:58:00
【问题描述】:

例子:

@Value
@Builder(toBuilder = true)
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public class A {
    //
    // RecordRootable fields
    //
    @NonNull
    private UUID a;
}

查看类文件,如果 IntelliJ 反编译器没有说谎,@NonNull 注释似乎存在。

现在我们想在运行时使用 Field.getAnnotations 或类似的东西从字段中读取注释,但没有找到。

有什么特别的方法可以用来阅读这个注释吗?

我需要在注释中添加什么内容才能阅读吗?

【问题讨论】:

    标签: reflection annotations lombok


    【解决方案1】:

    注解好像有 @Retention(RetentionPolicy.CLASS) 代替 @Retention(RetentionPolicy.RUNTIME) 所以它们不能被反射性地阅读

    【讨论】:

    • 另外,Lombok 在编译过程中会移除所有的注解,因为编译后的代码中不应该有任何对 Lombok 的运行时依赖。
    猜你喜欢
    • 2019-06-29
    • 2021-12-05
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 2022-12-26
    • 2017-12-07
    • 2019-11-06
    相关资源
    最近更新 更多