tags:

views:

1198

answers:

5

Does anyone know of an Eclipse plug-in or method to get Eclipse to generate getter/setters on one line like this:

public String getAbc() { return abc; }

Instead of

public String getAbc() {
   return abc;
}

I'm on Eclipse v. 3.2.2.

Thanks.

+3  A: 

I don't know how to make Eclipse generate them in the format you want, but you could do a search/replace using these regular expressions after the methods are generated:

Find:

(?m)((?:public |private |protected )?[\w$]+) (get|set)([\w$]+)\(([\w$]+ [\w$]+)?\) \{\s+(return [\w$]+;|this.[\w$]+ = [\w$]+;)\s+\}

Replace by:

$1 $2$3($4) { $5 }

This expression will transform the generated getters and setters to be one line. Don't worry about running it with a mixture of transformed and newly generated methods; it will work just fine.

Hosam Aly
Thanks Hosam. I'd ideally prefer a way to have Eclipse just generate the getters on a single line without having to perform a second step.
Marcus
You're welcome. Good luck finding one.
Hosam Aly
I would say that your RegEx does work well. One thing - seems like in my version/config of Eclipse, the "replace by" expression needs to use dollar signs instead of the back slash: $1 $2$3($4) { $5 }
Marcus
Yes @Marcus. I'm using Eclipse 3.4.1, but it appears that previous versions required the use of '$' instead of '\'. The latest version supports both. I'll edit my answer. Thanks for the information.
Hosam Aly
If you find a way to give a shortcut to this search/replace operation, I would be thankful if you could share it. Thank you.
Hosam Aly
This is the best solution I've found. Thanks Hosam.
Marcus
+1  A: 

Goto: Preferences > Java > Code Style > Code Templates / Formatter..
If it's at all possible to change this setting you'll find it there.

Tim
All you can change is the body format. The line breaks and braces location are global and not settable only on getters/setters.
Instantsoup
+1  A: 

Java code formatting in Eclipse does not differentiate between getters/setters and any other methods in a class. So this cannot be done by built-in eclipse formatting.

You will need either to:

  1. run a search/replace with the aforementioned regex
  2. get en external plugin like PMD or CheckStyle and enforce a regex rule based on previous option
Yuval A
A: 

I'd like to have this also .