简体   繁体   English

当在 class 上注释时,如何配置 lombok 以生成 static 成员的 Getters/Setter

[英]How to configure lombok to generate Getters/Setter for static members also when annotated on class

I have a class for all static members.我有一个 class 用于所有 static 成员。 The number of static members is more than 10 (which may increase with time). static成员数量超过10个(可能会随着时间增加)。

I am using lombok and I want to generate Getters/Setters for all static members using single @Getter and @Setter annotation on class like we do for non-static members.我正在使用 lombok,我想为所有 static 成员在 class 上使用单个@Getter@Setter注释生成 Getter/Setter,就像我们为非静态成员所做的那样。

I know that我知道

You can also put a @Getter and/or @Setter annotation on a class.您还可以在 class 上放置 @Getter 和/或 @Setter 注释。 In that case, it's as if you annotate all the non-static fields in that class with the annotation.在这种情况下,就好像您使用注释注释了 class 中的所有非静态字段。

I also know that我也知道

We can annotate static fields individually using @Getter @Setter to generate Getters/Setters for static fields.我们可以使用 @Getter @Setter 单独注释@Getter @Setter字段以生成 static 字段的 Getter/Setter。

But this looks ugly and I want to make my class look as clean as possible.但这看起来很难看,我想让我的 class 看起来尽可能干净。

Is there any way I can configure / Override @Getter and @Setter annotation so that I can annotate the class and it generate Getters and Setters for all members including static and non-static members, after all what those methods do is to return the mentioned variable.有什么方法可以配置/覆盖 @Getter 和 @Setter 注释,以便我可以注释 class 并为包括 static 和非静态成员在内的所有成员生成 Getter 和 Setter,毕竟这些方法所做的是返回提到的多变的。

To be more precise, i want following code snippet to generate Getters and Setters for all class variables-更准确地说,我希望以下代码片段为所有 class 变量生成 Getter 和 Setter -

@Getter
@Setter
public class myClass {
    private static String d;
    private static SomePojo c;

    private String a;
    private Integer b;
    private SomeClass d;

}

Add @Getter to the static member itself and it should work.将@Getter 添加到@Getter成员本身,它应该可以工作。

@Getter
private static final String DEFAULT_VAL = "TEST"; 

For static fields you have to add @Getter to the specific field:对于 static 字段,您必须将@Getter添加到特定字段:

@Getter
@Setter
public class Task {
    @Getter
    private static int numberOfTasks;
    @Getter
    private static int taskId;
    private String taskName;
    private Integer executionTime;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM