在 Eclipse 中生成 POJO 的所有 setXXX 调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5280512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:57:36  来源:igfitidea点击:

Generate all setXXX calls of a POJO in Eclipse?

eclipsejpa

提问by bertie

Im currently doing a lot of testing with JPA entities, where i have to keep calling the setter methods on the entity that looks something like this :

我目前对 JPA 实体进行了大量测试,在那里我必须继续调用实体上的 setter 方法,看起来像这样:

myEntity.setXXX(value);
myEntity.setYYY(value);
myEntity.setZZZ(value);

Is there any magic shortcut or menu in eclipse IDE to generate all the setter-method-calls that starts with "set", like those displayed in the ctrl-space (auto completion) popup (i think the inherited methods from Object are not being shown at popup) ?

eclipse IDE 中是否有任何神奇的快捷方式或菜单来生成所有以“set”开头的 setter-method-calls,就像在 ctrl-space(自动完成)弹出窗口中显示的那些(我认为从 Object 继承的方法不是显示在弹出窗口)?

So im imagining something like :

所以我想象着这样的事情:

  1. i type myEntity.set
  2. and myEntity.set* are generated right away
  1. 我输入 myEntity.set
  2. 和 myEntity.set* 立即生成

Im a lazy programmer and currently using Eclipse Helios IDE.

我是一个懒惰的程序员,目前正在使用 Eclipse Helios IDE。

Thank you !

谢谢 !



Edit

编辑

Im not looking for source -> generate getter and setter, because that would helps me in generating the methods itself. Generating the method callsis what i want to achieve.

我不是在寻找源代码 -> 生成 getter 和 setter,因为这会帮助我生成方法本身。生成method calls是我想要实现的。

回答by Oscar

I have found the answer (I was always searching for this thing)...

我找到了答案(我一直在寻找这个东西)......

The easiest way is to expand the class members in the "Package Explorer", sort them by name, multi-select all the setters, and then you have in the clipboard all the method names...

最简单的方法是在“包资源管理器”中展开类成员,按名称对它们进行排序,多选所有setter,然后在剪贴板中将所有方法名称...

;-)

;-)

回答by David I.

I like @Oscar's answer. It does lead to some cleanup though.

我喜欢@Oscar 的回答。不过,它确实会导致一些清理工作。

When I paste from the clipboard, I get something that looks like this:

当我从剪贴板粘贴时,我得到如下所示的内容:

setOne(int)  
setTwo(String)  

In order to clean this up, I first add semicolons with this search/replace regexp:

为了解决这个问题,我首先用这个搜索/替换正则表达式添加分号:

search = (.)$
replace = ;

Then I add the getter calls (assuming incoming data object is named "data"):

然后我添加 getter 调用(假设传入的数据对象名为“data”):

search = s(et.*)\(.*  
replace = s(data.g());  

This doesn't handle multiple arguments in a method call...

这不会处理方法调用中的多个参数......

回答by nick

you can use the outline at right side. There you can sort alphabetically or by declaration order using the toolbar button of the view.

您可以使用右侧的轮廓。在那里您可以使用视图的工具栏按钮按字母顺序或按声明顺序排序。

enter image description here

enter image description here

and then you can filter out non required this.

然后你可以过滤掉不需要的这个。

enter image description here

enter image description here

enter image description here

enter image description here

From here also you can copy..all setter functions or getters functions names...

从这里你也可以复制..所有的setter函数或getter函数名称......

回答by Mukesh Behere

There is eclipse plugin to do that. The name of the plugin is **

有 eclipse 插件可以做到这一点。插件的名字是**

FastCode

快码

**. There are so many templates. Among those there is template to generate code for create object of the class and all setters method.

**。有这么多模板。其中有模板来为类的创建对象和所有设置方法生成代码。

回答by Uriah Carpenter

Source --> Generate Getters and Setters...

Source --> Generate Getters and Setters...

You can also get at it via the Quick Fix command (Ctrl+1) when the cursor is on a property.

Ctrl+1当光标位于属性上时,您还可以通过快速修复命令 ( )获得它。



EDIT

编辑

If you are simply looking for a faster way to copy properties from one object to another I suggest that you look at using reflection. I think this path would be much easier long term then generating the same-looking code over-and-over.

如果您只是在寻找一种将属性从一个对象复制到另一个对象的更快方法,我建议您考虑使用反射。我认为从长远来看,这条路径会比一遍又一遍地生成外观相同的代码容易得多。

Commons BeanUtilscan take away some of the pain in writing pure reflection code. For example, copyPropertiestakes a destination bean and either another bean or a Map as the source.

Commons BeanUtils可以消除编写纯反射代码的一些痛苦。例如,copyProperties将目标 bean 和另一个 bean 或 Map 作为源。