0

Is it possible to change how lombok create generic methods?

For example, if I want for setters to have something like:

private String field1;

To become

public void setField1(String field1) {
// do something
this.field1 = field1;
}

public Object getField1() {
// do something
return this.field1;
}

Default is without //do something

2
  • Isn't the point of Lombok that you don't wanna do that? That it just generates at compile time plain standard getters and setters? I think you could add custom methods if you want, e.g. getCustomField1 Commented Dec 10, 2019 at 16:39
  • I think that the point of Lombok is to replace typewriting for you. It has default behaviour which gives you what is most in common. But it would be good that you can change that behaviour on standard way for ever suites you. Like you have some already some default annotations but it's good you can create your own annotation. And yes, I can do that, but I have to do it for each property I have and I want to do it at one place. Only once. And to be for each property in that class. Like it is that standard behaviour. Commented Dec 10, 2019 at 17:34

1 Answer 1

2

You cannot; this is intentional. What you can do is just.. write the method. Lombok will see it and will not generate anything. For example, if you have this in your file:

@Value public class Example {
    String name;
    int count;

    public int getCount() {
        return Math.abs(count);
    }
}

lombok would generate a getName method (with return this.name; as implementation), but wouldn't generate a getCount method; it is already there.

There wouldn't be a feasible syntax to somehow get lombok to generate the public String getField1() bit, and the return this.field1; bit, but let you write some code to go in between those two.

NB: I'm a lead dev on project lombok.

Sign up to request clarification or add additional context in comments.

3 Comments

This was my idea. For example I want to be aware when ever I change some property. I want to change generation. Now it is for settings like : public void setFieldName(Object object) { this.object = object; } to something like: public boolean setFieldName(Object object) { boolean changed = !Objects.equals(this.object,object); this.object = object; return changed; } So, it will be useful if we can change somehow default behaviour to behaviour we want, like in this my example.
So, will be useful if we can change signature and body of methods generating by lombok.
Never going to happen; it wouldn't be boilerplate anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.