如何修改 Eclipse 自动生成的 set 方法签名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1291625/
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
How do I modify the set method signature that Eclipse auto generates?
提问by Travis Dixon
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.
我当前的项目有这样的编码约定,即永远不会用 this 引用实例变量。前缀并且该参数永远不应该隐藏实例变量。
This results in setters that look like:
这会导致如下所示的 setter:
public void setFoo(final Foo aFoo)
{
foo = aFoo;
}
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.
不幸的是,eclipse 默认不会为我生成那个。我发现在代码风格中,我可以通过向参数前缀列表添加 a 来接近它,但是我只希望它适用于设置方法,并且我也想在那里添加最终标签。
Is there a way to achieve this using templates? Some other configuration?
有没有办法使用模板来实现这一点?还有什么配置?
回答by Rich Seller
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 questionfor some hints on creating a template.
我认为目前仅对 setter 方法应用参数前缀的唯一方法是为 setter 方法编写一个新模板,但访问器生成器不会使用此模板。您可以在Window-> Preferences-> Java-> Editor-> Templates下看到现有模板的列表,请参阅此问题以获取有关创建模板的一些提示。
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.
您可以修改 Eclipse 设置以在工作区或项目级别为所有类型的变量指定前缀(和后缀),但这将适用于所有方法,而不仅仅是 setter。您可以使用“清理”功能来确保您的参数是最终的。
To appease your code convention, you couldspecify 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.
为了满足您的代码约定,您可以指定所有实例变量都带有前缀,这样您的参数就不会覆盖实例变量,但您可能不想这样做。
Variable prefixes
变量前缀
To modify the workspace settings, go to Window->Preferences->Java->Code Style, and then edit the list to use your preferred prefixes/suffixes.
要修改工作区设置,请转至Window-> Preferences-> Java-> Code Style,然后编辑列表以使用首选前缀/后缀。
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.
要修改项目设置,请打开项目属性 ( Alt+ Enter),然后选择Java Code Style,选择Enable project specific settings,然后编辑工作区的首选项。
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
要仅为 setter 方法启用特定前缀,您必须深入研究代码模板的内部以识别和修改 setter
Final parameters
最终参数
To ensure all method parameters are final, you can modify the Java clean up processor to add finalto parameters. Under Window->Preferences->Java->Code Style->Clean Up, you can copy or edit the Active Profile. Under the Code Styletab, select Use modifier 'final' where possiblein the Variable declarationssection, then ensure Parameteris selected. Clean Up will be applied when you run Source->Clean Up
为确保所有方法参数都是 final,您可以修改 Java 清理处理器以将final添加到参数中。在Window-> Preferences-> Java-> Code Style-> Clean Up 下,您可以复制或编辑 Active Profile。在“代码样式”选项卡下,在“变量声明”部分中尽可能选择使用修饰符“最终”,然后确保选择“参数”。运行Source-> Clean Up时将应用清理
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 savebox is selected (this will also format your code and organise imports if you wish), select the Additional Actionsoption, and the Configure, then under Code Style, apply the same as above
要在每次保存时自动应用最终参数,您可以修改保存操作,在Window->**Preferences-> Java-> Editor-> Save Actions 下,确保选中 Perform the selected actions on savebox(这也将格式化您的代码并组织导入(如果您愿意),选择Additional Actions选项和Configure,然后在Code Style 下,应用与上述相同的内容
回答by zvikico
Very simple...
很简单...
- In your project, under the root project folder, create a folder called
.settings
(it might already be there). - In this folder, create a text file called
org.eclipse.jdt.core.prefs
- In this file, include the following line:
- 在您的项目中,在根项目文件夹下,创建一个名为
.settings
(它可能已经存在)的文件夹。 - 在此文件夹中,创建一个名为的文本文件
org.eclipse.jdt.core.prefs
- 在此文件中,包含以下行:
org.eclipse.jdt.core.codeComplete.argumentPrefixes=a
org.eclipse.jdt.core.codeComplete.argumentPrefixes=a
That's it, your done. It should just work. I couldn't find proper documentation for this, but here are some other options you might set:
就是这样,你完成了。它应该可以正常工作。我找不到合适的文档,但这里有一些您可以设置的其他选项:
org.eclipse.jdt.core.codeComplete.argumentPrefixes=
org.eclipse.jdt.core.codeComplete.argumentSuffixes=
org.eclipse.jdt.core.codeComplete.fieldPrefixes=
org.eclipse.jdt.core.codeComplete.fieldSuffixes=
org.eclipse.jdt.core.codeComplete.localPrefixes=
org.eclipse.jdt.core.codeComplete.localSuffixes=
org.eclipse.jdt.core.codeComplete.staticFieldPrefixes=
org.eclipse.jdt.core.codeComplete.staticFieldSuffixes=
org.eclipse.jdt.core.codeComplete.argumentPrefixes=
org.eclipse.jdt.core.codeComplete.argumentSuffixes=
org.eclipse.jdt.core.codeComplete.fieldPrefixes=
org.eclipse.jdt.core.codeComplete.fieldSuffixes=
org.eclipse.jdt.core.codeComplete.localPrefixes=
org.eclipse.jdt.core.codeComplete.localSuffixes=
org.eclipse.jdt.core.codeComplete.staticFieldPrefixes=
org.eclipse.jdt.core.codeComplete.staticFieldSuffixes=
回答by fastcodejava
You can use Fast Code Eclipse Pluginto do this pretty easily.
您可以使用Fast Code Eclipse Plugin轻松完成此操作。
回答by Rohan
Eclipse --> Preferences --> Java --> Code Style --> Code Templates --> Code
Eclipse --> 首选项 --> Java --> 代码样式 --> 代码模板 --> 代码