Eclipse 生成 setter
在 Eclipse 中,是否有任何方法可以给定一个具有定义的 setter 列表的类,以便您可以在填充它们之前将它们列出来?
例如:
public class Test {
private String valueA;
private String valueB;
private String valueC;
private String valueD;
public void setValueA(String val) {
this.valueA = val;
}
public void setValueB(String val) {
this.valueB = val;
}
public void setValueC(String val) {
this.valueC = val;
}
public void setValueD(String val) {
this.valueD = val;
}
}
如果有一个输出模板/快捷方式会非常方便:
test.setValueA(value);
test.setValueB(value);
test.setValueC(value);
test.setValueD(value);
显然,4 个字段没有该值,但是当您有 100 个字段时,那就太好了(想想 JAXB Bean 用于一段令人讨厌的 XML)。
注意:我不是在询问来源 ->生成 Getters/Setters
菜单。
谢谢。
In Eclipse is there any way that given a class with a list of defined setters, that you can list these out before populating them?
For example:
public class Test {
private String valueA;
private String valueB;
private String valueC;
private String valueD;
public void setValueA(String val) {
this.valueA = val;
}
public void setValueB(String val) {
this.valueB = val;
}
public void setValueC(String val) {
this.valueC = val;
}
public void setValueD(String val) {
this.valueD = val;
}
}
It would be very handy to have a template/shortcut to output:
test.setValueA(value);
test.setValueB(value);
test.setValueC(value);
test.setValueD(value);
Obviously the value isn't there for 4 fields but when you have 100 it would be nice (think JAXB Bean for a nasty piece of XML).
Note: I'm not asking about the Source -> Generate Getters / Setters
menu.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为 cat、grep 和 sed 的非常简单的组合可能对你有用。
这是我花了半分钟写的一个例子:
我在名为 Device 的类上运行了它。
这是产生的输出:
正如您所看到的,它适用于所有基元,但最后一个设置器需要一些修改。这是因为 sed 命令不支持全套正则表达式运算符。欢迎您改用 perl 或 awk 用户。在这种情况下你可以简单地说
s/\(\S+ //
,即删除左括号后面的所有非空白字符以及后面的空格。我认为编写 Eclipse 插件是一个完美但太昂贵的解决方案。
我使用这里是 Unix shell 命令。如果你在 Windows 上开发运气不好(像我一样),我建议你使用 cygwin(就像我一样)。
I think that pretty simple combination of cat, grep and sed may do work for you.
Here is an example I wrote during half a minute:
I ran it on my class named Device.
Here is the produced output:
As you can see it worked for all primitives but the last setter requires some modifications. It is because sed command does not support full set of regular expression operators. You are welcome to user perl or awk instead. In this case you can say simply
s/\(\S+ //
, i.e. remove all non-whitespace characters that are following the left bracket and space after that.I think that writing Eclipse plugin is a perfect but too expensive solution.
I used here Unix shell commands. If you have a bad luck :( to develop on Windows (like me) I'd recommend you to use cygwin (as I do).
这在 Eclipse 中不能直接实现,但当然可以在插件中完成。
例如,插件 Fluent-builder 您可能感兴趣:
该插件将允许您使用以下向导为每个设置器生成此类测试代码:
This is not directly possible in Eclipse, but certainly can be done in a plugin.
For instance, the plugin fluent-builder might interest you:
The plugin will allow you to generate that kind of test code for each setter with the following wizzard:
我不知道有什么专门做这件事的。然而,类大纲视图相当接近,特别是当您调整过滤器以排除您不感兴趣的内容(例如字段、静态成员、嵌套类)时。
I'm not aware of anything that specifically does this. However, the class outline view comes fairly close, especially if you adjust the filters to exclude things (e.g. fields, static members, nested classes) that you are not interested in.