简体   繁体   English

重载龙目岛二传手

[英]Overload lombok setter

We can use lombok to generate setter like this:我们可以使用 lombok 来生成 setter,如下所示:

@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.但是当我添加这个重载的方法时,lombok 认为我已经手动添加了一个 setter,所以它选择不自己添加一个。

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?有没有一种方法可以强制它添加普通(以int作为参数)设置器?
Or the only way is to add that normal setter myself (using IDE setter generator of course)?或者唯一的方法是自己添加那个普通的设置器(当然使用 IDE 设置器生成器)? I have a lot of fields and a lot of classes.我有很多领域和很多课程。

Adding the @Tolerate annotation on my overloaded method solved the issue.在我的重载方法上添加@Tolerate注释解决了这个问题。

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.放置任何方法或构造函数以使 lombok 假装它不存在,即生成一个方法,否则该方法会由于可能的冲突而被跳过。

It has been experimental though, since 2014.不过,自 2014 年以来,它一直是实验性的。

The documentation states that "No method is generated if any method already exists with the same name (case insensitive) and same parameter count."该文档指出"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相反,您应该使用新名称定义一个额外的自定义 setter 方法,例如

setAFromString(String aStr) setAFromString(String aStr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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