Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse getter/setter format

Tags:

eclipse

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.

like image 776
Marcus Leon Avatar asked Jan 27 '09 20:01

Marcus Leon


People also ask

What is return type of getter and setter?

Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively.

What is getter and setter properties?

What are Getters and Setters? Getters: These are the methods used in Object-Oriented Programming (OOPS) which helps to access the private attributes from a class. Setters: These are the methods used in OOPS feature which helps to set the value to private attributes in a class.


1 Answers

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.

like image 103
Hosam Aly Avatar answered Sep 21 '22 18:09

Hosam Aly