abd
abd

Reputation: 629

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

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

Answers (12)

Sylvester
Sylvester

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.

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

Upvotes: 1

Cliqxe
Cliqxe

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

user311086
user311086

Reputation: 1088

Shortcut for Windows Alt + Insert

Shortcut for Mac Command + N

then choose getter and setter

Upvotes: 3

Michael Jarvis
Michael Jarvis

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

Radouane ROUFID
Radouane ROUFID

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

Vishrant
Vishrant

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)

enter image description here

Upvotes: 1

Nilesh
Nilesh

Reputation: 2180

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

Upvotes: 18

Adrninistrator
Adrninistrator

Reputation: 1

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.

Upvotes: 0

yoke x
yoke x

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

rodrigobb
rodrigobb

Reputation: 714

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.

Upvotes: 29

serup
serup

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

enter image description here

Upvotes: 12

Vegard
Vegard

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.

http://projectlombok.org/

Upvotes: 6

Related Questions