如何修改Eclipse自动生成的set方法签名? [英] How do I modify the set method signature that Eclipse auto generates?

查看:158
本文介绍了如何修改Eclipse自动生成的set方法签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的当前项目具有编码惯例,实例变量永远不会被引用。前缀,该参数不应该隐藏实例变量。

My current project has the coding convention that instance variables are never referred to with the this. prefix and that parameters should never hide instance variables.

这导致setter看起来像:

This results in setters that look like:

public void setFoo(final Foo aFoo)
{
  foo = aFoo;
}

不幸的是,eclipse默认情况下不会为我生成。
我发现代码样式我可以通过添加到参数前缀列表来接近它,但是我只希望它适用于设置方法,我也想添加最后的标签。

Unfortunately eclipse won't generate that for me by default. I've found in code style I can get close to it by adding a to the parameter prefix list, however I only want it to apply to set methods and I'd like to add the final tag there as well.

有没有办法使用模板来实现?一些其他配置?

Is there a way to achieve this using templates? Some other configuration?

推荐答案

我认为目前,仅为setter方法应用参数前缀的唯一方法是写一个新的模板setter方法,但这个模板不会被访问者生成器使用。您可以在窗口 - > 首选项 - > Java - > 编辑器 - > 模板,有关创建模板的一些提示,请参阅问题

I think currently the only way to apply parameter prefixes for setter methods only is to write a new template for setter methods, but this template wouldn't be used by the accessor generator. You can see a list of existing templates under Window->Preferences->Java->Editor->Templates, see this question for some hints on creating a template.

您可以修改Eclipse设置,以便在工作区或项目级别为所有类型的变量指定前缀(和后缀),但这将适用于所有方法,不只是设置者。您可以使用清理功能来确保您的参数是最终的。

You can modify the Eclipse settings to specify prefixes (and suffixes) for all types of variables either at the workspace or project level, this will apply to all methods though, not just setters. You can use the "Clean Up" feature to ensure your parameters are final.

为了安抚您的代码约定,您可以指定所有实例变量的前缀代替,这样你的参数不会覆盖实例变量,你可能不想这样做。

To appease your code convention, you could specify that all instance variables are prefixed instead, that way your parameters will not override the instance variables, you may not want to do that though.

可变前缀

要修改工作区设置,请转到窗口 - > 首选项 - > Java - > 代码样式,然后编辑列表以使用您的首选前缀/后缀。

To modify the workspace settings, go to Window->Preferences->Java->Code Style, and then edit the list to use your preferred prefixes/suffixes.

要修改项目设置,请打开项目属性( Alt + 输入),然后选择 Java代码样式,选择启用项目具体设置,然后根据工作空间编辑首选项。

To modify the project settings, open the project properties (Alt + Enter), then select Java Code Style, select Enable project specific settings, then edit the preferences as for the workspace.

要为setter方法启用特定前缀,您必须深入研究内部的代码模板确定和修改设置者

To enable a particular prefix only for setter methods, you'd have to delve into the internals of the code templates to identify and modify the setter

最终参数

为了确保所有方法参数都是最终的,您可以修改Java清理处理器以将 final 添加到参数中。在窗口 - > 首选项 - > Java - > 代码样式 - > 清理,您可以复制或编辑活动配置文件。在代码样式标签下,在变量声明部分中选择使用修饰符'final',尽可能,然后确保参数被选中。源自 - > 清理

To ensure all method parameters are final, you can modify the Java clean up processor to add final to parameters. Under Window->Preferences->Java->Code Style->Clean Up, you can copy or edit the Active Profile. Under the Code Style tab, select Use modifier 'final' where possible in the Variable declarations section, then ensure Parameter is selected. Clean Up will be applied when you run Source->Clean Up

要使最终参数自动应用于每个保存,您可以修改保存操作,在窗口 - > **首选项 - > Java - > 编辑器 - > 保存操作,确保选择保存框执行所选操作(如果需要,还将格式化代码并整理导入),选择其他操作选项,和配置,然后在代码样式下,应用与上述相同

To have final parameters applied automatically on each save, you can modify the save actions, under Window->**Preferences->Java->Editor->Save Actions, ensure Perform the selected actions on save box is selected (this will also format your code and organise imports if you wish), select the Additional Actions option, and the Configure, then under Code Style, apply the same as above

这篇关于如何修改Eclipse自动生成的set方法签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆