Intellij Code Completion for all setter/getter methods of local variable object

JavaIntellij IdeaEditor

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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'.

Solution 2 - Java

This is an old question, but maybe it helps someone.

Also, it's not a "single-click" action, but...

Steps to follow:

  • Go to Class file
  • Open Structure view (left panel)
  • Select all setters (sort alphabetically if needed) and copy
  • Go back to your destination class/method
  • Paste copied
  • Edit (in column mode) to include person. at the beginning of each class and () at the end.

Solution 3 - Java

Shortcut for creating getter setter is Alt + Insert in Intellij.

Solution 4 - Java

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

enter image description here

Solution 5 - Java

This plugin may help GenerateAllSetter

Press CTRL + ENTER where you want to use and it will generate setter calls for you.

Solution 6 - Java

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.

http://projectlombok.org/

Solution 7 - Java

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();

Solution 8 - Java

Shortcut for Windows Alt + Insert

Shortcut for Mac Command + N

then choose getter and setter

Solution 9 - Java

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)

enter image description here

Solution 10 - Java

This is an example gif

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"

  1. Copy the Java/class file information.
  2. Execute the class "com.github.adrninistrator.gensettercalls.gen.GenSetterCallsAuto".
  3. Then you can paste the generated setter code.

Solution 11 - Java

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

Solution 12 - Java

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.

Setter call suggestions

Below are images of the resulting code genarated (both with and without defaults)

Setters without default values

Setters without default values

Setters with deafult values

Setters with deafult values

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlake DunnView Question on Stackoverflow
Solution 1 - JavaMichael JarvisView Answer on Stackoverflow
Solution 2 - JavarodrigobbView Answer on Stackoverflow
Solution 3 - JavaNileshView Answer on Stackoverflow
Solution 4 - JavaserupView Answer on Stackoverflow
Solution 5 - JavaRadouane ROUFIDView Answer on Stackoverflow
Solution 6 - JavaVegardView Answer on Stackoverflow
Solution 7 - Javayoke xView Answer on Stackoverflow
Solution 8 - Javauser311086View Answer on Stackoverflow
Solution 9 - JavaVishrantView Answer on Stackoverflow
Solution 10 - JavaAdrninistratorView Answer on Stackoverflow
Solution 11 - JavaCliqxeView Answer on Stackoverflow
Solution 12 - JavaSylvesterView Answer on Stackoverflow