CodeTalker
CodeTalker

Reputation: 1791

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

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;
    
}

Upvotes: 12

Views: 23405

Answers (2)

Ashraf Sarhan
Ashraf Sarhan

Reputation: 1697

Add @Getter to the static member itself.

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

Upvotes: 17

Mihai Vlasceanu
Mihai Vlasceanu

Reputation: 310

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;
}

Upvotes: 4

Related Questions