views:

812

answers:

2

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?

A: 

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=

zvikico
That's simple? :-P
Jonathan
I haven't tried it but that looks as though it would configure all generated code as opposed to just the setter and getter method signatures.Is it just a direct edit version of the other answer? Or does it do something different?
Travis Dixon
Yes, that's pretty much the case.
zvikico
+3  A: 

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

Rich Seller
That's a good answer, but it's basically saying "No, you can't modify the format of eclipse auto-generated getter and setter method signatures without modifying all other auto-generated method signatures" which is both disappointing and surprising given the depth of their template mechanism.Thanks for the detail on work around though, I'm sure they'll help someone coming after
Travis Dixon
Yes I'm afraid there isn't a means to do exactly what you want by default. But Eclipse allows you to prefix instance variables and use "this.", so it is a reasonable omission in my opinion. Your coding standards unfortunately fall into the gap. Note you can make parameters final, so you have a partial solution.
Rich Seller