Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut for calling all the setter methods on an object in Eclipse?

Tags:

I want to create an object of a class in and then set all the properties in it using setter methods. There are more than 150 setter methods and I do want to type each one of them or even type in the object instance name Object instance type in dot . and then hit the spacebar for Eclipse to give me suggestions and then go and select the setter method. I do not want to do that 150 times.

Thus, I was looking for some sort of shortcut in Eclipse that allows you to call all the setters on the method. So like you type in the instance name and Eclipse calls all the setter methods e.g.

  • instanceName.setterOne("valOne");
  • instanceName.setterTwo("valOne");
  • instanceName.setterThree("valOne");

I cannot create another constructor in the the class, I am not allowed to do so

like image 993
simranNarula Avatar asked Aug 03 '11 01:08

simranNarula


People also ask

How do I run getters and setters in Eclipse?

The shortcut key is Ctrl+Shift+D. In the dialog select getters and setters for which you want to generate Javadocs and click "OK" button. Javadocs are generated for the selected getters and setters.

How do I get caller method in Eclipse?

To open call hierarchy in Eclipse, select a method, click on Navigate on the main menu and select Open Call Hierarchy. Alternatively use a keyboard shortcut Ctrl+Alt+H.


2 Answers

From my experience last time , I cannot find eclipse has such feature .The most that I can do is open the Type Hierarchy View (by pressing F4 when viewing that class ), and then sort by the method name of that class and copy all the setters for further edit.

Or , you can use reflection to find out all the methods of this class , and print out the setter calls . Suppose this class is called Foo , you can have something like this:

for (Method m : Foo.class.getMethods()) {         if (m.getName().startsWith("set")) {             System.out.println(String.format("instanceName.%s(\"valOne\");", m.getName()));         } } 
like image 69
Ken Chan Avatar answered Sep 19 '22 13:09

Ken Chan


See this question: Generate all setXXX calls of a POJO in Eclipse?

It has a great and simple way of doing what you want to do.

Oh and try to ignore the haters!

like image 38
Grouchal Avatar answered Sep 19 '22 13:09

Grouchal