Marcus Leon
Marcus Leon

Reputation: 56679

Eclipse getter/setter format

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.

Upvotes: 10

Views: 8766

Answers (6)

John Rees
John Rees

Reputation: 1813

As a variation of the regexp replacement approach, the following reformats the whitespace so that setters are followed by a blank line, but getters are not.

Find:

(\s(?:get|is|set)\w+\([^)]*\))\s*\{\s*(?:([^=;}]+;)\s*\}\s*(\R)|([^=;}]+=[^=;}]+;)\s*\}\s*(\R))

Replace by:

$1 { $2$4 } \R$5

Results in:

int getTotal() { return total; } 
void setTotal(int total) { this.total = total; } 

List<String> getList() { return list; } 
void setList(List<String> list) { this.list = list; } 

Map.Entry<String, Integer> getEntry() { return entry; } 
void setEntry(Map.Entry<String, Integer> entry) { this.entry = entry; } 

It's a minor aesthetic thing, but I figured that if you're looking for an answer to this question, then you're probably (almost) as anal as me ;-)

I know my regexp conditions are not as strict as those of @Hosam, but I haven't experienced any "false positive" replacements.

Upvotes: 2

Hosam Aly
Hosam Aly

Reputation: 42453

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|is)([\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.

Upvotes: 12

Levente Holl&#243;
Levente Holl&#243;

Reputation: 724

I think matching generics is important as well, so the correct regexp is:

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

Upvotes: 4

James
James

Reputation: 315

I wanted to post as a comment to the designated answer, but I don't seem to be able to.

I modified Hosam Aly's answer to work with generic and inner types of the form:

List<X>

and

Map.Entry

The revised regular expression search string is:

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

This regular expression allows for angle brackets and a dot in the type.

For example:

public List<String> getStringList()

and

public void setStringList(List<String> list)

and

public Map.Entry getEntry ()

And the replace string is the same as before:

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

Upvotes: 0

fastcodejava
fastcodejava

Reputation: 41097

You can use fast code plug-in to generate this kind of getter setters. The details are given here : http://fast-code.sourceforge.net/documentation.htm#create-new-field.

Create Variable

Upvotes: 1

Yuval Adam
Yuval Adam

Reputation: 165242

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

Upvotes: 1

Related Questions