Generate calls to all setter methods of a bean
Hi,
is there a shortcut or the possibility to write a live template that generates method calls to all setter methods of a given Bean class? For instance, it is annoying if you have a Bean with 20 fields to type setName(..), setStreet(..) etc., so I am looking for a way to at least generate the skeleton of these calls. Of course I would have to specify any meaningful parameters myself.
Please sign in to leave a comment.
Press Alt + Enter. The generate menu will appear.
You can select to generate Getters, Setters, or Getters and Setters.
Michael, yes but I don't want generate setter methods but calls to setter methods, like:
someObject.setFirstname();
someObject.setLastname();
someObject.setStreet();
....
I don't want to generate the method itself (which is ALT+Insert for me btw.)
Reik
That generates the methods, not calls to them, which is what the OP clearly wants.
how would idea know which methods you want to call ?
have you tried type
myBean.set and then ctrl-space ? this should at least help
Well obviously IDEA know which fields to generate a setter method for in the first place (if you press Alt+Insert). It would be fine if it generated calls to all setter methods or had the user choose specific setters (fields). ctrl-space helps a bit but it is still a lot of typing if a bean contains like 20 fields/setters or so.
I guess I don't understand why you want it to generate calls to setters. From within the bean itself? Or from another class that is setting the values of the fields? How would IntelliJ know which fields you are wanting to populate?
From another class of course. Example, lets say you have two Bean objects. One internal, maybe an entity bean and one external, maybe a Form bean. You don't want to use a bean mapping framework like Dozer. So you write an Adapter class like this:
public static FormBean getExternal(EntityBean internal) {
FormBean external = new FormBean();
external.setWhatEver(internal.getWhatever()); // 19 lines to come ]]>
return external;
}
Well it could ask me from which Bean I want to generate setter calls from (when inside the method scope).
Apparently this conversation was completed by anything? It is very a pity.
I personally now searched for a way to generate calls of all setters for the object. It is convenient by operation with databases. When it is necessary in SqlRowMapper to create the object and to fill with its data through setters.
This is crazy that this has not been solved. It is really that simple. I have a bean with whatever number of attributes, say 20. I use the power of Intellij to generate my getters and my setters! That is all well and lovely.
In my client code, I create this bean and now I want to 'set' these attributes. Oh dear, I have 20 setters to reference. TWENTY! Maybe a keystroke combo at this point, while the cursor is on my bean, will display a dialog box (much like the one for 'Create Test') where we can select the setters we want to use. Select the setters we want, hit OK and BOOM, twenty lines of setters generated for our convenience. All we as developers will need to do now is remove any setters we do not need and add the references to each setter line.
JetBrains - make it so!
There is a plugin that does this: https://plugins.jetbrains.com/idea/plugin/9360-generateallsetter
Thank you Patrik! I'm sharing this with all the IntelliJ users at my company :)
Thank you Patrick for pointing out this plugin! It is very useful for unit tests!