Is there a way to modify the @Setter
annotation to add some custom logic to all set methods built via lombok.
I have a class with the fields initialized with some default values. Now I only want to set the value in the fields if the value is not null.
Example, something that generates-
public void setFoo(int foo) {
if (foo != null) {
this.foo = foo;
}
}
For example, if I am using the @Setter
annotation on a Jersey Update Request class, instead of doing things like-
if (request.getFoo() != null) {
this.foo = request.getFoo();
}
I should be able to directly do-
this.setFoo(request.getFoo());
with some results.
I'm adding this answer since Sumeet's is outdated as of 2019.
You can provide your own getter or setter logic by simply writing your own methods.
package com.example;
import lombok.Getter;
@Getter
public class Foo {
//Will not generate a getter for 'test'
private String test;
//Will generate a getter for 'num' because we haven't provided our own
private int num;
public String getTest(){
if(test == null){
throw new Exception("Don't mind me, I'm just a silly exception");
}
return test;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With