Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse formatter for getter/setter to single line?

Tags:

How can I tell (if ever) Eclipse to make a single line for a getter or setter when using auto formatting?

public User getUser() {
   return user;
}

to:

public User getUser() { return user; }
like image 366
membersound Avatar asked Apr 24 '12 18:04

membersound


People also ask

How do I format a line in Eclipse?

Go to Source | Format Document or press Ctrl+Shift+F.

Can getter setter be static?

You can't make getter and setter methods static if you use any attributes or properties that aren't static.


1 Answers

If you don't like all the boilerplate which Java forces you to write, you might be interested in Project Lombok as an alternative solution.

Instead of trying to format your code to minimize the visual impact of getters and setters, Project Lombok allows them to be added by the compiler behind the scenes, guided by annotations on your class's fields.

Instead of writing a class like this:

 public class GetterSetterExample {
   private int age = 10;
   private String name;

   public int getAge() {
     return age;
   }

   public void setAge(int age) {
     this.age = age;
   }

   protected void setName(String name) {
     this.name = name;
   }
 }

You would write:

 import lombok.AccessLevel;
 import lombok.Getter;
 import lombok.Setter;

 public class GetterSetterExample {
   @Getter @Setter private int age = 10;
   @Setter(AccessLevel.PROTECTED) private String name;
 }

(example from: http://projectlombok.org/features/GetterSetter.html)

like image 182
ulmangt Avatar answered Nov 02 '22 18:11

ulmangt