简体   繁体   中英

Overload lombok setter

We can use lombok to generate setter like this:

@Data                    //or @Setter
public class Test {  
    int a;
}

Say for instance I also want an overloaded setter that would take a String:

public void setA(String aStr){
    //parseInt and set 'a'
}

But when I add this overloaded method, lombok thinks that I have manually added a setter and so it chooses not to add one itself.

Apparently it looks only at the method name and not the parameters.

Is there a way I can force it to add the normal (that takes an int as parameter) setter?
Or the only way is to add that normal setter myself (using IDE setter generator of course)? I have a lot of fields and a lot of classes.

Adding the @Tolerate annotation on my overloaded method solved the issue.

Documentation :

Put on any method or constructor to make lombok pretend it doesn't exist, ie, to generate a method which would otherwise be skipped due to possible conflicts.

It has been experimental though, since 2014.

The documentation states that "No method is generated if any method already exists with the same name (case insensitive) and same parameter count." .

This is the case that you've described. Instead, you should define an additional custom setter method with a new name like

setAFromString(String aStr)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM