Reputation: 1354
Can I generate a setter for an object created in the main class
Does intelliJ IDE have a generator for setters to be y.set1() .. y.setn() instead of writing them down tens of times.
Upvotes: 2
Views: 2077
Reputation: 21
There is a plugin for that. GenerateAllSetter
To install the plugin, go to:
Usage:
Alt + Enter
. There will be an option to "generate all setter":
ExampleUpvotes: -1
Reputation: 1389
I was using the constructor trick, but found a better way (to me) :
Go to the "Structure" view for the class you'd like to generate the setters for, and sort members from a to z.
Then you can copy / paste setters
Now with a little bit of multi line editing (alt + left click) you 'll be able to add the missing parts of the line.
Hope that help.
Upvotes: 8
Reputation: 40388
No, not directly, however you may want to create a constructor which sets all fields (perhaps messy with dozens
of fields, but doable nonetheless...)
Generate
popup, hit Enter to select Constructor
, then ctrl-A for All, EnterThere is also the possibility to write an Action
in a small custom IntelliJ plugin to do this, if you think the effort it is worth the benefit...
Otherwise, if you are dealing with objects with many fields, the Builder
design pattern might be worth a look.
There is an IntelliJ refactoring for this!
Create your constructor using all fields (like above), and from the Refactor
menu (right click), choose Replace Constructor With Builder
.
Then look for a new class named XBuilder
and use it like so...
X y = new XBuilder.setA(1).setB(2).setC(3).createX();
Upvotes: 1