在 Eclipse 中调用对象上所有 setter 方法的快捷方式?
我想创建一个类的对象,然后使用 setter 方法设置其中的所有属性。有超过 150 个 setter 方法,我确实想键入其中的每一个,甚至键入对象实例名称 对象实例
在点 .
中键入,然后按空格键让 Eclipse 给我建议,然后去选择 setter 方法。我不想这样做 150 次。
因此,我在 Eclipse 中寻找某种快捷方式,允许您调用方法上的所有 setter。就像您输入实例名称一样,Eclipse 会调用所有setter 方法,例如
- instanceName.setterOne("valOne");
- instanceName.setterTwo("valOne");
- instanceName.setterThree("valOne");
我无法在类中创建另一个构造函数,我不被允许这样做
I want to create an object of a class in and then set all the properties in it using setter methods. There are more than 150 setter methods and I do want to type each one of them or even type in the object instance name Object instance
type in dot .
and then hit the spacebar for Eclipse to give me suggestions and then go and select the setter method. I do not want to do that 150 times.
Thus, I was looking for some sort of shortcut in Eclipse that allows you to call all the setters on the method. So like you type in the instance name and Eclipse calls all the setter methods e.g.
- instanceName.setterOne("valOne");
- instanceName.setterTwo("valOne");
- instanceName.setterThree("valOne");
I cannot create another constructor in the the class, I am not allowed to do so
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
根据我上次的经验,我找不到 Eclipse 有这样的功能。我能做的最多就是打开
Type Hierarchy
视图(通过在查看该类时按 F4 ),然后按该类的方法名称排序并复制所有设置器以供进一步编辑。或者,您可以使用反射来找出该类的所有方法,并打印出 setter 调用。假设这个类名为
Foo
,你可以有这样的东西:From my experience last time , I cannot find eclipse has such feature .The most that I can do is open the
Type Hierarchy
View (by pressing F4 when viewing that class ), and then sort by the method name of that class and copy all the setters for further edit.Or , you can use reflection to find out all the methods of this class , and print out the setter calls . Suppose this class is called
Foo
, you can have something like this:看到这个问题: 在 Eclipse 中生成 POJO 的所有 setXXX 调用?
它有一个伟大而简单的方法来完成你想做的事情。
哦,试着忽略那些仇恨者!
See this question: Generate all setXXX calls of a POJO in Eclipse?
It has a great and simple way of doing what you want to do.
Oh and try to ignore the haters!
如果您使用 IntelliJ,请查看此插件:
https://plugins.jetbrains.com/插件/9360-generateallsetter
If you're using IntelliJ, check out this plugin:
https://plugins.jetbrains.com/plugin/9360-generateallsetter
我不知道任何 Eclipse 功能可以帮助您。
您可以转而使用 emacs 键盘宏或 Perl 脚本等笨重的东西来处理 Eclipse 之外的源代码。
您的另一个选择(尽管可能不会有成效......)是请求修改繁琐的 API;例如支持“构建者”模式。
I'm not aware of any Eclipse feature that will help you here.
You could fall back to using something clunky like emacs keyboard macros or perl scripting to massage the source code outside of Eclipse.
Your other option (though it probably won't be productive ...) is to request that the cumbersome API be revised; e.g. to support the "builder" pattern.
鉴于您已经遇到这种情况,我会认真考虑重新设计您的课程。在不太了解这个课程或您的目标的情况下,我敢说几乎没有理由拥有 150 个单独的字段,特别是如果它们属于同一类型。想一想——如果你已经陷入这样的困境,以后维护这段代码谈何容易?
如果字段都属于同一类型,请考虑使用数组、
List
或Map
(属性->值)。如果它们有许多不同的类型,请考虑如何将它们分解为更容易管理的组件类。I would seriously consider redesigning your class, given that you have reached this situation. Without knowing much about this class or your goals, I would venture that there is almost no reason to have 150 individual fields, especially if they are of the same type. Think about it - if you are already in this predicament, how easy will it be to maintain this code in the future?
If the fields are all of the same type, consider using an array, a
List
or aMap
(property->value). If they are many diverse types, think about how you might break them up into component classes that could be more easily managed.我有 Eclipse 4.6.0 - 它有一个自动生成 getter 和 setter 的选项(不确定早期版本的 Eclipse)。
(a) 选择您的班级(然后右键单击)
(b) 选择源(Shift+Alt+S)
(c) 选择 Getter 和 Setter
生成的弹出窗口(生成 getter 和 setter)允许您选择全部或单个字段。
希望这会有所帮助(如果仍然相关)。
I have Eclipse 4.6.0 - which has an option to auto-generate getters and setters (not sure about earlier versions of Eclipse).
(a) Select your class (and right click)
(b) Select Source (Shift+Alt+S)
(c) Select Getters and Setters
The resultant pop-up window (Generate getters & setters) allows you to select ALL or Individual fields.
Hope this helps (if still relevant).
以下是我使用的一些技巧 -
Ctrl+Shift+A - > eclipse块选择(这就是神奇的工具01)
Ctrl + F -> eclipse 查找/查找和替换(神奇工具 02)
Ctrl +Shift + X ->改为大写(工具 03)
这是我的做法
阻止选择 ->选择所有字段名称(工具 01)
例如:用户
选择所有首字母并将首字母设为大写(工具 01/03)
例如:用户
全选并输入单词集(工具01)
例如:setUser
将所有字段对齐。因此,您可以直接使用块选择器
全选并设置“();”
例如:setUser();
现在你已经准备好了所有的setter。
当你想在括号内插入一个值时,你可以使用魔法工具02。查找和替换(稍微想一下,你会看到一个选项)
最后,节省你从getter/setter设置中无聊的时间。
干杯!
Here are some tips I used -
Ctrl+Shift+A - > eclipse block selection (this is the magic tool 01)
Ctrl + F -> eclipse find/ find and replace ( magic tool 02)
Ctrl +Shift + X -> to upper case (tool 03)
Here how I did
block selection -> select all field names (tool 01)
Ex : user
select all first letters and make the first letter uppercase (tool 01/03)
Ex : User
select all and put the word set (tool 01)
Ex: setUser
Align all fields nicely in line. So you can use the block selector straight
select all and set "();"
Ex: setUser();
now you have all the setters ready
When you want to insert a value inside parentheses you can use magic tool 02. Find and replace ( think a little and you will see an option )
Finally, save your time of boredom from getters/setters settings.
cheers !
我基于 @Ken Chan anwser 创建了自己的更通用的解决方案来解决这个问题,它不仅设置了基本类型。它远非完美,但对某些人来说,它可能是一个起点。
代码位于此处
I created my own more general solution to this problem based on @Ken Chan anwser, which set not only basic types. It is far from perfect, still to some it might be some starting point.
Code is here