Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

lombok

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 annotation on class like 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 those methods do is to return the mentioned variable.

To be more precise, i want 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;

}
like image 207
CodeTalker Avatar asked Apr 03 '20 21:04

CodeTalker


People also ask

Can we create getter and setter for static variables?

Yes, getters/setters can be made static based on your need.

How do you set the setter and getter with Lombok?

Overview. You can annotate any field with @Getter and/or @Setter , to let lombok generate the default getter/setter automatically. A default getter simply returns the field, and is named getFoo if the field is called foo (or isFoo if the field's type is boolean ).

What is @with in Lombok?

The @With relies on a constructor for all fields in order to do its work. If this constructor does not exist, your @With annotation will result in a compile time error message. You can use Lombok's own @AllArgsConstructor , or as Value will automatically produce an all args constructor as well, you can use that too.


1 Answers

Add @Getter to the static member itself and it should work.

@Getter
private static final String DEFAULT_VAL = "TEST"; 
like image 186
Ashraf Sarhan Avatar answered Sep 25 '22 17:09

Ashraf Sarhan