[英]Eclipse: How to automatically generate getter when adding a field?
I am using TDD, and have a very typical coding pattern, using Eclipse to autocreate methods and fields as I code the unit test. 我正在使用TDD,并且有一个非常典型的编码模式,在我对单元测试进行编码时使用Eclipse自动创建方法和字段。 For example: 例如:
type name of method that doesn't exist, eg: 类型名称不存在的方法,例如:
myObj.setValue(someValue);
Click on the little red error mark in the IDE to create the "setValue" method. 单击IDE中的小红色错误标记以创建“setValue”方法。
Type inside of setValue method: 在setValue方法内输入:
public void setValue(String value) { this.value = value; }
Click the red error mark to auto-create a private field (called "value in this case"); 单击红色错误标记以自动创建私有字段(在本例中称为“值”);
So at this point, I would like Eclipse to auto-create the getter method, without having to do it using the source -> generate getters and setters menu. 所以在这一点上,我希望Eclipse自动创建getter方法,而不必使用source - > generate getters和setters菜单。
I saw this question: How do you force Eclipse to prompt you to create a getter and setter when it doesn't do it automatically which seems to imply that Eclipse does this, but I couldn't find where to configure that. 我看到了这个问题: 当你没有自动执行时,你如何强制Eclipse提示你创建一个getter和setter,这似乎意味着Eclipse 会这样做 ,但是我找不到配置它的位置。
Is there some way to configure Eclipse to automatically add setter/getters when adding new private variable? 是否有一些方法可以将Eclipse配置为在添加新的私有变量时自动添加setter / getter?
UPDATE : To clarify further, I'm looking for something like I saw in the Spring Roo documentation. 更新 :为了进一步澄清,我正在寻找像我在Spring Roo文档中看到的那样的东西。 If you take a look at the "How It Works" section, it describes how the framework automatically adds additional methods to your class, whenever you add a private variable. 如果您查看“工作原理”部分,它将描述每当您添加私有变量时框架如何自动向您的类添加其他方法。 (My preference is not to run another process like that, however, and also not to get all the cruft that it seems to add). (我的偏好不是为了运行这样的另一个过程,也不是为了得到它似乎添加的所有瑕疵)。 I was hoping Eclipse had something similar. 我希望Eclipse有类似的东西。
When you move your mouse above the name of the variable, you can select "Create getter and setter for varname" in the popup menu. 将鼠标移动到变量名称上方时,可以在弹出菜单中选择“为varname创建getter和setter”。
Or you can create yourself a shortcut to do so. 或者您可以创建自己的快捷方式。 Preferences -> General -> Keys. 首选项 - >常规 - >键。 In the search box you enter "getter" and then you´ll find it. 在搜索框中输入“getter”,然后您就可以找到它。
Try this, It works like butter 试试这个,就像黄油一样
Goto Source --> Generate Getter and Setter Methods
Either select one instance varible, or all ---> Ok
I do somewhat differently. 我的确有所不同。
private int threadsInPool = 3
). 首先创建变量:(比如private int threadsInPool = 3
)。 threadsInPool
) 将光标放在变量名称上(比如threadsInPool
) EDIT : You can also try using Lombok annotations: 编辑:您也可以尝试使用Lombok注释:
@Getter @Setter private String value;
Hmm... dunno if this is what you are looking for, but if I create a field in a class there is a warning that the field is unused. 嗯...不知道如果这是你正在寻找的,但如果我在一个类中创建一个字段,则会发出一个警告,表明该字段未被使用。 If I click on the warning sign the option to generate getter and setter for the field appears: 如果我单击警告标志,则会显示为该字段生成getter和setter的选项:
I don't think that question implies that, since all answers gave a manual solution. 我不认为这个问题意味着,因为所有答案都提供了手动解决方案。
AFAIK it's not possible to do automatically without a plugin, and I don't know of a plugin that does it. 如果没有插件,AFAIK就不可能自动执行,而且我不知道有一个插件可以做到这一点。
I use a template to create artifacts like that all at once, but I don't follow the same path--I'm not sure a template solution would work if a getter or setter already existed, since I'm not sure you can check for the presence of a method and make template decisions based on that. 我使用模板一次创建这样的工件,但我不遵循相同的路径 - 我不确定如果已经存在getter或setter,模板解决方案将起作用,因为我不确定你能不能检查是否存在方法并根据该方法做出模板决策。 Maybe you can. 也许你可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.