Generating calls to setters/getters [message #903474] |
Thu, 23 August 2012 14:47  |
Eclipse User |
|
|
|
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 07:13   |
Eclipse User |
|
|
|
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 15:27   |
Eclipse User |
|
|
|
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 .
Thanks much,
Dave
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05821 seconds