How do I modify the set method signature that Eclipse auto generates?

后端 未结 4 1670
一整个雨季
一整个雨季 2021-02-05 11:22

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.

<
相关标签:
4条回答
  • 2021-02-05 12:09

    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.

    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.


    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.

    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.

    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


    Final parameters

    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

    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

    0 讨论(0)
  • 2021-02-05 12:12

    Very simple...

    1. In your project, under the root project folder, create a folder called .settings (it might already be there).
    2. In this folder, create a text file called org.eclipse.jdt.core.prefs
    3. In this file, include the following line:

    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=

    0 讨论(0)
  • 2021-02-05 12:13

    You can use Fast Code Eclipse Plugin to do this pretty easily.

    0 讨论(0)
  • 2021-02-05 12:20

    Eclipse --> Preferences --> Java --> Code Style --> Code Templates --> Code

    0 讨论(0)
提交回复
热议问题