Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Generating calls to setters/getters
Generating calls to setters/getters [message #903474] Thu, 23 August 2012 18:47 Go to next message
David Jaquay is currently offline David JaquayFriend
Messages: 5
Registered: August 2012
Junior Member
Is there a way to automatically generate a series of calls to all of the setters or getters on an object in code? For instance, if:

class Foo {
  void setOne(String one) {}
  void setTwo(String two) {}
  void setThree(String three) {}
}


and I'm in a method in class Bar, I'd like to type:

Foo foo = new Foo();

foo.


press Ctrl-Space, and have code assist not only give me the option to add any single setter, but give me the option to add all setters for Foo, so that with one popup or menu click, I'd get:

Foo foo = new Foo();

foo.setOne(one);
foo.setTwo(two);
foo.setThree(three);


rather than need to hit Ctrl-Space three times, once for each setter line.

Any way to do this?

Dave
Re: Generating calls to setters/getters [message #903743 is a reply to message #903474] Sat, 25 August 2012 11:13 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1048
Registered: July 2009
Senior Member
I don't see why this would be a typical scenario.
Please see that code assist can only support the most common scenarios,
otherwise JDT would overwhelm you with proposals what could possibly
be your intention, but the list would be way to long to find the proposal
you are interested in.

Generally, JDT's code assist only proposes small steps.
Larger changes would be invoked as refactoring or via Source > Generate XYZ.
I don't see your proposal fitting into either category.

The Code Recommenders project adds more ambitious recommendations strategies,
so, if any, they might be interested in your proposal, but still you
would have to argue why your usecase is a common situation and why
automating this will save lots of time.

One more option to consider: maybe this is not about adding statements to
an existing method, but about a template proposal to create a
method populated with a sequence of method calls?

Finally, if Foo is your class, why not provide a constructor that includes
setting all those fields, so the setter calls aren't even needed.

HTH,
Stephan
Re: Generating calls to setters/getters [message #903774 is a reply to message #903743] Sat, 25 August 2012 19:27 Go to previous messageGo to next message
David Jaquay is currently offline David JaquayFriend
Messages: 5
Registered: August 2012
Junior Member
Ah, yes, code assist probably isn't the right place for such a thing. Having something as a Generate makes a little more sense, perhaps.

My situation is common (for me) as we're writing a database app, and have JPA classes that we want to use to create rows in the database, for each of many tables. So, for each JPA class, somewhere in the app (and sometimes in multiple places), we'll call many/most of the setters on a given class. I've spent a lot of time of late hitting "foo." and picking the "next" setter from the list, and have thought that it'd be nice to get them all at once, and remove the few that I might not need.

I will read up more on templates, as that could very well be what I'm after, especially as it seems one can write new ones. And yes, better constructors is a good idea, although to some extent, this kinda kicks the whale down the beach, because then I'll be asking for a way to generate a ctor with all the setters called Smile.

Thanks much,
Dave
Re: Generating calls to setters/getters [message #903776 is a reply to message #903774] Sat, 25 August 2012 19:38 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1048
Registered: July 2009
Senior Member
David Jaquay wrote on Sat, 25 August 2012 21:27
And yes, better constructors is a good idea, although to some extent, this kinda kicks the whale down the beach, because then I'll be asking for a way to generate a ctor with all the setters called Smile.


Perhaps we already have that, what'you think of Source > Generate Constructor using Fields...?

Smile
Stephan

Re: Generating calls to setters/getters [message #903779 is a reply to message #903776] Sat, 25 August 2012 20:41 Go to previous message
David Jaquay is currently offline David JaquayFriend
Messages: 5
Registered: August 2012
Junior Member
And that's what I want! Thanks very much.

What a good problem to have: an IDE that's got so much good that you can't keep track of it all Smile.

Dave
Previous Topic:Juno next hotkey broken
Next Topic:JAR file behaves differently than in Eclipse
Goto Forum:
  


Current Time: Sat Dec 20 05:30:51 GMT 2014

Powered by FUDForum. Page generated in 0.07557 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software