Reputation: 629
I did try searching for this, but honestly the terms to actually search are escaping me. A small code snippet followed by the question.
Public class Person {
String firstName;
String lastName;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
...
}
In another object that uses this person object I would like to be able to expand all the setters methods in Intellij.
public class PersonAssembler {
public static Person assemblePerson(SomeOtherObject someOtherObject) {
Person person = new Person;
//intellj would provide this below.
person.setFirstName();
person.setLastName();
//end of what intellj provides.
return person;
}
Is this even possible? I have looked through the keyboard short cuts, etc. I have been using intellij for a while, so just wondered if anyone had some insight on this.
I was hoping for column edit mode, then code completion and perhaps a shift+down arrow to select multiple completions, but no luck. Which is not surprising. I would hate to have to write that in the GUI.
Edit: I added some clarification and honestly forgot about this question. The answers have nothing to do with the actual question if read carefully. I am not speaking of the Generate context menu.
Upvotes: 57
Views: 100742
Reputation: 189
There is a plugin that does this: https://plugins.jetbrains.com/idea/plugin/9360-generateallsetter
Go to File > Settings > Plugins and search for a plugin GenerateAllSetter (See the link above) and install it
NOTE: It does not need the IDE to restart.
Once the plugin is installed, create an instance of your object/dto. Place your cursor on the initialization line of code and IntelliJ IDEA show the yellow suggestions light-bulb. Click the light bulb(or use Alt+ Enter) and suggestions will appear. Here you can choose to generate setter with or without default value. See images.
Below are images of the resulting code genarated (both with and without defaults)
Setters without default values
Setters with deafult values
Upvotes: 1
Reputation: 31
In mac you can insert the getter by pressing ctrl + return -> selecting getters, setters or both -> selecting the variable you want it and press return
Upvotes: 1
Reputation: 1088
Shortcut for Windows Alt + Insert
Shortcut for Mac Command + N
then choose getter and setter
Upvotes: 3
Reputation: 1143
In your project, right-click anywhere on the typing screen, and click 'Generate...' and then 'Getter and Setter'. Then, hold down CTRL and click on the fields you wish to create getters and setters for, then click on 'OK'.
Upvotes: 51
Reputation: 10813
This plugin may help GenerateAllSetter
Press CTRL + ENTER where you want to use and it will generate setter calls for you.
Upvotes: 9
Reputation: 16628
Continuation to Michael Jarvis
answer:
So I am in IntelliJ IDEA 2018.1.4 (Community Edition)
and I realized it is important to select all the fields first in order for right-click
or Alt + Insert` to have the menu Property (getter and setter)
Upvotes: 1
Reputation: 1
I wrote a Jar, it can quickly and automatically generate Java dto set method calls, including import and new statements.
You can try this: https://github.com/Adrninistrator/GenSetterCalls
Add dependeny: "com.github.adrninistrator:GenSetterCalls:0.0.1"
Upvotes: 0
Reputation: 51
here is my plugin genSets
Foo foo = new Foo();
code like this
foo.allSet
will generate
foo.setName();
foo.setAge();
foo.setBar();
foo.setTest();
foo.setLike();
Upvotes: 5
Reputation: 714
This is an old question, but maybe it helps someone.
Also, it's not a "single-click" action, but...
Steps to follow:
person.
at the beginning of each class and ()
at the end.Upvotes: 29
Reputation: 3822
when your curser is inside your newly created class then you can press ALT+Insert -- a dialog should appear and you can then select Getter and Setter
Upvotes: 12
Reputation: 1932
I am not sure how to do this in IntelliJ, but I will link you to a framework that makes these kinds of things so much smoother in Java: Project Lombok
take your code for example, it will look like this:
public class Person {
@Setter
@Getter
String firstName;
String lastName;
}
there is a pluging for intellij that fixes it so you dont get any annoying warnings as well.
Upvotes: 6