Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 results in setters that look like:

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.

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

like image 366
Travis Dixon Avatar asked Aug 18 '09 03:08

Travis Dixon


1 Answers

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

like image 174
Rich Seller Avatar answered Oct 14 '22 17:10

Rich Seller