英文:
How to configure lombok to generate Getters/Setter for static members also when annotated on class
问题
我有一个用于所有静态成员的类。静态成员的数量超过10个(可能会随时间增加)。
我正在使用Lombok,并且我希望使用单个@Getter
和@Setter
注解在类上为所有静态成员生成Getter/Setter,就像我们为非静态成员所做的那样。
我知道:
> 您还可以在类上放置@Getter
和/或@Setter
注解。在这种情况下,就好像您使用注解注释了该类中的所有非静态字段一样。
我还知道:
> 我们可以使用@Getter @Setter
单独注释静态字段,以生成静态字段的Getter/Setter。
但是这看起来很不美观,我希望使我的类看起来尽可能干净。
有没有办法可以配置/覆盖@Getter
和@Setter
注解,以便我可以注释类并为所有成员生成Getter和Setter,包括静态和非静态成员,毕竟,这些方法所做的就是返回所提到的变量。
为了更加精确,我希望以下代码段能够为所有类变量生成Getter和Setter -
@Getter
@Setter
public class myClass {
private static String d;
private static SomePojo c;
private String a;
private Integer b;
private SomeClass d;
}
英文:
I have a class for all static members. The number of static members is more than 10 (which may increase with time).
I am using lombok and I want to generate Getters/Setters for all static members using single @Getter
and @Setter
annotations on class as we do for non-static members.
I know that
> You can also put a @Getter and/or @Setter annotation on a class. In
> that case, it's as if you annotate all the non-static fields in that
> class with the annotation.
I also know that
> We can annotate static fields individually using @Getter @Setter
to generate Getters/Setters for static fields.
But this looks ugly and I want to make my class look as clean as possible.
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 do those methods do is return the mentioned variable.
To be more precise, I want the following code snippet to generate Getters and Setters for all class variables-
@Getter
@Setter
public class myClass {
private static String d;
private static SomePojo c;
private String a;
private Integer b;
private SomeClass d;
}
答案1
得分: 8
将 @Getter
添加到静态成员本身,然后应该可以工作。
@Getter
private static final String DEFAULT_VAL = "TEST";
英文:
Add @Getter
to the static member itself and it should work.
@Getter
private static final String DEFAULT_VAL = "TEST";
答案2
得分: 0
对于静态字段,您需要在特定字段上添加@Getter
:
@Getter
@Setter
public class Task {
@Getter
private static int numberOfTasks;
@Getter
private static int taskId;
private String taskName;
private Integer executionTime;
}
英文:
For static fields you have to add @Getter
to the specific field:
@Getter
@Setter
public class Task {
@Getter
private static int numberOfTasks;
@Getter
private static int taskId;
private String taskName;
private Integer executionTime;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论